在 GitHub 上查看

Lbs V12 策略

概述

Lbs V12 策略基于 MetaTrader 专家顾问 LBS_V12.mq4 改写。系统会在设定的触发小时开始时,围绕上一根 15 分钟 K 线区间布置一组突破性止损挂单。为了吸收短期波动,挂单价格会根据当前的平均真实波幅(ATR)数值进行偏移。持仓之后,策略利用虚拟止损、止盈和移动止损,在每根收盘 K 线上重新评估离场条件。

交易逻辑

  1. 仅处理所选时间框架的已完成 K 线(默认 15 分钟)。
  2. 当出现分钟数为 00 的新 K 线,且其小时数等于参数 TriggerHour 时,上一根 K 线被视为参考区间。
  3. 当日没有持仓且没有活动挂单时,会同时发送两张止损单:
    • Buy Stop:位于参考高点之上,再加上点差、一个最小价位和最新 ATR 值。
    • Sell Stop:位于参考低点之下,再扣除同样的缓冲。
  4. 策略为多空双方记录虚拟保护价格:
    • 止损设置在参考 K 线的另一端之外。
    • 止盈按照 MetaTrader 风格的点数距离计算。
    • 移动止损在利润超过指定距离后启动。
  5. 当仓位建立时,会取消相反方向的挂单。之后若 K 线的最高价或最低价触及保护价格,策略会用市价单退出。
  6. 策略每天只执行一次。切换到新的交易日时,会清理所有挂单并重置内部状态。

参数

名称 说明 默认值
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;
		}
	}
}