GitHub で見る

MOC Delta MOO Entry v2 戦略

この戦略は午後セッション中の買い売り出来高を記録し、得られたMOCデルタを使って翌日の始値を取引します。

14:50から14:55にかけて高値・安値・買い/売り出来高を別々に蓄積します。14:55に総日次出来高に対する買い引く売りのデルタのパーセンテージを計算します。翌日の8:30に、デルタがしきい値を上回り、始値が15期間と30期間のSMAの両方を上回っている場合にロングエントリーします。ショートは逆の条件を使用します。ポジションにはティックベースのテイクプロフィットとストップロスが含まれ、14:50にクローズされます。

詳細

  • エントリー条件: 8:30に、デルタのパーセンテージがしきい値を上回りSMA15とSMA30を価格が上回っている場合にロング;デルタが負のしきい値を下回りSMAを価格が下回っている場合にショート。
  • ロング/ショート: 両方。
  • エグジット条件: テイクプロフィットまたはストップロス;14:50にすべてのポジションをクローズ。
  • ストップ: はい。
  • デフォルト値:
    • TpTicks = 20
    • SlTicks = 10
    • DeltaThreshold = 2
    • CandleType = TimeSpan.FromMinutes(1)
  • フィルター:
    • カテゴリ: モメンタム
    • 方向: 両方
    • インジケーター: 出来高、SMA
    • ストップ: はい
    • 複雑さ: 中級
    • 時間軸: イントラデイ
    • 季節性: はい
    • ニューラルネットワーク: いいえ
    • ダイバージェンス: いいえ
    • リスクレベル: 中
using System;

using Ecng.Common;

using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Momentum strategy based on aggregated volume delta windows.
/// </summary>
public class MocDeltaMooEntryV2Strategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _deltaWindow;
	private readonly StrategyParam<decimal> _deltaThresholdPercent;
	private readonly StrategyParam<int> _signalCooldownBars;

	private decimal _windowBuyVolume;
	private decimal _windowSellVolume;
	private int _windowBarCount;
	private int _barsFromSignal;

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

	/// <summary>
	/// Number of bars per delta window.
	/// </summary>
	public int DeltaWindow
	{
		get => _deltaWindow.Value;
		set => _deltaWindow.Value = value;
	}

	/// <summary>
	/// Absolute delta percent needed to trigger an entry.
	/// </summary>
	public decimal DeltaThresholdPercent
	{
		get => _deltaThresholdPercent.Value;
		set => _deltaThresholdPercent.Value = value;
	}

	/// <summary>
	/// Minimum bars between entries.
	/// </summary>
	public int SignalCooldownBars
	{
		get => _signalCooldownBars.Value;
		set => _signalCooldownBars.Value = value;
	}

	public MocDeltaMooEntryV2Strategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
			.SetDisplay("Candle Type", "Candles timeframe", "General");
		_deltaWindow = Param(nameof(DeltaWindow), 24)
			.SetGreaterThanZero()
			.SetDisplay("Delta Window", "Bars per delta calculation window", "General");
		_deltaThresholdPercent = Param(nameof(DeltaThresholdPercent), 12m)
			.SetGreaterThanZero()
			.SetDisplay("Delta Threshold %", "Minimum delta percent for momentum", "General");
		_signalCooldownBars = Param(nameof(SignalCooldownBars), 16)
			.SetGreaterThanZero()
			.SetDisplay("Signal Cooldown Bars", "Minimum bars between entries", "General");
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_windowBuyVolume = 0m;
		_windowSellVolume = 0m;
		_windowBarCount = 0;
		_barsFromSignal = 0;
	}

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

		_windowBuyVolume = 0m;
		_windowSellVolume = 0m;
		_windowBarCount = 0;
		_barsFromSignal = SignalCooldownBars;

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(ProcessCandle).Start();
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (candle.ClosePrice > candle.OpenPrice)
			_windowBuyVolume += candle.TotalVolume;
		else if (candle.ClosePrice < candle.OpenPrice)
			_windowSellVolume += candle.TotalVolume;
		else
		{
			_windowBuyVolume += candle.TotalVolume * 0.5m;
			_windowSellVolume += candle.TotalVolume * 0.5m;
		}

		_windowBarCount++;
		_barsFromSignal++;

		if (_windowBarCount < DeltaWindow)
			return;

		var totalVolume = _windowBuyVolume + _windowSellVolume;
		var deltaPercent = totalVolume > 0m
			? (_windowBuyVolume - _windowSellVolume) / totalVolume * 100m
			: 0m;

		var momentumSignal = 0;
		if (deltaPercent > DeltaThresholdPercent)
			momentumSignal = 1;
		else if (deltaPercent < -DeltaThresholdPercent)
			momentumSignal = -1;

		if (_barsFromSignal >= SignalCooldownBars && momentumSignal != 0)
		{
			if (momentumSignal > 0 && Position <= 0)
			{
				var volume = Volume + Math.Abs(Position);
				BuyMarket(volume);
				_barsFromSignal = 0;
			}
			else if (momentumSignal < 0 && Position >= 0)
			{
				var volume = Volume + Math.Abs(Position);
				SellMarket(volume);
				_barsFromSignal = 0;
			}
		}

		_windowBuyVolume = 0m;
		_windowSellVolume = 0m;
		_windowBarCount = 0;
	}
}