Ver en GitHub

Estrategia de Cruce EMA 2-35

Esta estrategia sigue un simple cruce entre dos Medias Móviles Exponenciales. La EMA rápida con longitud 2 reacciona rápidamente a los cambios de precio, mientras que la EMA lenta con longitud 35 representa la tendencia a largo plazo. Se abre una posición cuando la EMA rápida cruza la EMA lenta; las posiciones se invierten cuando ocurre el cruce opuesto.

La gestión del riesgo se maneja con niveles fijos de stop-loss y take-profit expresados en pasos de precio. También se aplica un trailing stop para asegurar ganancias a medida que la operación avanza en una dirección favorable.

Detalles

  • Criterios de entrada:
    • Largo: EMA(2) cruza por encima de EMA(35).
    • Corto: EMA(2) cruza por debajo de EMA(35).
  • Largo/Corto: Ambos lados.
  • Criterios de salida:
    • Cruce opuesto.
    • Stop-loss o take-profit alcanzado.
    • Trailing stop activado.
  • Stops: Stop-loss fijo, take-profit y trailing stop (todos en pasos de precio).
  • Valores predeterminados:
    • FastLength = 2
    • SlowLength = 35
    • StopLoss = 50
    • TakeProfit = 150
    • TrailingStop = 50
  • Filtros:
    • Categoría: Seguimiento de tendencia
    • Dirección: Ambos
    • Indicadores: Medias móviles
    • Stops: Sí
    • Complejidad: Simple
    • Marco temporal: Corto plazo
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>
/// EMA(2) / EMA(35) crossover strategy.
/// </summary>
public class Ema235CrossStrategy : Strategy
{
	private readonly StrategyParam<int> _fastLength;
	private readonly StrategyParam<int> _slowLength;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevFast;
	private decimal _prevSlow;
	private bool _hasPrev;

	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 Ema235CrossStrategy()
	{
		_fastLength = Param(nameof(FastLength), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA length", "Parameters");
		_slowLength = Param(nameof(SlowLength), 35)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Slow EMA length", "Parameters");
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Candle type", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFast = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

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

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

		SubscribeCandles(CandleType)
			.Bind(fast, slow, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevFast = fastVal;
			_prevSlow = slowVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevFast <= _prevSlow && fastVal > slowVal;
		var crossDown = _prevFast >= _prevSlow && fastVal < slowVal;

		if (crossUp && Position <= 0)
		{
			if (Position < 0) BuyMarket();
			BuyMarket();
		}
		else if (crossDown && Position >= 0)
		{
			if (Position > 0) SellMarket();
			SellMarket();
		}

		_prevFast = fastVal;
		_prevSlow = slowVal;
	}
}