GitHub で見る

ボラティリティ調整済み平均回帰戦略

この平均回帰の変形版は、ATRと標準偏差の比率によってエントリー閾値をスケールします。典型的なノイズに対してボラティリティが増加すると、取引を発動させるのに必要な距離が大きくなり、混乱した変動の中での時期尚早なシグナルを回避するのに役立ちます。

テストによると、平均年間リターンは約115%です。株式市場で最も高いパフォーマンスを発揮します。

価格が調整済み閾値以上に移動平均を下回ると、ロングポジションが開かれます。価格が同じ幅だけ移動平均を上回ると、ショートポジションが開かれます。価格が平均水準付近に戻って終値をつけると、ポジションが決済されます。

適応的な閾値により、この戦略はボラティリティ体制が変化する市場に適しています。ATRの2倍に相当するストップロスにより、回帰を待つ間のリスクを制限します。

詳細

  • エントリー条件:
    • ロング: 終値 < MA - Multiplier * ATR / (ATR/StdDev)
    • ショート: 終値 > MA + Multiplier * ATR / (ATR/StdDev)
  • ロング/ショート: 両方。
  • エグジット条件:
    • ロング: 終値 >= MA の時に決済
    • ショート: 終値 <= MA の時に決済
  • ストップ: あり、ATRに基づく動的ストップ。
  • デフォルト値:
    • Period = 20
    • Multiplier = 2 MA、ATR、StdDev(終値の標準偏差)はすべてPeriod本で計算し、閾値はMultiplier × StdDevに簡約されます。ストップはエントリー時の終値からATRのMultiplier倍(既定では2 ATR)離れた位置に置かれ、ローソク足の終値で確認されます。 保有ポジションと逆方向のエントリーシグナルはドテンになります。
    • CandleType = TimeSpan.FromMinutes(5)
  • フィルター:
    • カテゴリ: 平均回帰
    • 方向: 両方
    • インジケーター: ATR, StdDev
    • ストップ: あり
    • 複雑さ: 中級
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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);
		}
	}
}