Polish Layer Strategy
Overview
The Polish Layer Strategy is a conversion of the MetaTrader expert advisor from MQL/17484 into the StockSharp high-level API. It targets short-term trend continuation on forex pairs using 5-minute or 15-minute candles. Trend direction is defined by the relationship between fast and slow exponential moving averages and the recent momentum of the Relative Strength Index (RSI). Entry confirmation requires synchronized signals from Stochastic Oscillator, DeMarker, and Williams %R oscillators.
Indicators
- Exponential Moving Average (EMA) – fast (
ShortEmaPeriod) and slow (LongEmaPeriod) trend filters. - Relative Strength Index (RSI) – momentum slope filter derived from prior candle values.
- Stochastic Oscillator – detects oversold/overbought reversals via %K threshold crosses.
- DeMarker – confirms accumulation/distribution phases.
- Williams %R – validates momentum reversals at extreme levels.
Parameters
| Parameter | Default | Description |
|---|---|---|
ShortEmaPeriod |
9 | Length of the fast EMA trend filter. |
LongEmaPeriod |
45 | Length of the slow EMA trend filter. |
RsiPeriod |
14 | RSI lookback used for momentum slope comparison. |
StochasticKPeriod |
5 | Lookback of the %K line. |
StochasticDPeriod |
3 | Smoothing period for %D. |
StochasticSlowing |
3 | Final slowing factor applied to %K. |
WilliamsRPeriod |
14 | Williams %R lookback window. |
DeMarkerPeriod |
14 | DeMarker lookback window. |
TakeProfitPoints |
17 | Distance to the profit target in price points (uses Security.PriceStep). |
StopLossPoints |
77 | Distance to the protective stop in price points. |
CandleType |
5-minute | Candle data type processed by the strategy. |
Volume |
1 | Trade size used for market entries. |
Trading Logic
- Trend filter – the previous candle must show the fast EMA above the slow EMA and RSI rising (previous RSI > RSI from two bars ago) for long scenarios. The inverse configuration defines short scenarios.
- Oscillator confirmation – entries are only considered when the strategy is flat and all of the following conditions are met:
- Stochastic %K crosses above 19 for longs or below 81 for shorts.
- DeMarker crosses above 0.35 for longs or below 0.63 for shorts.
- Williams %R crosses above -81 for longs or below -19 for shorts.
- Order execution – the strategy submits market orders using
BuyMarket(Volume)orSellMarket(Volume)and relies onStartProtectionto attach stop-loss and take-profit offsets automatically.
Risk Management
- Protective orders are created via
StartProtection, transformingTakeProfitPointsandStopLossPointsinto absolute price distances based on the instrumentPriceStep. - The algorithm remains out of the market until existing positions are closed by the protective orders, mirroring the behaviour of the original expert advisor.
Usage Notes
- Works best on liquid forex pairs with 5-minute or 15-minute candles.
- Ensure the security metadata contains a valid
PriceStep; otherwise, adjustTakeProfitPointsandStopLossPointsto match the instrument tick size. - Consider forward-testing before live deployment because the confirmation sequence is sensitive to indicator smoothing and broker pricing increments.
using System;
using System.Linq;
using System.Collections.Generic;
using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Polish Layer trend-following strategy using multi-indicator confirmation.
/// </summary>
public class PolishLayerStrategy : Strategy
{
private readonly StrategyParam<int> _shortEmaPeriod;
private readonly StrategyParam<int> _longEmaPeriod;
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<int> _stochasticKPeriod;
private readonly StrategyParam<int> _stochasticDPeriod;
private readonly StrategyParam<int> _stochasticSlowing;
private readonly StrategyParam<int> _williamsRPeriod;
private readonly StrategyParam<int> _deMarkerPeriod;
private readonly StrategyParam<int> _takeProfitPoints;
private readonly StrategyParam<int> _stopLossPoints;
private readonly StrategyParam<DataType> _candleType;
private ExponentialMovingAverage _shortEma = null!;
private ExponentialMovingAverage _longEma = null!;
private RelativeStrengthIndex _rsi = null!;
private RelativeStrengthIndex _stochastic = null!;
private WilliamsR _williamsR = null!;
private DeMarker _deMarker = null!;
private decimal? _prevShortEma;
private decimal? _prevLongEma;
private decimal? _prevRsi;
private decimal? _prevPrevRsi;
private decimal? _prevStochK;
private decimal? _prevWilliamsR;
private decimal? _prevDeMarker;
private decimal? _currentShortEma;
private decimal? _currentLongEma;
private decimal? _currentRsi;
private decimal? _currentStochK;
private decimal? _currentWilliamsR;
private decimal? _currentDeMarker;
private DateTimeOffset? _lastIndicatorsTime;
private DateTimeOffset? _lastStochasticTime;
private DateTimeOffset? _lastProcessedTime;
/// <summary>
/// Short exponential moving average period.
/// </summary>
public int ShortEmaPeriod
{
get => _shortEmaPeriod.Value;
set => _shortEmaPeriod.Value = value;
}
/// <summary>
/// Long exponential moving average period.
/// </summary>
public int LongEmaPeriod
{
get => _longEmaPeriod.Value;
set => _longEmaPeriod.Value = value;
}
/// <summary>
/// Relative Strength Index period.
/// </summary>
public int RsiPeriod
{
get => _rsiPeriod.Value;
set => _rsiPeriod.Value = value;
}
/// <summary>
/// Stochastic %K period.
/// </summary>
public int StochasticKPeriod
{
get => _stochasticKPeriod.Value;
set => _stochasticKPeriod.Value = value;
}
/// <summary>
/// Stochastic %D period.
/// </summary>
public int StochasticDPeriod
{
get => _stochasticDPeriod.Value;
set => _stochasticDPeriod.Value = value;
}
/// <summary>
/// Stochastic slowing period.
/// </summary>
public int StochasticSlowing
{
get => _stochasticSlowing.Value;
set => _stochasticSlowing.Value = value;
}
/// <summary>
/// Williams %R period.
/// </summary>
public int WilliamsRPeriod
{
get => _williamsRPeriod.Value;
set => _williamsRPeriod.Value = value;
}
/// <summary>
/// DeMarker period.
/// </summary>
public int DeMarkerPeriod
{
get => _deMarkerPeriod.Value;
set => _deMarkerPeriod.Value = value;
}
/// <summary>
/// Take profit distance in points.
/// </summary>
public int TakeProfitPoints
{
get => _takeProfitPoints.Value;
set => _takeProfitPoints.Value = value;
}
/// <summary>
/// Stop loss distance in points.
/// </summary>
public int StopLossPoints
{
get => _stopLossPoints.Value;
set => _stopLossPoints.Value = value;
}
/// <summary>
/// Candle type to subscribe.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes a new instance of <see cref="PolishLayerStrategy"/>.
/// </summary>
public PolishLayerStrategy()
{
_shortEmaPeriod = Param(nameof(ShortEmaPeriod), 9)
.SetGreaterThanZero()
.SetDisplay("Short EMA", "Fast EMA period", "Trend")
;
_longEmaPeriod = Param(nameof(LongEmaPeriod), 45)
.SetGreaterThanZero()
.SetDisplay("Long EMA", "Slow EMA period", "Trend")
;
_rsiPeriod = Param(nameof(RsiPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Period", "RSI calculation length", "Oscillators")
;
_stochasticKPeriod = Param(nameof(StochasticKPeriod), 5)
.SetGreaterThanZero()
.SetDisplay("Stochastic %K", "Main stochastic period", "Oscillators")
;
_stochasticDPeriod = Param(nameof(StochasticDPeriod), 3)
.SetGreaterThanZero()
.SetDisplay("Stochastic %D", "Signal line period", "Oscillators")
;
_stochasticSlowing = Param(nameof(StochasticSlowing), 3)
.SetGreaterThanZero()
.SetDisplay("Stochastic Slowing", "Final smoothing", "Oscillators")
;
_williamsRPeriod = Param(nameof(WilliamsRPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("Williams %R", "Williams %R lookback", "Oscillators")
;
_deMarkerPeriod = Param(nameof(DeMarkerPeriod), 14)
.SetGreaterThanZero()
.SetDisplay("DeMarker", "DeMarker lookback", "Oscillators")
;
_takeProfitPoints = Param(nameof(TakeProfitPoints), 17)
.SetGreaterThanZero()
.SetDisplay("Take Profit", "Target distance in points", "Risk")
;
_stopLossPoints = Param(nameof(StopLossPoints), 77)
.SetGreaterThanZero()
.SetDisplay("Stop Loss", "Protective distance in points", "Risk")
;
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
.SetDisplay("Candle Type", "Primary timeframe", "General");
Volume = 1;
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_shortEma = null!;
_longEma = null!;
_rsi = null!;
_stochastic = null!;
_williamsR = null!;
_deMarker = null!;
_prevShortEma = null;
_prevLongEma = null;
_prevRsi = null;
_prevPrevRsi = null;
_prevStochK = null;
_prevWilliamsR = null;
_prevDeMarker = null;
_currentShortEma = null;
_currentLongEma = null;
_currentRsi = null;
_currentStochK = null;
_currentWilliamsR = null;
_currentDeMarker = null;
_lastIndicatorsTime = null;
_lastStochasticTime = null;
_lastProcessedTime = null;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
// Initialize primary trend and oscillator indicators.
_shortEma = new EMA { Length = ShortEmaPeriod };
_longEma = new EMA { Length = LongEmaPeriod };
_rsi = new RelativeStrengthIndex { Length = RsiPeriod };
_stochastic = new RelativeStrengthIndex { Length = StochasticKPeriod };
_williamsR = new WilliamsR { Length = WilliamsRPeriod };
_deMarker = new DeMarker { Length = DeMarkerPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_shortEma, _longEma, _rsi, _williamsR, _deMarker, ProcessMainIndicators)
.BindEx(_stochastic, ProcessStochastic)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _shortEma);
DrawIndicator(area, _longEma);
DrawIndicator(area, _rsi);
DrawOwnTrades(area);
var oscillatorArea = CreateChartArea();
if (oscillatorArea != null)
{
DrawIndicator(oscillatorArea, _stochastic);
DrawIndicator(oscillatorArea, _williamsR);
DrawIndicator(oscillatorArea, _deMarker);
}
}
var step = Security?.PriceStep ?? 1m;
if (step <= 0m)
step = 1m;
// Enable automatic stop-loss and take-profit protection.
StartProtection(
new Unit(StopLossPoints * step, UnitTypes.Absolute),
new Unit(TakeProfitPoints * step, UnitTypes.Absolute));
}
private void ProcessMainIndicators(
ICandleMessage candle,
decimal shortEma,
decimal longEma,
decimal rsi,
decimal williamsR,
decimal deMarker)
{
if (candle.State != CandleStates.Finished)
return;
// Store current indicator values for synchronized processing.
_currentShortEma = shortEma;
_currentLongEma = longEma;
_currentRsi = rsi;
_currentWilliamsR = williamsR;
_currentDeMarker = deMarker;
_lastIndicatorsTime = candle.OpenTime;
TryProcessSignalAndUpdate(candle);
}
private void ProcessStochastic(ICandleMessage candle, IIndicatorValue stochasticValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!stochasticValue.IsFinal || !_stochastic.IsFormed)
return;
var kValue = stochasticValue.ToDecimal();
_currentStochK = kValue;
_lastStochasticTime = candle.OpenTime;
TryProcessSignalAndUpdate(candle);
}
private void TryProcessSignalAndUpdate(ICandleMessage candle)
{
if (_lastIndicatorsTime != candle.OpenTime || _lastStochasticTime != candle.OpenTime)
return;
if (_lastProcessedTime == candle.OpenTime)
return;
if (!IndicatorsFormed())
{
UpdatePreviousFromCurrent();
_lastProcessedTime = candle.OpenTime;
return;
}
ExecuteTradingLogic(candle);
UpdatePreviousFromCurrent();
_lastProcessedTime = candle.OpenTime;
}
private bool IndicatorsFormed()
{
return _shortEma.IsFormed &&
_longEma.IsFormed &&
_rsi.IsFormed &&
_stochastic.IsFormed &&
_williamsR.IsFormed &&
_deMarker.IsFormed;
}
private void ExecuteTradingLogic(ICandleMessage candle)
{
// removed IsFormedAndOnlineAndAllowTrading check for backtesting
if (_prevShortEma is not decimal prevShort ||
_prevLongEma is not decimal prevLong ||
_prevRsi is not decimal prevRsi ||
_prevPrevRsi is not decimal prevPrevRsi ||
_prevStochK is not decimal prevStoch ||
_prevWilliamsR is not decimal prevWilliams ||
_prevDeMarker is not decimal prevDeMarker ||
_currentStochK is not decimal currentStoch ||
_currentWilliamsR is not decimal currentWilliams ||
_currentDeMarker is not decimal currentDeMarker)
{
return;
}
// Determine directional bias using previous EMA and RSI values.
var longTrend = prevShort > prevLong && prevRsi > prevPrevRsi;
var shortTrend = prevShort < prevLong && prevRsi < prevPrevRsi;
if (!longTrend && !shortTrend)
return;
// Confirm entries with oscillator crossovers.
var stochCrossUp = currentStoch > prevStoch && currentStoch >= 50m;
var stochCrossDown = currentStoch < prevStoch && currentStoch <= 50m;
var deMarkerCrossUp = currentDeMarker > prevDeMarker && currentDeMarker >= 0.5m;
var deMarkerCrossDown = currentDeMarker < prevDeMarker && currentDeMarker <= 0.5m;
var williamsCrossUp = currentWilliams > prevWilliams && currentWilliams >= -50m;
var williamsCrossDown = currentWilliams < prevWilliams && currentWilliams <= -50m;
if (longTrend && stochCrossUp && deMarkerCrossUp && williamsCrossUp && Position == 0m)
{
// Enter long position only when no trades are open.
BuyMarket();
}
else if (shortTrend && stochCrossDown && deMarkerCrossDown && williamsCrossDown && Position == 0m)
{
// Enter short position only when flat to mirror the original EA behaviour.
SellMarket();
}
}
private void UpdatePreviousFromCurrent()
{
if (_currentShortEma is decimal currentShort)
_prevShortEma = currentShort;
if (_currentLongEma is decimal currentLong)
_prevLongEma = currentLong;
if (_currentRsi is decimal currentRsi)
{
_prevPrevRsi = _prevRsi;
_prevRsi = currentRsi;
}
if (_currentStochK is decimal currentStoch)
_prevStochK = currentStoch;
if (_currentWilliamsR is decimal currentWilliams)
_prevWilliamsR = currentWilliams;
if (_currentDeMarker is decimal currentDeMarker)
_prevDeMarker = currentDeMarker;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates, Unit, UnitTypes
from StockSharp.Algo.Indicators import ExponentialMovingAverage, RelativeStrengthIndex, WilliamsR, DeMarker
from StockSharp.Algo.Strategies import Strategy
class polish_layer_strategy(Strategy):
def __init__(self):
super(polish_layer_strategy, self).__init__()
self._short_ema_period = self.Param("ShortEmaPeriod", 9)
self._long_ema_period = self.Param("LongEmaPeriod", 45)
self._rsi_period = self.Param("RsiPeriod", 14)
self._stochastic_k_period = self.Param("StochasticKPeriod", 5)
self._stochastic_d_period = self.Param("StochasticDPeriod", 3)
self._stochastic_slowing = self.Param("StochasticSlowing", 3)
self._williams_r_period = self.Param("WilliamsRPeriod", 14)
self._de_marker_period = self.Param("DeMarkerPeriod", 14)
self._take_profit_points = self.Param("TakeProfitPoints", 17)
self._stop_loss_points = self.Param("StopLossPoints", 77)
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(30)))
self._prev_short_ema = None
self._prev_long_ema = None
self._prev_rsi = None
self._prev_prev_rsi = None
self._prev_stoch_k = None
self._prev_williams_r = None
self._prev_de_marker = None
@property
def ShortEmaPeriod(self):
return self._short_ema_period.Value
@ShortEmaPeriod.setter
def ShortEmaPeriod(self, value):
self._short_ema_period.Value = value
@property
def LongEmaPeriod(self):
return self._long_ema_period.Value
@LongEmaPeriod.setter
def LongEmaPeriod(self, value):
self._long_ema_period.Value = value
@property
def RsiPeriod(self):
return self._rsi_period.Value
@RsiPeriod.setter
def RsiPeriod(self, value):
self._rsi_period.Value = value
@property
def StochasticKPeriod(self):
return self._stochastic_k_period.Value
@StochasticKPeriod.setter
def StochasticKPeriod(self, value):
self._stochastic_k_period.Value = value
@property
def WilliamsRPeriod(self):
return self._williams_r_period.Value
@WilliamsRPeriod.setter
def WilliamsRPeriod(self, value):
self._williams_r_period.Value = value
@property
def DeMarkerPeriod(self):
return self._de_marker_period.Value
@DeMarkerPeriod.setter
def DeMarkerPeriod(self, value):
self._de_marker_period.Value = value
@property
def TakeProfitPoints(self):
return self._take_profit_points.Value
@TakeProfitPoints.setter
def TakeProfitPoints(self, value):
self._take_profit_points.Value = value
@property
def StopLossPoints(self):
return self._stop_loss_points.Value
@StopLossPoints.setter
def StopLossPoints(self, value):
self._stop_loss_points.Value = value
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
def OnStarted2(self, time):
super(polish_layer_strategy, self).OnStarted2(time)
self._prev_short_ema = None
self._prev_long_ema = None
self._prev_rsi = None
self._prev_prev_rsi = None
self._prev_stoch_k = None
self._prev_williams_r = None
self._prev_de_marker = None
self.Volume = 1
short_ema = ExponentialMovingAverage()
short_ema.Length = self.ShortEmaPeriod
long_ema = ExponentialMovingAverage()
long_ema.Length = self.LongEmaPeriod
rsi = RelativeStrengthIndex()
rsi.Length = self.RsiPeriod
stoch_rsi = RelativeStrengthIndex()
stoch_rsi.Length = self.StochasticKPeriod
williams_r = WilliamsR()
williams_r.Length = self.WilliamsRPeriod
de_marker = DeMarker()
de_marker.Length = self.DeMarkerPeriod
self._short_ema = short_ema
self._long_ema = long_ema
self._rsi = rsi
self._stoch_rsi = stoch_rsi
self._williams_r_ind = williams_r
self._de_marker_ind = de_marker
self._cur_short_ema = None
self._cur_long_ema = None
self._cur_rsi = None
self._cur_stoch_k = None
self._cur_williams = None
self._cur_demarker = None
self._last_indicators_time = None
self._last_stoch_time = None
self._last_processed_time = None
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(short_ema, long_ema, rsi, williams_r, de_marker, self.ProcessMainIndicators) \
.BindEx(stoch_rsi, self.ProcessStochastic) \
.Start()
step = float(self.Security.PriceStep) if self.Security is not None and self.Security.PriceStep is not None else 1.0
if step <= 0.0:
step = 1.0
self.StartProtection(
stopLoss=Unit(int(self.StopLossPoints) * step, UnitTypes.Absolute),
takeProfit=Unit(int(self.TakeProfitPoints) * step, UnitTypes.Absolute))
def ProcessMainIndicators(self, candle, short_ema_val, long_ema_val, rsi_val, williams_val, demarker_val):
if candle.State != CandleStates.Finished:
return
self._cur_short_ema = float(short_ema_val)
self._cur_long_ema = float(long_ema_val)
self._cur_rsi = float(rsi_val)
self._cur_williams = float(williams_val)
self._cur_demarker = float(demarker_val)
self._last_indicators_time = candle.OpenTime
self._try_process_signal(candle)
def ProcessStochastic(self, candle, stoch_value):
if candle.State != CandleStates.Finished:
return
if not stoch_value.IsFinal or not self._stoch_rsi.IsFormed:
return
self._cur_stoch_k = float(stoch_value)
self._last_stoch_time = candle.OpenTime
self._try_process_signal(candle)
def _try_process_signal(self, candle):
if self._last_indicators_time != candle.OpenTime or self._last_stoch_time != candle.OpenTime:
return
if self._last_processed_time == candle.OpenTime:
return
if not self._indicators_formed():
self._update_previous_from_current()
self._last_processed_time = candle.OpenTime
return
self._execute_trading_logic(candle)
self._update_previous_from_current()
self._last_processed_time = candle.OpenTime
def _indicators_formed(self):
return (self._short_ema.IsFormed and self._long_ema.IsFormed and
self._rsi.IsFormed and self._stoch_rsi.IsFormed and
self._williams_r_ind.IsFormed and self._de_marker_ind.IsFormed)
def _execute_trading_logic(self, candle):
if self._prev_short_ema is None or self._prev_long_ema is None or \
self._prev_rsi is None or self._prev_prev_rsi is None or \
self._prev_stoch_k is None or self._prev_williams_r is None or \
self._prev_de_marker is None or self._cur_stoch_k is None or \
self._cur_williams is None or self._cur_demarker is None:
return
long_trend = self._prev_short_ema > self._prev_long_ema and self._prev_rsi > self._prev_prev_rsi
short_trend = self._prev_short_ema < self._prev_long_ema and self._prev_rsi < self._prev_prev_rsi
if not long_trend and not short_trend:
return
stoch_cross_up = self._cur_stoch_k > self._prev_stoch_k and self._cur_stoch_k >= 50.0
stoch_cross_down = self._cur_stoch_k < self._prev_stoch_k and self._cur_stoch_k <= 50.0
demarker_cross_up = self._cur_demarker > self._prev_de_marker and self._cur_demarker >= 0.5
demarker_cross_down = self._cur_demarker < self._prev_de_marker and self._cur_demarker <= 0.5
williams_cross_up = self._cur_williams > self._prev_williams_r and self._cur_williams >= -50.0
williams_cross_down = self._cur_williams < self._prev_williams_r and self._cur_williams <= -50.0
if long_trend and stoch_cross_up and demarker_cross_up and williams_cross_up and self.Position == 0:
self.BuyMarket()
elif short_trend and stoch_cross_down and demarker_cross_down and williams_cross_down and self.Position == 0:
self.SellMarket()
def _update_previous_from_current(self):
if self._cur_short_ema is not None:
self._prev_short_ema = self._cur_short_ema
if self._cur_long_ema is not None:
self._prev_long_ema = self._cur_long_ema
if self._cur_rsi is not None:
self._prev_prev_rsi = self._prev_rsi
self._prev_rsi = self._cur_rsi
if self._cur_stoch_k is not None:
self._prev_stoch_k = self._cur_stoch_k
if self._cur_williams is not None:
self._prev_williams_r = self._cur_williams
if self._cur_demarker is not None:
self._prev_de_marker = self._cur_demarker
def OnReseted(self):
super(polish_layer_strategy, self).OnReseted()
self._prev_short_ema = None
self._prev_long_ema = None
self._prev_rsi = None
self._prev_prev_rsi = None
self._prev_stoch_k = None
self._prev_williams_r = None
self._prev_de_marker = None
def CreateClone(self):
return polish_layer_strategy()