Открыть на GitHub

Ema612CrossoverStrategy

Краткое описание

  • Порт советника MetaTrader 5 "EMA 6.12 (barabashkakvn's edition)" на высокоуровневый API StockSharp.
  • Использует пересечение быстрой и медленной простых скользящих средних (в оригинале также применялась MODE_SMA, несмотря на название EMA).
  • Реализует необязательные тейк-профит и трейлинг-стоп в абсолютных ценовых единицах, чтобы пользователь мог адаптировать значения под свой инструмент.

Логика торговли

Подготовка данных

  • Стратегия подписывается на свечи типа CandleType (по умолчанию 15-минутные бары).
  • Рассчитываются две SMA: быстрая длиной FastPeriod и медленная длиной SlowPeriod. Значение SlowPeriod обязательно должно быть больше FastPeriod.

Условия входа

  • Сигналы формируются только после закрытия свечи.
  • Бычье пересечение: медленная SMA была выше быстрой на предыдущей свече и опустилась ниже на текущей. Любая короткая позиция закрывается, затем открывается длинная позиция объёмом Volume.
  • Медвежье пересечение: медленная SMA была ниже быстрой на предыдущей свече и поднялась выше на текущей. Любая длинная позиция закрывается, затем открывается короткая позиция объёмом Volume.

Условия выхода

  • Позиция закрывается при обратном пересечении скользящих средних.
  • При TakeProfitOffset > 0 фиксируется цель: для лонга выход по цене entry + TakeProfitOffset, для шорта entry - TakeProfitOffset.
  • При TrailingStopOffset > 0 активируется трейлинг-стоп. Он начинает срабатывать только после того, как нереализованная прибыль превысит TrailingStopOffset + TrailingStepOffset. Затем стоп подтягивается на расстояние TrailingStopOffset от цены закрытия, но лишь если новое значение ближе к цене минимум на TrailingStepOffset. Для лонгов используется минимум свечи, для шортов максимум.

Параметры

Параметр Значение по умолчанию Описание
CandleType Таймфрейм 15 минут Таймфрейм свечей для расчётов и сигналов.
FastPeriod 6 Период быстрой SMA. Должен быть > 0 и меньше SlowPeriod.
SlowPeriod 54 Период медленной SMA. Должен быть > 0 и больше FastPeriod.
Volume 1 Объём заявки при открытии новой позиции.
TakeProfitOffset 0.001 Абсолютное расстояние до тейк-профита от цены входа. Установите 0, чтобы отключить.
TrailingStopOffset 0.005 Абсолютное расстояние между ценой и трейлинг-стопом. 0 — отключение трейлинга.
TrailingStepOffset 0.0005 Дополнительное движение цены в прибыль, необходимое для подтяжки стопа.

Важно: значения задаются в абсолютных ценовых единицах. Подбирайте их в соответствии с шагом цены инструмента (например, для EURUSD с шагом 0.0001 значения 0.001, 0.005 и 0.0005 соответствуют 10, 50 и 5 пунктам).

Особенности реализации

  • Используется высокоуровневый пайплайн SubscribeCandles().Bind() в соответствии с требованиями проекта.
  • При наличии графика стратегия отображает обе SMA и сделки.
  • Вспомогательные переменные отслеживают цену входа, уровень трейлинг-стопа и тейк-профит аналогично версии на MQL5.
  • В конструкторе выполняется проверка SlowPeriod > FastPeriod, чтобы избежать некорректной настройки индикаторов.

Рекомендации по использованию

  • Оптимизируйте таймфрейм свечей и периоды SMA под ваш рынок (короче для скальпинга, длиннее для свинговой торговли).
  • Переводите значения трейлинга и тейк-профита из пунктов/тиков в абсолютные единицы цены перед запуском.
  • Чтобы отключить трейлинг, установите TrailingStopOffset в ноль — тогда выход будет происходить только по противоположному пересечению или тейк-профиту.
