在 GitHub 上查看
OverHedge V2 网格策略
OverHedge V2 是一个对冲型网格系统,会在多空之间交替开仓,并在每次成交后按照倍数放大手数。策略通过比较快、慢指数移动平均线 (EMA) 的位置来确定新一轮循环的起始方向。循环启动时记录当前买价,构建一个包含价差的“隧道”,然后按方向立即成交第一单,反向订单则在价格触及隧道边界时执行。
随着价格继续逆行,网格会以“买→卖→买→…”的顺序不断扩展,每一单的手数都按乘数放大,从而在反弹时更快弥补浮亏。策略使用 Level 1 报价追踪最佳买卖价,分别维护多头与空头的平均价格与持仓量,一旦浮动利润达到目标值或用户开启停机选项,就会同时平掉所有头寸并重置循环。
工作流程
- 方向过滤:在收盘 K 线计算两条 EMA,快线高于慢线时从做多开始,否则从做空开始。
- 循环初始化:记录当前 Bid,并按照配置的宽度加上实时点差确定隧道上下边界。第一笔顺势成交,反向交易挂在隧道另一端等待触发。
- 网格扩张:如果行情继续逆势运行,策略交替发送买入和卖出市价单。每一笔成交的手数都会乘以对冲倍数。
- 利润结算:根据当前最佳买卖价计算浮动盈亏。达到
Profit Target 或者 Shutdown Grid 被激活时,策略会立即平掉所有仓位。
- 头寸管理:通过跟踪多空平均价与挂单状态,避免在未完成的订单上重复下单,并确保利润计算准确。
默认参数
Base Volume = 0.1 手 —— 第一笔订单的基础手数。
Hedge Multiplier = 2.0 —— 每次新增订单的手数倍数。
Tunnel Width (points) = 20 —— 在当前点差基础上额外增加的网格宽度(以点为单位)。
Profit Target = 100 —— 未实现利润达到该金额时关闭整个网格。
Short EMA = 8 —— 快速 EMA 周期。
Long EMA = 21 —— 慢速 EMA 周期。
Candle Type = 1 分钟 —— 计算 EMA 所使用的时间框架。
Shutdown Grid = false —— 为 true 时立即平仓并停止交易。
说明
- 需要有 Level 1 最优买卖价数据,较大的点差会自动让隧道变宽。
- 策略会根据合约的最小交易单位调整手数,避免下出无效订单。
- 由于采用类似马丁的加倍方式,若市场长时间单边运行,浮亏和保证金压力会迅速增加。
- 重新允许交易时,请把
Shutdown Grid 设回 false 或者重新启动策略。
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>
/// OverHedge V2 Grid strategy using RSI mean reversion with grid averaging.
/// Buy when RSI is oversold, sell when RSI is overbought.
/// </summary>
public class OverHedgeV2GridStrategy : Strategy
{
private readonly StrategyParam<int> _rsiPeriod;
private readonly StrategyParam<decimal> _oversold;
private readonly StrategyParam<decimal> _overbought;
private readonly StrategyParam<DataType> _candleType;
public int RsiPeriod { get => _rsiPeriod.Value; set => _rsiPeriod.Value = value; }
public decimal Oversold { get => _oversold.Value; set => _oversold.Value = value; }
public decimal Overbought { get => _overbought.Value; set => _overbought.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public OverHedgeV2GridStrategy()
{
_rsiPeriod = Param(nameof(RsiPeriod), 14)
.SetDisplay("RSI Period", "RSI period", "Indicators");
_oversold = Param(nameof(Oversold), 30m)
.SetDisplay("Oversold", "RSI oversold level", "Indicators");
_overbought = Param(nameof(Overbought), 70m)
.SetDisplay("Overbought", "RSI overbought level", "Indicators");
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(1).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
}
/// <inheritdoc />
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
}
/// <inheritdoc />
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var rsi = new RelativeStrengthIndex { Length = RsiPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(rsi, ProcessCandle)
.Start();
}
private void ProcessCandle(ICandleMessage candle, decimal rsiValue)
{
if (candle.State != CandleStates.Finished)
return;
if (rsiValue <= Oversold && Position <= 0)
{
if (Position < 0)
BuyMarket();
BuyMarket();
}
else if (rsiValue >= Overbought && Position >= 0)
{
if (Position > 0)
SellMarket();
SellMarket();
}
}
}
import clr
clr.AddReference("StockSharp.Messages")
clr.AddReference("StockSharp.Algo")
clr.AddReference("StockSharp.Algo.Indicators")
clr.AddReference("StockSharp.Algo.Strategies")
from System import TimeSpan, Math
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import RelativeStrengthIndex
from StockSharp.Algo.Strategies import Strategy
class over_hedge_v2_grid_strategy(Strategy):
"""OverHedge V2 Grid strategy using RSI mean reversion.
Buy when RSI is oversold, sell when RSI is overbought."""
def __init__(self):
super(over_hedge_v2_grid_strategy, self).__init__()
self._rsi_period = self.Param("RsiPeriod", 14) \
.SetDisplay("RSI Period", "RSI period", "Indicators")
self._oversold = self.Param("Oversold", 30.0) \
.SetDisplay("Oversold", "RSI oversold level", "Indicators")
self._overbought = self.Param("Overbought", 70.0) \
.SetDisplay("Overbought", "RSI overbought level", "Indicators")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(1))) \
.SetDisplay("Candle Type", "Candle timeframe", "General")
@property
def CandleType(self):
return self._candle_type.Value
@CandleType.setter
def CandleType(self, value):
self._candle_type.Value = value
@property
def RsiPeriod(self):
return self._rsi_period.Value
@property
def Oversold(self):
return self._oversold.Value
@property
def Overbought(self):
return self._overbought.Value
def OnReseted(self):
super(over_hedge_v2_grid_strategy, self).OnReseted()
def OnStarted2(self, time):
super(over_hedge_v2_grid_strategy, self).OnStarted2(time)
rsi = RelativeStrengthIndex()
rsi.Length = self.RsiPeriod
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(rsi, self._process_candle).Start()
def _process_candle(self, candle, rsi_value):
if candle.State != CandleStates.Finished:
return
rsi_val = float(rsi_value)
if rsi_val <= float(self.Oversold) and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif rsi_val >= float(self.Overbought) and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
def CreateClone(self):
return over_hedge_v2_grid_strategy()