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 that reacts to large single-bar moves expecting a mean reversion.
/// </summary>
public class InverseReactionStrategy : Strategy
{
private readonly StrategyParam<decimal> _stopLossPoints;
private readonly StrategyParam<decimal> _takeProfitPoints;
private readonly StrategyParam<decimal> _coefficient;
private readonly StrategyParam<int> _maPeriod;
private readonly StrategyParam<DataType> _candleType;
private readonly List<decimal> _absChanges = new();
public decimal StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
public decimal TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }
public decimal Coefficient { get => _coefficient.Value; set => _coefficient.Value = value; }
public int MaPeriod { get => _maPeriod.Value; set => _maPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public InverseReactionStrategy()
{
_stopLossPoints = Param(nameof(StopLossPoints), 1000m)
.SetGreaterThanZero()
.SetDisplay("Stop Loss", "Stop-loss distance in points", "Risk");
_takeProfitPoints = Param(nameof(TakeProfitPoints), 250m)
.SetGreaterThanZero()
.SetDisplay("Take Profit", "Take-profit distance in points", "Risk");
_coefficient = Param(nameof(Coefficient), 1.618m)
.SetGreaterThanZero()
.SetDisplay("Coefficient", "Confidence coefficient", "Signal");
_maPeriod = Param(nameof(MaPeriod), 3)
.SetGreaterThanZero()
.SetDisplay("MA Period", "Moving average length", "Signal");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(30).TimeFrame())
.SetDisplay("Candle Type", "Primary timeframe", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
protected override void OnReseted()
{
base.OnReseted();
_absChanges.Clear();
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var step = Security?.PriceStep ?? 1m;
if (step <= 0m) step = 1m;
StartProtection(
takeProfit: new Unit(TakeProfitPoints * step, UnitTypes.Absolute),
stopLoss: new Unit(StopLossPoints * step, UnitTypes.Absolute),
useMarketOrders: true);
var subscription = SubscribeCandles(CandleType);
subscription.Bind(ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle)
{
if (candle.State != CandleStates.Finished)
return;
var change = candle.ClosePrice - candle.OpenPrice;
var absChange = Math.Abs(change);
_absChanges.Add(absChange);
if (_absChanges.Count > MaPeriod)
_absChanges.RemoveAt(0);
if (_absChanges.Count < MaPeriod)
return;
var avg = 0m;
for (int i = 0; i < _absChanges.Count; i++)
avg += _absChanges[i];
avg /= _absChanges.Count;
var threshold = avg * Coefficient;
if (Position != 0)
return;
if (absChange > threshold && absChange > 0m)
{
if (change < 0m)
{
// Large bearish bar => expect reversion upward
BuyMarket();
}
else
{
// Large bullish bar => expect reversion downward
SellMarket();
}
}
}
}
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
class inverse_reaction_strategy(Strategy):
"""
Reacts to large single-bar moves expecting mean reversion.
Buys after large bearish bars, sells after large bullish bars.
Uses StartProtection for SL/TP.
"""
def __init__(self):
super(inverse_reaction_strategy, self).__init__()
self._stop_loss_points = self.Param("StopLossPoints", 1000.0) \
.SetDisplay("Stop Loss", "Stop-loss distance in points", "Risk")
self._take_profit_points = self.Param("TakeProfitPoints", 250.0) \
.SetDisplay("Take Profit", "Take-profit distance in points", "Risk")
self._coefficient = self.Param("Coefficient", 1.618) \
.SetDisplay("Coefficient", "Confidence coefficient", "Signal")
self._ma_period = self.Param("MaPeriod", 3) \
.SetDisplay("MA Period", "Moving average length", "Signal")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(30))) \
.SetDisplay("Candle Type", "Primary timeframe", "General")
self._abs_changes = []
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(inverse_reaction_strategy, self).OnReseted()
self._abs_changes = []
def OnStarted2(self, time):
super(inverse_reaction_strategy, self).OnStarted2(time)
step = 1.0
if self.Security is not None and self.Security.PriceStep is not None:
step = float(self.Security.PriceStep)
if step <= 0:
step = 1.0
sl = float(self._stop_loss_points.Value)
tp = float(self._take_profit_points.Value)
self.StartProtection(
Unit(tp * step, UnitTypes.Absolute),
Unit(sl * step, UnitTypes.Absolute),
False, None, None, True)
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._process_candle).Start()
def _process_candle(self, candle):
if candle.State != CandleStates.Finished:
return
close = float(candle.ClosePrice)
open_price = float(candle.OpenPrice)
change = close - open_price
abs_change = abs(change)
period = self._ma_period.Value
self._abs_changes.append(abs_change)
if len(self._abs_changes) > period:
self._abs_changes.pop(0)
if len(self._abs_changes) < period:
return
avg = sum(self._abs_changes) / len(self._abs_changes)
threshold = avg * self._coefficient.Value
if self.Position != 0:
return
if abs_change > threshold and abs_change > 0:
if change < 0:
self.BuyMarket()
else:
self.SellMarket()
def CreateClone(self):
return inverse_reaction_strategy()