Открыть на GitHub

Стратегия возврата к среднему по ATR

Стратегия измеряет, насколько далеко цена отклоняется от скользящей средней относительно текущей волатильности. Индикатор ATR позволяет адаптировать порог: во времена повышенной активности он увеличивается, а в спокойные периоды уменьшается.

Тестирование показывает среднегодичную доходность около 109%. Стратегию лучше запускать на крипторынке.

Длинная позиция открывается, когда закрытие оказывается ниже скользящей средней более чем на Multiplier величин ATR. Короткая позиция — когда закрытие выше средней на ту же величину. Позиции закрываются, как только цена возвращается к средней.

Эта техника рассчитана на краткосрочных трейдеров, ожидающих возврата цены после чрезмерных движений. Стоп, основанный на ATR, делает риск пропорциональным рыночной волатильности.

Подробности

  • Условия входа:
    • Long: закрытие < MA − Multiplier * ATR
    • Short: закрытие > MA + Multiplier * ATR
  • Long/Short: обе стороны.
  • Условия выхода:
    • Long: выход при закрытии >= MA
    • Short: выход при закрытии <= MA
  • Стопы: да, по умолчанию около 2*ATR.
  • Параметры по умолчанию:
    • MaPeriod = 20
    • AtrPeriod = 14
    • Multiplier = 2 MA — простая скользящая средняя за MaPeriod, ATR — средний истинный диапазон за AtrPeriod. Стоп ставится на расстоянии Multiplier ATR от цены закрытия входа, по умолчанию 2 ATR, и проверяется по закрытию свечей. Сигнал на вход против открытой позиции разворачивает её.
    • CandleType = TimeSpan.FromMinutes(5)
  • Фильтры:
    • Категория: Возврат к среднему
    • Направление: Обе стороны
    • Индикаторы: MA, ATR
    • Стопы: Да
    • Сложность: Средняя
    • Таймфрейм: Внутридневной
    • Сезонность: Нет
    • Нейросети: Нет
    • Дивергенция: Нет
    • Уровень риска: Средний
using System;
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;

/// <summary>
/// ATR Mean Reversion strategy.
/// A close more than Multiplier times the AtrPeriod ATR below the MaPeriod simple moving average goes long and one that far above it goes
/// short, reversing an opposite position. A long closes once the close is back at or above the average and a short once it is back at or
/// below it. The stop lies Multiplier ATR from the entry close and is checked on candle closes.
/// </summary>
public class AtrMeanReversionStrategy : Strategy
{
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<int> _atrPeriod;
	private readonly StrategyParam<decimal> _multiplier;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _stopPrice;

	/// <summary>
	/// Period of the simple moving average.
	/// </summary>
	public int MaPeriod
	{
		get => _maPeriod.Value;
		set => _maPeriod.Value = value;
	}

	/// <summary>
	/// Period of the ATR.
	/// </summary>
	public int AtrPeriod
	{
		get => _atrPeriod.Value;
		set => _atrPeriod.Value = value;
	}

	/// <summary>
	/// ATR multiplier for the entry distance and the stop.
	/// </summary>
	public decimal Multiplier
	{
		get => _multiplier.Value;
		set => _multiplier.Value = value;
	}

	/// <summary>
	/// Candle type.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public AtrMeanReversionStrategy()
	{
		_maPeriod = Param(nameof(MaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("MA Period", "Period of the simple moving average", "Parameters");

		_atrPeriod = Param(nameof(AtrPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("ATR Period", "Period of the ATR", "Parameters");

		_multiplier = Param(nameof(Multiplier), 2m)
			.SetGreaterThanZero()
			.SetDisplay("Multiplier", "ATR multiplier for the entry distance and the stop", "Parameters");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).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();
		_stopPrice = default;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		_stopPrice = default;

		var sma = new SimpleMovingAverage { Length = MaPeriod };
		var atr = new AverageTrueRange { Length = AtrPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(sma, atr, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, sma);
			DrawOwnTrades(area);

			var oscillators = CreateChartArea();
			if (oscillators != null)
			{
				DrawIndicator(oscillators, atr);
			}
		}
	}

	private void ProcessCandle(ICandleMessage candle, IIndicatorValue smaValue, IIndicatorValue atrValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!smaValue.IsFormed || !atrValue.IsFormed)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var sma = smaValue.GetValue<decimal>();
		var distance = Multiplier * atrValue.GetValue<decimal>();
		var close = candle.ClosePrice;

		if (close < sma - distance && Position <= 0)
		{
			BuyMarket(Volume + Math.Abs(Position));
			_stopPrice = close - distance;
		}
		else if (close > sma + distance && Position >= 0)
		{
			SellMarket(Volume + Math.Abs(Position));
			_stopPrice = close + distance;
		}
		else if (Position > 0 && (close >= sma || close <= _stopPrice))
		{
			SellMarket(Position);
		}
		else if (Position < 0 && (close <= sma || close >= _stopPrice))
		{
			BuyMarket(-Position);
		}
	}
}