using System;
using System.Linq;
using System.Collections.Generic;

using Ecng.Common;
using Ecng.Collections;
using Ecng.Serialization;

using StockSharp.Algo.Indicators;
using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// EMA 6/12 crossover strategy with trailing stop management.
/// </summary>
public class Ema612CrossoverStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<decimal> _takeProfitOffset;
	private readonly StrategyParam<decimal> _trailingStopOffset;
	private readonly StrategyParam<decimal> _trailingStepOffset;

	private ExponentialMovingAverage _fastSma;
	private ExponentialMovingAverage _slowSma;

	private decimal? _prevFast;
	private decimal? _prevSlow;

	private decimal? _entryPrice;
	private decimal? _stopPrice;
	private decimal? _takeProfitPrice;

	/// <summary>
	/// Candle type for calculations.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Fast moving average period.
	/// </summary>
	public int FastPeriod
	{
		get => _fastPeriod.Value;
		set => _fastPeriod.Value = value;
	}

	/// <summary>
	/// Slow moving average period.
	/// </summary>
	public int SlowPeriod
	{
		get => _slowPeriod.Value;
		set => _slowPeriod.Value = value;
	}


	/// <summary>
	/// Take profit distance in absolute price units.
	/// </summary>
	public decimal TakeProfitOffset
	{
		get => _takeProfitOffset.Value;
		set => _takeProfitOffset.Value = value;
	}

	/// <summary>
	/// Trailing stop distance in absolute price units.
	/// </summary>
	public decimal TrailingStopOffset
	{
		get => _trailingStopOffset.Value;
		set => _trailingStopOffset.Value = value;
	}

	/// <summary>
	/// Additional distance required to move the trailing stop.
	/// </summary>
	public decimal TrailingStepOffset
	{
		get => _trailingStepOffset.Value;
		set => _trailingStepOffset.Value = value;
	}

	/// <summary>
	/// Initializes <see cref="Ema612CrossoverStrategy"/>.
	/// </summary>
	public Ema612CrossoverStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
			.SetDisplay("Candle Type", "Candle resolution", "General");
		_fastPeriod = Param(nameof(FastPeriod), 6)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast SMA length", "Moving Averages");
		_slowPeriod = Param(nameof(SlowPeriod), 54)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow SMA length", "Moving Averages");
		_takeProfitOffset = Param(nameof(TakeProfitOffset), 0.001m)
			.SetNotNegative()
			.SetDisplay("Take Profit", "Target distance in price units", "Risk");
		_trailingStopOffset = Param(nameof(TrailingStopOffset), 0.005m)
			.SetNotNegative()
			.SetDisplay("Trailing Stop", "Trailing stop distance", "Risk");
		_trailingStepOffset = Param(nameof(TrailingStepOffset), 0.0005m)
			.SetNotNegative()
			.SetDisplay("Trailing Step", "Additional profit required to tighten stop", "Risk");
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		ResetPositionState();
		_prevFast = null;
		_prevSlow = null;
	}

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

		if (SlowPeriod <= FastPeriod)
			throw new InvalidOperationException("Slow period must be greater than fast period.");

		_fastSma = new ExponentialMovingAverage { Length = FastPeriod };
		_slowSma = new ExponentialMovingAverage { Length = SlowPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_fastSma, _slowSma, ProcessCandle)
			.Start();

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

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

		if (!_fastSma.IsFormed || !_slowSma.IsFormed)
			return;

		var bullishCross = false;
		var bearishCross = false;

		if (_prevFast.HasValue && _prevSlow.HasValue)
		{
			// Detect crossovers using previous candle values.
			bullishCross = _prevSlow > _prevFast && slow < fast;
			bearishCross = _prevSlow < _prevFast && slow > fast;
		}

		HandleExistingPosition(candle, bullishCross, bearishCross);

		if (Position == 0)
		{
			if (bullishCross)
			{
				// Slow MA crossed below the fast MA - go long.
				EnterLong(candle);
			}
			else if (bearishCross)
			{
				// Slow MA crossed above the fast MA - go short.
				EnterShort(candle);
			}
		}

		_prevFast = fast;
		_prevSlow = slow;
	}

	private void HandleExistingPosition(ICandleMessage candle, bool bullishCross, bool bearishCross)
	{
		if (Position > 0)
		{
			// Update trailing stop for the long position before evaluating exits.
			UpdateLongTrailing(candle);

			var exit = bearishCross;
			if (!exit && _takeProfitPrice.HasValue && candle.HighPrice >= _takeProfitPrice.Value)
			{
				// Price reached the take profit objective.
				exit = true;
			}

			if (!exit && _stopPrice.HasValue && candle.LowPrice <= _stopPrice.Value)
			{
				// Price retraced to the trailing stop.
				exit = true;
			}

			if (exit)
			{
				SellMarket(Position);
				ResetPositionState();
			}
		}
		else if (Position < 0)
		{
			// Update trailing stop for the short position before evaluating exits.
			UpdateShortTrailing(candle);

			var exit = bullishCross;
			if (!exit && _takeProfitPrice.HasValue && candle.LowPrice <= _takeProfitPrice.Value)
			{
				// Price reached the take profit objective for the short trade.
				exit = true;
			}

			if (!exit && _stopPrice.HasValue && candle.HighPrice >= _stopPrice.Value)
			{
				// Price rallied back to the trailing stop level.
				exit = true;
			}

			if (exit)
			{
				BuyMarket(Math.Abs(Position));
				ResetPositionState();
			}
		}
	}

	private void EnterLong(ICandleMessage candle)
	{
		BuyMarket(Volume);
		_entryPrice = candle.ClosePrice;
		_takeProfitPrice = TakeProfitOffset > 0m ? candle.ClosePrice + TakeProfitOffset : null;
		_stopPrice = null;
	}

	private void EnterShort(ICandleMessage candle)
	{
		SellMarket(Volume);
		_entryPrice = candle.ClosePrice;
		_takeProfitPrice = TakeProfitOffset > 0m ? candle.ClosePrice - TakeProfitOffset : null;
		_stopPrice = null;
	}

	private void UpdateLongTrailing(ICandleMessage candle)
	{
		if (TrailingStopOffset <= 0m || !_entryPrice.HasValue)
			return;

		var gain = candle.ClosePrice - _entryPrice.Value;
		var triggerDistance = TrailingStopOffset + TrailingStepOffset;

		if (gain <= triggerDistance)
			return;

		var candidate = candle.ClosePrice - TrailingStopOffset;
		var minAdvance = TrailingStepOffset <= 0m ? 0m : TrailingStepOffset;

		if (!_stopPrice.HasValue || candidate - _stopPrice.Value > minAdvance)
		{
			// Move stop loss closer only when price progressed enough.
			_stopPrice = candidate;
		}
	}

	private void UpdateShortTrailing(ICandleMessage candle)
	{
		if (TrailingStopOffset <= 0m || !_entryPrice.HasValue)
			return;

		var gain = _entryPrice.Value - candle.ClosePrice;
		var triggerDistance = TrailingStopOffset + TrailingStepOffset;

		if (gain <= triggerDistance)
			return;

		var candidate = candle.ClosePrice + TrailingStopOffset;
		var minAdvance = TrailingStepOffset <= 0m ? 0m : TrailingStepOffset;

		if (!_stopPrice.HasValue || _stopPrice.Value - candidate > minAdvance)
		{
			// Move stop loss for the short only after sufficient favorable movement.
			_stopPrice = candidate;
		}
	}

	private void ResetPositionState()
	{
		_entryPrice = null;
		_stopPrice = null;
		_takeProfitPrice = null;
	}
}