GitHub で見る

モメンタムブレイクアウト戦略

このブレイクアウトシステムは歴史的平均と比較してモメンタムの突然の急増を探します。モメンタムの読み取り値が平均を大きく超えると、価格が速い方向性のある動きを始める可能性があります。

テストでは平均年間リターンが約82%であることが示されています。株式市場で最も良いパフォーマンスを発揮します。

モメンタムが平均値にMultiplier倍の標準偏差を足した値を上回ったときに買いエントリーします。モメンタムが平均値から同じ乗数を引いた値を下回ったときにショートを開始します。モメンタムが平均値に向かって戻ったらポジションを決済します。

速い動きを好むトレーダーは、力強いバーストを捉えるための明確なルールを評価するかもしれません。価格のパーセンテージに基づいたストップロスが失敗したブレイクアウトから保護します。

詳細

  • エントリー条件:
    • ロング: Momentum > Avg + Multiplier * StdDev
    • ショート: Momentum < Avg - Multiplier * StdDev
  • ロング/ショート: 両方。
  • エグジット条件:
    • ロング: Momentum < Avg のときに決済
    • ショート: Momentum > Avg のときに決済
  • ストップ: あり、パーセントストップロス。
  • デフォルト値:
    • MomentumPeriod = 14
    • AveragePeriod = 20
    • Multiplier = 2.0m
    • CandleType = TimeSpan.FromMinutes(5)
  • フィルター:
    • カテゴリ: ブレイクアウト
    • 方向: 両方
    • インジケーター: Momentum
    • ストップ: あり
    • 複雑さ: 中級
    • 時間軸: イントラデイ
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Momentum Breakout Strategy (245).
/// Enter when momentum breaks out above/below its average by a certain multiple of standard deviation.
/// Exit when momentum returns to its average.
/// </summary>
public class MomentumBreakoutStrategy : Strategy
{
	private readonly StrategyParam<int> _momentumPeriod;
	private readonly StrategyParam<int> _averagePeriod;
	private readonly StrategyParam<decimal> _multiplier;
	private readonly StrategyParam<DataType> _candleType;

	private Momentum _momentum;
	private SimpleMovingAverage _momentumAverage;
	private StandardDeviation _momentumStdDev;
	
	private decimal? _currentMomentum;
	private decimal? _momentumAvgValue;
	private decimal? _momentumStdDevValue;

	/// <summary>
	/// Momentum period.
	/// </summary>
	public int MomentumPeriod
	{
		get => _momentumPeriod.Value;
		set => _momentumPeriod.Value = value;
	}

	/// <summary>
	/// Period for momentum average calculation.
	/// </summary>
	public int AveragePeriod
	{
		get => _averagePeriod.Value;
		set => _averagePeriod.Value = value;
	}

	/// <summary>
	/// Standard deviation multiplier for entry.
	/// </summary>
	public decimal Multiplier
	{
		get => _multiplier.Value;
		set => _multiplier.Value = value;
	}

	/// <summary>
	/// Type of candles to use.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Initializes a new instance of the <see cref="MomentumBreakoutStrategy"/>.
	/// </summary>
	public MomentumBreakoutStrategy()
	{
		_momentumPeriod = Param(nameof(MomentumPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("Momentum Period", "Period for momentum calculation", "Strategy Parameters")
			
			.SetOptimize(10, 20, 2);

		_averagePeriod = Param(nameof(AveragePeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("Average Period", "Period for momentum average calculation", "Strategy Parameters")
			
			.SetOptimize(10, 30, 5);

		_multiplier = Param(nameof(Multiplier), 2.0m)
			.SetGreaterThanZero()
			.SetDisplay("StdDev Multiplier", "Standard deviation multiplier for entry", "Strategy Parameters")
			
			.SetOptimize(1.0m, 3.0m, 0.5m);

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "Strategy Parameters");
	}

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

		_currentMomentum = default;
		_momentumAvgValue = default;
		_momentumStdDevValue = default;
	}


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

		// Create indicators
		_momentum = new Momentum { Length = MomentumPeriod };
		_momentumAverage = new SMA { Length = AveragePeriod };
		_momentumStdDev = new StandardDeviation { Length = AveragePeriod };

		// Create candle subscription
		var subscription = SubscribeCandles(CandleType);

		// Create processing chain
		subscription
			.Bind(_momentum, ProcessMomentum)
			.Start();

		// Setup chart visualization if available
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, _momentum);
			DrawOwnTrades(area);
		}

		// Enable position protection
		StartProtection(
			takeProfit: new Unit(5, UnitTypes.Percent),
			stopLoss: new Unit(2, UnitTypes.Percent)
		);
	}

	private void ProcessMomentum(ICandleMessage candle, decimal momentumValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		// Store the current momentum value
		_currentMomentum = momentumValue;

		// Process momentum through average and standard deviation indicators
		var avgIndicatorValue = _momentumAverage.Process(new DecimalIndicatorValue(_momentumAverage, momentumValue, candle.ServerTime) { IsFinal = true });
		var stdDevIndicatorValue = _momentumStdDev.Process(new DecimalIndicatorValue(_momentumStdDev, momentumValue, candle.ServerTime) { IsFinal = true });
		
		_momentumAvgValue = avgIndicatorValue.ToDecimal();
		_momentumStdDevValue = stdDevIndicatorValue.ToDecimal();
		
		if (!_momentumAverage.IsFormed || !_momentumStdDev.IsFormed)
			return;

		// Ensure we have all needed values
		if (!_currentMomentum.HasValue || !_momentumAvgValue.HasValue || !_momentumStdDevValue.HasValue)
			return;

		// Calculate bands
		var upperBand = _momentumAvgValue.Value + Multiplier * _momentumStdDevValue.Value;
		var lowerBand = _momentumAvgValue.Value - Multiplier * _momentumStdDevValue.Value;

		LogInfo($"Momentum: {_currentMomentum}, Avg: {_momentumAvgValue}, Upper: {upperBand}, Lower: {lowerBand}");

		// Entry logic - BREAKOUT (not mean reversion)
		if (Position == 0)
		{
			// Long Entry: Momentum breaks above upper band (strong upward momentum)
			if (_currentMomentum.Value > upperBand)
			{
				BuyMarket();
			}
			// Short Entry: Momentum breaks below lower band (strong downward momentum)
			else if (_currentMomentum.Value < lowerBand)
			{
				SellMarket();
			}
		}
	}
}