Ver no GitHub

Estratégia de Impulso de Day Trading

Visão geral

A Estratégia DayTrading é uma conversão C# fiel do clássico MetaTrader 4 consultor especialista "DayTrading" lançado pela NazFunds em 2005. O robô original foi projetado para gráficos Forex de 5 minutos e combina vários indicadores de impulso e acompanhamento de tendências para capturar movimentos direcionais de curto prazo com um alvo fixo modesto e um trailing stop opcional. Esta implementação StockSharp reproduz a lógica de decisão central enquanto expõe cada limite importante como um parâmetro de estratégia para que possa ser otimizado ou adaptado a diferentes instrumentos.

Pilha de Indicadores

A estratégia avalia quatro indicadores na série de velas selecionada:

  • Parabolic SAR (ParabolicSar) com aceleração, incremento e limite configuráveis. Ele define a direção da tendência da linha de base e precisa virar abaixo/acima do preço para permitir novas entradas.
  • MACD (12, 26, 9) (MovingAverageConvergenceDivergenceSignal). A linha MACD deve estar abaixo da linha de sinal para posições compradas e acima dela para posições vendidas, refletindo a comparação original do histograma/sinal em MQL.
  • Stochastic Oscilador (5, 3, 3) (StochasticOscillator). A linha %K deve permanecer abaixo de 35 para posições longas e acima de 60 para posições curtas para garantir que o mercado esteja saindo de uma zona de sobrevenda/sobrecompra.
  • Momentum (14) (Momentum). Um valor abaixo de 100 desbloqueia negociações longas, enquanto um valor acima de 100 autoriza operações curtas, exatamente como no script MT4.

Todos os indicadores são processados por meio do pipeline BindEx de alto nível, portanto, nenhum gerenciamento manual de buffer ou indexação histórica é necessário.

Regras de negociação

Condições de Entrada

Uma posição longa é aberta quando todas as afirmações a seguir são verdadeiras na última vela finalizada:

  1. O ponto Parabolic SAR é impresso no preço de venda atual ou abaixo dele e o ponto anterior estava acima do ponto atual (nova SAR mudança para alta).
  2. O impulso está abaixo de 100.
  3. A linha MACD está abaixo de sua linha de sinal.
  4. Stochastic %K está abaixo de 35.

Uma posição curta é aberta quando as condições simétricas são satisfeitas:

  1. O ponto Parabolic SAR é impresso no preço de oferta atual ou acima dele e o ponto anterior estava abaixo do ponto atual (inversão de baixa).
  2. O impulso está acima de 100.
  3. A linha MACD está acima de sua linha de sinal.
  4. Stochastic %K está acima de 60.

Apenas uma posição pode ser aberta por vez. Sempre que um sinal oposto aparece, a posição existente é fechada e nenhuma reentrada acontece na mesma vela - assim como na implementação MetaTrader onde a varredura OrdersTotal impede a recarga imediata.

Gerenciamento de saída

  • Stop Loss/Take Profit: Distâncias fixas opcionais (em pontos) são convertidas em preços absolutos usando o tamanho do tick do instrumento. Eles são reavaliados em cada vela e fecham a posição se a intrabar for violada.
  • Trailing Stop: Quando o preço avança pelo número de pontos configurado, um trailing stop é ativado. Para negociações longas, o stop fica abaixo do fechamento; para negociações curtas, ele fica acima do fechamento. O stop nunca recua, portanto o lucro é bloqueado progressivamente.
  • Sinal Oposto: Uma configuração oposta válida liquida imediatamente a posição atual antes que qualquer nova entrada seja considerada.

Nenhuma lógica adicional de grade, escala ou cobertura é adicionada; a estratégia permanece tão leve e determinística quanto o EA original.

Parâmetros

