Ver en GitHub

Estrategia Hedge Average

Esta estrategia reproduce el experto "Hedge Average" de MetaTrader. Compara medias móviles simples de los precios de apertura y cierre en dos períodos de tiempo.

Lógica de trading

  • Calcular la SMA del precio de apertura y cierre para Period1 y Period2.
  • Si la media de apertura del período largo está por encima de su media de cierre y la media de apertura del período corto está por debajo de su media de cierre, se abre una posición larga.
  • Si la media de apertura del período largo está por debajo de su media de cierre y la media de apertura del período corto está por encima de su media de cierre, se abre una posición corta.
  • El trading solo está permitido entre StartHour y EndHour.
  • El stop-loss y el take-profit opcionales se establecen en unidades de precio absolutas. El trailing stop mueve el stop protector junto con el precio cuando está habilitado.

Parámetros

  • Period1 – período para las medias rápidas.
  • Period2 – período para las medias lentas.
  • StartHour – hora del día en que el trading se activa.
  • EndHour – hora del día en que el trading se detiene.
  • CandleType – marco temporal de velas utilizado para los cálculos.
  • TakeProfit – distancia del take profit en unidades de precio.
  • StopLoss – distancia del stop loss en unidades de precio.
  • UseTrailing – activar trailing stop basado en la distancia del stop-loss.

Notas

La estrategia utiliza un enfoque de posición única y no replica el objetivo de ganancia basado en dinero de la versión MQL original.

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>
/// Hedge Average strategy using fast and slow SMA crossover.
/// Adapted from open/close MA comparison to close-price SMA crossover.
/// </summary>
public class HedgeAverageStrategy : Strategy
{
	private readonly StrategyParam<int> _fastPeriod;
	private readonly StrategyParam<int> _slowPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal? _prevFast;
	private decimal? _prevSlow;

	public int FastPeriod { get => _fastPeriod.Value; set => _fastPeriod.Value = value; }
	public int SlowPeriod { get => _slowPeriod.Value; set => _slowPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public HedgeAverageStrategy()
	{
		_fastPeriod = Param(nameof(FastPeriod), 5)
			.SetGreaterThanZero()
			.SetDisplay("Fast Period", "Fast SMA period", "Parameters");

		_slowPeriod = Param(nameof(SlowPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("Slow Period", "Slow SMA period", "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();
		_prevFast = null;
		_prevSlow = null;
	}

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

		var fast = new ExponentialMovingAverage { Length = FastPeriod };
		var slow = new ExponentialMovingAverage { Length = SlowPeriod };

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(fast, slow, ProcessCandle)
			.Start();

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

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

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			_prevFast = fastValue;
			_prevSlow = slowValue;
			return;
		}

		if (_prevFast is decimal pf && _prevSlow is decimal ps)
		{
			if (pf <= ps && fastValue > slowValue && Position <= 0)
				BuyMarket();
			else if (pf >= ps && fastValue < slowValue && Position >= 0)
				SellMarket();
		}

		_prevFast = fastValue;
		_prevSlow = slowValue;
	}
}