Ver no GitHub

Estratégia EMA Sticker

Esta estratégia utiliza uma Média Móvel Exponencial (EMA) para seguir tendências de curto prazo. Uma posição comprada é aberta quando o preço de fechamento cruza acima da EMA, enquanto uma posição vendida é aberta quando cruza abaixo. Níveis opcionais fixos de stop-loss e take-profit ajudam a gerenciar o risco.

Detalhes

  • Critérios de entrada:
    • Comprado: Close > EMA.
    • Vendido: Close < EMA.
  • Comprado/Vendido: Ambos os lados.
  • Critérios de saída:
    • Sinal oposto ou níveis de stop configurados atingidos.
  • Stops: Sim, stop-loss e take-profit opcionais em unidades de preço.
  • Valores padrão:
    • MA period = 5.
    • Stop loss = 0.001.
    • Take profit = 0.001.
  • Filtros:
    • Categoria: Seguidor de tendência
    • Direção: Ambos
    • Indicadores: Único
    • Stops: Sim
    • Complexidade: Simples
    • Período: Curto prazo
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;
	}
}