using System;
using System.Collections.Generic;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// SuperTrend-based strategy from NinaEA.
/// Trades on SuperTrend direction flips - buy on up trend, sell on down trend.
/// </summary>
public class NinaEaStrategy : Strategy
{
private readonly StrategyParam<int> _atrPeriod;
private readonly StrategyParam<decimal> _atrMultiplier;
private readonly StrategyParam<DataType> _candleType;
private bool? _previousTrendUp;
public int AtrPeriod { get => _atrPeriod.Value; set => _atrPeriod.Value = value; }
public decimal AtrMultiplier { get => _atrMultiplier.Value; set => _atrMultiplier.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public NinaEaStrategy()
{
_atrPeriod = Param(nameof(AtrPeriod), 10)
.SetDisplay("ATR Period", "ATR length for SuperTrend", "Indicators");
_atrMultiplier = Param(nameof(AtrMultiplier), 1m)
.SetDisplay("ATR Multiplier", "SuperTrend multiplier", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_previousTrendUp = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_previousTrendUp = null;
var superTrend = new SuperTrend
{
Length = AtrPeriod,
Multiplier = AtrMultiplier
};
var subscription = SubscribeCandles(CandleType);
subscription
.BindEx(superTrend, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, IIndicatorValue superTrendValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!superTrendValue.IsFinal)
return;
var stValue = (SuperTrendIndicatorValue)superTrendValue;
var isUpTrend = stValue.IsUpTrend;
if (_previousTrendUp is bool prevUp)
{
// Trend flipped to up - go long
if (isUpTrend && !prevUp)
{
if (Position < 0)
BuyMarket();
if (Position <= 0)
BuyMarket();
}
// Trend flipped to down - go short
else if (!isUpTrend && prevUp)
{
if (Position > 0)
SellMarket();
if (Position >= 0)
SellMarket();
}
}
_previousTrendUp = 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, Math
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import SuperTrend
from StockSharp.Algo.Strategies import Strategy
class nina_ea_strategy(Strategy):
"""SuperTrend-based strategy. Trades on SuperTrend direction flips.
Go long on up trend, go short on down trend."""
def __init__(self):
super(nina_ea_strategy, self).__init__()
self._atr_period = self.Param("AtrPeriod", 10) \
.SetDisplay("ATR Period", "ATR length for SuperTrend", "Indicators")
self._atr_multiplier = self.Param("AtrMultiplier", 1.0) \
.SetDisplay("ATR Multiplier", "SuperTrend multiplier", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Candle timeframe", "General")
self._previous_trend_up = None
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def AtrPeriod(self):
return self._atr_period.Value
@property
def AtrMultiplier(self):
return self._atr_multiplier.Value
def OnReseted(self):
super(nina_ea_strategy, self).OnReseted()
self._previous_trend_up = None
def OnStarted2(self, time):
super(nina_ea_strategy, self).OnStarted2(time)
self._previous_trend_up = None
super_trend = SuperTrend()
super_trend.Length = self.AtrPeriod
super_trend.Multiplier = self.AtrMultiplier
subscription = self.SubscribeCandles(self.CandleType)
subscription.BindEx(super_trend, self._process_candle).Start()
def _process_candle(self, candle, super_trend_value):
if candle.State != CandleStates.Finished:
return
if not super_trend_value.IsFinal:
return
is_up_trend = super_trend_value.IsUpTrend if hasattr(super_trend_value, 'IsUpTrend') else None
if is_up_trend is None:
return
if self._previous_trend_up is not None:
prev_up = self._previous_trend_up
# Trend flipped to up - go long
if is_up_trend and not prev_up:
if self.Position < 0:
self.BuyMarket()
if self.Position <= 0:
self.BuyMarket()
# Trend flipped to down - go short
elif not is_up_trend and prev_up:
if self.Position > 0:
self.SellMarket()
if self.Position >= 0:
self.SellMarket()
self._previous_trend_up = is_up_trend
def CreateClone(self):
return nina_ea_strategy()