GitHub で見る

Straddle News戦略

高ボラティリティのニュース発表向けに設計された戦略です。現在価格の両側に対称的なストップ注文を設定し、ブレイクアウトを捉えます。一方の注文が約定すると、反対の未決注文がキャンセルされ、トレーリングストップでオープンポジションを保護します。

詳細

  • エントリー条件: スプレッドがSpreadOperationを下回るのを待ち、Ask + PipsAwayポイントに買いストップ、Bid - PipsAwayポイントに売りストップを設定する
  • ロング/ショート: 両方
  • エグジット条件: 保護的なストップロスまたはテイクプロフィット、あるいは価格がTrailingStopポイント戻った場合のトレーリングストップ
  • ストップ: StartProtectionによる初期ストップロスとテイクプロフィット;コード内のカスタムトレーリングストップ
  • デフォルト値:
    • StopLoss = 100
    • TakeProfit = 300
    • TrailingStop = 50
    • PipsAway = 50
    • BalanceUsed = 0.01
    • SpreadOperation = 25
    • Leverage = 400
  • フィルター:
    • カテゴリ: ブレイクアウト
    • 方向: 両方
    • インジケーター: なし
    • ストップ: はい
    • 複雑さ: 基本
    • 時間軸: Level1 / Tick
    • 季節性: いいえ
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 高

仕組み

  1. 現在のビッド価格とアスク価格にアクセスするためにLevel1クォートを購読します。
  2. スプレッドが十分小さくなったら、ポートフォリオ価値、レバレッジ、BalanceUsedを使用して取引量を計算します。
  3. PipsAwayで定義されたオフセットで保留中の買いと売りストップ注文を設定します。
  4. ポジションが開かれたら、反対の保留注文をキャンセルします。
  5. StopLossTakeProfitに基づいてストップロスとテイクプロフィット注文を付加します。
  6. エントリー以来の最高値/最安値を追跡し、価格がTrailingStopポイント以上戻った場合に決済します。
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>
/// EMA crossover strategy.
/// </summary>
public class StraddleNewsStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public StraddleNewsStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 12)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast EMA period", "Indicators");
		_slowPeriod = Param(nameof(SlowPeriod), 26)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow EMA period", "Indicators");
		_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();
		_prevFast = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

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

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		SubscribeCandles(CandleType)
			.Bind(fast, slow, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevSlow = slowVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
		var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevFast = fastVal;
		_prevSlow = slowVal;
	}
}