Parâmetro Padrão Descrição
LotSize 1 Volume de cada ordem de mercado. A propriedade Strategy.Volume é sincronizada com este valor durante a inicialização.
TrailingStopPoints 15 Distância final em pontos. Defina como zero para desativar o rastreamento.
TakeProfitPoints 20 Distância fixa de lucro em pontos. Defina como zero para remover o alvo.
StopLossPoints 0 Distância de parada protetora em pontos. Zero reproduz o comportamento original de "sem parada".
SlippagePoints 3 Espaço reservado para deslizamento máximo de execução (para compatibilidade com a entrada MT4). Não aplicado automaticamente, mas mantido para fins de integridade.
CandleType Período de 5 minutos Série de velas usada por todos os indicadores. Mantenha-se em M5 para corresponder à recomendação original do EA.
MacdFastPeriod 12 Comprimento EMA rápido no cálculo de MACD.
MacdSlowPeriod 26 Comprimento EMA lento no cálculo MACD.
MacdSignalPeriod 9 Comprimento do sinal EMA no cálculo MACD.
StochasticLength 5 %K comprimento de lookback para o oscilador Stochastic.
StochasticSignal 3 %D comprimento de suavização.
StochasticSlow 3 Desaceleração adicional aplicada à linha %K.
MomentumPeriod 14 Comprimento retrospectivo do momento.
SarAcceleration 0,02 Fator de aceleração inicial para Parabolic SAR.
SarStep 0,02 Incremento aplicado ao fator de aceleração após cada novo extremo.
SarMaximum 0,2 Fator de aceleração máximo para Parabolic SAR.

Todos os parâmetros numéricos podem ser otimizados por meio do fluxo de trabalho de otimização do StockSharp graças às dicas do SetCanOptimize(true).

Notas de implementação

  • Os preços de compra/venda são derivados de dados em tempo real do Nível 1, quando disponíveis; caso contrário, o fechamento da vela atua como um substituto para que a lógica permaneça robusta nos testes históricos.
  • A conversão de pontos depende do Step/PriceStep do instrumento. Se nenhum for fornecido, um substituto conservador 0.0001 será usado, que corresponde a um pip Forex padrão.
  • O gerenciamento de posição reflete o MT4 EA: a estratégia nunca forma pirâmide e nunca mantém as duas direções simultaneamente.
  • Os comentários dentro do código estão em inglês de acordo com as diretrizes do projeto, enquanto este README inclui documentação estendida para facilitar a integração.

Dicas de uso

  1. Atribua o par Forex desejado à estratégia, deixe o tipo de vela em 5 minutos e inicie a estratégia. Os indicadores aquecerão automaticamente.
  2. Considere ativar um stop loss diferente de zero ao executar dados em tempo real – o script original recomendava negociar sem ele, mas os trailing stops por si só podem não ser suficientes para o controle de risco.
  3. Para portfólios algorítmicos, você pode adicionar esta estratégia a um BasketStrategy e gerenciar a alocação de capital externamente enquanto ainda se beneficia dos parâmetros expostos para otimização.

Esta documentação, juntamente com as traduções para russo e chinês na mesma pasta, fornece total transparência da lógica convertida.

namespace StockSharp.Samples.Strategies;

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;

/// <summary>
/// Intraday trend strategy converted from the MetaTrader "DayTrading" expert advisor.
/// Combines Parabolic SAR, MACD, Stochastic and Momentum filters with trailing exits.
/// </summary>
public class DayTradingImpulseStrategy : Strategy
{
	private readonly StrategyParam<decimal> _lotSize;
	private readonly StrategyParam<decimal> _trailingStopPoints;
	private readonly StrategyParam<decimal> _takeProfitPoints;
	private readonly StrategyParam<decimal> _stopLossPoints;
	private readonly StrategyParam<decimal> _slippagePoints;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _macdFastPeriod;
	private readonly StrategyParam<int> _macdSlowPeriod;
	private readonly StrategyParam<int> _macdSignalPeriod;
	private readonly StrategyParam<int> _stochasticLength;
	private readonly StrategyParam<int> _stochasticSignal;
	private readonly StrategyParam<int> _stochasticSlow;
	private readonly StrategyParam<decimal> _stochasticBuyThreshold;
	private readonly StrategyParam<decimal> _stochasticSellThreshold;
	private readonly StrategyParam<int> _momentumPeriod;
	private readonly StrategyParam<decimal> _momentumNeutralLevel;
	private readonly StrategyParam<decimal> _sarAcceleration;
	private readonly StrategyParam<decimal> _sarStep;
	private readonly StrategyParam<decimal> _sarMaximum;

