在 GitHub 上查看

蜡烛追踪止损策略

Candle Trailing Stop 策略是同名 MetaTrader 专家的 StockSharp 版本。原始 EA 通过多周期趋势过滤、动量确认以及基于 蜡烛极值的激进追踪止损来管理仓位。本移植版完全采用 StockSharp 的高层 API,保留了原始逻辑,并以参数形式公开所有 重要设置。

策略流程

  1. 数据订阅
    • 交易周期负责生成进出场信号以及更新追踪止损;
    • 更高一个周期提供线性加权移动平均线(LWMA)和动量指标;
    • 第三个订阅在更慢的周期(默认月线)上计算 MACD 作为趋势确认。
  2. 趋势对齐:只有当快、中、慢三条 LWMA 在两个周期上都呈现多头(或空头)排列时才允许开仓。
  3. 动量过滤:最近三个高周期动量值中至少有一个需要接近 100。
  4. MACD 确认:做多要求 MACD 线高于信号线,做空则相反。
  5. 触发条件:价格在前一根蜡烛触碰快线,当前蜡烛收盘突破该均线并满足持仓上限时生成信号。
  6. 风控与退出
    • 初始止损和止盈用“点”表示并自动换算为价格步长;
    • 可启用移动保本、按蜡烛极值或固定距离的追踪止损;
    • 额外的资金管理功能包括金额/百分比止盈、浮动利润追踪和权益回撤保护。

参数

参数说明与英文 README 相同,默认值对应原始专家的设置,包含:

  • 交易量与最大持仓;
  • 两个周期上的 LWMA 长度、动量长度、MACD 参数;
  • 止损、止盈、保本触发、追踪步长等风险控制;
  • 金额与百分比止盈、权益追踪与回撤阈值;
  • 三个可独立配置的蜡烛数据类型。

注意事项

  • “点”会根据品种的最小价格变动自动转换,如一个点不等于一个最小跳动需手动调整距离参数。
  • 金额管理基于近似的未实现盈亏 (收盘价 - 均价) * 仓位,不会模拟点差、交换或手续费。
  • 止盈订单在开仓后注册,止损由策略内部计算并在价格触发时用市价单平仓。
  • 代码中的注释全部使用英文,以符合项目规范。
using System;
using System.Collections.Generic;

using Ecng.Common;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

public class CandleTrailingStopStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<int> _stopLossPoints;
	private readonly StrategyParam<int> _takeProfitPoints;

	private ExponentialMovingAverage _fast;
	private ExponentialMovingAverage _slow;

	private decimal _prevFast;
	private decimal _prevSlow;
	private decimal _entryPrice;
	private int _cooldown;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public int StopLossPoints { get => _stopLossPoints.Value; set => _stopLossPoints.Value = value; }
	public int TakeProfitPoints { get => _takeProfitPoints.Value; set => _takeProfitPoints.Value = value; }

	public CandleTrailingStopStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 14).SetGreaterThanZero().SetDisplay("Fast Period", "Fast EMA period", "Indicator");
		_slowPeriod = Param(nameof(SlowPeriod), 50).SetGreaterThanZero().SetDisplay("Slow Period", "Slow EMA period", "Indicator");
		_stopLossPoints = Param(nameof(StopLossPoints), 200).SetNotNegative().SetDisplay("Stop Loss", "Stop-loss in price steps", "Risk");
		_takeProfitPoints = Param(nameof(TakeProfitPoints), 400).SetNotNegative().SetDisplay("Take Profit", "Take-profit in price steps", "Risk");
	}

	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		yield return (Security, TimeSpan.FromMinutes(5).TimeFrame());
	}

	protected override void OnReseted()
	{
		base.OnReseted();
		_fast = null; _slow = null;
		_prevFast = 0; _prevSlow = 0; _entryPrice = 0; _cooldown = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		_fast = new ExponentialMovingAverage { Length = FastPeriod };
		_slow = new ExponentialMovingAverage { Length = SlowPeriod };
		var subscription = SubscribeCandles(TimeSpan.FromMinutes(5).TimeFrame());
		subscription.Bind(_fast, _slow, ProcessCandle);
		subscription.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
	{
		if (candle.State != CandleStates.Finished) return;
		if (!_fast.IsFormed || !_slow.IsFormed) { _prevFast = fastValue; _prevSlow = slowValue; return; }
		if (_cooldown > 0) { _cooldown--; _prevFast = fastValue; _prevSlow = slowValue; return; }

		var close = candle.ClosePrice;
		var step = Security?.PriceStep ?? 1m;

		if (Position > 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close <= _entryPrice - StopLossPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close >= _entryPrice + TakeProfitPoints * step) { SellMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}
		else if (Position < 0 && _entryPrice > 0)
		{
			if (StopLossPoints > 0 && close >= _entryPrice + StopLossPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
			if (TakeProfitPoints > 0 && close <= _entryPrice - TakeProfitPoints * step) { BuyMarket(); _entryPrice = 0; _cooldown = 100; _prevFast = fastValue; _prevSlow = slowValue; return; }
		}

		if (_prevFast <= _prevSlow && fastValue > slowValue && Position <= 0)
		{ if (Position < 0) BuyMarket(); BuyMarket(); _entryPrice = close; _cooldown = 100; }
		else if (_prevFast >= _prevSlow && fastValue < slowValue && Position >= 0)
		{ if (Position > 0) SellMarket(); SellMarket(); _entryPrice = close; _cooldown = 100; }

		_prevFast = fastValue; _prevSlow = slowValue;
	}
}