Открыть на GitHub

Виртуальный трейлинг-стоп

Стратегия реализует виртуальный трейлинг-стоп для длинных и коротких позиций. Она не генерирует сигналы входа: позиции должны открываться вручную или внешней логикой. После появления позиции стратегия сопровождает её трейлинг-стопом, который смещается при движении цены в прибыльную сторону. При достижении цены уровня стопа позиция закрывается рыночным приказом.

Параметры

  • StopLoss – фиксированное расстояние стоп-лосса в шагах цены.
  • TakeProfit – фиксированное расстояние тейк-профита в шагах цены.
  • TrailingStop – расстояние от текущей цены до трейлинг-стопа.
  • TrailingStart – минимальная прибыль в шагах цены до начала сопровождения.
  • TrailingStep – минимальное дополнительное смещение в шагах цены для обновления трейлинг-стопа.
  • CandleType – тип свечей, используемых для расчётов.

Особенности

Стратегия обрабатывает только завершённые свечи выбранного типа.

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();
		}
	}
}