在 GitHub 上查看

Aeron JJN 突破策略

该策略重现原始 Aeron JJN EA 的逻辑。观察到强烈反转蜡烛后,在最后一个相反方向蜡烛的开盘价放置止损单。止损和止盈设置在一个 ATR 之外,可选的跟踪止损保护持仓。

测试表明该方法在主要外汇对上使用 1 分钟周期效果最好。

当上一根蜡烛为实体大于 DojiDiff1 的看跌蜡烛,而当前蜡烛看涨且收盘价低于最后一个显著看跌开盘价时,放置买入止损单。卖出止损单使用相反条件。未成交的挂单在 ResetTime 分钟后取消。

细节

  • 入场条件
    • 多头:上一根蜡烛看跌,当前蜡烛看涨并收于最后看跌开盘价下方。
    • 空头:上一根蜡烛看涨,当前蜡烛看跌并收于最后看涨开盘价上方。
  • 方向:多空双向。
  • 出场条件
    • 基于 ATR 的止损和止盈。
    • 可选的按点数跟踪止损。
  • 止损:是,初始止损与目标基于 ATR,并可启用跟踪。
  • 过滤器
    • 挂单在设定时间后过期。

参数

  • AtrPeriod – ATR 计算周期。
  • DojiDiff1 – 前一根蜡烛实体阈值。
  • DojiDiff2 – 搜索最后一个相反蜡烛的实体阈值。
  • TrailSl – 是否启用跟踪止损。
  • TrailPips – 跟踪止损的点数距离。
  • ResetTime – 取消挂单前的分钟数。
  • CandleType – 使用的时间框架。
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;
	}
}