在 GitHub 上查看
Gann Grid 策略
该策略将 MQL/25065/Gann Grid.mq4 中的 Gann Grid 专家顾问迁移到 StockSharp 的高级 API。原始脚本依赖人工绘制的图形对象并在多个时间框架上过滤;C# 版本保留总体流程,同时用指标计算替代手工操作,使策略可以完全自动化运行。
交易逻辑
- 合成 Gann 网格:在主时间框架上取
AnchorPeriod 根 K 线的最高价和最低价,近似于 MetaTrader 中手绘的两条 Gann 线。向上突破最高价触发做多,向下跌破最低价触发做空。
- 趋势确认:在
TrendCandleType 指定的高一级时间框架上计算快、慢两条线性加权均线(LWMA),方向必须支持当前突破。
- 动量过滤:同一高时间框架上的 Momentum 指标与当前收盘价的百分比差值需大于
MomentumThreshold,以保证行情具有足够动能。
- MACD 过滤:通过
MacdCandleType 订阅的蜡烛序列计算 MACD(默认 12/26/9)。MACD 线必须与信号线、零轴在同一侧。
- 风险控制:从入场价开始应用对称的止盈与止损距离,可选的保本和跟踪止损模块复现原始 MQL 代码中的资金保护逻辑。
策略仅处理收盘后的 K 线,与原脚本检测新柱的方式保持一致。
与 MQL 版本的差异
- MetaTrader 版本需要手动绘制
GANNGRID 对象。移植版本使用 Highest/Lowest 指标自动生成网格,更适合回测与批量运行。
- MetaTrader 中的 Momentum 指标围绕 100 波动;StockSharp 的
Momentum 返回价格差值,因此策略将其换算成相对于收盘价的百分比再与 MomentumThreshold 比较。
- 邮件、推送等通知以及所有图形相关操作均被移除。
- 头寸管理通过市价单执行,不再修改已有挂单,因为 StockSharp 更侧重仓位级别的控制。
参数
| 名称 |
类型 |
默认值 |
说明 |
CandleType |
DataType |
5 分钟 |
用于识别突破的主时间框架。 |
TrendCandleType |
DataType |
15 分钟 |
计算 LWMA 与 Momentum 的高一级时间框架。 |
MacdCandleType |
DataType |
1 天 |
计算 MACD 的时间框架。 |
FastMaPeriod |
int |
6 |
高时间框架上的快 LWMA 长度。 |
SlowMaPeriod |
int |
85 |
高时间框架上的慢 LWMA 长度。 |
MomentumPeriod |
int |
14 |
Momentum 指标周期。 |
MomentumThreshold |
decimal |
0.3 |
Momentum 相对价格的最小百分比偏离。 |
AnchorPeriod |
int |
100 |
参与生成合成网格的主时间框架 K 线数量。 |
TakeProfitOffset |
decimal |
0.005 |
与入场价的绝对止盈距离。 |
StopLossOffset |
decimal |
0.002 |
与入场价的绝对止损距离。 |
EnableTrailing |
bool |
true |
是否启用跟踪止损。 |
TrailingActivation |
decimal |
0.003 |
跟踪止损启动前所需的利润。 |
TrailingStep |
decimal |
0.0015 |
启动后跟踪止损与局部高点/低点的距离。 |
EnableBreakEven |
bool |
true |
是否启用自动保本。 |
BreakEvenTrigger |
decimal |
0.0025 |
启动保本所需的利润。 |
BreakEvenOffset |
decimal |
0.0 |
保本平仓时相对入场价的偏移量。 |
MacdFastPeriod |
int |
12 |
MACD 中快 EMA 的周期。 |
MacdSlowPeriod |
int |
26 |
MACD 中慢 EMA 的周期。 |
MacdSignalPeriod |
int |
9 |
MACD 信号 EMA 的周期。 |
所有距离参数均以价格绝对值表示,请结合交易品种的最小报价单位调整(例如 0.001 ≈ 外汇五位报价的 10 点)。
使用建议
- 将策略绑定到目标证券,并设置需要的蜡烛类型。如果希望只使用单一时间框架,可以让多个参数指向同一
DataType。
- 根据市场波动调整
AnchorPeriod 及各类价格偏移。
- 按照风险偏好启用或关闭保本、跟踪止损功能。
- 启动策略后,系统会自动订阅所需蜡烛并通过市价单管理仓位。
文件结构
CS/GannGridStrategy.cs:策略实现。
README.md:英文说明。
README_ru.md:俄文说明。
README_zh.md:中文说明。
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;
public class GannGridStrategy : Strategy
{
private readonly StrategyParam<int> _fastPeriod;
private readonly StrategyParam<int> _slowPeriod;
private readonly StrategyParam<int> _stopLossPoints;
private readonly StrategyParam<int> _takeProfitPoints;
private ExponentialMovingAverage _fast;
private ExponentialMovingAverage _slow;
private decimal _prevFast;
private decimal _prevSlow;
private decimal _entryPrice;
private int _cooldown;
public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
public int StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
public int TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }
public GannGridStrategy()
{
_fastPeriod = Param(nameof(FastPeriod), 14).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
_slowPeriod = Param(nameof(SlowPeriod), 50).SetGreaterThanZero().SetDisplay("Slow Period", "Slow EMA period", "Indicator");
_stopLossPoints = Param(nameof(StopLossPoints), 200).SetNotNegative().SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");
_takeProfitPoints = Param(nameof(TakeProfitPoints), 400).SetNotNegative().SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
}
protected override void OnReseted()
{
base.OnReseted();
_fast = null; _slow = null;
_prevFast = 0; _prevSlow = 0; _entryPrice = 0; _cooldown = 0;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_fast = new ExponentialMovingAverage { Length = FastPeriod };
_slow = new ExponentialMovingAverage { Length = SlowPeriod };
var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
subscription.Bind(_fast, _slow, ProcessCandle);
subscription.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
{
if (candle.State != CandleStates.Finished) return;
if (!_fast.IsFormed || !_slow.IsFormed) { _prevFast = fastValue; _prevSlow = slowValue; return; }
if (_cooldown > 0) { _cooldown--; _prevFast = fastValue; _prevSlow = slowValue; return; }
var close = candle.ClosePrice;
var step = Security?.PriceStep ?? 1m;
if (Position > 0 && _entryPrice > 0)
{
if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
}
else if (Position < 0 && _entryPrice > 0)
{
if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
}
if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
{ if (Position < 0) BuyMarket(); BuyMarket(); _entryPrice = close; _cooldown = 100; }
else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
{ if (Position > 0) SellMarket(); SellMarket(); _entryPrice = close; _cooldown = 100; }
_prevFast = fastValue; _prevSlow = slowValue;
}
}
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
class gann_grid_strategy(Strategy):
def __init__(self):
super(gann_grid_strategy, self).__init__()
self._fast_period = self.Param("FastPeriod", 14) \
.SetDisplay("Fast Period", "Fast MA period", "Indicator")
self._slow_period = self.Param("SlowPeriod", 50) \
.SetDisplay("Slow Period", "Slow MA period", "Indicator")
self._stop_loss_points = self.Param("StopLossPoints", 200) \
.SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk")
self._take_profit_points = self.Param("TakeProfitPoints", 400) \
.SetDisplay("Take Profit", "Take-profit in price steps", "Risk")
self._fast = None
self._slow = None
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
@property
def fast_period(self):
return self._fast_period.Value
@property
def slow_period(self):
return self._slow_period.Value
@property
def stop_loss_points(self):
return self._stop_loss_points.Value
@property
def take_profit_points(self):
return self._take_profit_points.Value
def OnReseted(self):
super(gann_grid_strategy, self).OnReseted()
self._fast = None
self._slow = None
self._prev_fast = 0.0
self._prev_slow = 0.0
self._entry_price = 0.0
self._cooldown = 0
def OnStarted2(self, time):
super(gann_grid_strategy, self).OnStarted2(time)
self._fast = ExponentialMovingAverage()
self._fast.Length = self.fast_period
self._slow = ExponentialMovingAverage()
self._slow.Length = self.slow_period
subscription = self.SubscribeCandles(DataType.TimeFrame(TimeSpan.FromMinutes(5)))
subscription.Bind(self._fast, self._slow, self._process_candle)
subscription.Start()
def _process_candle(self, candle, fast_value, slow_value):
if candle.State != CandleStates.Finished:
return
fast_val = float(fast_value)
slow_val = float(slow_value)
if not self._fast.IsFormed or not self._slow.IsFormed:
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self._cooldown > 0:
self._cooldown -= 1
self._prev_fast = fast_val
self._prev_slow = slow_val
return
close = float(candle.ClosePrice)
step = float(self.Security.PriceStep) if self.Security is not None and self.Security.PriceStep is not None else 1.0
if self.Position > 0 and self._entry_price > 0:
if self.stop_loss_points > 0 and close <= self._entry_price - self.stop_loss_points * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self.take_profit_points > 0 and close >= self._entry_price + self.take_profit_points * step:
self.SellMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
elif self.Position < 0 and self._entry_price > 0:
if self.stop_loss_points > 0 and close >= self._entry_price + self.stop_loss_points * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self.take_profit_points > 0 and close <= self._entry_price - self.take_profit_points * step:
self.BuyMarket()
self._entry_price = 0.0
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
return
if self._prev_fast <= self._prev_slow and fast_val > slow_val and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
self._entry_price = close
self._cooldown = 100
elif self._prev_fast >= self._prev_slow and fast_val < slow_val and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._entry_price = close
self._cooldown = 100
self._prev_fast = fast_val
self._prev_slow = slow_val
def CreateClone(self):
return gann_grid_strategy()