Ver no GitHub

Estratégia Pipsover

Visão geral

Pipsover é uma estratégia de reversão de momentum que reage a extremos fortes do oscilador Chaikin. O Expert Advisor original do MetaTrader 5 abre uma nova operação quando o oscilador imprime um pico pronunciado enquanto a vela anterior recua para a média móvel simples de 20 períodos. O porte em C# mantém a mesma ideia reconstruindo o oscilador Chaikin com a linha de acumulação/distribuição e dois médias móveis exponenciais. Cada operação é protegida com as mesmas distâncias de stop-loss e take-profit definidas no script para que o controle de risco corresponda à implementação de referência.

Indicadores e ferramentas

  • Média Móvel Simples (SMA 20) – fornece a âncora de reversão à média. A estratégia requer que a vela anterior toque ou cruze a média antes de se tornar elegível para uma operação.
  • Oscilador Chaikin (EMA 3 – EMA 10 de ADL) – mede a pressão entre preço e volume. Leituras extremamente negativas acionam oportunidades de compra e valores positivos extremos acionam oportunidades de venda.
  • Linha de Acumulação/Distribuição (ADL) – alimenta o oscilador Chaikin. As EMAs rápida e lenta rodam sobre este fluxo de valores para imitar o indicador iChaikin do MQL5.

Lógica de negociação

Entrada comprada

  1. Aguardar uma vela completada para que todos os valores de indicadores sejam finais.
  2. Verificar que a vela anterior fechou com alta (Close > Open).
  3. Confirmar que a mínima anterior mergulhou abaixo da SMA20, sinalizando um recuo.
  4. Ler o valor do oscilador Chaikin da barra anterior. Deve ser menor que -OpenLevel para refletir um pico de sobrecompra negativa.
  5. Quando todas as condições são atendidas e nenhuma posição está atualmente aberta, enviar uma ordem de compra a mercado.

Entrada vendida

  1. Aguardar uma vela completada.
  2. Verificar que a vela anterior fechou com baixa (Close < Open).
  3. Confirmar que a máxima anterior ultrapassou a SMA20.
  4. Garantir que o oscilador Chaikin na barra anterior seja maior que OpenLevel.
  5. Se não houver posição ativa, colocar uma ordem de venda a mercado.

Lógica de saída

  • As posições compradas fecham quando a próxima vela após a entrada mostra uma estrutura baixista (fechamento abaixo da abertura), sua máxima permanece acima da SMA20 e o oscilador Chaikin sobe acima de CloseLevel.
  • As posições vendidas fecham quando a próxima vela mostra uma estrutura altista, sua mínima desce abaixo da SMA20 e o oscilador Chaikin cai abaixo de -CloseLevel.
  • As saídas de proteção monitoram cada vela terminada. Uma posição comprada fecha se o preço negocia em ou abaixo do stop-loss calculado ou em ou acima do take-profit calculado. Para vendas a comparação é invertida.

Gerenciamento de posição

  • Apenas uma posição líquida é permitida a qualquer momento. Ordens pendentes são canceladas antes de abrir uma nova operação para replicar o comportamento de posição única do MQL5.
  • Os valores de stop-loss e take-profit são calculados a partir do passo de preço do instrumento. Para compradas, o stop é definido StopLossPoints * PriceStep abaixo do preço de execução e o take-profit TakeProfitPoints * PriceStep acima. Vendas usam distâncias simétricas mas invertidas.

Parâmetros

Nome Padrão Descrição
TradeVolume 0.1 Tamanho da ordem usado para cada ordem a mercado.
MaLength 20 Período da SMA de recuo.
StopLossPoints 65 Offset de stop-loss em passos de preço desde a entrada.
TakeProfitPoints 100 Offset de take-profit em passos de preço desde a entrada.
OpenLevel 100 Limiar absoluto de Chaikin que habilita novas entradas.
CloseLevel 125 Limiar absoluto de Chaikin que força a saída da posição.
ChaikinFastLength 3 Comprimento da EMA rápida do oscilador Chaikin.
ChaikinSlowLength 10 Comprimento da EMA lenta do oscilador Chaikin.
CandleType 1 hora Período usado para a assinatura de velas; ajustar para corresponder à sessão de negociação de interesse.

