Ver en GitHub

Estrategia X-Trail

Esta estrategia genera operaciones cuando una media móvil simple rápida y una lenta, calculadas sobre el precio mediano, se cruzan entre sí. La lógica refleja el script MQL original X_trail.mq4 que usaba alertas en dichos cruces.

Se abre una posición larga cuando la MA rápida permanece por encima de la MA lenta en la vela actual y la anterior, mientras estaba por debajo dos velas atrás. El patrón opuesto activa una posición corta. Las posiciones se invierten en cada nueva señal.

Detalles

  • Criterios de entrada:
    • Largo: MA rápida > MA lenta en las últimas dos velas terminadas y MA rápida estaba por debajo de la MA lenta dos velas antes.
    • Corto: MA rápida < MA lenta en las últimas dos velas terminadas y MA rápida estaba por encima de la MA lenta dos velas antes.
  • Largo/Corto: Ambos.
  • Criterios de salida:
    • Cruce opuesto (inversión de posición).
  • Stops: Ninguno.
  • Indicadores:
    • Dos medias móviles simples calculadas a partir del precio mediano.
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>
/// Moving average crossover strategy based on median price.
/// Enters long when fast MA crosses above slow MA.
/// Enters short when fast MA crosses below slow MA.
/// </summary>
public class XTrailStrategy : Strategy
{
	private readonly StrategyParam<int> _ma1Length;
	private readonly StrategyParam<int> _ma2Length;
	private readonly StrategyParam<DataType> _candleType;

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

	public int Ma1Length
	{
		get => _ma1Length.Value;
		set => _ma1Length.Value = value;
	}

	public int Ma2Length
	{
		get => _ma2Length.Value;
		set => _ma2Length.Value = value;
	}

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

	public XTrailStrategy()
	{
		_ma1Length = Param(nameof(Ma1Length), 10)
			.SetGreaterThanZero()
			.SetDisplay("Fast MA Length", "Length of the fast moving average", "General");

		_ma2Length = Param(nameof(Ma2Length), 30)
			.SetGreaterThanZero()
			.SetDisplay("Slow MA Length", "Length of the slow moving average", "General");

		_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();
		_prevFast = 0;
		_prevSlow = 0;
		_hasPrev = false;
	}

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

		var ma1 = new SimpleMovingAverage { Length = Ma1Length };
		var ma2 = new SimpleMovingAverage { Length = Ma2Length };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(ma1, ma2, ProcessCandle)
			.Start();

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

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

		if (_hasPrev)
		{
			var crossUp = _prevFast <= _prevSlow && fast > slow;
			var crossDown = _prevFast >= _prevSlow && fast < slow;

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

		_prevFast = fast;
		_prevSlow = slow;
		_hasPrev = true;
	}
}