在 GitHub 上查看

I Gap 策略

I Gap 策略 复刻自 MetaTrader 的 "i-GAP" 专家顾问。它监控上一根蜡烛的收盘价与当前蜡烛开盘价之间的价格缺口。若向下跳空超过设定的价格步数,可以开多单并可选择性地关闭空单;向上跳空则相反。

细节

  • 入场条件:相邻蜡烛之间的开盘缺口超过设定阈值。
  • 多/空:均可。
  • 出场条件:相反方向的缺口信号。
  • 止损:无固定止损或止盈。
  • 默认参数
    • CandleType = 1 小时
    • GapSize = 5
    • BuyPosOpen = true
    • SellPosOpen = true
    • BuyPosClose = true
    • SellPosClose = true
  • 筛选
    • 类型:缺口
    • 方向:双向
    • 指标:无
    • 止损:无
    • 复杂度:初级
    • 时间框架:日内
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:低
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 based on gap between previous close and current open.
/// </summary>
public class IGapStrategy : Strategy
{
	private readonly StrategyParam<decimal> _gapSize;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<bool> _buyPosOpen;
	private readonly StrategyParam<bool> _sellPosOpen;
	private readonly StrategyParam<bool> _buyPosClose;
	private readonly StrategyParam<bool> _sellPosClose;

	private decimal? _prevClose;

	/// <summary>
	/// Gap size in price steps.
	/// </summary>
	public decimal GapSize
	{
		get => _gapSize.Value;
		set => _gapSize.Value = value;
	}

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

	/// <summary>
	/// Allow opening long positions on gap down.
	/// </summary>
	public bool BuyPosOpen
	{
		get => _buyPosOpen.Value;
		set => _buyPosOpen.Value = value;
	}

	/// <summary>
	/// Allow opening short positions on gap up.
	/// </summary>
	public bool SellPosOpen
	{
		get => _sellPosOpen.Value;
		set => _sellPosOpen.Value = value;
	}

	/// <summary>
	/// Close long position on opposite signal.
	/// </summary>
	public bool BuyPosClose
	{
		get => _buyPosClose.Value;
		set => _buyPosClose.Value = value;
	}

	/// <summary>
	/// Close short position on opposite signal.
	/// </summary>
	public bool SellPosClose
	{
		get => _sellPosClose.Value;
		set => _sellPosClose.Value = value;
	}

	/// <summary>
	/// Constructor.
	/// </summary>
	public IGapStrategy()
	{
		_gapSize = Param(nameof(GapSize), 5m)
			.SetGreaterThanZero()
			.SetDisplay("Gap Size", "Gap in price steps required to trigger signal", "General")
			;

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe for gap detection", "General");

		_buyPosOpen = Param(nameof(BuyPosOpen), true)
			.SetDisplay("Enable Buy", "Allow opening long positions", "Trading");

		_sellPosOpen = Param(nameof(SellPosOpen), true)
			.SetDisplay("Enable Sell", "Allow opening short positions", "Trading");

		_buyPosClose = Param(nameof(BuyPosClose), true)
			.SetDisplay("Close Buy", "Close long on opposite signal", "Trading");

		_sellPosClose = Param(nameof(SellPosClose), true)
			.SetDisplay("Close Sell", "Close short on opposite signal", "Trading");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevClose = null;
	}

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

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

		StartProtection(
			takeProfit: new Unit(2, UnitTypes.Percent),
			stopLoss: new Unit(1, UnitTypes.Percent)
		);
	}

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

		if (_prevClose is null)
		{
			_prevClose = candle.ClosePrice;
			return;
		}

		// Use percentage-based gap: GapSize * 0.01%
		var threshold = _prevClose.Value * GapSize * 0.01m / 100m;
		var gap = _prevClose.Value - candle.OpenPrice;

		if (gap > threshold && Position == 0)
		{
			if (BuyPosOpen)
				BuyMarket();
		}
		else if (-gap > threshold && Position == 0)
		{
			if (SellPosOpen)
				SellMarket();
		}

		_prevClose = candle.ClosePrice;
	}
}