NonLagDot Strategy
Strategy inspired by the NonLagDot indicator. The indicator approximates price trend using a smooth moving average and color-coded dots. The strategy opens a long position when the indicator turns upward and a short position when it turns downward. Previous opposite positions are closed before opening a new one.
Details
- Entry Criteria:
- Long: Indicator turns from down to up (moving average slope becomes positive)
- Short: Indicator turns from up to down (moving average slope becomes negative)
- Long/Short: Both
- Exit Criteria: Opposite signal
- Stops: Optional stop-loss percentage
- Default Values:
Length= 10CandleType= TimeSpan.FromHours(1).TimeFrame()StopLossPercent= 1m
- Filters:
- Category: Trend-following
- Direction: Both
- Indicators: SMA slope approximation of NonLagDot
- Stops: Yes
- Complexity: Basic
- Timeframe: Intraday
- Seasonality: No
- Neural Networks: No
- Divergence: No
- Risk Level: Medium
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>
/// Strategy based on NonLagDot indicator trend changes.
/// Uses a simple moving average to approximate NonLagDot behavior.
/// When the moving average slope turns positive, a long position is opened.
/// When the slope turns negative, a short position is opened.
/// Opposite positions are closed before opening new ones.
/// </summary>
public class NonLagDotStrategy : Strategy
{
private readonly StrategyParam<int> _length;
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<decimal> _stopLossPercent;
private ExponentialMovingAverage _sma;
private decimal? _prevSma;
private int _prevTrend;
/// <summary>
/// Moving average length approximating NonLagDot.
/// </summary>
public int Length
{
get => _length.Value;
set => _length.Value = value;
}
/// <summary>
/// Candle type for indicator calculations.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Stop-loss percentage.
/// </summary>
public decimal StopLossPercent
{
get => _stopLossPercent.Value;
set => _stopLossPercent.Value = value;
}
/// <summary>
/// Initializes a new instance of the strategy.
/// </summary>
public NonLagDotStrategy()
{
_length = Param(nameof(Length), 10)
.SetGreaterThanZero()
.SetDisplay("Length", "Moving average period", "Indicator");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Timeframe for calculations", "General");
_stopLossPercent = Param(nameof(StopLossPercent), 1m)
.SetGreaterThanZero()
.SetDisplay("Stop Loss %", "Percent based stop-loss", "Risk");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_sma = null;
_prevSma = null;
_prevTrend = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_sma = new ExponentialMovingAverage { Length = Length };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(_sma, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, _sma);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal sma)
{
if (candle.State != CandleStates.Finished)
return;
if (_prevSma is null)
{
_prevSma = sma;
return;
}
var trend = sma > _prevSma ? 1 : sma < _prevSma ? -1 : _prevTrend;
if (!IsFormedAndOnlineAndAllowTrading())
{
_prevTrend = trend;
_prevSma = sma;
return;
}
if (trend > 0 && _prevTrend < 0 && Position <= 0)
BuyMarket();
else if (trend < 0 && _prevTrend > 0 && Position >= 0)
SellMarket();
_prevTrend = trend;
_prevSma = sma;
}
}
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
from StockSharp.Algo.Strategies import Strategy
class non_lag_dot_strategy(Strategy):
def __init__(self):
super(non_lag_dot_strategy, self).__init__()
self._length = self.Param("Length", 10) \
.SetDisplay("Length", "Moving average period", "Indicator")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Timeframe for calculations", "General")
self._stop_loss_percent = self.Param("StopLossPercent", 1.0) \
.SetDisplay("Stop Loss %", "Percent based stop-loss", "Risk")
self._prev_sma = None
self._prev_trend = 0
@property
def length(self):
return self._length.Value
@property
def candle_type(self):
return self._candle_type.Value
@property
def stop_loss_percent(self):
return self._stop_loss_percent.Value
def OnReseted(self):
super(non_lag_dot_strategy, self).OnReseted()
self._prev_sma = None
self._prev_trend = 0
def OnStarted2(self, time):
super(non_lag_dot_strategy, self).OnStarted2(time)
sma = ExponentialMovingAverage()
sma.Length = self.length
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(sma, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, sma)
self.DrawOwnTrades(area)
def process_candle(self, candle, sma_val):
if candle.State != CandleStates.Finished:
return
sma_val = float(sma_val)
if self._prev_sma is None:
self._prev_sma = sma_val
return
if sma_val > self._prev_sma:
trend = 1
elif sma_val < self._prev_sma:
trend = -1
else:
trend = self._prev_trend
if trend > 0 and self._prev_trend < 0 and self.Position <= 0:
self.BuyMarket()
elif trend < 0 and self._prev_trend > 0 and self.Position >= 0:
self.SellMarket()
self._prev_trend = trend
self._prev_sma = sma_val
def CreateClone(self):
return non_lag_dot_strategy()