在 GitHub 上查看
Breakdown Pending Stop 策略
策略概述
该策略复刻 MetaTrader 平台上的 "breakdown" 专家顾问,通过监控前一日的价格区间在新交易日自动挂出突破性买入/卖出止损单,并在仓位获利时使用阶梯式的追踪止损算法保护收益。
工作原理
- 日线准备:每日 K 线收盘后记录该日的最高价和最低价。下一交易日开始时先取消残余挂单,再在前高上方与前低下方分别放置买入止损和卖出止损,
Min Distance (ticks) 参数用于给水平留出缓冲距离。
- 挂单维护:无论挂单被触发还是进入新交易日,策略都会取消旧挂单并重新按相同的前一日区间提交新的挂单,从而始终保持双向突破机会,与原始 MQL 逻辑一致。
- 风控设置:成交后按照输入的 tick 距离自动计算止损与止盈位置。追踪止损要求价格先取得
Trailing Stop (ticks) + Trailing Step (ticks) 的利润后才会抬高/压低止损,实现与原 EA 相同的阶梯式移动。
- 退出机制:当价格触及当前止损或止盈时立即平仓;若触发追踪止损条件,则以市价平仓,效果等同于在 MetaTrader 中对仓位执行
PositionModify。
参数说明
| 参数 |
说明 |
Working Candles |
用于管理仓位和检查追踪止损的工作周期,默认 15 分钟。 |
Stop Loss (ticks) |
初始止损距离(按合约最小变动价位换算),为 0 则不设置。 |
Take Profit (ticks) |
初始止盈距离,为 0 则不设置。 |
Trailing Stop (ticks) |
追踪止损的核心距离,为 0 则关闭追踪止损。 |
Trailing Step (ticks) |
追踪止损每次移动所需额外盈利幅度。 |
Min Distance (ticks) |
挂单相对于前高/前低的附加偏移。 |
Order Volume |
两个挂单使用的下单数量。 |
使用提示
- 确保交易品种提供稳定的日线数据,以便正确计算上一交易日区间。
- 策略假设最小跳动价位固定,如合约 tick 值会变化需手动调整参数。
- 原 MQL 脚本中的基于账户权益的仓位计算未引入,仓位大小由
Order Volume 参数直接控制。
- 暂未提供该策略的 Python 版本。
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>
/// Breakout strategy using Highest/Lowest channel.
/// Enters long when price breaks above the channel high, enters short when price breaks below the channel low.
/// </summary>
public class BreakdownPendingStopStrategy : Strategy
{
private readonly StrategyParam<DataType> _candleType;
private readonly StrategyParam<int> _period;
private decimal? _prevHigh;
private decimal? _prevLow;
public DataType CandleType
{
get => _candleType.Value;
set => _candleType.Value = value;
}
public int Period
{
get => _period.Value;
set => _period.Value = value;
}
public BreakdownPendingStopStrategy()
{
_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
.SetDisplay("Candle Type", "Timeframe", "General");
_period = Param(nameof(Period), 20)
.SetGreaterThanZero()
.SetDisplay("Period", "Channel lookback period", "Indicators");
}
public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
{
return [(Security, CandleType)];
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_prevHigh = null;
_prevLow = null;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_prevHigh = null;
_prevLow = null;
var highest = new Highest { Length = Period };
var lowest = new Lowest { Length = Period };
var subscription = SubscribeCandles(CandleType);
subscription
.Bind(highest, lowest, ProcessCandle)
.Start();
var area = CreateChartArea();
if (area != null)
{
DrawCandles(area, subscription);
DrawIndicator(area, highest);
DrawIndicator(area, lowest);
DrawOwnTrades(area);
}
}
private void ProcessCandle(ICandleMessage candle, decimal high, decimal low)
{
if (candle.State != CandleStates.Finished)
return;
if (!IsFormedAndOnlineAndAllowTrading())
{
_prevHigh = high;
_prevLow = low;
return;
}
if (_prevHigh == null || _prevLow == null)
{
_prevHigh = high;
_prevLow = low;
return;
}
var close = candle.ClosePrice;
if (close > _prevHigh.Value && Position <= 0)
{
if (Position < 0)
BuyMarket();
BuyMarket();
}
else if (close < _prevLow.Value && Position >= 0)
{
if (Position > 0)
SellMarket();
SellMarket();
}
_prevHigh = high;
_prevLow = low;
}
}
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 breakdown_pending_stop_strategy(Strategy):
def __init__(self):
super(breakdown_pending_stop_strategy, self).__init__()
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromHours(4))) \
.SetDisplay("Candle Type", "Timeframe", "General")
self._period = self.Param("Period", 20) \
.SetDisplay("Period", "Channel lookback period", "Indicators")
self._prev_high = None
self._prev_low = None
@property
def CandleType(self):
return self._candle_type.Value
@property
def Period(self):
return self._period.Value
def OnReseted(self):
super(breakdown_pending_stop_strategy, self).OnReseted()
self._prev_high = None
self._prev_low = None
def OnStarted2(self, time):
super(breakdown_pending_stop_strategy, self).OnStarted2(time)
self._prev_high = None
self._prev_low = None
highest = Highest()
highest.Length = self.Period
lowest = Lowest()
lowest.Length = self.Period
subscription = self.SubscribeCandles(self.CandleType)
subscription.Bind(highest, lowest, self._on_process).Start()
area = self.CreateChartArea()
if area is not None:
self.DrawCandles(area, subscription)
self.DrawIndicator(area, highest)
self.DrawIndicator(area, lowest)
self.DrawOwnTrades(area)
def _on_process(self, candle, high_value, low_value):
if candle.State != CandleStates.Finished:
return
hv = float(high_value)
lv = float(low_value)
if self._prev_high is None or self._prev_low is None:
self._prev_high = hv
self._prev_low = lv
return
close = float(candle.ClosePrice)
if close > self._prev_high and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
elif close < self._prev_low and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._prev_high = hv
self._prev_low = lv
def CreateClone(self):
return breakdown_pending_stop_strategy()