在 GitHub 上查看

纽约首根K线突破回测策略

本策略在纽约交易时段记录首根K线的高低点,当价格突破并回测该水平后入场。使用ATR设定止损与盈亏比目标,可选EMA趋势过滤和追踪止损。

细节

  • 入场条件:价格突破首根K线的高点或低点并在RetestThreshold ATR内回测。
  • 多空方向:均可。
  • 出场条件:ATR止损与RewardRiskRatio目标,可启用追踪止损。
  • 止损AtrMultiplier × ATR。
  • 默认参数
    • NyStartHour = 9
    • NyStartMinute = 30
    • SessionLength = 4
    • AtrPeriod = 14
    • AtrMultiplier = 1.2
    • RewardRiskRatio = 1.5
    • MinBreakSize = 0.15
    • RetestThreshold = 0.25
    • UseEmaFilter = true
    • EmaLength = 13
  • 筛选
    • 分类: Breakout
    • 方向: 双向
    • 指标: ATR, EMA
    • 止损: ATR
    • 复杂度: 中等
    • 时间框架: 日内
    • 季节性: 是
    • 神经网络: 否
    • 背离: 否
    • 风险: 中
using System;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

public class NyFirstCandleBreakAndRetestStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _emaLength;

	private DateTime _currentDay;
	private decimal _dayHigh;
	private decimal _dayLow;
	private bool _dayRangeSet;
	private bool _tradedToday;

	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public int EmaLength { get => _emaLength.Value; set => _emaLength.Value = value; }

	public NyFirstCandleBreakAndRetestStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
		_emaLength = Param(nameof(EmaLength), 20).SetGreaterThanZero();
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_currentDay = default;
		_dayHigh = 0m;
		_dayLow = 0m;
		_dayRangeSet = false;
		_tradedToday = false;
	}

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

		_currentDay = default;
		_dayHigh = 0m;
		_dayLow = 0m;
		_dayRangeSet = false;
		_tradedToday = false;

		var ema = new ExponentialMovingAverage { Length = EmaLength };

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

		var area = CreateChartArea();
		if (area != null)
		{
			DrawCandles(area, subscription);
			DrawOwnTrades(area);
			DrawIndicator(area, ema);
		}
	}

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

		var day = candle.OpenTime.Date;

		// New day: close positions, set first candle as range
		if (day != _currentDay)
		{
			_currentDay = day;
			if (Position > 0) SellMarket();
			else if (Position < 0) BuyMarket();
			_dayHigh = candle.HighPrice;
			_dayLow = candle.LowPrice;
			_dayRangeSet = true;
			_tradedToday = false;
			return;
		}

		if (!_dayRangeSet || _tradedToday)
			return;

		// Entry: breakout above first candle high with EMA confirmation
		if (Position <= 0 && candle.ClosePrice > _dayHigh && candle.ClosePrice > ema)
		{
			BuyMarket();
			_tradedToday = true;
		}
		// Entry: breakdown below first candle low with EMA confirmation
		else if (Position >= 0 && candle.ClosePrice < _dayLow && candle.ClosePrice < ema)
		{
			SellMarket();
			_tradedToday = true;
		}
	}
}