This strategy is a StockSharp conversion of the MetaTrader expert advisor "ICHMOKU RETRACEMENT". It keeps the original idea o
f trading Ichimoku pullbacks that happen inside a higher timeframe trend while being filtered by long-term momentum and MACD read
ings. The StockSharp implementation focuses on clarity, indicator reuse and risk control through the high-level API.
Trading Idea
Trend filter – the strategy looks for a bullish or bearish bias using a pair of Linear Weighted Moving Averages (LWMA). A
bullish context requires the fast LWMA to be above the slow LWMA, while a bearish context requires the opposite relation.
Ichimoku retracement – after a trend is detected, the previous candle must touch any of the Ichimoku lines (Tenkan-sen, Ki
jun-sen or the two leading spans). The current candle must open back on the trend side of the touched line, signaling a momentum p
ullback.
Momentum confirmation – the close-to-close momentum ratio must deviate from its neutral value (100) by at least a configurable
threshold. The ratio is computed on the same timeframe used for the Ichimoku indicator.
Macro filter – a monthly MACD (12/26/9) confirms the dominant long-term direction. Long trades require MACD main line above
the signal line, short trades require the opposite.
Order management – the strategy keeps at most one net position. Protective stop-loss and take-profit levels are placed in pi
ps and evaluated on every finished candle.
Parameters
Name
Description
Default
Signal Candle Type
Timeframe used for the LWMA, Ichimoku and momentum calculations.
1 hour candles
Macro Candle Type
Higher timeframe used for the MACD trend filter.
30-day candles
Fast LWMA
Period for the fast linear weighted moving average.
6
Slow LWMA
Period for the slow linear weighted moving average.
85
Tenkan Period
Ichimoku Tenkan-sen period.
9
Kijun Period
Ichimoku Kijun-sen period.
26
Span B Period
Ichimoku Senkou Span B period.
52
Momentum Period
Lookback for the close-to-close momentum ratio.
14
Momentum Threshold
Minimum absolute deviation from 100 required by the momentum ratio.
0.3
Take Profit (pips)
Take-profit distance expressed in pips.
50
Stop Loss (pips)
Stop-loss distance expressed in pips.
20
The base Volume parameter controls the size of new orders. When a reversal signal appears, the strategy closes the current posit
ion (if any) and opens a new position in the opposite direction using Volume + |Position| contracts.
Trade Rules
Long Entries
Fast LWMA > slow LWMA.
MACD main line > MACD signal line on the macro timeframe.
Momentum ratio deviation ≥ threshold.
Previous candle low touched at least one Ichimoku level and the current candle opened back above that level.
Net position must be flat or short.
Short Entries
Fast LWMA < slow LWMA.
MACD main line < MACD signal line on the macro timeframe.
Momentum ratio deviation ≥ threshold.
Previous candle high touched at least one Ichimoku level and the current candle opened back below that level.
Net position must be flat or long.
Exits
A long position closes when the candle’s low hits the stop-loss or the high reaches the take-profit level.
A short position closes when the candle’s high hits the stop-loss or the low reaches the take-profit level.
Differences vs. Original EA
Money-management ladders, break-even moves and trailing features from the MQL version are not replicated; risk control is limite
d to fixed stop-loss and take-profit levels.
StockSharp works with a single net position, so the martingale order stack is replaced by one entry per direction.
Alerts, e-mail and push notifications from the MetaTrader environment are omitted.
Usage Notes
Add the strategy to a StockSharp Designer or Shell project.
Select the desired instrument and adjust the Signal Candle Type to match your target timeframe.
Ensure the Macro Candle Type can be synthesized from the available data (the subscription uses allowBuildFromSmallerTimeFram e).
Tune stop-loss, take-profit and the momentum threshold according to the instrument’s volatility.
The included comments describe each decision step so the logic can be adapted or extended easily.
using System;
using System.Collections.Generic;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
public class IchimokuRetracementStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private decimal? _prevFast, _prevSlow;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
public IchimokuRetracementStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame()).SetDisplay("Candle Type", "Timeframe", "General");
_fastPeriod = Param(nameof(FastPeriod), 9).SetGreaterThanZero().SetDisplay("Fast DEMA", "Fast DEMA period", "Indicators");
_slowPeriod = Param(nameof(SlowPeriod), 26).SetGreaterThanZero().SetDisplay("Slow DEMA", "Slow DEMA period", "Indicators");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities() => [(Security, CandleType)];
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevFast = null;
_prevSlow = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevFast = null; _prevSlow = null;
var fast = new DoubleExponentialMovingAverage { Length = FastPeriod };
var slow = new DoubleExponentialMovingAverage { Length = SlowPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(fast, slow, ProcessCandle).Start();
var area = CreateChartArea();
if (area != null) { DrawCandles(area, subscription); DrawIndicator(area, fast); DrawIndicator(area, slow); DrawOwnTrades(area); }
}
private void ProcessCandle(ICandleMessage candle, decimal fast, decimal slow)
{
if (candle.State != CandleStates.Finished) return;
if (!IsFormedAndOnlineAndAllowTrading()) { _prevFast = fast; _prevSlow = slow; return; }
if (_prevFast == null || _prevSlow == null) { _prevFast = fast; _prevSlow = slow; return; }
var prevAbove = _prevFast.Value > _prevSlow.Value;
var currAbove = fast > slow;
_prevFast = fast; _prevSlow = slow;
if (!prevAbove && currAbove && Position <= 0) { if (Position < 0) BuyMarket(); BuyMarket(); }
else if (prevAbove && !currAbove && Position >= 0) { if (Position > 0) SellMarket(); SellMarket(); }
}
}
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 DoubleExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
class ichimoku_retracement_strategy(Strategy):
def __init__(self):
super(ichimoku_retracement_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Timeframe", "General")
self._fast_period = self.Param("FastPeriod", 9) \
.SetDisplay("Fast DEMA", "Fast DEMA period", "Indicators")
self._slow_period = self.Param("SlowPeriod", 26) \
.SetDisplay("Slow DEMA", "Slow DEMA period", "Indicators")
self._prev_fast = None
self._prev_slow = None
@property
def CandleType(self):
return self._candle_type.Value
@property
def FastPeriod(self):
return self._fast_period.Value
@property
def SlowPeriod(self):
return self._slow_period.Value
def OnReseted(self):
super(ichimoku_retracement_strategy, self).OnReseted()
self._prev_fast = None
self._prev_slow = None
def OnStarted2(self, time):
super(ichimoku_retracement_strategy, self).OnStarted2(time)
self._prev_fast = None
self._prev_slow = None
fast = DoubleExponentialMovingAverage()
fast.Length = self.FastPeriod
slow = DoubleExponentialMovingAverage()
slow.Length = self.SlowPeriod
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(fast, slow, self._on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, fast)
self.DrawIndicator(area, slow)
self.DrawOwnTrades(area)
def _on_process(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
fv = float(fast_value)
sv = float(slow_value)
if self._prev_fast is None or self._prev_slow is None:
self._prev_fast = fv
self._prev_slow = sv
return
prev_above = self._prev_fast > self._prev_slow
curr_above = fv > sv
self._prev_fast = fv
self._prev_slow = sv
if not prev_above and curr_above and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif prev_above and not curr_above and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
def CreateClone(self):
return ichimoku_retracement_strategy()