This strategy opens positions when the closing price crosses the SuperTrend line. A long trade is placed when price moves above the line, and a short trade is opened when price drops below it. Opposite signals close and reverse existing positions.
The SuperTrend indicator uses the Average True Range (ATR) to follow price and define the prevailing trend. Parameters allow configuring ATR period, multiplier and candle timeframe.
Details
Entry Criteria:
Long: Close price crosses above SuperTrend
Short: Close price crosses below SuperTrend
Long/Short: Long and Short
Exit Criteria:
Opposite SuperTrend crossover
Stops: None
Default Values:
AtrPeriod = 5
Multiplier = 3
CandleType = TimeSpan.FromMinutes(15).TimeFrame()
Filters:
Category: Trend following
Direction: Both
Indicators: SuperTrend (ATR-based)
Stops: No
Complexity: Beginner
Timeframe: Mid-term
Seasonality: None
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>
/// Simple SuperTrend crossover strategy.
/// </summary>
public class SupertrendSignalStrategy : Strategy
{
private readonly StrategyParam<int> _atrPeriod;
private readonly StrategyParam<decimal> _multiplier;
private readonly StrategyParam<DataType> _candleType;
private bool? _prevIsUpTrend;
public int AtrPeriod { get => _atrPeriod.Value; set => _atrPeriod.Value = value; }
public decimal Multiplier { get => _multiplier.Value; set => _multiplier.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public SupertrendSignalStrategy()
{
_atrPeriod = Param(nameof(AtrPeriod), 5)
.SetDisplay("ATR Period", "ATR period for SuperTrend", "Parameters");
_multiplier = Param(nameof(Multiplier), 3m)
.SetDisplay("Multiplier", "ATR multiplier for SuperTrend", "Parameters");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Timeframe for candles", "Parameters");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
=> [(Security, CandleType)];
protected override void OnReseted()
{
base.OnReseted();
_prevIsUpTrend = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var st = new SuperTrend { Length = AtrPeriod, Multiplier = Multiplier };
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(st, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, st);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue stValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!stValue.IsFormed)
return;
var stv = stValue as SuperTrendIndicatorValue;
if (stv == null)
return;
var isUpTrend = stv.IsUpTrend;
if (IsFormedAndOnlineAndAllowTrading() && _prevIsUpTrend.HasValue)
{
if (isUpTrend && !_prevIsUpTrend.Value && Position <= 0)
BuyMarket();
else if (!isUpTrend && _prevIsUpTrend.Value && Position >= 0)
SellMarket();
}
_prevIsUpTrend = isUpTrend;
}
}
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 SuperTrend
from StockSharp.Algo.Strategies import Strategy
class supertrend_signal_strategy(Strategy):
def __init__(self):
super(supertrend_signal_strategy, self).__init__()
self._atr_period = self.Param("AtrPeriod", 5) \
.SetDisplay("ATR Period", "ATR period for SuperTrend", "Parameters")
self._multiplier = self.Param("Multiplier", 3.0) \
.SetDisplay("Multiplier", "ATR multiplier for SuperTrend", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Timeframe for candles", "Parameters")
self._prev_is_up_trend = None
@property
def atr_period(self):
return self._atr_period.Value
@property
def multiplier(self):
return self._multiplier.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(supertrend_signal_strategy, self).OnReseted()
self._prev_is_up_trend = None
def OnStarted2(self, time):
super(supertrend_signal_strategy, self).OnStarted2(time)
st = SuperTrend()
st.Length = self.atr_period
st.Multiplier = self.multiplier
subscription = self.SubscribeCandles(self.candle_type)
subscription.BindEx(st, self.process_candle).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, st)
self.DrawOwnTrades(area)
def process_candle(self, candle, st_value):
if candle.State != CandleStates.Finished:
return
if not st_value.IsFormed:
return
is_up_trend = st_value.IsUpTrend
if self._prev_is_up_trend is not None:
if is_up_trend and not self._prev_is_up_trend and self.Position <= 0:
self.BuyMarket()
elif not is_up_trend and self._prev_is_up_trend and self.Position >= 0:
self.SellMarket()
self._prev_is_up_trend = is_up_trend
def CreateClone(self):
return supertrend_signal_strategy()