Открыть на GitHub

Стратегия простого трейлинг-стопа

Пример демонстрирует управление открытой позицией с трейлинг-стопом на основе высокоуровневого API StockSharp.

Обзор

  • Открывает одну длинную позицию после получения первой завершённой свечи.
  • Включает защиту позиции с трейлинг-стопом.
  • Уровень стопа следует за ценой на фиксированном расстоянии.

Параметры

  • TrailPoints – расстояние в пунктах, на которое стоп следует за ценой.
  • CandleType – тип свечей, используемых стратегией.

Логика

  1. При старте стратегия подписывается на свечи и включает StartProtection с трейлинг-стопом.
  2. После первой завершённой свечи стратегия покупает по рынку.
  3. При движении цены в сторону позиции стоп переносится, сохраняя расстояние TrailPoints.
  4. Когда цена разворачивается и достигает трейлинг-стопа, позиция закрывается автоматически.

Стратегия упрощена и предназначена для демонстрации базового использования трейлинг-стопа.

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>
/// Simple trailing stop strategy.
/// Uses EMA crossover for entries with a trailing stop for protection.
/// </summary>
public class SimpleTrailingStopStrategy : Strategy
{
	private readonly StrategyParam<decimal> _trailPercent;
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<DataType> _candleType;

	public decimal TrailPercent { get => _trailPercent.Value; set => _trailPercent.Value = value; }
	public int FastLength { get => _fastLength.Value; set => _fastLength.Value = value; }
	public int SlowLength { get => _slowLength.Value; set => _slowLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public SimpleTrailingStopStrategy()
	{
		_trailPercent = Param(nameof(TrailPercent), 2m)
			.SetDisplay("Trail %", "Trailing stop distance as percentage", "Protection");

		_fastLength = Param(nameof(FastLength), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicator");

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

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(8).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "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(TrailPercent, UnitTypes.Percent),
			isStopTrailing: true,
			useMarketOrders: true);

		var fast = new ExponentialMovingAverage { Length = FastLength };
		var slow = new ExponentialMovingAverage { Length = SlowLength };

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