View on GitHub

EMA Sticker Strategy

This strategy uses an Exponential Moving Average (EMA) to follow short‑term trends. A long position is opened when the close price crosses above the EMA, while a short position is opened when the close price crosses below the EMA. Optional fixed stop‑loss and take‑profit levels help manage risk.

Details

  • Entry Criteria:
    • Long: Close > EMA.
    • Short: Close < EMA.
  • Long/Short: Both sides.
  • Exit Criteria:
    • Opposite signal or configured stop levels reached.
  • Stops: Yes, optional stop‑loss and take‑profit in price units.
  • Default Values:
    • MA period = 5.
    • Stop loss = 0.001.
    • Take profit = 0.001.
  • Filters:
    • Category: Trend following
    • Direction: Both
    • Indicators: Single
    • Stops: Yes
    • Complexity: Simple
    • Timeframe: Short-term
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 EMA-based trend following strategy.
/// Buys when price crosses above EMA, sells on opposite cross.
/// </summary>
public class EmaStickerStrategy : Strategy
{
	private readonly StrategyParam<int> _maPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private bool _wasAbove;
	private bool _hasPrev;

	public int MaPeriod { get => _maPeriod.Value; set => _maPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public EmaStickerStrategy()
	{
		_maPeriod = Param(nameof(MaPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("EMA Period", "Length of the EMA", "Parameters");

		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_wasAbove = false;
		_hasPrev = false;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		var ema = new ExponentialMovingAverage { Length = MaPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(ema, ProcessCandle)
			.Start();
	}

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

		var isAbove = candle.ClosePrice > emaValue;

		if (!_hasPrev)
		{
			_wasAbove = isAbove;
			_hasPrev = true;
			return;
		}

		// Cross above EMA -> buy
		if (isAbove && !_wasAbove)
		{
			if (Position < 0)
				BuyMarket();
			if (Position <= 0)
				BuyMarket();
		}
		// Cross below EMA -> sell
		else if (!isAbove && _wasAbove)
		{
			if (Position > 0)
				SellMarket();
			if (Position >= 0)
				SellMarket();
		}

		_wasAbove = isAbove;
	}
}