Notas de implementação

  • A estratégia vincula a linha de acumulação/distribuição e a SMA ao feed de velas através de SubscribeCandles().Bind(...), garantindo que os valores dos indicadores cheguem já sincronizados com cada vela terminada.
  • Os valores de Chaikin são reconstruídos manualmente dentro de ProcessCandle para evitar o acesso de baixo nível a buffers proibido pelas diretrizes de conversão.
  • O algoritmo armazena a última vela completada, o valor da SMA e a leitura de Chaikin para reproduzir a lógica shift=1 (iClose(...,1), iLow(...,1), iChaikin(...,1)) usada no script MQL5.
  • Os níveis de alvo de proteção são rastreados dentro da classe de estratégia em vez de depender de stops gerenciados pelo broker, então o comportamento é consistente entre simulações e negociação ao vivo.
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>
/// Pipsover strategy rebuilt on the high level StockSharp API.
/// The strategy opens positions when a strong Chaikin oscillator spike aligns with a pullback to the 20-period SMA on the previous candle.
/// Protective stop-loss and take-profit levels are recreated using price step distances defined in the original Expert Advisor.
/// </summary>
public class PipsoverStrategy : Strategy
{
	private readonly StrategyParam<decimal> _tradeVolume;
	private readonly StrategyParam<int> _maLength;
	private readonly StrategyParam<decimal> _stopLossPoints;
	private readonly StrategyParam<decimal> _takeProfitPoints;
	private readonly StrategyParam<decimal> _openLevel;
	private readonly StrategyParam<decimal> _closeLevel;
	private readonly StrategyParam<int> _chaikinFastLength;
	private readonly StrategyParam<int> _chaikinSlowLength;
	private readonly StrategyParam<DataType> _candleType;

	private SimpleMovingAverage _sma;
	private AccumulationDistributionLine _accumulationDistribution;
	private ExponentialMovingAverage _chaikinFast;
	private ExponentialMovingAverage _chaikinSlow;

	private decimal _prevOpen;
	private decimal _prevHigh;
	private decimal _prevLow;
	private decimal _prevClose;
	private decimal _prevSma;
	private decimal _prevChaikin;
	private bool _hasPrevCandle;

	private decimal _stopPrice;
	private decimal _takeProfitPrice;
	private bool _hasTargets;

	/// <summary>
	/// Trading volume used for market orders.
	/// </summary>
	public decimal TradeVolume
	{
		get => _tradeVolume.Value;
		set => _tradeVolume.Value = value;
	}

	/// <summary>
	/// Period of the simple moving average that acts as a pullback filter.
	/// </summary>
	public int MaLength
	{
		get => _maLength.Value;
		set => _maLength.Value = value;
	}

	/// <summary>
	/// Stop-loss distance expressed in price steps.
	/// </summary>
	public decimal StopLossPoints
	{
		get => _stopLossPoints.Value;
		set => _stopLossPoints.Value = value;
	}

	/// <summary>
	/// Take-profit distance expressed in price steps.
	/// </summary>
	public decimal TakeProfitPoints
	{
		get => _takeProfitPoints.Value;
		set => _takeProfitPoints.Value = value;
	}

	/// <summary>
	/// Chaikin oscillator level required to allow new entries.
	/// </summary>
	public decimal OpenLevel
	{
		get => _openLevel.Value;
		set => _openLevel.Value = value;
	}

	/// <summary>
	/// Chaikin oscillator level that closes existing positions.
	/// </summary>
	public decimal CloseLevel
	{
		get => _closeLevel.Value;
		set => _closeLevel.Value = value;
	}

	/// <summary>
	/// Fast EMA period for Chaikin oscillator reconstruction.
	/// </summary>
	public int ChaikinFastLength
	{
		get => _chaikinFastLength.Value;
		set => _chaikinFastLength.Value = value;
	}

	/// <summary>
	/// Slow EMA period for Chaikin oscillator reconstruction.
	/// </summary>
	public int ChaikinSlowLength
	{
		get => _chaikinSlowLength.Value;
		set => _chaikinSlowLength.Value = value;
	}

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

	/// <summary>
	/// Initializes a new instance of the <see cref="PipsoverStrategy"/> class.
	/// </summary>
	public PipsoverStrategy()
	{
		_tradeVolume = Param(nameof(TradeVolume), 1m)
			.SetGreaterThanZero()
			.SetDisplay("Trade Volume", "Order size used for market entries", "Trading");

		_maLength = Param(nameof(MaLength), 20)
			.SetGreaterThanZero()
			.SetDisplay("SMA Length", "Simple moving average length", "Indicators");

		_stopLossPoints = Param(nameof(StopLossPoints), 65m)
			.SetGreaterThanZero()
			.SetDisplay("Stop-Loss Points", "Stop-loss distance expressed in price steps", "Risk");

		_takeProfitPoints = Param(nameof(TakeProfitPoints), 100m)
			.SetGreaterThanZero()
			.SetDisplay("Take-Profit Points", "Take-profit distance expressed in price steps", "Risk");

		_openLevel = Param(nameof(OpenLevel), 20m)
			.SetGreaterThanZero()
			.SetDisplay("Open Level", "Chaikin oscillator threshold for entries", "Chaikin");

		_closeLevel = Param(nameof(CloseLevel), 30m)
			.SetGreaterThanZero()
			.SetDisplay("Close Level", "Chaikin oscillator threshold for exits", "Chaikin");

		_chaikinFastLength = Param(nameof(ChaikinFastLength), 3)
			.SetGreaterThanZero()
			.SetDisplay("Chaikin Fast Length", "Fast EMA length for Chaikin oscillator", "Chaikin");

		_chaikinSlowLength = Param(nameof(ChaikinSlowLength), 10)
			.SetGreaterThanZero()
			.SetDisplay("Chaikin Slow Length", "Slow EMA length for Chaikin oscillator", "Chaikin");

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

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

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

		// Reset cached candle and indicator state.
		_hasPrevCandle = false;
		_prevOpen = 0m;
		_prevHigh = 0m;
		_prevLow = 0m;
		_prevClose = 0m;
		_prevSma = 0m;
		_prevChaikin = 0m;

		// Reset protective price levels.
		_stopPrice = 0m;
		_takeProfitPrice = 0m;
		_hasTargets = false;
	}

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

