Ver en GitHub

Estrategia Dual Supertrend MACD

La estrategia Dual Supertrend MACD combina dos indicadores Supertrend con un filtro MACD. Se abre una posición larga cuando el precio opera por encima de ambas líneas Supertrend y el histograma MACD es positivo. Las posiciones cortas aparecen cuando el precio está por debajo de ambas líneas y el histograma es negativo. Las posiciones se cierran una vez que cualquier Supertrend cambia de dirección o el histograma MACD cruza cero.

Detalles

  • Datos: Velas de precio.
  • Criterios de entrada:
    • Largo: Close > Supertrend1 && Close > Supertrend2 && MACD Histogram > 0
    • Corto: Close < Supertrend1 && Close < Supertrend2 && MACD Histogram < 0
  • Criterios de salida:
    • Largo: Close < Supertrend1 || Close < Supertrend2 || MACD Histogram < 0
    • Corto: Close > Supertrend1 || Close > Supertrend2 || MACD Histogram > 0
  • Stops: Ninguno por defecto.
  • Valores predeterminados:
    • MacdFast = 12
    • MacdSlow = 26
    • MacdSignal = 9
    • OscillatorMaType = Exponential
    • SignalMaType = Exponential
    • AtrPeriod1 = 10
    • Factor1 = 3.0
    • AtrPeriod2 = 20
    • Factor2 = 5.0
    • TradeDirection = "Both"
  • Filtros:
    • Categoría: Seguimiento de tendencia
    • Dirección: Configurable
    • Indicadores: Supertrend, MACD
    • Complejidad: Intermedio
    • Nivel de riesgo: Medio
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;

public class DualSupertrendMacdStrategy : Strategy
{
	private readonly StrategyParam<int> _fastEmaPeriod;
	private readonly StrategyParam<int> _slowEmaPeriod;
	private readonly StrategyParam<DataType> _candleType;
	private decimal _prevFastEma;
	private decimal _prevSlowEma;

	public int FastEmaPeriod { get => _fastEmaPeriod.Value; set => _fastEmaPeriod.Value = value; }
	public int SlowEmaPeriod { get => _slowEmaPeriod.Value; set => _slowEmaPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public DualSupertrendMacdStrategy()
	{
		_fastEmaPeriod = Param(nameof(FastEmaPeriod), 120)
			.SetGreaterThanZero()
			.SetDisplay("Fast EMA", "Fast EMA period", "Indicators");
		_slowEmaPeriod = Param(nameof(SlowEmaPeriod), 450)
			.SetGreaterThanZero()
			.SetDisplay("Slow EMA", "Slow EMA period", "Indicators");
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(1).TimeFrame())
			.SetDisplay("Candle Type", "Type of candles to use", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevFastEma = 0m;
		_prevSlowEma = 0m;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);
		var fastEma = new ExponentialMovingAverage { Length = FastEmaPeriod };
		var slowEma = new ExponentialMovingAverage { Length = SlowEmaPeriod };
		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(fastEma, slowEma, ProcessCandle).Start();
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, fastEma);
			DrawIndicator(area, slowEma);
			DrawOwnTrades(area);
		}
	}

	private void ProcessCandle(ICandleMessage candle, decimal fastEmaValue, decimal slowEmaValue)
	{
		if (candle.State != CandleStates.Finished) return;
		if (_prevFastEma == 0m || _prevSlowEma == 0m)
		{
			_prevFastEma = fastEmaValue;
			_prevSlowEma = slowEmaValue;
			return;
		}
		if (_prevFastEma <= _prevSlowEma && fastEmaValue > slowEmaValue && Position <= 0)
			BuyMarket();
		else if (_prevFastEma >= _prevSlowEma && fastEmaValue < slowEmaValue && Position >= 0)
			SellMarket();
		_prevFastEma = fastEmaValue;
		_prevSlowEma = slowEmaValue;
	}
}