GitHub で見る

H4L4 ブレイクアウト戦略

前日の高値・安値・終値からH4とL4レベルを計算する日次ブレイクアウト戦略です。 毎日の開始時にH4に売り指値、L4に買い指値を設定します。 新規注文を出す前に、すべてのオープンポジションと未約定注文をキャンセルします。 ティックベースの距離を使用して保護的なストップロスとテイクプロフィットを適用します。

詳細

  • エントリー条件: 前日のローソク足から算出したH4での売り指値とL4での買い指値。
  • ロング/ショート: 両方向。
  • エグジット条件: ストップロスまたはテイクプロフィット。
  • ストップ: あり。
  • デフォルト値:
    • TakeProfit = 57
    • StopLoss = 7
    • CandleType = TimeSpan.FromDays(1)
  • フィルター:
    • カテゴリ: ブレイクアウト
    • 方向: 両方
    • インジケーター: なし
    • ストップ: あり
    • 複雑さ: 基本
    • 時間軸: 日足
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
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>
/// Breakout strategy based on calculated H4 and L4 levels.
/// When price range expands, places limit orders above and below to catch breakouts.
/// </summary>
public class H4L4BreakoutStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private decimal _prevHigh;
	private decimal _prevLow;
	private int _lastSignal;
	private bool _hasPrev;

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

	/// <summary>
	/// Initialize <see cref="H4L4BreakoutStrategy"/>.
	/// </summary>
	public H4L4BreakoutStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Working candle timeframe", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevHigh = 0m;
		_prevLow = 0m;
		_lastSignal = 0;
		_hasPrev = false;
	}

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

		var sma = new SimpleMovingAverage { Length = 10 };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(sma, (candle, ma) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				if (!_hasPrev)
				{
					_prevHigh = candle.HighPrice;
					_prevLow = candle.LowPrice;
					_hasPrev = true;
					return;
				}

				if (candle.ClosePrice > _prevHigh && candle.ClosePrice > ma && _lastSignal != 1 && Position <= 0)
				{
					BuyMarket();
					_lastSignal = 1;
				}
				else if (candle.ClosePrice < _prevLow && candle.ClosePrice < ma && _lastSignal != -1 && Position >= 0)
				{
					SellMarket();
					_lastSignal = -1;
				}

				_prevHigh = candle.HighPrice;
				_prevLow = candle.LowPrice;
			})
			.Start();

		StartProtection(
			new Unit(2000m, UnitTypes.Absolute),
			new Unit(1000m, UnitTypes.Absolute));
	}
}