	private ParabolicSar _parabolicSar = null!;
	private MovingAverageConvergenceDivergenceSignal _macd = null!;
	private StochasticOscillator _stochastic = null!;
	private Momentum _momentum = null!;

	private decimal? _previousSar;
	private decimal? _longStopPrice;
	private decimal? _shortStopPrice;
	private decimal? _longTakeProfit;
	private decimal? _shortTakeProfit;
	private decimal? _longEntryPrice;
	private decimal? _shortEntryPrice;
	private decimal _pointSize;

	/// <summary>
	/// Initializes a new instance of <see cref="DayTradingImpulseStrategy"/>.
	/// </summary>
	public DayTradingImpulseStrategy()
	{
		_lotSize = Param(nameof(LotSize), 1m)
			.SetGreaterThanZero()
			.SetDisplay("Order Volume", "Trade volume used for each market entry", "Trading")
			;

		_trailingStopPoints = Param(nameof(TrailingStopPoints), 15m)
			.SetNotNegative()
			.SetDisplay("Trailing Stop (points)", "Distance used to trail profitable positions", "Risk")
			;

		_takeProfitPoints = Param(nameof(TakeProfitPoints), 20m)
			.SetNotNegative()
			.SetDisplay("Take Profit (points)", "Fixed profit target measured in points", "Risk")
			;

		_stopLossPoints = Param(nameof(StopLossPoints), 0m)
			.SetNotNegative()
			.SetDisplay("Stop Loss (points)", "Protective stop distance measured in points", "Risk")
			;

		_slippagePoints = Param(nameof(SlippagePoints), 3m)
			.SetNotNegative()
			.SetDisplay("Slippage (points)", "Maximum acceptable execution slippage", "Trading");

		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame())
			.SetDisplay("Candle Type", "Time frame used for indicator calculations", "Data");

		_macdFastPeriod = Param(nameof(MacdFastPeriod), 12)
			.SetGreaterThanZero()
			.SetDisplay("MACD Fast", "Length of the fast EMA in MACD", "Indicators")
			;

		_macdSlowPeriod = Param(nameof(MacdSlowPeriod), 26)
			.SetGreaterThanZero()
			.SetDisplay("MACD Slow", "Length of the slow EMA in MACD", "Indicators")
			;

		_macdSignalPeriod = Param(nameof(MacdSignalPeriod), 9)
			.SetGreaterThanZero()
			.SetDisplay("MACD Signal", "Length of the MACD signal EMA", "Indicators")
			;

		_stochasticLength = Param(nameof(StochasticLength), 5)
			.SetGreaterThanZero()
			.SetDisplay("Stochastic %K", "Period of the %K line", "Indicators")
			;

		_stochasticSignal = Param(nameof(StochasticSignal), 3)
			.SetGreaterThanZero()
			.SetDisplay("Stochastic %D", "Period of the %D smoothing", "Indicators")
			;

		_stochasticSlow = Param(nameof(StochasticSlow), 3)
			.SetGreaterThanZero()
			.SetDisplay("Stochastic Slowing", "Final smoothing applied to %K", "Indicators")
			;
		_stochasticBuyThreshold = Param(nameof(StochasticBuyThreshold), 35m)
			.SetDisplay("Stochastic Buy", "Oversold %K threshold for long entries", "Indicators")
			;

		_stochasticSellThreshold = Param(nameof(StochasticSellThreshold), 60m)
			.SetDisplay("Stochastic Sell", "Overbought %K threshold for short entries", "Indicators")
			;


		_momentumPeriod = Param(nameof(MomentumPeriod), 14)
			.SetGreaterThanZero()
			.SetDisplay("Momentum Period", "Number of candles used for Momentum", "Indicators")
			;

