Lbs V12 策略
概述
Lbs V12 策略基于 MetaTrader 专家顾问 LBS_V12.mq4 改写。系统会在设定的触发小时开始时,围绕上一根 15 分钟 K 线区间布置一组突破性止损挂单。为了吸收短期波动,挂单价格会根据当前的平均真实波幅(ATR)数值进行偏移。持仓之后,策略利用虚拟止损、止盈和移动止损,在每根收盘 K 线上重新评估离场条件。
交易逻辑
- 仅处理所选时间框架的已完成 K 线(默认 15 分钟)。
- 当出现分钟数为
00的新 K 线,且其小时数等于参数TriggerHour时,上一根 K 线被视为参考区间。 - 当日没有持仓且没有活动挂单时,会同时发送两张止损单:
- Buy Stop:位于参考高点之上,再加上点差、一个最小价位和最新 ATR 值。
- Sell Stop:位于参考低点之下,再扣除同样的缓冲。
- 策略为多空双方记录虚拟保护价格:
- 止损设置在参考 K 线的另一端之外。
- 止盈按照 MetaTrader 风格的点数距离计算。
- 移动止损在利润超过指定距离后启动。
- 当仓位建立时,会取消相反方向的挂单。之后若 K 线的最高价或最低价触及保护价格,策略会用市价单退出。
- 策略每天只执行一次。切换到新的交易日时,会清理所有挂单并重置内部状态。
参数
| 名称 | 说明 | 默认值 |
|---|---|---|
Volume |
交易手数。 | 1 |
TriggerHour |
触发挂单的小时(终端时间)。 | 9 |
TakeProfitPoints |
入场价与止盈之间的点数。 | 100 |
TrailingStopPoints |
移动止损的点数距离。 | 20 |
AtrPeriod |
用于偏移挂单的 ATR 周期。 | 3 |
CandleType |
信号使用的 K 线类型,默认 15 分钟。 | 15 分钟 |
风险控制
- 当 K 线极值触及虚拟止损或止盈时,策略通过市价单平仓。
- 对于多头,移动止损会随着价格创新高而上移;对于空头,移动止损会随着价格创新低而下移。
- 每日重置避免了累积过期挂单或重复开仓。
注意事项
- 若能获取实时买卖价,点差补偿会更加准确;否则策略会退回到一个最小价位作为补偿。
- 转换版本保留了原始的默认参数,同时对空头止盈方向做了修正,以确保目标价格始终位于盈利方向。
using System;
using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;
namespace StockSharp.Samples.Strategies;
/// <summary>
/// LBS V12 strategy - ATR channel breakout with EMA trend.
/// Buys when close breaks above EMA + ATR.
/// Sells when close breaks below EMA - ATR.
/// </summary>
public class LbsV12Strategy : Strategy
{
private readonly StrategyParam<int> _emaPeriod;
private readonly StrategyParam<int> _atrPeriod;
private readonly StrategyParam<decimal> _atrMultiplier;
private readonly StrategyParam<int> _cooldownCandles;
private readonly StrategyParam<DataType> _candleType;
private int _cooldownRemaining;
public int EmaPeriod { get => _emaPeriod.Value; set => _emaPeriod.Value = value; }
public int AtrPeriod { get => _atrPeriod.Value; set => _atrPeriod.Value = value; }
public decimal AtrMultiplier { get => _atrMultiplier.Value; set => _atrMultiplier.Value = value; }
public int CooldownCandles { get => _cooldownCandles.Value; set => _cooldownCandles.Value = value; }
public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
public LbsV12Strategy()
{
_emaPeriod = Param(nameof(EmaPeriod), 50)
.SetDisplay("EMA Period", "EMA lookback", "Indicators");
_atrPeriod = Param(nameof(AtrPeriod), 14)
.SetDisplay("ATR Period", "ATR lookback", "Indicators");
_atrMultiplier = Param(nameof(AtrMultiplier), 3m)
.SetDisplay("ATR Multiplier", "Channel width multiplier", "Indicators");
_cooldownCandles = Param(nameof(CooldownCandles), 100)
.SetDisplay("Cooldown", "Candles between signals", "General");
_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
.SetDisplay("Candle Type", "Candle timeframe", "General");
}
/// <inheritdoc />
protected override void OnReseted()
{
base.OnReseted();
_cooldownRemaining = default;
}
protected override void OnStarted2(DateTime time)
{
base.OnStarted2(time);
_cooldownRemaining = 0;
var ema = new ExponentialMovingAverage { Length = EmaPeriod };
var atr = new AverageTrueRange { Length = AtrPeriod };
var subscription = SubscribeCandles(CandleType);
subscription.Bind(ema, atr, ProcessCandle).Start();
}
private void ProcessCandle(ICandleMessage candle, decimal ema, decimal atr)
{
if (candle.State != CandleStates.Finished) return;
if (_cooldownRemaining > 0)
{
_cooldownRemaining--;
return;
}
var close = candle.ClosePrice;
var mult = AtrMultiplier;
if (close > ema + atr * mult && Position <= 0)
{
if (Position < 0) BuyMarket();
BuyMarket();
_cooldownRemaining = CooldownCandles;
}
else if (close < ema - atr * mult && Position >= 0)
{
if (Position > 0) SellMarket();
SellMarket();
_cooldownRemaining = CooldownCandles;
}
}
}
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, AverageTrueRange
from StockSharp.Algo.Strategies import Strategy
class lbs_v12_strategy(Strategy):
def __init__(self):
super(lbs_v12_strategy, self).__init__()
self._ema_period = self.Param("EmaPeriod", 50) \
.SetDisplay("EMA Period", "EMA lookback", "Indicators")
self._atr_period = self.Param("AtrPeriod", 14) \
.SetDisplay("ATR Period", "ATR lookback", "Indicators")
self._atr_multiplier = self.Param("AtrMultiplier", 3.0) \
.SetDisplay("ATR Multiplier", "Channel width multiplier", "Indicators")
self._cooldown_candles = self.Param("CooldownCandles", 100) \
.SetDisplay("Cooldown", "Candles between signals", "General")
self._candle_type = self.Param("CandleType", DataType.TimeFrame(TimeSpan.FromMinutes(5))) \
.SetDisplay("Candle Type", "Candle timeframe", "General")
self._cooldown_remaining = 0
@property
def ema_period(self):
return self._ema_period.Value
@property
def atr_period(self):
return self._atr_period.Value
@property
def atr_multiplier(self):
return self._atr_multiplier.Value
@property
def cooldown_candles(self):
return self._cooldown_candles.Value
@property
def candle_type(self):
return self._candle_type.Value
def OnReseted(self):
super(lbs_v12_strategy, self).OnReseted()
self._cooldown_remaining = 0
def OnStarted2(self, time):
super(lbs_v12_strategy, self).OnStarted2(time)
self._cooldown_remaining = 0
ema = ExponentialMovingAverage()
ema.Length = self.ema_period
atr = AverageTrueRange()
atr.Length = self.atr_period
subscription = self.SubscribeCandles(self.candle_type)
subscription.Bind(ema, atr, self.process_candle).Start()
def process_candle(self, candle, ema, atr):
if candle.State != CandleStates.Finished:
return
if self._cooldown_remaining > 0:
self._cooldown_remaining -= 1
return
close = float(candle.ClosePrice)
ema_val = float(ema)
atr_val = float(atr)
mult = float(self.atr_multiplier)
if close > ema_val + atr_val * mult and self.Position <= 0:
if self.Position < 0:
self.BuyMarket()
self.BuyMarket()
self._cooldown_remaining = self.cooldown_candles
elif close < ema_val - atr_val * mult and self.Position >= 0:
if self.Position > 0:
self.SellMarket()
self.SellMarket()
self._cooldown_remaining = self.cooldown_candles
def CreateClone(self):
return lbs_v12_strategy()