Ver no GitHub

Estratégia Psi Proc EMA MACD

Esta estratégia replica o sistema T4 do expert MQL original e-PSI@PROC.mq4. Opera com base no alinhamento de múltiplas médias móveis exponenciais e um filtro MACD.

Lógica da Estratégia

  1. Calcular EMA(200), EMA(50) e EMA(10) em cada candle recebido.
  2. Calcular MACD com parâmetros 12, 26, 9.
  3. Ir comprado quando:
    • EMA200 sobe e EMA50 > EMA200.
    • EMA50 sobe e EMA10 > EMA50.
    • MACD sobe e está acima de LimitMACD.
  4. Ir vendido quando:
    • EMA200 cai e EMA50 < EMA200.
    • EMA50 cai e EMA10 < EMA50.
    • MACD cai e está abaixo de -LimitMACD.
  5. Sair do comprado quando o preço fecha abaixo do EMA50.
  6. Sair do vendido quando o preço fecha acima do EMA50.

Proteções opcionais de take-profit e trailing stop são suportadas.

Parâmetros

Nome Descrição
LimitMACD Nível mínimo absoluto de MACD para permitir entrada.
TakeProfitPoints Nível de take-profit em pontos de preço.
TrailStopPoints Nível de trailing stop em pontos de preço.
CandleType Período dos candles usados pela estratégia.

Notas

  • As operações são abertas com ordens a mercado.
  • Apenas candles completos são processados.
  • A estratégia opera em um único ativo.
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>
/// Strategy based on multiple EMA alignment with MACD confirmation.
/// Buys when EMA10 > EMA50 > EMA200 and MACD positive.
/// Sells on opposite alignment.
/// </summary>
public class PsiProcEmaMacdStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevEma200;
	private decimal _prevEma50;
	private decimal _prevEma10;
	private bool _initialized;

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

	public PsiProcEmaMacdStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromHours(4).TimeFrame())
			.SetDisplay("Candle Type", "Timeframe", "General");
	}

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

	protected override void OnReseted()
	{
		base.OnReseted();
		_prevEma200 = 0;
		_prevEma50 = 0;
		_prevEma10 = 0;
		_initialized = false;
	}

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

		var ema200 = new ExponentialMovingAverage { Length = 50 };
		var ema50 = new ExponentialMovingAverage { Length = 20 };
		var ema10 = new ExponentialMovingAverage { Length = 10 };
		var macd = new MovingAverageConvergenceDivergence();

		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(ema200, ema50, ema10, macd, ProcessCandle)
			.Start();
	}

	private void ProcessCandle(ICandleMessage candle, decimal ema200, decimal ema50, decimal ema10, decimal macdVal)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!_initialized)
		{
			_prevEma200 = ema200;
			_prevEma50 = ema50;
			_prevEma10 = ema10;
			_initialized = true;
			return;
		}

		// Entry/reversal conditions - EMA alignment
		if (ema10 > ema50 && Position <= 0)
			BuyMarket();
		else if (ema10 < ema50 && Position >= 0)
			SellMarket();

		_prevEma200 = ema200;
		_prevEma50 = ema50;
		_prevEma10 = ema10;
	}
}