GitHub で見る

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

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

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

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

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

詳細

  • エントリー条件:
    • ロング: 終値 < MA - Multiplier * ATR / (ATR/StdDev)
    • ショート: 終値 > MA + Multiplier * ATR / (ATR/StdDev)
  • ロング/ショート: 両方。
  • エグジット条件:
    • ロング: 終値 >= MA の時に決済
    • ショート: 終値 <= MA の時に決済
  • ストップ: あり、ATRに基づく動的ストップ。
  • デフォルト値:
    • Period = 20
    • Multiplier = 2.0m
    • CandleType = TimeSpan.FromMinutes(5)
  • フィルター:
    • カテゴリ: 平均回帰
    • 方向: 両方
    • インジケーター: ATR, StdDev
    • ストップ: あり
    • 複雑さ: 中級
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Volatility Adjusted Mean Reversion strategy.
/// Uses ATR and Standard Deviation to create adaptive entry thresholds.
/// </summary>
public class VolatilityAdjustedMeanReversionStrategy : Strategy
{
	private readonly StrategyParam<int> _periodParam;
	private readonly StrategyParam<decimal> _multiplierParam;
	private readonly StrategyParam<DataType> _candleTypeParam;

	private SimpleMovingAverage _sma;
	private AverageTrueRange _atr;
	private StandardDeviation _stdDev;
	
	/// <summary>
	/// Period for indicators.
	/// </summary>
	public int Period
	{
		get => _periodParam.Value;
		set => _periodParam.Value = value;
	}

	/// <summary>
	/// Multiplier for entry threshold.
	/// </summary>
	public decimal Multiplier
	{
		get => _multiplierParam.Value;
		set => _multiplierParam.Value = value;
	}

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

	/// <summary>
	/// Constructor.
	/// </summary>
	public VolatilityAdjustedMeanReversionStrategy()
	{
		_periodParam = Param(nameof(Period), 20)
			.SetGreaterThanZero()
			.SetDisplay("Period", "Period for indicators", "Parameters")
			
			.SetOptimize(10, 50, 10);

		_multiplierParam = Param(nameof(Multiplier), 2.0m)
			.SetRange(0.1m, decimal.MaxValue)
			.SetDisplay("Multiplier", "Multiplier for entry threshold", "Parameters")
			
			.SetOptimize(1.0m, 3.0m, 0.5m);

		_candleTypeParam = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Candle type for strategy", "Common");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_sma = null;
		_atr = null;
		_stdDev = null;
	}

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

		// Create indicators
		_sma = new SMA { Length = Period };
		_atr = new AverageTrueRange { Length = Period };
		_stdDev = new StandardDeviation { Length = Period };

		// Create subscription and bind indicators
		var subscription = SubscribeCandles(CandleType);
		
		// First, bind SMA and ATR
		subscription
			.Bind(_sma, _atr, _stdDev, ProcessCandle)
			.Start();

		// Setup chart visualization if available
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, _sma);
			DrawIndicator(area, _atr);
			DrawOwnTrades(area);
		}
		
		// Enable position protection
		StartProtection(
			takeProfit: new Unit(0, UnitTypes.Absolute), // No take profit
			stopLoss: new Unit(2, UnitTypes.Absolute) // Stop loss at 2*ATR
		);
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;
		
		// Skip if standard deviation is too small to avoid division by zero
		if (stdDevValue < 0.0001m)
			return;
			
		// Calculate volatility ratio
		var volatilityRatio = atrValue / stdDevValue;
		
		// Calculate volatility-adjusted thresholds
		var threshold = Multiplier * atrValue / volatilityRatio;
		var upperThreshold = smaValue + threshold;
		var lowerThreshold = smaValue - threshold;
		
		// Long setup - price below lower threshold
		if (candle.ClosePrice < lowerThreshold && Position <= 0)
		{
			// Buy signal - price has deviated too much below average
			BuyMarket(Volume + Math.Abs(Position));
		}
		// Short setup - price above upper threshold
		else if (candle.ClosePrice > upperThreshold && Position >= 0)
		{
			// Sell signal - price has deviated too much above average
			SellMarket(Volume + Math.Abs(Position));
		}
		// Exit long position when price returns to average
		else if (Position > 0 && candle.ClosePrice >= smaValue)
		{
			// Close long position
			SellMarket(Position);
		}
		// Exit short position when price returns to average
		else if (Position < 0 && candle.ClosePrice <= smaValue)
		{
			// Close short position
			BuyMarket(Math.Abs(Position));
		}
	}
}