在 GitHub 上查看

Accelerator Trailing TP & SL 策略

概述

Accelerator Trailing TP & SL 策略是将 MetaTrader 平台上的 "Accelerator Trailing TP&SL" 智能交易系统移植到 StockSharp 高级 API 的版本。策略结合了比尔·威廉姆斯的加速指标、多周期动量确认以及月线 MACD 趋势过滤。仓位按照几何级数逐步加码,退出部分则整合了固定止损止盈、移动止损以及保本移动。

交易逻辑

  • 动量过滤:在更高周期上计算的 14 周期动量指标,最近三根完成的 K 线中任意一根偏离 100 的绝对值必须达到设定阈值。
  • 加速指标:在信号周期上加速指标为正时允许做多,为负时允许做空。
  • 移动平均:快速线性加权移动平均 (LWMA) 必须高于慢速 LWMA 才能做多,反之才能做空,复现原策略中的趋势过滤。
  • 月线 MACD 趋势:默认使用月线数据。做多时要求 MACD 主线高于信号线(即使两者都为负),做空则要求主线低于信号线。
  • 阶梯式加仓:策略可以在同一方向上逐次加仓,直到达到最大层数。每次新仓位都会乘以指定的手数指数,模拟原始 EA 的马丁格尔加仓方式。

风险管理

  • 固定止损/止盈:以点数定义的止损和止盈距离与原策略一致。
  • 移动止损:启用后按照设定的点数对浮动盈利进行跟踪。
  • 保本移动:当浮动盈利达到触发距离时,将止损移动到设定的保本偏移位置,锁定利润。
  • MACD 反转退出:当高周期 MACD 与持仓方向相反时,可立即平掉全部仓位,对应原策略中的人工退出逻辑。

参数

参数 说明
FastMaLength / SlowMaLength 信号周期上快/慢 LWMA 的周期数。
MomentumThreshold 高周期动量指标相对 100 的最小绝对偏差。
StopLossPips / TakeProfitPips 固定止损与止盈的点数距离。
TrailingStopPips 移动止损的点数距离。
BreakEvenTriggerPips / BreakEvenOffsetPips 定义保本触发位置与移动后的偏移量。
MaxTrades 同一方向允许的最大加仓次数。
BaseVolume 第一笔仓位的交易量。
LotExponent 每次加仓的手数倍率。
EnableTrailing 是否启用移动止损。
UseBreakEven 是否启用保本移动。
CloseOnMacdFlip MACD 反向时是否立即清仓。
CandleType 主信号使用的 K 线周期(默认 15 分钟)。
MomentumCandleType 用于动量过滤的更高周期 K 线(默认 1 小时)。
MacdCandleType MACD 过滤使用的 K 线周期(默认月线)。

注意事项

  • 策略依赖品种的 PriceStep 信息来将点值转换为实际价格距离,运行前请确认交易标的的元数据已正确填充。
  • 由于 StockSharp 使用净持仓模式,加仓通过重复发送市价单实现,并在达到最大层数后停止。平仓将一次性关闭所有净持仓,与原始 EA 中的 "全部平仓" 行为一致。
  • 可通过 MacdCandleType 参数调整 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 AcceleratorTrailingTPSLStrategy : 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 AcceleratorTrailingTPSLStrategy()
	{
		_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;
	}
}