GitHub で見る

ボラティリティ・ブレイクアウト戦略

ボラティリティ・ブレイクアウト戦略は、価格が平均レンジから脱出したときの強い方向性のある動きを捉えます。ATRを使用して単純移動平均からの距離を測定することで、アルゴリズムはボラティリティに応じてスケールするブレイクアウト閾値を定義します。

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

終値がSMAをMultiplier倍のATR以上上回ると買いオーダーが発動されます。終値が同じ距離だけSMAを下回ると売りシグナルが現れます。反対方向のブレイクアウトが発生するか保護的なストップに達するまでポジションは開いたままです。

この手法は、モメンタムの急騰で利益を得る日中トレーダー向けです。ATRベースの閾値を使用することで、重要な動きだけが取引を生成するようノイズのフィルタリングに役立ちます。

詳細

  • エントリー条件:
    • ロング: Close > SMA + Multiplier * ATR
    • ショート: Close < SMA - Multiplier * ATR
  • ロング/ショート: 両方。
  • エグジット条件:
    • ロング: 反対方向のブレイクアウトが発動するかストップロスに達した時に決済
    • ショート: 反対方向のブレイクアウトが発動するかストップロスに達した時に決済
  • ストップ: あり、エントリーからMultiplier * ATRのストップロス。
  • デフォルト値:
    • Period = 20
    • Multiplier = 2.0m
    • CandleType = TimeSpan.FromMinutes(5)
  • フィルター:
    • カテゴリ: ブレイクアウト
    • 方向: 両方
    • インジケーター: SMA, ATR
    • ストップ: あり
    • 複雑さ: 中級
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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 Breakout strategy. Enters trades when price breaks out from average price with volatility threshold.
/// </summary>
public class VolatilityBreakoutStrategy : Strategy
{
	private readonly StrategyParam<int> _periodParam;
	private readonly StrategyParam<decimal> _multiplierParam;
	private readonly StrategyParam<DataType> _candleTypeParam;

	private SimpleMovingAverage _sma;
	private AverageTrueRange _atr;
	
	private decimal _prevSma;
	private decimal _prevAtr;

	/// <summary>
	/// Period for SMA and ATR calculations.
	/// </summary>
	public int Period
	{
		get => _periodParam.Value;
		set => _periodParam.Value = value;
	}

	/// <summary>
	/// Volatility multiplier for breakout 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 VolatilityBreakoutStrategy()
	{
		_periodParam = Param(nameof(Period), 20)
			.SetGreaterThanZero()
			.SetDisplay("Period", "Period for SMA and ATR", "Parameters")
			
			.SetOptimize(10, 50, 5);

		_multiplierParam = Param(nameof(Multiplier), 2.0m)
			.SetRange(0.1m, decimal.MaxValue)
			.SetDisplay("Multiplier", "Volatility multiplier for breakout 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;
		_prevSma = 0;
		_prevAtr = 0;
	}

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

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

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

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;
		
		// Save values for the next candle
		var currentSma = smaValue;
		var currentAtr = atrValue;
		
		// Skip first candle after indicators become formed
		if (_prevSma == 0 || _prevAtr == 0)
		{
			_prevSma = currentSma;
			_prevAtr = currentAtr;
			return;
		}
		
		// Calculate volatility threshold
		var threshold = Multiplier * currentAtr;
		
		// Check for long setup - price breaks above SMA + threshold
		if (candle.ClosePrice > currentSma + threshold && Position <= 0)
		{
			// Close any short position and open long
			BuyMarket(Volume + Math.Abs(Position));
		}
		// Check for short setup - price breaks below SMA - threshold
		else if (candle.ClosePrice < currentSma - threshold && Position >= 0)
		{
			// Close any long position and open short
			SellMarket(Volume + Math.Abs(Position));
		}
		
		// Update previous values for next candle
		_prevSma = currentSma;
		_prevAtr = currentAtr;
	}
}