GitHub で見る

ブレイクスルー戦略 (BreakThrough)

BreakThrough戦略は、価格がユーザー定義のトレンドラインレベルを超えたときに取引を実行します。 2つの主要レベルが使用されます:

  • Buy Line – ロングポジションを発動させる価格レベル。
  • Sell Line – ショートポジションを発動させる価格レベル。

ラインが反対側から交差されると、戦略はその方向で市場に参入します。 オプションの追加ラインにより、価格が特定のレベルに触れたときにポジションを閉じることができます。 保護ストップロス、テイクプロフィット、トレーリングストップ距離はエントリー価格からpipで測定されます。

詳細

  • エントリー条件:
    • ロング: 価格がその初期位置に応じてBuy Lineを上下に交差する。
    • ショート: 価格がその初期位置に応じてSell Lineを上下に交差する。
  • ロング/ショート: 両サイド。
  • エグジット条件:
    • 価格がオプションのテイクプロフィットまたはストップロスラインに達する。
    • 価格がpipでのテイクプロフィットまたはストップロス距離に達する。
    • トレーリングストップが発動する。
  • ストップ: あり、StopLossPipsTakeProfitPipsTrailingStopPipsを使用。
  • デフォルト値:
    • BuyLinePrice = 0 (無効)
    • SellLinePrice = 0 (無効)
    • TakeProfitPips = 100
    • StopLossPips = 30
    • TrailingStopPips = 20
  • フィルター:
    • カテゴリ: ブレイクアウト
    • 方向: 両方
    • インジケーター: なし
    • ストップ: はい
    • 複雑さ: シンプル
    • 時間軸: 任意(デフォルト 1分)
    • リスクレベル: 中
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>
/// Breakout strategy using Highest/Lowest channel.
/// </summary>
public class BreakThroughStrategy : Strategy
{
	private readonly StrategyParam<int> _period;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevHigh;
	private decimal _prevLow;
	private bool _hasPrev;

	public int Period { get => _period.Value; set => _period.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public BreakThroughStrategy()
	{
		_period = Param(nameof(Period), 20)
			.SetGreaterThanZero()
			.SetDisplay("Period", "Channel lookback period", "Parameters");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
		=> [(Security, CandleType)];

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevHigh = 0;
		_prevLow = 0;
		_hasPrev = false;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var highest = new Highest { Length = Period };
		var lowest = new Lowest { Length = Period };

		SubscribeCandles(CandleType)
			.Bind(highest, lowest, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevHigh = high;
			_prevLow = low;
			_hasPrev = true;
			return;
		}

		var close = candle.ClosePrice;

		if (close > _prevHigh && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (close < _prevLow && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevHigh = high;
		_prevLow = low;
	}
}