Открыть на GitHub

Стратегия Mean Reversion Pro

Mean Reversion Pro — система возврата к среднему для основных индексов. Использует две скользящие средние и уровни диапазона свечи для поиска откатов. Предпочитаются длинные сделки, так как индексы имеют восходящее смещение.

Детали

  • Условия входа:
    • Лонг: цена закрытия ниже быстрой SMA, ниже уровня 20% диапазона, выше медленной SMA, позиция отсутствует.
    • Шорт: цена закрытия выше быстрой SMA, выше уровня 80% диапазона, ниже медленной SMA, позиция отсутствует.
  • Лонг/Шорт: Оба направления (рекомендуется лонг).
  • Условия выхода:
    • Лонг: цена пересекает быструю SMA вверх.
    • Шорт: цена пересекает быструю SMA вниз.
  • Стопы: нет.
  • Значения по умолчанию:
    • Быстрая SMA = 5
    • Медленная SMA = 100
    • Направление = только лонг
  • Фильтры:
    • Категория: Возврат к среднему
    • Направление: Настраиваемое
    • Индикаторы: SMA
    • Стопы: Нет
    • Сложность: Простая
    • Таймфрейм: Дневной
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

public class MeanReversionProStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<DataType> _candleType;

	private SimpleMovingAverage _fastSma;
	private SimpleMovingAverage _slowSma;

	public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
	public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public MeanReversionProStrategy()
	{
		_fastLength = Param(nameof(FastLength), 5);
		_slowLength = Param(nameof(SlowLength), 50);
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		_fastSma = new SimpleMovingAverage { Length = FastLength };
		_slowSma = new SimpleMovingAverage { Length = SlowLength };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_fastSma, _slowSma, ProcessCandle)
			.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fast, decimal slow)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_fastSma.IsFormed || !_slowSma.IsFormed)
			return;

		var range = candle.HighPrice - candle.LowPrice;
		var longThreshold = candle.LowPrice + 0.2m * range;
		var shortThreshold = candle.HighPrice - 0.2m * range;

		var longSignal = candle.ClosePrice < fast &&
			candle.ClosePrice < longThreshold &&
			candle.ClosePrice > slow;

		var shortSignal = candle.ClosePrice > fast &&
			candle.ClosePrice > shortThreshold &&
			candle.ClosePrice < slow;

		var exitLong = candle.ClosePrice > fast && Position > 0;
		var exitShort = candle.ClosePrice < fast && Position < 0;

		if (longSignal && Position <= 0)
			BuyMarket();
		else if (shortSignal && Position >= 0)
			SellMarket();
		else if (exitLong)
			SellMarket();
		else if (exitShort)
			BuyMarket();
	}
}