The strategy mirrors the idea of the original MQL "SyncCharts" utility by monitoring two candle feeds of the same instrument and
making trading decisions only when both streams confirm the same trend direction. The master series acts as the reference chart
(the one a trader typically watches), while the follower series represents an auxiliary chart (for example, a faster timeframe or
an alternative aggregation). By forcing both streams to agree before entering the market, the system filters out noise coming from
temporary desynchronization between chart intervals.
The setup works best on instruments that exhibit multi-timeframe trend structure such as index futures and liquid currency pairs.
Because both charts must move together before a trade is taken, false signals are reduced and the strategy naturally limits
exposure during chaotic market phases when timeframes disagree or new candles print at different moments.
Details
Entry Criteria:
Long: Both the master and follower simple moving averages (SMAs) slope upward on their most recent finished candles, and
the timestamps of those candles differ by less than the synchronization tolerance.
Short: Both SMAs slope downward and the timestamp difference is within the tolerance window.
Exit Criteria:
Time desynchronization: if the latest candles are separated by more than the allowed tolerance the position is flattened.
Trend disagreement: if one SMA turns up while the other turns down the open position is closed immediately.
Stops: Implicit flatten logic acts as a soft stop. No separate hard stop is submitted.
Long/Short: Both sides are traded.
Default Values:
Master candle: 5 minute timeframe.
Follower candle: 1 minute timeframe.
SMA length: 20 periods on both streams.
Synchronization tolerance: 15 seconds between candle open times.
Filters:
Category: Trend confirmation / multi-timeframe.
Direction: Bi-directional.
Indicators: SMA (dual stream).
Stops: No fixed stop, auto-flatten when charts diverge.
Complexity: Medium (multi-subscription with synchronization checks).
Timeframe: Configurable (default intraday).
Seasonality: None.
Neural networks: No.
Divergence: Uses timeframe divergence as a filter (requires agreement, not price divergence).
Risk level: Moderate due to confirmation requirement.
How it works
Two candle subscriptions are created via the high-level StockSharp API: one for the master chart and another for the follower.
Each feed is processed by an SMA with the same length, yielding a trend direction flag (up if the SMA value rises versus the
previous candle, down otherwise).
Whenever both candles finish, the strategy verifies that their timestamps are close enough (absolute difference below the
configured tolerance).
If the charts are synchronized and both trends point up, the strategy buys (closing any short first). If both trends point down,
it sells short (closing any long first).
Any loss of synchronization or trend disagreement triggers an immediate flatten to keep the account aligned with the charts the
trader watches.
Recommended usage
Apply to the same instrument on two different timeframes that normally correlate (e.g., 5-minute and 1-minute, or hourly and
15-minute).
Increase the synchronization tolerance if you work with exotic data sources that print candles with minor delays.
Combine with an external risk manager or add-on stop module when deploying to live trading.
namespace StockSharp.Samples.Strategies;
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.Messages;
/// <summary>
/// Sync Charts Confirmation strategy: dual SMA trend confirmation.
/// Uses fast and slow SMAs to confirm trend direction.
/// Buys when both SMAs trend up, sells when both trend down.
/// </summary>
public class SyncChartsConfirmationStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<int> _signalCooldownCandles;
private bool _wasBullish;
private bool _hasPrevTrend;
private int _candlesSinceTrade;
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 int SignalCooldownCandles { get => _signalCooldownCandles.Value; set => _signalCooldownCandles.Value = value; }
public SyncChartsConfirmationStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_fastPeriod = Param(nameof(FastPeriod), 10)
.SetGreaterThanZero()
.SetDisplay("Fast Period", "Fast SMA period", "Indicators");
_slowPeriod = Param(nameof(SlowPeriod), 40)
.SetGreaterThanZero()
.SetDisplay("Slow Period", "Slow SMA period", "Indicators");
_signalCooldownCandles = Param(nameof(SignalCooldownCandles), 6)
.SetGreaterThanZero()
.SetDisplay("Signal Cooldown", "Bars to wait between signals", "Trading");
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_wasBullish = false;
_hasPrevTrend = false;
_candlesSinceTrade = SignalCooldownCandles;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_hasPrevTrend = false;
_candlesSinceTrade = SignalCooldownCandles;
var fast = new SimpleMovingAverage { Length = FastPeriod };
var slow = new SimpleMovingAverage { Length = SlowPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(fast, slow, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal fast, decimal slow)
{
if (candle.State != CandleStates.Finished) return;
if (_candlesSinceTrade < SignalCooldownCandles)
_candlesSinceTrade++;
var isBullish = fast > slow;
if (_hasPrevTrend && isBullish != _wasBullish && _candlesSinceTrade >= SignalCooldownCandles)
{
if (isBullish && Position <= 0)
{
BuyMarket();
_candlesSinceTrade = 0;
}
else if (!isBullish && Position >= 0)
{
SellMarket();
_candlesSinceTrade = 0;
}
}
_wasBullish = isBullish;
_hasPrevTrend = true;
}
}
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 SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
from datatype_extensions import *
from indicator_extensions import *
class sync_charts_confirmation_strategy(Strategy):
"""Dual SMA trend confirmation with cooldown."""
def __init__(self):
super(sync_charts_confirmation_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 10).SetGreaterThanZero().SetDisplay("Fast Period", "Fast SMA period", "Indicators")
self._slow_period = self.Param("SlowPeriod", 40).SetGreaterThanZero().SetDisplay("Slow Period", "Slow SMA period", "Indicators")
self._cooldown = self.Param("SignalCooldownCandles", 6).SetGreaterThanZero().SetDisplay("Signal Cooldown", "Bars to wait between signals", "Trading")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Candle timeframe", "General")
@property
def CandleType(self): return self._candle_type.Value
@CandleType.setter
def CandleType(self, value): self._candle_type.Value = value
def OnReseted(self):
super(sync_charts_confirmation_strategy, self).OnReseted()
self._was_bullish = False
self._has_prev = False
self._candles_since_trade = self._cooldown.Value
def OnStarted2(self, time):
super(sync_charts_confirmation_strategy, self).OnStarted2(time)
self._was_bullish = False
self._has_prev = False
self._candles_since_trade = self._cooldown.Value
fast = SimpleMovingAverage()
fast.Length = self._fast_period.Value
slow = SimpleMovingAverage()
slow.Length = self._slow_period.Value
sub = self.SubscribeCandles(self.CandleType)
sub.Bind(fast, slow, self.OnProcess).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, sub)
self.DrawIndicator(area, fast)
self.DrawIndicator(area, slow)
self.DrawOwnTrades(area)
def OnProcess(self, candle, fast, slow):
if candle.State != CandleStates.Finished:
return
if self._candles_since_trade < self._cooldown.Value:
self._candles_since_trade += 1
is_bullish = fast > slow
if self._has_prev and is_bullish != self._was_bullish and self._candles_since_trade >= self._cooldown.Value:
if is_bullish and self.Position <= 0:
self.BuyMarket()
self._candles_since_trade = 0
elif not is_bullish and self.Position >= 0:
self.SellMarket()
self._candles_since_trade = 0
self._was_bullish = is_bullish
self._has_prev = True
def CreateClone(self):
return sync_charts_confirmation_strategy()