Ver no GitHub

Estratégia MOC Delta MOO Entry v2 Reverse

Esta estratégia inverte a lógica clássica do MOC Delta MOO Entry. Mede o delta de volume de compra-venda na sessão da tarde (14:50–14:55) e armazena o delta como porcentagem do volume do dia. Na manhã seguinte às 08:30, uma posição é aberta na direção oposta ao delta se ele ultrapassar um limiar, filtrado por duas médias móveis. As posições são encerradas com take profit e stop loss baseados em ticks ou às 14:50.

Detalhes

  • Critérios de entrada:
    • Comprado: Às 08:30, quando o percentual de delta salvo está abaixo de -DeltaThreshold e o preço de abertura está acima do SMA15 e SMA30, com SMA15 acima do SMA30.
    • Vendido: Às 08:30, quando o percentual de delta salvo está acima de DeltaThreshold e o preço de abertura está abaixo do SMA15 e SMA30, com SMA15 abaixo do SMA30.
  • Comprado/Vendido: Ambos os lados.
  • Critérios de saída:
    • Take profit e stop loss em ticks.
    • Encerramento de todas as posições abertas às 14:50.
  • Stops:
    • TpTicks = 20 ticks de take profit.
    • SlTicks = 10 ticks de stop loss.
  • Valores padrão:
    • DeltaThreshold = 2
    • TpTicks = 20
    • SlTicks = 10
    • CandleType = TimeSpan.FromMinutes(1).TimeFrame().
  • Filtros:
    • Categoria: Volume
    • Direção: Ambos
    • Indicadores: SMA
    • Stops: Sim
    • Complexidade: Intermediário
    • Período: Intradiário
    • Sazonalidade: Não
    • Redes neurais: Não
    • Divergência: Não
    • Nível de risco: Médio
using System;

using Ecng.Common;

using StockSharp.Algo.Strategies;
using StockSharp.BusinessEntities;
using StockSharp.Messages;

namespace StockSharp.Samples.Strategies;

/// <summary>
/// Reverse strategy based on aggregated volume delta windows.
/// </summary>
public class MocDeltaMooEntryV2ReverseStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _deltaWindow;
	private readonly StrategyParam<decimal> _deltaThresholdPercent;
	private readonly StrategyParam<int> _signalCooldownBars;

	private decimal _windowBuyVolume;
	private decimal _windowSellVolume;
	private int _windowBarCount;
	private int _barsFromSignal;

	/// <summary>
	/// Candle timeframe.
	/// </summary>
	public DataType CandleType
	{
		get => _candleType.Value;
		set => _candleType.Value = value;
	}

	/// <summary>
	/// Number of bars per delta window.
	/// </summary>
	public int DeltaWindow
	{
		get => _deltaWindow.Value;
		set => _deltaWindow.Value = value;
	}

	/// <summary>
	/// Absolute delta percent needed to trigger a reversal.
	/// </summary>
	public decimal DeltaThresholdPercent
	{
		get => _deltaThresholdPercent.Value;
		set => _deltaThresholdPercent.Value = value;
	}

	/// <summary>
	/// Minimum bars between entries.
	/// </summary>
	public int SignalCooldownBars
	{
		get => _signalCooldownBars.Value;
		set => _signalCooldownBars.Value = value;
	}

	public MocDeltaMooEntryV2ReverseStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
			.SetDisplay("Candle Type", "Candles timeframe", "General");
		_deltaWindow = Param(nameof(DeltaWindow), 24)
			.SetGreaterThanZero()
			.SetDisplay("Delta Window", "Bars per delta calculation window", "General");
		_deltaThresholdPercent = Param(nameof(DeltaThresholdPercent), 12m)
			.SetGreaterThanZero()
			.SetDisplay("Delta Threshold %", "Minimum delta percent for reversal", "General");
		_signalCooldownBars = Param(nameof(SignalCooldownBars), 16)
			.SetGreaterThanZero()
			.SetDisplay("Signal Cooldown Bars", "Minimum bars between entries", "General");
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_windowBuyVolume = 0m;
		_windowSellVolume = 0m;
		_windowBarCount = 0;
		_barsFromSignal = 0;
	}

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

		_windowBuyVolume = 0m;
		_windowSellVolume = 0m;
		_windowBarCount = 0;
		_barsFromSignal = SignalCooldownBars;

		var subscription = SubscribeCandles(CandleType);
		subscription.Bind(ProcessCandle).Start();
	}

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

		if (!IsFormedAndOnlineAndAllowTrading())
			return;

		if (candle.ClosePrice > candle.OpenPrice)
			_windowBuyVolume += candle.TotalVolume;
		else if (candle.ClosePrice < candle.OpenPrice)
			_windowSellVolume += candle.TotalVolume;
		else
		{
			_windowBuyVolume += candle.TotalVolume * 0.5m;
			_windowSellVolume += candle.TotalVolume * 0.5m;
		}

		_windowBarCount++;
		_barsFromSignal++;

		if (_windowBarCount < DeltaWindow)
			return;

		var totalVolume = _windowBuyVolume + _windowSellVolume;
		var deltaPercent = totalVolume > 0m
			? (_windowBuyVolume - _windowSellVolume) / totalVolume * 100m
			: 0m;

		var reverseSignal = 0;
		if (deltaPercent > DeltaThresholdPercent)
			reverseSignal = -1;
		else if (deltaPercent < -DeltaThresholdPercent)
			reverseSignal = 1;

		if (_barsFromSignal >= SignalCooldownBars && reverseSignal != 0)
		{
			if (reverseSignal > 0 && Position <= 0)
			{
				var volume = Volume + Math.Abs(Position);
				BuyMarket(volume);
				_barsFromSignal = 0;
			}
			else if (reverseSignal < 0 && Position >= 0)
			{
				var volume = Volume + Math.Abs(Position);
				SellMarket(volume);
				_barsFromSignal = 0;
			}
		}

		_windowBuyVolume = 0m;
		_windowSellVolume = 0m;
		_windowBarCount = 0;
	}
}