在 GitHub 上查看
缺口回补策略
概述
Gaps 策略 是 MetaTrader 4 专家顾问 gaps.mq4 的直接移植版本。系统监控 15 分钟蜡烛图,并寻找在上一根蜡烛高低区间之外开盘的缺口。一旦出现缺口,策略立即建立仓位,期待价格向缺口方向回补。
StockSharp 版本遵循原始逻辑,完全基于高级蜡烛订阅 API 实现。和 MQL 代码一样,所有交易都通过市价单执行,不会自动放置固定的止损或止盈订单。
交易规则
- 订阅 15 分钟蜡烛(可通过
CandleType 参数调整)。
- 保存上一根已完成蜡烛的最高价和最低价。
- 当新蜡烛开始时:
- 计算缺口缓冲区:
(MinGapSize + spreadInSteps) * pointValue。
- 如果开盘价 高于
previousHigh + gapBuffer,则开立 空单。
- 如果开盘价 低于
previousLow - gapBuffer,则开立 多单。
- 每根蜡烛最多只允许一次交易。下单后必须等待下一根蜡烛才能生成新的信号。
当可用时,策略使用实时买价/卖价计算点差;若没有报价数据,则退回到单个价格步长作为保守的缓冲设置。
参数
| 参数 |
默认值 |
说明 |
MinGapSize |
1 |
触发交易所需的最小缺口大小(以价格步长表示)。 |
GapVolume |
0.1 |
缺口信号触发时下单的交易量。 |
CandleType |
15m TimeFrame |
用于计算的蜡烛类型(默认 15 分钟)。 |
所有参数都通过 StrategyParam<T> 注册,可在 StockSharp Designer 等工具中进行优化。
实现细节
- 使用
SubscribeCandles + Bind 组合,仅处理已完成的蜡烛。
- 持续保存上一根蜡烛的价格范围,避免在内存中维护额外数据序列。
- 通过记录触发交易的蜡烛开盘时间,阻止同一根蜡烛出现重复下单。
- 图表输出会绘制订阅的蜡烛以及策略交易,方便快速可视化检查。
与 MQL 版本的差异
- 原始 EA 中的止盈/止损参数传递位置错误,因此实际运行时相当于没有保护单。移植版保持这一行为,不会自动设置保护订单。
- 点差处理逻辑改为优先使用实时买卖报价,在无法获取报价时再退回到最小价格步长。
使用要求
- 需要具备 StockSharp API 以及对应品种的蜡烛数据。
- Level1 报价不是必须项,但会提升点差估计的准确度。
using System;
using System.Collections.Generic;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// Gap Reversion strategy - detects gap openings and trades mean reversion.
/// Buys when candle opens below previous low (gap down reversion).
/// Sells when candle opens above previous high (gap up reversion).
/// Uses EMA as trend filter for exits.
/// </summary>
public class GapReversionStrategy : Strategy
{
private readonly StrategyParam<int> _emaPeriod;
private readonly StrategyParam<DataType> _candleType;
private decimal _prevHigh;
private decimal _prevLow;
private bool _hasPrev;
public int EmaPeriod { get => _emaPeriod.Value; set => _emaPeriod.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public GapReversionStrategy()
{
_emaPeriod = Param(nameof(EmaPeriod), 20)
.SetDisplay("EMA Period", "EMA trend filter", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities() => [(Security, CandleType)];
protected override void OnReseted() { base.OnReseted(); _prevHigh = 0m; _prevLow = 0m; _hasPrev = false; }
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_hasPrev = false;
var ema = new ExponentialMovingAverage { Length = EmaPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(ema, ProcessCandle)
.Start();
StartProtection(
takeProfit: new Unit(2, UnitTypes.Percent),
stopLoss: new Unit(1, UnitTypes.Percent)
);
}
private void ProcessCandle(ICandleMessage candle, decimal ema)
{
if (candle.State != CandleStates.Finished)
return;
if (!_hasPrev)
{
_prevHigh = candle.HighPrice;
_prevLow = candle.LowPrice;
_hasPrev = true;
return;
}
var open = candle.OpenPrice;
var close = candle.ClosePrice;
// Gap down reversion - open below previous low, expect bounce
if (open < _prevLow && close > ema && Position == 0)
BuyMarket();
// Gap up reversion - open above previous high, expect pullback
else if (open > _prevHigh && close < ema && Position == 0)
SellMarket();
_prevHigh = candle.HighPrice;
_prevLow = candle.LowPrice;
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import ExponentialMovingAverage
from StockSharp.Algo.Strategies import Strategy
from StockSharp.Messages import Unit, UnitTypes
class gap_reversion_strategy(Strategy):
def __init__(self):
super(gap_reversion_strategy, self).__init__()
self._ema_period = self.Param("EmaPeriod", 20).SetDisplay("EMA Period", "EMA trend filter", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))).SetDisplay("Candle Type", "Candle timeframe", "General")
self._prev_high = 0.0
self._prev_low = 0.0
self._has_prev = False
@property
def ema_period(self): return self._ema_period.Value
@property
def candle_type(self): return self._candle_type.Value
def OnReseted(self):
super(gap_reversion_strategy, self).OnReseted()
self._prev_high = 0.0
self._prev_low = 0.0
self._has_prev = False
def OnStarted2(self, time):
super(gap_reversion_strategy, self).OnStarted2(time)
self._has_prev = False
ema = ExponentialMovingAverage()
ema.Length = self.ema_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(ema, self.process_candle).Start()
self.StartProtection(takeProfit=Unit(2, UnitTypes.Percent), stopLoss=Unit(1, UnitTypes.Percent))
def process_candle(self, candle, ema):
if candle.State != CandleStates.Finished:
return
if not self._has_prev:
self._prev_high = float(candle.HighPrice)
self._prev_low = float(candle.LowPrice)
self._has_prev = True
return
op = float(candle.OpenPrice)
close = float(candle.ClosePrice)
ema_val = float(ema)
if op < self._prev_low and close > ema_val and self.Position == 0:
self.BuyMarket()
elif op > self._prev_high and close < ema_val and self.Position == 0:
self.SellMarket()
self._prev_high = float(candle.HighPrice)
self._prev_low = float(candle.LowPrice)
def CreateClone(self):
return gap_reversion_strategy()