Стратегия средневозврата с поэтапным входом
Стратегия открывает позицию, когда цена отклоняется от простой скользящей средней на заданный процент. Дополнительные заявки добавляются по мере дальнейшего удаления цены от средней.
Позиция закрывается, когда цена возвращается к скользящей средней.
Детали
- Условия входа:
- Лонг:
Low < SMAи процентное отклонение междуLowиSMA≥Initial Percent. - Шорт:
High > SMAи процентное отклонение междуHighиSMA≥Initial Percent.
- Лонг:
- Дополнительные входы: Новые заявки добавляются каждые
Percent Stepот предыдущей цены входа. - Условия выхода:
- Лонг:
Close ≥ SMA. - Шорт:
Close ≤ SMA.
- Лонг:
- Индикаторы: SMA.
- Значения по умолчанию:
MA Length= 30.Initial Percent= 5.Percent Step= 1.
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>
/// Mean reversion strategy with incremental entries around a moving average.
/// </summary>
public class MeanReversionWithIncrementalEntryStrategy : Strategy
{
private readonly StrategyParam<int> _maLength;
private readonly StrategyParam<decimal> _initialPercent;
private readonly StrategyParam<decimal> _percentStep;
private readonly StrategyParam<int> _maxEntriesPerSide;
private readonly StrategyParam<DataType> _candleType;
private decimal? _lastBuyPrice;
private decimal? _lastSellPrice;
private int _buyEntries;
private int _sellEntries;
/// <summary>
/// Moving average length.
/// </summary>
public int MaLength
{
get => _maLength.Value;
set => _maLength.Value = value;
}
/// <summary>
/// Percent difference from MA for the first entry.
/// </summary>
public decimal InitialPercent
{
get => _initialPercent.Value;
set => _initialPercent.Value = value;
}
/// <summary>
/// Percent step for additional entries.
/// </summary>
public decimal PercentStep
{
get => _percentStep.Value;
set => _percentStep.Value = value;
}
/// <summary>
/// Maximum number of incremental entries per side.
/// </summary>
public int MaxEntriesPerSide
{
get => _maxEntriesPerSide.Value;
set => _maxEntriesPerSide.Value = value;
}
/// <summary>
/// Candle type used by the strategy.
/// </summary>
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
/// <summary>
/// Initializes parameters.
/// </summary>
public MeanReversionWithIncrementalEntryStrategy()
{
_maLength = Param(nameof(MaLength), 40)
.SetGreaterThanZero()
.SetDisplay("MA Length", "Moving average period", "Parameters")
.SetOptimize(10, 100, 10);
_initialPercent = Param(nameof(InitialPercent), 3.5m)
.SetGreaterThanZero()
.SetDisplay("Initial Percent", "Percent from MA for first entry", "Parameters")
.SetOptimize(1m, 10m, 1m);
_percentStep = Param(nameof(PercentStep), 2m)
.SetGreaterThanZero()
.SetDisplay("Percent Step", "Additional order percent step", "Parameters")
.SetOptimize(0.5m, 5m, 0.5m);
_maxEntriesPerSide = Param(nameof(MaxEntriesPerSide), 1)
.SetGreaterThanZero()
.SetDisplay("Max Entries Per Side", "Maximum incremental entries for each direction", "Parameters");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
.SetDisplay("Candle Type", "Type of candles to use", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_lastBuyPrice = null;
_lastSellPrice = null;
_buyEntries = 0;
_sellEntries = 0;
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var sma = new SMA { Length = MaLength };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(sma, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal maValue)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
var low = candle.LowPrice;
var high = candle.HighPrice;
var close = candle.ClosePrice;
if (low < maValue && Position <= 0)
{
if (_lastBuyPrice is null)
{
if (_buyEntries < MaxEntriesPerSide && PricePercentDiff(low, maValue) >= InitialPercent)
{
BuyMarket();
_lastBuyPrice = low;
_buyEntries++;
}
}
else if (_buyEntries < MaxEntriesPerSide && low < _lastBuyPrice && PricePercentDiff(low, _lastBuyPrice.Value) >= PercentStep)
{
BuyMarket();
_lastBuyPrice = low;
_buyEntries++;
}
}
if (high > maValue && Position >= 0)
{
if (_lastSellPrice is null)
{
if (_sellEntries < MaxEntriesPerSide && PricePercentDiff(high, maValue) >= InitialPercent)
{
SellMarket();
_lastSellPrice = high;
_sellEntries++;
}
}
else if (_sellEntries < MaxEntriesPerSide && high > _lastSellPrice && PricePercentDiff(high, _lastSellPrice.Value) >= PercentStep)
{
SellMarket();
_lastSellPrice = high;
_sellEntries++;
}
}
if (close >= maValue && Position > 0)
{
SellMarket(Position);
_lastBuyPrice = null;
_buyEntries = 0;
}
else if (close <= maValue && Position < 0)
{
BuyMarket(-Position);
_lastSellPrice = null;
_sellEntries = 0;
}
if (Position == 0)
{
_lastBuyPrice = null;
_lastSellPrice = null;
_buyEntries = 0;
_sellEntries = 0;
}
}
private static decimal PricePercentDiff(decimal price1, decimal price2)
{
return Math.Abs(price1 - price2) / price2 * 100m;
}
}
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 SimpleMovingAverage
from StockSharp.Algo.Strategies import Strategy
class mean_reversion_with_incremental_entry_strategy(Strategy):
def __init__(self):
super(mean_reversion_with_incremental_entry_strategy, self).__init__()
self._ma_length = self.Param("MaLength", 40) \
.SetGreaterThanZero() \
.SetDisplay("MA Length", "Moving average period", "Parameters")
self._initial_percent = self.Param("InitialPercent", 3.5) \
.SetGreaterThanZero() \
.SetDisplay("Initial Percent", "Percent from MA for first entry", "Parameters")
self._percent_step = self.Param("PercentStep", 2.0) \
.SetGreaterThanZero() \
.SetDisplay("Percent Step", "Additional order percent step", "Parameters")
self._max_entries_per_side = self.Param("MaxEntriesPerSide", 1) \
.SetGreaterThanZero() \
.SetDisplay("Max Entries Per Side", "Maximum incremental entries for each direction", "Parameters")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(15))) \
.SetDisplay("Candle Type", "Type of candles to use", "General")
self._last_buy_price = None
self._last_sell_price = None
self._buy_entries = 0
self._sell_entries = 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(mean_reversion_with_incremental_entry_strategy, self).OnReseted()
self._last_buy_price = None
self._last_sell_price = None
self._buy_entries = 0
self._sell_entries = 0
def OnStarted2(self, time):
super(mean_reversion_with_incremental_entry_strategy, self).OnStarted2(time)
self._last_buy_price = None
self._last_sell_price = None
self._buy_entries = 0
self._sell_entries = 0
self._sma = SimpleMovingAverage()
self._sma.Length = self._ma_length.Value
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(self._sma, self.OnProcess).Start()
def _price_pct_diff(self, p1, p2):
return abs(p1 - p2) / p2 * 100.0 if p2 != 0.0 else 0.0
def OnProcess(self, candle, ma_value):
if candle.State != CandleStates.Finished:
return
if not self._sma.IsFormed:
return
mv = float(ma_value)
low = float(candle.LowPrice)
high = float(candle.HighPrice)
close = float(candle.ClosePrice)
init_pct = float(self._initial_percent.Value)
step_pct = float(self._percent_step.Value)
max_e = self._max_entries_per_side.Value
if low < mv and self.Position <= 0:
if self._last_buy_price is None:
if self._buy_entries < max_e and self._price_pct_diff(low, mv) >= init_pct:
self.BuyMarket()
self._last_buy_price = low
self._buy_entries += 1
elif self._buy_entries < max_e and low < self._last_buy_price and self._price_pct_diff(low, self._last_buy_price) >= step_pct:
self.BuyMarket()
self._last_buy_price = low
self._buy_entries += 1
if high > mv and self.Position >= 0:
if self._last_sell_price is None:
if self._sell_entries < max_e and self._price_pct_diff(high, mv) >= init_pct:
self.SellMarket()
self._last_sell_price = high
self._sell_entries += 1
elif self._sell_entries < max_e and high > self._last_sell_price and self._price_pct_diff(high, self._last_sell_price) >= step_pct:
self.SellMarket()
self._last_sell_price = high
self._sell_entries += 1
if close >= mv and self.Position > 0:
self.SellMarket()
self._last_buy_price = None
self._buy_entries = 0
elif close <= mv and self.Position < 0:
self.BuyMarket()
self._last_sell_price = None
self._sell_entries = 0
if self.Position == 0:
self._last_buy_price = None
self._last_sell_price = None
self._buy_entries = 0
self._sell_entries = 0
def CreateClone(self):
return mean_reversion_with_incremental_entry_strategy()