using System;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Ard Order Management Stochastic: RSI overbought/oversold reversal
/// with EMA trend filter and ATR-based trailing stops.
/// </summary>
public class ArdOrderManagementStochasticStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _rsiLength;
private readonly StrategyParam<int> _emaLength;
private readonly StrategyParam<int> _atrLength;
private readonly StrategyParam<decimal> _buyThreshold;
private readonly StrategyParam<decimal> _sellThreshold;
private decimal _prevRsi;
public ArdOrderManagementStochasticStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Timeframe.", "General");
_rsiLength = Param(nameof(RsiLength), 10)
.SetDisplay("RSI Length", "RSI period.", "Indicators");
_emaLength = Param(nameof(EmaLength), 14)
.SetDisplay("EMA Length", "EMA trend filter period.", "Indicators");
_atrLength = Param(nameof(AtrLength), 10)
.SetDisplay("ATR Length", "ATR period for stops.", "Indicators");
_buyThreshold = Param(nameof(BuyThreshold), 30m)
.SetDisplay("Buy Threshold", "RSI oversold level for buy.", "Signals");
_sellThreshold = Param(nameof(SellThreshold), 70m)
.SetDisplay("Sell Threshold", "RSI overbought level for sell.", "Signals");
}
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int RsiLength
{
get => _rsiLength.Value;
set => _rsiLength.Value = value;
}
public int EmaLength
{
get => _emaLength.Value;
set => _emaLength.Value = value;
}
public int AtrLength
{
get => _atrLength.Value;
set => _atrLength.Value = value;
}
public decimal BuyThreshold
{
get => _buyThreshold.Value;
set => _buyThreshold.Value = value;
}
public decimal SellThreshold
{
get => _sellThreshold.Value;
set => _sellThreshold.Value = value;
}
/// <inheritdoc />
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevRsi = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevRsi = 0;
var rsi = new RelativeStrengthIndex { Length = RsiLength };
var ema = new ExponentialMovingAverage { Length = EmaLength };
var atr = new AverageTrueRange { Length = AtrLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(rsi, ema, atr, ProcessCandle)
.Start();
StartProtection(
takeProfit: new Unit(2, UnitTypes.Percent),
stopLoss: new Unit(1, UnitTypes.Percent)
);
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, ema);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal rsiVal, decimal emaVal, decimal atrVal)
{
if (candle.State != CandleStates.Finished)
return;
if (_prevRsi == 0)
{
_prevRsi = rsiVal;
return;
}
// Entry: RSI crosses threshold
if (Position == 0)
{
if (rsiVal < BuyThreshold && _prevRsi >= BuyThreshold)
{
BuyMarket();
}
else if (rsiVal > SellThreshold && _prevRsi <= SellThreshold)
{
SellMarket();
}
}
_prevRsi = rsiVal;
}
}
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, Unit, UnitTypes
from StockSharp.Algo.Strategies import Strategy
from StockSharp.Algo.Indicators import RelativeStrengthIndex, ExponentialMovingAverage, AverageTrueRange
class ard_order_management_stochastic_strategy(Strategy):
def __init__(self):
super(ard_order_management_stochastic_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Timeframe.", "General")
self._rsi_length = self.Param("RsiLength", 10) \
.SetDisplay("RSI Length", "RSI period.", "Indicators")
self._ema_length = self.Param("EmaLength", 14) \
.SetDisplay("EMA Length", "EMA trend filter period.", "Indicators")
self._atr_length = self.Param("AtrLength", 10) \
.SetDisplay("ATR Length", "ATR period for stops.", "Indicators")
self._buy_threshold = self.Param("BuyThreshold", 30.0) \
.SetDisplay("Buy Threshold", "RSI oversold level for buy.", "Signals")
self._sell_threshold = self.Param("SellThreshold", 70.0) \
.SetDisplay("Sell Threshold", "RSI overbought level for sell.", "Signals")
self._prev_rsi = 0.0
@property
def CandleType(self):
return self._candle_type.Value
@property
def RsiLength(self):
return self._rsi_length.Value
@property
def EmaLength(self):
return self._ema_length.Value
@property
def AtrLength(self):
return self._atr_length.Value
@property
def BuyThreshold(self):
return self._buy_threshold.Value
@property
def SellThreshold(self):
return self._sell_threshold.Value
def OnStarted2(self, time):
super(ard_order_management_stochastic_strategy, self).OnStarted2(time)
self._prev_rsi = 0.0
self._rsi = RelativeStrengthIndex()
self._rsi.Length = self.RsiLength
self._ema = ExponentialMovingAverage()
self._ema.Length = self.EmaLength
self._atr = AverageTrueRange()
self._atr.Length = self.AtrLength
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(self._rsi, self._ema, self._atr, self.ProcessCandle).Start()
self.StartProtection(
takeProfit=Unit(2, UnitTypes.Percent),
stopLoss=Unit(1, UnitTypes.Percent)
)
def ProcessCandle(self, candle, rsi_val, ema_val, atr_val):
if candle.State != CandleStates.Finished:
return
rv = float(rsi_val)
if self._prev_rsi == 0:
self._prev_rsi = rv
return
if not self.IsFormedAndOnlineAndAllowTrading():
self._prev_rsi = rv
return
# Entry: RSI crosses threshold
if self.Position == 0:
if rv < float(self.BuyThreshold) and self._prev_rsi >= float(self.BuyThreshold):
self.BuyMarket()
elif rv > float(self.SellThreshold) and self._prev_rsi <= float(self.SellThreshold):
self.SellMarket()
self._prev_rsi = rv
def OnReseted(self):
super(ard_order_management_stochastic_strategy, self).OnReseted()
self._prev_rsi = 0.0
def CreateClone(self):
return ard_order_management_stochastic_strategy()