View on GitHub

Ema612CrossoverStrategy

Summary

  • Port of the MetaTrader 5 expert advisor "EMA 6.12 (barabashkakvn's edition)" into the StockSharp high level API.
  • Trades the crossover between a fast and a slow simple moving average (the original script also used MODE_SMA despite its EMA name).
  • Adds optional take profit and trailing stop management expressed in absolute price units so the behaviour can be tuned per instrument.

Trading logic

Data preparation

  • The strategy subscribes to candles of the type defined by CandleType (15 minute timeframe by default).
  • Two simple moving averages are calculated: FastPeriod length for the fast curve and SlowPeriod length for the slow curve. The slow period must be greater than the fast period.

Entry rules

  • Signals are evaluated on the close of each finished candle.
  • A bullish crossover occurs when the slow SMA was above the fast SMA on the previous candle and drops below it on the current candle. Any open short position is closed and a long position is opened with the configured Volume.
  • A bearish crossover occurs when the slow SMA was below the fast SMA on the previous candle and rises above it on the current candle. Any open long position is closed and a short position is opened with the configured Volume.

Exit rules

  • Open positions are closed on the opposite crossover as described above.
  • Optional take profit: if TakeProfitOffset is greater than zero, the strategy calculates a fixed price target from the entry price. Long trades exit when price reaches entry + TakeProfitOffset; short trades exit when price reaches entry - TakeProfitOffset.
  • Optional trailing stop: when TrailingStopOffset is greater than zero the strategy waits until unrealised profit exceeds TrailingStopOffset + TrailingStepOffset. Once that threshold is crossed the stop price is tightened to stay TrailingStopOffset away from the latest close, but only if the new level is at least TrailingStepOffset closer to price than the previous stop. Long trades use lows to trigger the stop, shorts use highs.

Parameters

Parameter Default Description
CandleType 15 minute time frame Candle resolution used for SMA calculations and signal evaluation.
FastPeriod 6 Period for the fast simple moving average. Must be > 0 and less than SlowPeriod.
SlowPeriod 54 Period for the slow simple moving average. Must be > 0 and greater than FastPeriod.
Volume 1 Order volume used for new entries.
TakeProfitOffset 0.001 Optional absolute price distance for the take profit target. Set to 0 to disable.
TrailingStopOffset 0.005 Absolute distance between price and trailing stop. Set to 0 to disable trailing.
TrailingStepOffset 0.0005 Additional favourable move required before the trailing stop is moved.

Important: the offsets are specified in absolute price units. Adjust them to match the instrument's tick size (for example, on EURUSD with a 0.0001 step the defaults correspond to 10, 50 and 5 pips respectively).

Implementation notes

  • Uses the high level SubscribeCandles().Bind() workflow as required by the project guidelines.
  • Chart output plots both SMAs and trade markers when charting is available in the environment.
  • State variables track the entry price, trailing stop level and take profit target exactly like the MQL version.
  • The C# implementation enforces SlowPeriod > FastPeriod at start-up to avoid invalid indicator configuration.

Usage tips

  • Optimise the candle timeframe and SMA periods to match the market being traded (e.g., shorter periods for intraday futures, longer for swing trading).
  • Convert the offsets from pips or ticks into absolute price units before running the strategy.
  • Trailing can be deactivated by setting TrailingStopOffset to zero; the strategy will then rely solely on the opposite crossover or the optional take profit for exits.
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;
	}
}