This strategy emulates the Charles expert advisor by combining exponential moving averages (EMA) with an RSI filter and a trailing stop. It trades both directions and dynamically protects positions.
The system monitors a fast and a slow EMA on the selected timeframe. When the fast EMA crosses above the slow EMA and RSI exceeds 55, the strategy enters a long position. Conversely, when the fast EMA crosses below the slow EMA and RSI drops below 45, it enters a short position. After entry, a trailing stop follows price to lock in profits while a fixed take profit and stop loss are managed through built-in position protection.
Details
Entry Criteria:
Long: Fast EMA crosses above Slow EMA and RSI > 55.
Short: Fast EMA crosses below Slow EMA and RSI < 45.
Long/Short: Both.
Exit Criteria:
Trailing stop.
Stop-loss or take-profit.
Stops: Uses built-in protection with trailing.
Default Values:
FastPeriod = 18
SlowPeriod = 60
RsiPeriod = 14
TakeProfit = 0.02
StopLoss = 0.008
TrailStart = 0.006
TrailOffset = 0.003
Filters:
Category: Trend following
Direction: Both
Indicators: Multiple
Stops: Yes
Complexity: Intermediate
Timeframe: 1 hour by default
Seasonality: No
Neural networks: No
Divergence: No
Risk level: Medium
using System;
using System.Collections.Generic;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Charles strategy: EMA crossover with RSI filter.
/// </summary>
public class CharlesStrategy : Strategy
{
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<DataType> _candleType;
private bool _wasFastBelowSlow;
private bool _isInitialized;
public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public CharlesStrategy()
{
_fastPeriod = Param(nameof(FastPeriod), 18)
.SetGreaterThanZero()
.SetDisplay("Fast EMA", "Period of the fast EMA", "Indicators");
_slowPeriod = Param(nameof(SlowPeriod), 60)
.SetGreaterThanZero()
.SetDisplay("Slow EMA", "Period of the slow EMA", "Indicators");
_rsiPeriod = Param(nameof(RsiPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Period", "RSI calculation length", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Time frame for candles", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_wasFastBelowSlow = false;
_isInitialized = false;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var fast = new ExponentialMovingAverage { Length = FastPeriod };
var slow = new ExponentialMovingAverage { Length = SlowPeriod };
var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
SubscribeCandles(CandleType)
.Bind(fast, slow, rsi, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue, decimal rsiValue)
{
if (candle.State != CandleStates.Finished) return;
if (!_isInitialized)
{
_wasFastBelowSlow = fastValue < slowValue;
_isInitialized = true;
return;
}
var isFastBelowSlow = fastValue < slowValue;
// Long signal: fast crosses above slow + RSI > 55
if (_wasFastBelowSlow && !isFastBelowSlow && rsiValue > 55 && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
}
// Short signal: fast crosses below slow + RSI < 45
else if (!_wasFastBelowSlow && isFastBelowSlow && rsiValue < 45 && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
}
_wasFastBelowSlow = isFastBelowSlow;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ExponentialMovingAverage, RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class charles_strategy(Strategy):
def __init__(self):
super(charles_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 18) \
.SetDisplay("Fast EMA", "Period of the fast EMA", "Indicators")
self._slow_period = self.Param("SlowPeriod", 60) \
.SetDisplay("Slow EMA", "Period of the slow EMA", "Indicators")
self._rsi_period = self.Param("RsiPeriod", 14) \
.SetDisplay("RSI Period", "RSI calculation length", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Time frame for candles", "General")
self._was_fast_below_slow = False
self._is_initialized = False
@property
def fast_period(self):
return self._fast_period.Value
@property
def slow_period(self):
return self._slow_period.Value
@property
def rsi_period(self):
return self._rsi_period.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(charles_strategy, self).OnReseted()
self._was_fast_below_slow = False
self._is_initialized = False
def OnStarted2(self, time):
super(charles_strategy, self).OnStarted2(time)
fast = ExponentialMovingAverage()
fast.Length = self.fast_period
slow = ExponentialMovingAverage()
slow.Length = self.slow_period
rsi = RelativeStrengthIndex()
rsi.Length = self.rsi_period
self.SubscribeCandles(self.candle_type).Bind(fast, slow, rsi, self.process_candle).Start()
def process_candle(self, candle, fast_value, slow_value, rsi_value):
if candle.State != CandleStates.Finished:
return
fv = float(fast_value)
sv = float(slow_value)
rv = float(rsi_value)
if not self._is_initialized:
self._was_fast_below_slow = fv < sv
self._is_initialized = True
return
is_fast_below_slow = fv < sv
if self._was_fast_below_slow and not is_fast_below_slow and rv > 55 and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif not self._was_fast_below_slow and is_fast_below_slow and rv < 45 and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._was_fast_below_slow = is_fast_below_slow
def CreateClone(self):
return charles_strategy()