		_momentumNeutralLevel = Param(nameof(MomentumNeutralLevel), 100m)
			.SetDisplay("Momentum Neutral", "Neutral momentum value used for signal confirmation", "Indicators")
			;

		_sarAcceleration = Param(nameof(SarAcceleration), 0.02m)
			.SetGreaterThanZero()
			.SetDisplay("SAR Acceleration", "Initial acceleration factor of Parabolic SAR", "Indicators")
			;

		_sarStep = Param(nameof(SarStep), 0.02m)
			.SetGreaterThanZero()
			.SetDisplay("SAR Step", "Increment applied to the acceleration factor", "Indicators")
			;

		_sarMaximum = Param(nameof(SarMaximum), 0.2m)
			.SetGreaterThanZero()
			.SetDisplay("SAR Maximum", "Maximum acceleration factor of Parabolic SAR", "Indicators")
			;
	}

	/// <summary>
	/// Trade volume used for each market entry.
	/// </summary>
	public decimal LotSize
	{
		get => _lotSize.Value;
		set => _lotSize.Value = value;
	}

	/// <summary>
	/// Distance used to trail profitable positions.
	/// </summary>
	public decimal TrailingStopPoints
	{
		get => _trailingStopPoints.Value;
		set => _trailingStopPoints.Value = value;
	}

	/// <summary>
	/// Fixed profit target measured in points.
	/// </summary>
	public decimal TakeProfitPoints
	{
		get => _takeProfitPoints.Value;
		set => _takeProfitPoints.Value = value;
	}

	/// <summary>
	/// Protective stop distance measured in points.
	/// </summary>
	public decimal StopLossPoints
	{
		get => _stopLossPoints.Value;
		set => _stopLossPoints.Value = value;
	}

	/// <summary>
	/// Maximum acceptable execution slippage.
	/// </summary>
	public decimal SlippagePoints
	{
		get => _slippagePoints.Value;
		set => _slippagePoints.Value = value;
	}

	/// <summary>
	/// Time frame used for indicator calculations.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Length of the fast EMA in MACD.
	/// </summary>
	public int MacdFastPeriod
	{
		get => _macdFastPeriod.Value;
		set => _macdFastPeriod.Value = value;
	}

	/// <summary>
	/// Length of the slow EMA in MACD.
	/// </summary>
	public int MacdSlowPeriod
	{
		get => _macdSlowPeriod.Value;
		set => _macdSlowPeriod.Value = value;
	}

	/// <summary>
	/// Length of the MACD signal EMA.
	/// </summary>
	public int MacdSignalPeriod
	{
		get => _macdSignalPeriod.Value;
		set => _macdSignalPeriod.Value = value;
	}

	/// <summary>
	/// Period of the %K line.
	/// </summary>
	public int StochasticLength
	{
		get => _stochasticLength.Value;
		set => _stochasticLength.Value = value;
	}

	/// <summary>
	/// Period of the %D smoothing.
	/// </summary>
	public int StochasticSignal
	{
		get => _stochasticSignal.Value;
		set => _stochasticSignal.Value = value;
	}

	/// <summary>
	/// Final smoothing applied to %K.
	/// </summary>
	public int StochasticSlow
	{
		get => _stochasticSlow.Value;
		set => _stochasticSlow.Value = value;
	}

	/// <summary>
	/// Stochastic %K level that qualifies oversold conditions.
	/// </summary>
	public decimal StochasticBuyThreshold
	{
		get => _stochasticBuyThreshold.Value;
		set => _stochasticBuyThreshold.Value = value;
	}

	/// <summary>
	/// Stochastic %K level that qualifies overbought conditions.
	/// </summary>
	public decimal StochasticSellThreshold
	{
		get => _stochasticSellThreshold.Value;
		set => _stochasticSellThreshold.Value = value;
	}

	/// <summary>
	/// Number of candles used for Momentum.
	/// </summary>
	public int MomentumPeriod
	{
		get => _momentumPeriod.Value;
		set => _momentumPeriod.Value = value;
	}

	/// <summary>
	/// Momentum value considered neutral for trend confirmation.
	/// </summary>
	public decimal MomentumNeutralLevel
	{
		get => _momentumNeutralLevel.Value;
		set => _momentumNeutralLevel.Value = value;
	}

	/// <summary>
	/// Initial acceleration factor of Parabolic SAR.
	/// </summary>
	public decimal SarAcceleration
	{
		get => _sarAcceleration.Value;
		set => _sarAcceleration.Value = value;
	}

	/// <summary>
	/// Increment applied to the acceleration factor.
	/// </summary>
	public decimal SarStep
	{
		get => _sarStep.Value;
		set => _sarStep.Value = value;
	}

	/// <summary>
	/// Maximum acceleration factor of Parabolic SAR.
	/// </summary>
	public decimal SarMaximum
	{
		get => _sarMaximum.Value;
		set => _sarMaximum.Value = value;
	}

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_previousSar = null;
		_longStopPrice = null;
		_shortStopPrice = null;
		_longTakeProfit = null;
		_shortTakeProfit = null;
		_longEntryPrice = null;
		_shortEntryPrice = null;
		_pointSize = 0m;
	}

	/// <inheritdoc />
	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		Volume = LotSize;
		_pointSize = CalculatePointSize();

		_parabolicSar = new ParabolicSar
		{
			Acceleration = SarAcceleration,
			AccelerationStep = SarStep,
			AccelerationMax = SarMaximum,
		};

		_macd = new MovingAverageConvergenceDivergenceSignal
		{
			Macd =
			{
				ShortMa = { Length = MacdFastPeriod },
				LongMa = { Length = MacdSlowPeriod },
			},
			SignalMa = { Length = MacdSignalPeriod },
		};

		_stochastic = new StochasticOscillator();
		_stochastic.K.Length = StochasticLength;
		_stochastic.D.Length = StochasticSignal;

		_momentum = new Momentum
		{
			Length = MomentumPeriod,
		};

		var subscription = SubscribeCandles(CandleType);
		subscription
			.BindEx(_parabolicSar, _macd, _stochastic, _momentum, ProcessCandle)
			.Start();

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, _parabolicSar);
			DrawIndicator(area, _macd);
			DrawIndicator(area, _stochastic);
			DrawIndicator(area, _momentum);
			DrawOwnTrades(area);
		}
	}

	private void ProcessCandle(
		ICandleMessage candle,
		IIndicatorValue sarValue,
		IIndicatorValue macdValue,
		IIndicatorValue stochasticValue,
		IIndicatorValue momentumValue)
	{
		if (candle.State != CandleStates.Finished)
			return;

		if (!sarValue.IsFinal || !macdValue.IsFinal || !stochasticValue.IsFinal || !momentumValue.IsFinal)
			return;

		if (macdValue is not MovingAverageConvergenceDivergenceSignalValue macd)
			return;

		if (stochasticValue is not StochasticOscillatorValue stochastic)
			return;

		var sar = sarValue.ToDecimal();
		var previousSar = _previousSar;
		_previousSar = sar;

		if (previousSar is null)
			return;

		var momentum = momentumValue.ToDecimal();
		var ask = GetAskPrice(candle);
		var bid = GetBidPrice(candle);

		var buySignal = sar <= ask && previousSar.Value > sar && momentum < MomentumNeutralLevel &&
			macd.Macd < macd.Signal && stochastic.K < StochasticBuyThreshold;
		var sellSignal = sar >= bid && previousSar.Value < sar && momentum > MomentumNeutralLevel &&
			macd.Macd > macd.Signal && stochastic.K > StochasticSellThreshold;

		var closedPosition = false;

		if (Position > 0)
		{
			if (sellSignal)
			{
				SellMarket(Math.Abs(Position));
				ResetLongState();
				closedPosition = true;
			}
			else if (HandleLongRisk(candle))
			{
				closedPosition = true;
			}
		}
		else if (Position < 0)
		{
			if (buySignal)
			{
				BuyMarket(Math.Abs(Position));
				ResetShortState();
				closedPosition = true;
			}
			else if (HandleShortRisk(candle))
			{
				closedPosition = true;
			}
		}

		if (closedPosition)
			return;

		if (Position == 0)
		{
			if (buySignal)
			{
				var entryPrice = ask;
				BuyMarket(Volume);
				_longEntryPrice = entryPrice;
				_longStopPrice = StopLossPoints > 0m ? entryPrice - ConvertPoints(StopLossPoints) : null;
				_longTakeProfit = TakeProfitPoints > 0m ? entryPrice + ConvertPoints(TakeProfitPoints) : null;
			}
			else if (sellSignal)
			{
				var entryPrice = bid;
				SellMarket(Volume);
				_shortEntryPrice = entryPrice;
				_shortStopPrice = StopLossPoints > 0m ? entryPrice + ConvertPoints(StopLossPoints) : null;
				_shortTakeProfit = TakeProfitPoints > 0m ? entryPrice - ConvertPoints(TakeProfitPoints) : null;
			}
		}
	}

	private bool HandleLongRisk(ICandleMessage candle)
	{
		if (Math.Abs(Position) <= 0m)
			return false;

		if (_longTakeProfit is decimal takeProfit && candle.HighPrice >= takeProfit)
		{
			SellMarket(Math.Abs(Position));
			ResetLongState();
			return true;
		}

		if (_longStopPrice is decimal stop && candle.LowPrice <= stop)
		{
			SellMarket(Math.Abs(Position));
			ResetLongState();
			return true;
		}

		var trailingDistance = ConvertPoints(TrailingStopPoints);
		if (trailingDistance > 0m && _longEntryPrice is decimal entry)
		{
			var progressed = candle.HighPrice - entry;
			if (progressed >= trailingDistance)
			{
				var candidate = candle.ClosePrice - trailingDistance;
				if (!_longStopPrice.HasValue || candidate > _longStopPrice.Value)
					_longStopPrice = candidate;
			}
		}

		return false;
	}

	private bool HandleShortRisk(ICandleMessage candle)
	{
		if (Math.Abs(Position) <= 0m)
			return false;

		if (_shortTakeProfit is decimal takeProfit && candle.LowPrice <= takeProfit)
		{
			BuyMarket(Math.Abs(Position));
			ResetShortState();
			return true;
		}

		if (_shortStopPrice is decimal stop && candle.HighPrice >= stop)
		{
			BuyMarket(Math.Abs(Position));
			ResetShortState();
			return true;
		}

		var trailingDistance = ConvertPoints(TrailingStopPoints);
		if (trailingDistance > 0m && _shortEntryPrice is decimal entry)
		{
			var progressed = entry - candle.LowPrice;
			if (progressed >= trailingDistance)
			{
				var candidate = candle.ClosePrice + trailingDistance;
				if (!_shortStopPrice.HasValue || candidate < _shortStopPrice.Value)
					_shortStopPrice = candidate;
			}
		}

		return false;
	}

	private void ResetLongState()
	{
		_longEntryPrice = null;
		_longStopPrice = null;
		_longTakeProfit = null;
	}

	private void ResetShortState()
	{
		_shortEntryPrice = null;
		_shortStopPrice = null;
		_shortTakeProfit = null;
	}

	private decimal GetBidPrice(ICandleMessage candle)
	{
		return candle.ClosePrice;
	}

	private decimal GetAskPrice(ICandleMessage candle)
	{
		return candle.ClosePrice;
	}

	private decimal ConvertPoints(decimal points)
	{
		if (points <= 0m)
			return 0m;

		if (_pointSize > 0m)
			return points * _pointSize;

		var step = Security?.PriceStep ?? 0m;
		return step > 0m ? points * step : points;
	}

	private decimal CalculatePointSize()
	{
		var step = Security?.PriceStep ?? 0m;
		return step > 0m ? step : 0.0001m;
	}
}