The AMS ES RSI strategy replicates the behaviour of the MetaTrader expert Expert_AMS_ES_RSI inside StockSharp. It combines morning/evening star candlestick formations with a Relative Strength Index (RSI) confirmation filter. Long trades are opened when a bullish morning star appears while RSI indicates oversold conditions. Short trades are taken when a bearish evening star forms in conjunction with an overbought RSI. Positions are closed when RSI crosses back through configurable threshold levels.
Market Assumptions
Works on any instrument that produces regular OHLC candles. Spot FX and index futures were the original targets of the MQL expert.
The strategy expects smooth price action where Japanese candlestick patterns are meaningful. Extremely noisy tick charts may not produce reliable signals.
Entry Logic
Subscribe to the configured timeframe (default: 1 hour) and wait for three fully closed candles.
Compute the average body size across the last BodyAveragePeriod candles (default: 3).
Detect a Morning Star when:
Candle 3 is strongly bearish (Open - Close larger than the averaged body size).
Candle 2 has a small real body (less than half of the average) and gaps below candle 3.
Candle 1 closes above the midpoint of candle 3.
Detect an Evening Star with the symmetric bearish conditions.
Confirm long entries when the current RSI value is below LongEntryRsi (default: 40). Confirm short entries when RSI is above ShortEntryRsi (default: 60).
Execute market orders using the strategy Volume.
Exit Logic
Close long positions when RSI crosses downward through UpperExitRsi (default: 70) or LowerExitRsi (default: 30).
Close short positions when RSI crosses upward through the same levels.
No hard stop-loss or take-profit is applied. Risk management must be handled externally or by adjusting the thresholds.
Parameters
Name
Description
Default
Range
CandleType
Data type representing the candle series to subscribe.
1-hour time frame
Any supported candle type
RsiPeriod
RSI calculation length.
47
Optimisable (10–70)
BodyAveragePeriod
Number of candles used to calculate the average body size required for pattern validation.
3
Optimisable (2–6)
LongEntryRsi
Maximum RSI value that allows long entries.
40
Optimisable (20–50)
ShortEntryRsi
Minimum RSI value that allows short entries.
60
Optimisable (50–80)
LowerExitRsi
Lower boundary that triggers exits when crossed upward.
30
Optimisable (20–40)
UpperExitRsi
Upper boundary that triggers exits when crossed downward.
70
Optimisable (60–80)
Implementation Notes
Uses the StockSharp high-level API with automatic candle subscriptions.
Relies solely on indicator values provided by Bind, avoiding manual GetValue calls in accordance with the project guidelines.
Maintains only a minimal in-memory history (three recent candles) for pattern validation.
The strategy automatically calls StartProtection() on launch to enable built-in safety mechanisms.
Usage Tips
Attach the strategy to an instrument/portfolio pair and ensure that the candle series is available from your connector.
Tune RSI levels according to the asset volatility. Wider thresholds reduce the number of trades but increase confirmation quality.
Combine with external position sizing modules (e.g., risk-based volume) to emulate the fixed lot behaviour of the original EA.
When backtesting, ensure the candle data contains gaps so that the star patterns can be correctly identified.
namespace StockSharp.Samples.Strategies;
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
/// <summary>
/// Morning/Evening Star + RSI strategy.
/// Buys on morning star with low RSI, sells on evening star with high RSI.
/// </summary>
public class AmsEsRsiStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<decimal> _rsiLow;
private readonly StrategyParam<decimal> _rsiHigh;
private ICandleMessage _prevCandle;
private ICandleMessage _prevPrevCandle;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
public decimal RsiLow { get => _rsiLow.Value; set => _rsiLow.Value = value; }
public decimal RsiHigh { get => _rsiHigh.Value; set => _rsiHigh.Value = value; }
public AmsEsRsiStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_rsiPeriod = Param(nameof(RsiPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Period", "RSI period", "Indicators");
_rsiLow = Param(nameof(RsiLow), 40m)
.SetDisplay("RSI Low", "RSI oversold threshold", "Signals");
_rsiHigh = Param(nameof(RsiHigh), 60m)
.SetDisplay("RSI High", "RSI overbought threshold", "Signals");
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevCandle = null;
_prevPrevCandle = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevCandle = null;
_prevPrevCandle = null;
var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(rsi, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal rsiValue)
{
if (candle.State != CandleStates.Finished) return;
if (_prevCandle != null && _prevPrevCandle != null)
{
var prevBody = Math.Abs(_prevCandle.ClosePrice - _prevCandle.OpenPrice);
var prevRange = _prevCandle.HighPrice - _prevCandle.LowPrice;
var isSmallBody = prevRange > 0 && prevBody < prevRange * 0.3m;
var firstBearish = _prevPrevCandle.OpenPrice > _prevPrevCandle.ClosePrice;
var currBullish = candle.ClosePrice > candle.OpenPrice;
var isMorningStar = firstBearish && isSmallBody && currBullish;
var firstBullish = _prevPrevCandle.ClosePrice > _prevPrevCandle.OpenPrice;
var currBearish = candle.OpenPrice > candle.ClosePrice;
var isEveningStar = firstBullish && isSmallBody && currBearish;
if (isMorningStar && rsiValue < RsiLow && Position <= 0)
BuyMarket();
else if (isEveningStar && rsiValue > RsiHigh && Position >= 0)
SellMarket();
}
_prevPrevCandle = _prevCandle;
_prevCandle = candle;
}
}
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 RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class ams_es_rsi_strategy(Strategy):
def __init__(self):
super(ams_es_rsi_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5)))
self._rsi_period = self.Param("RsiPeriod", 14)
self._rsi_low = self.Param("RsiLow", 40.0)
self._rsi_high = self.Param("RsiHigh", 60.0)
self._prev_candle = None
self._prev_prev_candle = None
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def RsiPeriod(self):
return self._rsi_period.Value
@RsiPeriod.setter
def RsiPeriod(self, value):
self._rsi_period.Value = value
@property
def RsiLow(self):
return self._rsi_low.Value
@RsiLow.setter
def RsiLow(self, value):
self._rsi_low.Value = value
@property
def RsiHigh(self):
return self._rsi_high.Value
@RsiHigh.setter
def RsiHigh(self, value):
self._rsi_high.Value = value
def OnReseted(self):
super(ams_es_rsi_strategy, self).OnReseted()
self._prev_candle = None
self._prev_prev_candle = None
def OnStarted2(self, time):
super(ams_es_rsi_strategy, self).OnStarted2(time)
self._prev_candle = None
self._prev_prev_candle = None
rsi = RelativeStrengthIndex()
rsi.Length = self.RsiPeriod
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(rsi, self._process_candle).Start()
def _process_candle(self, candle, rsi_value):
if candle.State != CandleStates.Finished:
return
rsi_val = float(rsi_value)
if self._prev_candle is not None and self._prev_prev_candle is not None:
prev_body = abs(float(self._prev_candle.ClosePrice) - float(self._prev_candle.OpenPrice))
prev_range = float(self._prev_candle.HighPrice) - float(self._prev_candle.LowPrice)
is_small_body = prev_range > 0 and prev_body < prev_range * 0.3
first_bearish = float(self._prev_prev_candle.OpenPrice) > float(self._prev_prev_candle.ClosePrice)
curr_bullish = float(candle.ClosePrice) > float(candle.OpenPrice)
is_morning_star = first_bearish and is_small_body and curr_bullish
first_bullish = float(self._prev_prev_candle.ClosePrice) > float(self._prev_prev_candle.OpenPrice)
curr_bearish = float(candle.OpenPrice) > float(candle.ClosePrice)
is_evening_star = first_bullish and is_small_body and curr_bearish
if is_morning_star and rsi_val < self.RsiLow and self.Position <= 0:
self.BuyMarket()
elif is_evening_star and rsi_val > self.RsiHigh and self.Position >= 0:
self.SellMarket()
self._prev_prev_candle = self._prev_candle
self._prev_candle = candle
def CreateClone(self):
return ams_es_rsi_strategy()