GitHub で見る

FORTS向けHFTスプレッダー

概要

この戦略はFORTS市場でのHFTスプレッダーの動作を再現します。注文板を継続的に監視し、ビッド・アスクスプレッドを取り込むために市場の両側に指値注文を出します。

戦略ロジック

  • リアルタイムの注文板更新を購読します。
  • ポジションがなく、スプレッドが十分に広い場合(SpreadMultiplier で決定)、戦略は以下を実行します:
    • 最良ビットの1ティック上に買い指値注文を出します。
    • 最良アスクの1ティック下に売り指値注文を出します。
  • ポジションが存在しアクティブな注文がない場合、ポジションをクローズして反転させるために反対側に単一の指値注文を出します。
  • 最良価格が動いた場合、注文はキャンセルされ、注文板の先頭に維持するために置き換えられます。

パラメーター

  • SpreadMultiplier – 買いと売り両方の注文を出すために必要なスプレッド(ティック単位)。デフォルトは4ティック。
  • Volume – 注文数量。デフォルトは1ロット。

使用上の注意

  • FORTS取引所の先物など、ティックサイズが小さい商品向けに設計されています。
  • 指値注文のみを使用します。必要な場合に保護メカニズムによるものを除き、成行注文は送信されません。
  • 効果的な運用のために十分な流動性と低遅延環境を確保してください。
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>
/// Strategy that captures the spread on FORTS by trading when spread is wide enough.
/// </summary>
public class HftSpreaderForFortsStrategy : Strategy
{
	private readonly StrategyParam<int> _spreadMultiplier;
	private readonly StrategyParam<DataType> _candleType;

	/// <summary>
	/// Required spread in ticks to place both buy and sell orders.
	/// </summary>
	public int SpreadMultiplier
	{
		get => _spreadMultiplier.Value;
		set => _spreadMultiplier.Value = value;
	}

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

	/// <summary>
	/// Initialize <see cref="HftSpreaderForFortsStrategy"/>.
	/// </summary>
	public HftSpreaderForFortsStrategy()
	{
		_spreadMultiplier = Param(nameof(SpreadMultiplier), 4)
			.SetGreaterThanZero()
			.SetDisplay("Spread Multiplier", "Spread ticks required to place orders", "General")
			.SetOptimize(1, 10, 1);

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
			.SetDisplay("Candle Type", "Candle type for price feed", "General");

		Volume = 1;
	}

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

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

		StartProtection(null, null);

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

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

		var step = Security.PriceStep ?? 1m;
		var spread = candle.HighPrice - candle.LowPrice;

		if (spread >= SpreadMultiplier * step)
		{
			if (Position == 0)
			{
				// Wide spread detected - buy at low, will exit when spread narrows
				BuyMarket();
			}
			else if (Position > 0)
			{
				// Close long position for spread capture
				SellMarket();
			}
			else if (Position < 0)
			{
				// Close short position for spread capture
				BuyMarket();
			}
		}
	}
}