Ver en GitHub

Virtual Trailing Stop

This strategy emulates a virtual trailing stop for both long and short positions. It does not generate entry signals; orders should be opened externally or manually. Once a position exists, the strategy maintains a trailing stop that follows price as it moves in a favorable direction. If price hits the stop level, the position is closed by market.

Parameters

  • StopLoss – fixed stop-loss distance in price steps.
  • TakeProfit – fixed take-profit distance in price steps.
  • TrailingStop – distance from current price to the trailing stop.
  • TrailingStart – minimal profit in price steps before trailing begins.
  • TrailingStep – minimal additional profit required to move the trailing level.
  • CandleType – candle series used to process price data.

Notes

The strategy subscribes to candles of the specified type and evaluates the trailing logic on closed candles only.

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;

/// <summary>
/// Virtual trailing stop strategy.
/// Uses EMA crossover for entries with trailing stop protection.
/// </summary>
public class VirtualTrailingStopStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<decimal> _trailingPct;
	private readonly StrategyParam<DataType> _candleType;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public decimal TrailingPct { get => _trailingPct.Value; set => _trailingPct.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public VirtualTrailingStopStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicator");

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

		_trailingPct = Param(nameof(TrailingPct), 2m)
			.SetDisplay("Trailing %", "Trailing stop distance percentage", "Risk");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(8).TimeFrame())
			.SetDisplay("Candle Type", "Candle timeframe", "General");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
	}

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

		StartProtection(
			takeProfit: null,
			stopLoss: new Unit(TrailingPct, UnitTypes.Percent),
			isStopTrailing: true,
			useMarketOrders: true);

		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 fastVal, decimal slowVal)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (fastVal > slowVal && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (fastVal < slowVal && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}
	}
}