在 GitHub 上查看
ZigZag Climber 策略
概述
fxDreema 生成的 ZigZag Climber 智能交易系统只有三个模块:一个 No trade 过滤器以及紧随其后的 Buy now、Sell now 操作。当终端确认当前没有持仓时,会立即按预设的止损/止盈距离下达一笔市价买单,并且不再做任何判断就挂出等量的市价卖单。两笔交易采用完全相同的风险设置,旨在形成对冲组合。
本 C# 版本在 StockSharp 中复刻了这一行为:策略等待所选周期的第一根完整 K 线,然后连续提交买入和卖出指令,并附加相同的保护距离。代码中没有额外的信号判定、持仓管理或移动止损逻辑,完全保持了原始 MQL 项目的结构。
交易流程
- 等待配置周期的第一根 K 线收盘。
- 如果允许交易且尚未下单,则按固定手数发送一笔市价买单。
- 使用 MetaTrader 风格的点值(通过
PriceStep 自动换算)为多头挂出止损与止盈。
- 立即按相同手数发送一笔市价卖单,并设置镜像的止损/止盈。
- 当两笔订单提交完成后,本次运行不再产生新的交易。
注意: MetaTrader 4 采用对冲模式,允许同一品种同时持有多空仓位。StockSharp 遵循底层经纪商的结算方式——在净额账户中,第二笔卖单会抵消第一笔买单,策略将以空仓结束。若需要保持双向持仓,请连接支持对冲的网关(例如配置为对冲账户的 MetaTrader 网关)。
参数
| 名称 |
默认值 |
说明 |
Candle Type |
1 分钟 |
用于触发一次性进场流程的周期。 |
Trade Volume |
0.01 |
两笔市价单共用的固定交易量。 |
Stop-Loss (pips) |
99.9 |
以 MetaTrader 点数表示的止损距离(自动兼容 4/5 位报价)。 |
Take-Profit (pips) |
100 |
以 MetaTrader 点数表示的止盈距离。 |
在调用 SetStopLoss/SetTakeProfit 之前,所有点数都会按照品种的 PriceStep 和小数精度换算为实际价格距离。
风险控制
策略依赖 StartProtection() 以及 SetStopLoss/SetTakeProfit 方法,在市价单提交后立刻挂出保护单。没有实现追踪止损或保本功能。
使用提示
- 启动前请选定交易品种和投资组合,并确保品种提供
PriceStep 与 Decimals,以便正确换算点值。
- 进场逻辑只会触发一次,如需重复执行,需要重新启动策略。
- 在净额模式的回测环境中,表现会与 MetaTrader 不同:卖单几乎会立即抵消买单。
using System;
using Ecng.Common;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// ZigZag Climber strategy: Highest/Lowest channel breakout.
/// Buys when close >= highest, sells when close <= lowest.
/// </summary>
public class ZigZagClimberStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _channelPeriod;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int ChannelPeriod
{
get => _channelPeriod.Value;
set => _channelPeriod.Value = value;
}
public ZigZagClimberStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
_channelPeriod = Param(nameof(ChannelPeriod), 12)
.SetGreaterThanZero()
.SetDisplay("Channel Period", "Highest/Lowest lookback", "Indicators");
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
var high = new Highest { Length = ChannelPeriod };
var low = new Lowest { Length = ChannelPeriod };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(high, low, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal high, decimal low)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
return;
if (candle.ClosePrice >= high && Position <= 0)
{
BuyMarket();
}
else if (candle.ClosePrice <= low && Position >= 0)
{
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
from StockSharp.Messages import DataType, CandleStates
from StockSharp.Algo.Indicators import Highest, Lowest
from StockSharp.Algo.Strategies import Strategy
class zig_zag_climber_strategy(Strategy):
def __init__(self):
super(zig_zag_climber_strategy, self).__init__()
self._channel_period = self.Param("ChannelPeriod", 12) \
.SetDisplay("Channel Period", "Highest/Lowest lookback", "Indicators")
self._highest = None
self._lowest = None
@property
def channel_period(self):
return self._channel_period.Value
def OnReseted(self):
super(zig_zag_climber_strategy, self).OnReseted()
self._highest = None
self._lowest = None
def OnStarted2(self, time):
super(zig_zag_climber_strategy, self).OnStarted2(time)
self._highest = Highest()
self._highest.Length = self.channel_period
self._lowest = Lowest()
self._lowest.Length = self.channel_period
subscription = self.SubscribeCandles(DataType.TimeFrame(TimeSpan.FromMinutes(5)))
subscription.Bind(self._highest, self._lowest, self._process_candle)
subscription.Start()
def _process_candle(self, candle, high_val, low_val):
if candle.State != CandleStates.Finished:
return
if not self._highest.IsFormed or not self._lowest.IsFormed:
return
close = float(candle.ClosePrice)
h = float(high_val)
l = float(low_val)
if close >= h and self.Position <= 0:
self.BuyMarket()
elif close <= l and self.Position >= 0:
self.SellMarket()
def CreateClone(self):
return zig_zag_climber_strategy()