在 GitHub 上查看

通用跟踪止损策略

该策略基于原始的 MQL4 脚本 cm_universal_trailing_stop.mq4。策略本身不生成入场信号,而是在已有仓位上根据盈利自动上移或下移止损。

算法保持与当前价格的固定偏移,当价格按照设定步长移动时更新止损。当达到最小盈利阈值后,跟踪止损被激活,并在多头和空头头寸上自动跟随价格。

细节

  • 入场条件:无。仓位需手动或由其他策略开立。
  • 多/空方向:均可。
  • 出场条件:价格回撤到设定偏移时触发止损单。
  • 止损:基于点数的跟踪止损。
  • 参数
    • Delta – 价格到止损的距离(点)。
    • Step – 移动止损所需的最小价格变动(点)。
    • StartProfit – 激活跟踪所需的盈利(点)。
    • CandleType – 用于计算的时间框架。
  • 过滤器
    • 分类:风险管理
    • 方向:双向
    • 指标:无
    • 止损:跟踪
    • 复杂度:简单
    • 时间框架:任意
    • 季节性:无
    • 神经网络:无
    • 背离:无
    • 风险等级:中等
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>
/// Strategy that enters on EMA crossover and manages position with a trailing stop.
/// Uses two EMAs for entries and a price-based trailing stop for exits.
/// </summary>
public class UniversalTrailingStopStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<decimal> _trailPercent;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _isInitialized;
	private decimal _entryPrice;
	private decimal _bestPrice;

	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	public int FastPeriod
	{
		get => _fastPeriod.Value;
		set => _fastPeriod.Value = value;
	}

	public int SlowPeriod
	{
		get => _slowPeriod.Value;
		set => _slowPeriod.Value = value;
	}

	public decimal TrailPercent
	{
		get => _trailPercent.Value;
		set => _trailPercent.Value = value;
	}

	public UniversalTrailingStopStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle timeframe", "General");

		_fastPeriod = Param(nameof(FastPeriod), 10)
			.SetDisplay("Fast Period", "Fast EMA period", "Entry");

		_slowPeriod = Param(nameof(SlowPeriod), 30)
			.SetDisplay("Slow Period", "Slow EMA period", "Entry");

		_trailPercent = Param(nameof(TrailPercent), 1.5m)
			.SetDisplay("Trail %", "Trailing stop distance in percent", "Trailing");
	}

	/// <inheritdoc />
	public override IEnumerable<(Security sec, DataType dt)> GetWorkingSecurities()
	{
		return [(Security, CandleType)];
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevSlow = 0;
		_isInitialized = false;
		_entryPrice = 0;
		_bestPrice = 0;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		_prevFast = 0;
		_prevSlow = 0;
		_isInitialized = false;
		_entryPrice = 0;
		_bestPrice = 0;

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fast, slow, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, fast);
			DrawIndicator(area, slow);
			DrawOwnTrades(area);
		}
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastValue, decimal slowValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_isInitialized)
		{
			_prevFast = fastValue;
			_prevSlow = slowValue;
			_isInitialized = true;
			return;
		}

		var price = candle.ClosePrice;

		// Trailing stop check
		if (Position > 0)
		{
			if (price > _bestPrice)
				_bestPrice = price;

			var stopLevel = _bestPrice * (1 - TrailPercent / 100m);
			if (price <= stopLevel)
			{
				SellMarket();
				_prevFast = fastValue;
				_prevSlow = slowValue;
				return;
			}
		}
		else if (Position < 0)
		{
			if (price < _bestPrice)
				_bestPrice = price;

			var stopLevel = _bestPrice * (1 + TrailPercent / 100m);
			if (price >= stopLevel)
			{
				BuyMarket();
				_prevFast = fastValue;
				_prevSlow = slowValue;
				return;
			}
		}

		// Entry signals: EMA crossover
		var prevCrossUp = _prevFast <= _prevSlow;
		var currCrossUp = fastValue > slowValue;
		var prevCrossDown = _prevFast >= _prevSlow;
		var currCrossDown = fastValue < slowValue;

		if (prevCrossUp && currCrossUp && !(_prevFast > _prevSlow))
		{
			if (Position < 0)
				BuyMarket();
			if (Position <= 0)
			{
				BuyMarket();
				_entryPrice = price;
				_bestPrice = price;
			}
		}
		else if (prevCrossDown && currCrossDown && !(_prevFast < _prevSlow))
		{
			if (Position > 0)
				SellMarket();
			if (Position >= 0)
			{
				SellMarket();
				_entryPrice = price;
				_bestPrice = price;
			}
		}

		_prevFast = fastValue;
		_prevSlow = slowValue;
	}
}