Multi-Step FlexiSuperTrend Strategy
A SuperTrend filter combined with a smoothed deviation oscillator. The strategy includes three configurable take-profit levels.
Details
- Entry Criteria:
- Price below SuperTrend and deviation (SMA of price minus SuperTrend) > 0 → buy.
- Price above SuperTrend and deviation < 0 → sell.
- Long/Short: Long, short or both directions.
- Exit Criteria:
- Partial take profit at 3 levels.
- Remaining position closed on trend reversal when price crosses SuperTrend.
- Stops: No stop logic by default.
- Default Values:
- ATR period = 10.
- ATR factor = 3.0.
- SMA length = 10.
- Take profit levels = 2%, 8%, 18%.
- Take profit percents = 30%, 20%, 15%.
- Filters:
- Category: Trend following
- Direction: Both
- Indicators: SuperTrend, SMA
- Stops: Take profit
- Complexity: Medium
- Timeframe: Any
- 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>
/// Multi-Step FlexiSuperTrend strategy - SMA deviation crossover.
/// </summary>
public class MultiStepFlexiSuperTrendStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _fastLength;
private readonly StrategyParam<int> _slowLength;
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<decimal> _rsiHigh;
private readonly StrategyParam<decimal> _rsiLow;
private readonly StrategyParam<int> _signalCooldownBars;
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
public int RsiLength { get => _rsiLength.Value; set => _rsiLength.Value = value; }
public decimal RsiHigh { get => _rsiHigh.Value; set => _rsiHigh.Value = value; }
public decimal RsiLow { get => _rsiLow.Value; set => _rsiLow.Value = value; }
public int SignalCooldownBars { get => _signalCooldownBars.Value; set => _signalCooldownBars.Value = value; }
public MultiStepFlexiSuperTrendStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
.SetDisplay("Candle Type", "Type of candles", "General");
_fastLength = Param(nameof(FastLength), 10)
.SetGreaterThanZero()
.SetDisplay("Fast Length", "Fast EMA period", "Indicators");
_slowLength = Param(nameof(SlowLength), 30)
.SetGreaterThanZero()
.SetDisplay("Slow Length", "Slow EMA period", "Indicators");
_rsiLength = Param(nameof(RsiLength), 14)
.SetGreaterThanZero()
.SetDisplay("RSI Length", "RSI period", "Indicators");
_rsiHigh = Param(nameof(RsiHigh), 55m)
.SetDisplay("RSI High", "RSI overbought", "Indicators");
_rsiLow = Param(nameof(RsiLow), 45m)
.SetDisplay("RSI Low", "RSI oversold", "Indicators");
_signalCooldownBars = Param(nameof(SignalCooldownBars), 6)
.SetGreaterThanZero()
.SetDisplay("Signal Cooldown", "Bars to wait between trades", "Trading");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var fast = new ExponentialMovingAverage { Length = FastLength };
var slow = new ExponentialMovingAverage { Length = SlowLength };
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var prevFast = 0m;
var prevSlow = 0m;
var initialized = false;
var cooldownRemaining = 0;
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(fast, slow, rsi, (candle, fastVal, slowVal, rsiVal) =>
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (cooldownRemaining > 0)
cooldownRemaining--;
if (!initialized)
{
prevFast = fastVal;
prevSlow = slowVal;
initialized = true;
return;
}
if (cooldownRemaining == 0 && prevFast <= prevSlow && fastVal > slowVal && rsiVal > RsiHigh && Position <= 0)
{
BuyMarket();
cooldownRemaining = SignalCooldownBars;
}
else if (cooldownRemaining == 0 && prevFast >= prevSlow && fastVal < slowVal && rsiVal < RsiLow && Position > 0)
{
SellMarket();
cooldownRemaining = SignalCooldownBars;
}
prevFast = fastVal;
prevSlow = slowVal;
})
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, fast);
DrawIndicator(area, slow);
DrawOwnTrades(area);
}
}
}
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, RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class multi_step_flexi_super_trend_strategy(Strategy):
def __init__(self):
super(multi_step_flexi_super_trend_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(15))) \
.SetDisplay("Candle Type", "Type of candles", "General")
self._fast_length = self.Param("FastLength", 10) \
.SetGreaterThanZero() \
.SetDisplay("Fast Length", "Fast EMA period", "Indicators")
self._slow_length = self.Param("SlowLength", 30) \
.SetGreaterThanZero() \
.SetDisplay("Slow Length", "Slow EMA period", "Indicators")
self._rsi_length = self.Param("RsiLength", 14) \
.SetGreaterThanZero() \
.SetDisplay("RSI Length", "RSI period", "Indicators")
self._rsi_high = self.Param("RsiHigh", 55.0) \
.SetDisplay("RSI High", "RSI overbought", "Indicators")
self._rsi_low = self.Param("RsiLow", 45.0) \
.SetDisplay("RSI Low", "RSI oversold", "Indicators")
self._signal_cooldown_bars = self.Param("SignalCooldownBars", 6) \
.SetGreaterThanZero() \
.SetDisplay("Signal Cooldown", "Bars to wait between trades", "Trading")
self._prev_fast = 0.0
self._prev_slow = 0.0
self._initialized = False
self._cooldown_remaining = 0
@property
def candle_type(self):
return self._candle_type.Value
@candle_type.setter
def candle_type(self, value):
self._candle_type.Value = value
def OnReseted(self):
super(multi_step_flexi_super_trend_strategy, self).OnReseted()
self._prev_fast = 0.0
self._prev_slow = 0.0
self._initialized = False
self._cooldown_remaining = 0
def OnStarted2(self, time):
super(multi_step_flexi_super_trend_strategy, self).OnStarted2(time)
self._prev_fast = 0.0
self._prev_slow = 0.0
self._initialized = False
self._cooldown_remaining = 0
self._fast = ExponentialMovingAverage()
self._fast.Length = self._fast_length.Value
self._slow = ExponentialMovingAverage()
self._slow.Length = self._slow_length.Value
self._rsi = RelativeStrengthIndex()
self._rsi.Length = self._rsi_length.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._fast, self._slow, self._rsi, self.OnProcess).Start()
def OnProcess(self, candle, fast_val, slow_val, rsi_val):
if candle.State != CandleStates.Finished:
return
fv = float(fast_val)
sv = float(slow_val)
rv = float(rsi_val)
if self._cooldown_remaining > 0:
self._cooldown_remaining -= 1
if not self._initialized:
self._prev_fast = fv
self._prev_slow = sv
self._initialized = True
return
rh = float(self._rsi_high.Value)
rl = float(self._rsi_low.Value)
if self._cooldown_remaining == 0 and self._prev_fast <= self._prev_slow and fv > sv and rv > rh and self.Position <= 0:
self.BuyMarket()
self._cooldown_remaining = self._signal_cooldown_bars.Value
elif self._cooldown_remaining == 0 and self._prev_fast >= self._prev_slow and fv < sv and rv < rl and self.Position > 0:
self.SellMarket()
self._cooldown_remaining = self._signal_cooldown_bars.Value
self._prev_fast = fv
self._prev_slow = sv
def CreateClone(self):
return multi_step_flexi_super_trend_strategy()