Ver no GitHub

Estratégia de Reversão à Média Ajustada pela Volatilidade

Esta variação da reversão à média escala os limiares de entrada pela razão entre ATR e desvio padrão. Quando a volatilidade aumenta em relação ao ruído típico, a distância necessária para acionar uma operação cresce, ajudando a evitar sinais prematuros durante oscilações caóticas.

Os testes indicam um retorno anual médio de aproximadamente 115%. Funciona melhor no mercado de ações.

Uma posição comprada é aberta quando o preço cai abaixo da média móvel em mais do limiar ajustado. Uma posição vendida é aberta quando o preço sobe acima da média pela mesma medida. As posições são encerradas assim que o preço fecha de volta próximo ao nível médio.

O limiar adaptativo torna esta estratégia adequada para mercados com regimes de volatilidade em mudança. Um stop-loss igual ao dobro do ATR limita o risco enquanto se aguarda a reversão.

Detalhes

  • Critérios de entrada:
    • Comprado: Fechamento < MA - Multiplier * ATR / (ATR/StdDev)
    • Vendido: Fechamento > MA + Multiplier * ATR / (ATR/StdDev)
  • Comprado/Vendido: Ambos os lados.
  • Critérios de saída:
    • Comprado: Sair quando fechamento >= MA
    • Vendido: Sair quando fechamento <= MA
  • Stops: Sim, dinâmico baseado em ATR.
  • Valores padrão:
    • Period = 20
    • Multiplier = 2 MA, ATR e StdDev (o desvio padrão dos fechamentos) abrangem Period candles; o limiar se reduz a Multiplier vezes StdDev. O stop fica a Multiplier vezes o ATR do fechamento de entrada, dois ATR por padrão, e é verificado no fechamento dos candles. Um sinal de entrada contra uma posição aberta a inverte.
    • CandleType = TimeSpan.FromMinutes(5)
  • Filtros:
    • Categoria: Reversão à média
    • Direção: Ambos
    • Indicadores: ATR, StdDev
    • Stops: Sim
    • Complexidade: Intermediário
    • Período: Intradiário
    • Sazonalidade: Não
    • Redes neurais: Não
    • Divergência: Não
    • Nível de risco: Médio
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>
/// Volatility Adjusted Mean Reversion strategy.
/// The threshold is Multiplier times the ATR divided by the ATR to standard deviation ratio, all over Period candles. A close more than the
/// threshold below the Period 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 VolatilityAdjustedMeanReversionStrategy : Strategy
{
	private readonly StrategyParam<int> _period;
	private readonly StrategyParam<decimal> _multiplier;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _stopPrice;

	/// <summary>
	/// Period of the SMA, ATR and standard deviation.
	/// </summary>
	public int Period
	{
		get => _period.Value;
		set => _period.Value = value;
	}

	/// <summary>
	/// Multiplier of the threshold 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 VolatilityAdjustedMeanReversionStrategy()
	{
		_period = Param(nameof(Period), 20)
			.SetGreaterThanZero()
			.SetDisplay("Period", "Period of the SMA, ATR and standard deviation", "Parameters");

		_multiplier = Param(nameof(Multiplier), 2m)
			.SetGreaterThanZero()
			.SetDisplay("Multiplier", "Multiplier of the threshold 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 = Period };
		var atr = new AverageTrueRange { Length = Period };
		var stdev = new StandardDeviation { Length = Period };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(sma, atr, stdev, 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, IIndicatorValue stdevValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

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

		var atr = atrValue.GetValue<decimal>();
		var deviation = stdevValue.GetValue<decimal>();

		if (atr <= 0 || deviation <= 0)
			return;

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		var sma = smaValue.GetValue<decimal>();
		var threshold = Multiplier * atr / (atr / deviation);
		var close = candle.ClosePrice;

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