Ver no GitHub

Estratégia MADX-07 ADX MA

Esta estratégia foi convertida do consultor especialista MQL4 MADX-07. Opera em velas H4 e combina duas médias móveis com o Índice de Movimento Direcional Médio (ADX) como filtros.

Lógica

  • Entrada comprada: Preço acima da MA lenta, MA rápida acima da MA lenta, preço pelo menos MaDifference pontos acima da MA rápida nas duas últimas velas, ADX subindo acima de AdxMainLevel com +DI subindo e -DI caindo.
  • Entrada vendida: Condições espelho.
  • A posição é fechada quando o lucro em pontos atinge CloseProfit ou quando uma ordem limitada a uma distância de TakeProfit é executada.

Parâmetros

  • BigMaPeriod (25) – período da MA mais lenta.
  • BigMaType – tipo da MA mais lenta.
  • SmallMaPeriod (5) – período da MA mais rápida.
  • SmallMaType – tipo da MA mais rápida.
  • MaDifference (5) – distância mínima entre o preço e a MA rápida em pontos.
  • AdxPeriod (11) – período de cálculo do ADX.
  • AdxMainLevel (13) – valor mínimo do ADX.
  • AdxPlusLevel (13) – valor mínimo do +DI.
  • AdxMinusLevel (14) – valor mínimo do -DI.
  • TakeProfit (299) – distância do take-profit em pontos.
  • CloseProfit (13) – lucro em pontos para saída antecipada.
  • Volume (0.1) – volume de negociação.
  • CandleType – período das velas (padrão H4).
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 combining EMA crossover for trend following.
/// </summary>
public class Madx07AdxMaStrategy : Strategy
{
	private readonly StrategyParam<int> _bigMaPeriod;
	private readonly StrategyParam<int> _smallMaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevSmall;
	private decimal _prevBig;
	private bool _hasPrev;

	public int BigMaPeriod { get => _bigMaPeriod.Value; set => _bigMaPeriod.Value = value; }
	public int SmallMaPeriod { get => _smallMaPeriod.Value; set => _smallMaPeriod.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }

	public Madx07AdxMaStrategy()
	{
		_bigMaPeriod = Param(nameof(BigMaPeriod), 25)
			.SetGreaterThanZero()
			.SetDisplay("Big MA Period", "Period of the slower MA", "MA");

		_smallMaPeriod = Param(nameof(SmallMaPeriod), 10)
			.SetGreaterThanZero()
			.SetDisplay("Small MA Period", "Period of the faster MA", "MA");

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

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

		var bigMa = new ExponentialMovingAverage { Length = BigMaPeriod };
		var smallMa = new ExponentialMovingAverage { Length = SmallMaPeriod };

		SubscribeCandles(CandleType)
			.Bind(bigMa, smallMa, ProcessCandle)
			.Start();
	}

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

		if (!_hasPrev)
		{
			_prevSmall = smallMaVal;
			_prevBig = bigMaVal;
			_hasPrev = true;
			return;
		}

		var crossUp = _prevSmall <= _prevBig && smallMaVal > bigMaVal;
		var crossDown = _prevSmall >= _prevBig && smallMaVal < bigMaVal;

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

		_prevSmall = smallMaVal;
		_prevBig = bigMaVal;
	}
}