Ver no GitHub

Estratégia de Rompimento Aeron JJN

Esta estratégia replica a lógica do consultor especialista original Aeron JJN. Ela observa uma vela de reversão forte e coloca uma ordem stop na abertura da última vela oposta. O stop e o alvo são definidos a um ATR de distância, e um trailing stop opcional protege as posições abertas.

Os testes mostram que a ideia funciona melhor em pares Forex principais usando velas de 1 minuto.

Uma ordem buy stop é colocada quando a vela anterior é baixista com corpo maior que DojiDiff1 e a vela atual é altista, mas ainda abaixo da última abertura baixista significativa. Uma ordem sell stop usa as condições espelho. Ordens pendentes são removidas após ResetTime minutos se permanecerem não executadas.

Detalhes

  • Critérios de entrada:
    • Comprado: Vela anterior baixista, vela atual altista e fecha abaixo da última abertura baixista.
    • Vendido: Vela anterior altista, vela atual baixista e fecha acima da última abertura altista.
  • Comprado/Vendido: Ambos.
  • Critérios de saída:
    • Stop-loss e take-profit baseados em ATR.
    • Trailing stop opcional em pips.
  • Stops: Sim, stop inicial e alvo baseados em ATR mais trailing opcional.
  • Filtros:
    • Ordens pendentes expiram após o tempo configurado.

Parâmetros

  • AtrPeriod – período de cálculo do ATR.
  • DojiDiff1 – limiar de tamanho do corpo para a vela anterior.
  • DojiDiff2 – limiar de tamanho do corpo ao buscar a última vela oposta.
  • TrailSl – ativar trailing stop.
  • TrailPips – distância de trailing em pips.
  • ResetTime – minutos antes de cancelar ordens stop.
  • CandleType – período de trabalho.
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>
/// Aeron JJN breakout strategy.
/// Buys when candle reverses from bearish to bullish with body confirmation.
/// Sells when candle reverses from bullish to bearish.
/// </summary>
public class AeronJjnStrategy : Strategy
{
	private readonly StrategyParam<int> _emaPeriod;
	private readonly StrategyParam<DataType> _candleType;

	private decimal _prevOpen;
	private decimal _prevClose;
	private bool _hasPrev;

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

	public AeronJjnStrategy()
	{
		_emaPeriod = Param(nameof(EmaPeriod), 20)
			.SetGreaterThanZero()
			.SetDisplay("EMA Period", "EMA trend filter", "Indicator");

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

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

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_prevOpen = 0;
		_prevClose = 0;
		_hasPrev = false;
	}

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

		var ema = new ExponentialMovingAverage { Length = EmaPeriod };

		SubscribeCandles(CandleType)
			.Bind(ema, ProcessCandle)
			.Start();
	}

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

		if (_hasPrev)
		{
			var prevBull = _prevClose > _prevOpen;
			var prevBear = _prevClose < _prevOpen;
			var currBull = candle.ClosePrice > candle.OpenPrice;
			var currBear = candle.ClosePrice < candle.OpenPrice;

			// Buy: bearish to bullish reversal with price above EMA
			if (prevBear && currBull && candle.ClosePrice > emaValue && Position <= 0)
			{
				if (Position < 0) BuyMarket();
				BuyMarket();
			}
			// Sell: bullish to bearish reversal with price below EMA
			else if (prevBull && currBear && candle.ClosePrice < emaValue && Position >= 0)
			{
				if (Position > 0) SellMarket();
				SellMarket();
			}
		}

		_prevOpen = candle.OpenPrice;
		_prevClose = candle.ClosePrice;
		_hasPrev = true;
	}
}