		// Apply configured trading volume to base strategy.
		Volume = TradeVolume;

		// Prepare indicators that replicate the MQL Expert Advisor logic.
		_sma = new SMA { Length = MaLength };
		_accumulationDistribution = new AccumulationDistributionLine();
		_chaikinFast = new EMA { Length = ChaikinFastLength };
		_chaikinSlow = new EMA { Length = ChaikinSlowLength };

		// Subscribe to candle data and bind indicators.
		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(_accumulationDistribution, _sma, ProcessCandle)
			.Start();

		// Optional charting for visual validation.
		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawIndicator(area, _sma);
			DrawIndicator(area, _accumulationDistribution);
			DrawOwnTrades(area);
		}
	}

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

		// Rebuild Chaikin oscillator values via EMA of the ADL indicator.
		var fastResult = _chaikinFast.Process(new DecimalIndicatorValue(_chaikinFast, adlValue, candle.ServerTime) { IsFinal = true });
		var slowResult = _chaikinSlow.Process(new DecimalIndicatorValue(_chaikinSlow, adlValue, candle.ServerTime) { IsFinal = true });
		var chaikinValue = fastResult.ToDecimal() - slowResult.ToDecimal();

		// Wait for all indicators to be fully formed before trading.
		if (!_chaikinFast.IsFormed || !_chaikinSlow.IsFormed || !_sma.IsFormed)
		{
			UpdateState(candle, chaikinValue, smaValue);
			return;
		}

		if (!_hasPrevCandle)
		{
			UpdateState(candle, chaikinValue, smaValue);
			return;
		}

		var step = Security?.PriceStep ?? 1m;
		var stopLossDistance = StopLossPoints * step;
		var takeProfitDistance = TakeProfitPoints * step;

		// Check protective stop-loss and take-profit targets before new decisions.
		if (_hasTargets)
		{
			if (Position > 0)
			{
				if (candle.LowPrice <= _stopPrice || candle.HighPrice >= _takeProfitPrice)
				{
					SellMarket(Position);
					ResetTargets();
				}
			}
			else if (Position < 0)
			{
				if (candle.HighPrice >= _stopPrice || candle.LowPrice <= _takeProfitPrice)
				{
					BuyMarket(Math.Abs(Position));
					ResetTargets();
				}
			}
			else
			{
				ResetTargets();
			}
		}

		if (!IsFormedAndOnlineAndAllowTrading())
		{
			UpdateState(candle, chaikinValue, smaValue);
			return;
		}

		var prevBullish = _prevClose > _prevOpen;
		var prevBearish = _prevClose < _prevOpen;

		if (Position > 0)
		{
			var shouldExitLong = prevBearish && _prevHigh > _prevSma && _prevChaikin > CloseLevel;
			if (shouldExitLong)
			{
				SellMarket(Position);
				ResetTargets();
			}
		}
		else if (Position < 0)
		{
			var shouldExitShort = prevBullish && _prevLow < _prevSma && _prevChaikin < -CloseLevel;
			if (shouldExitShort)
			{
				BuyMarket(Math.Abs(Position));
				ResetTargets();
			}
		}
		else
		{
			// No position is open, evaluate entry signals.

			var allowLong = prevBullish && _prevLow < _prevSma && _prevChaikin < -OpenLevel;
			var allowShort = prevBearish && _prevHigh > _prevSma && _prevChaikin > OpenLevel;

			if (allowLong)
			{
				BuyMarket();

				var entryPrice = candle.ClosePrice;
				_stopPrice = entryPrice - stopLossDistance;
				_takeProfitPrice = entryPrice + takeProfitDistance;
				_hasTargets = true;
			}
			else if (allowShort)
			{
				SellMarket();

				var entryPrice = candle.ClosePrice;
				_stopPrice = entryPrice + stopLossDistance;
				_takeProfitPrice = entryPrice - takeProfitDistance;
				_hasTargets = true;
			}
		}

		UpdateState(candle, chaikinValue, smaValue);
	}

	private void UpdateState(ICandleMessage candle, decimal chaikinValue, decimal smaValue)
	{
		// Store previous candle data for next iteration checks.
		_prevOpen = candle.OpenPrice;
		_prevHigh = candle.HighPrice;
		_prevLow = candle.LowPrice;
		_prevClose = candle.ClosePrice;
		_prevSma = smaValue;
		_prevChaikin = chaikinValue;
		_hasPrevCandle = true;
	}

	private void ResetTargets()
	{
		// Clear stop-loss and take-profit levels once position is closed.
		_stopPrice = 0m;
		_takeProfitPrice = 0m;
		_hasTargets = false;
	}
}