Ver no GitHub

NY First Candle Break and Retest Strategy

Trades breakouts of the first New York session candle with retest confirmation. Uses ATR for stop placement and reward-to-risk targets with optional EMA trend filter and trailing stop.

Details

  • Entry Criteria: Break of the first session candle high or low followed by a retest within RetestThreshold ATR.
  • Long/Short: Both.
  • Exit Criteria: ATR-based stop and RewardRiskRatio target. Optional trailing stop.
  • Stops: AtrMultiplier * ATR.
  • Default Values:
    • NyStartHour = 9
    • NyStartMinute = 30
    • SessionLength = 4
    • AtrPeriod = 14
    • AtrMultiplier = 1.2
    • RewardRiskRatio = 1.5
    • MinBreakSize = 0.15
    • RetestThreshold = 0.25
    • UseEmaFilter = true
    • EmaLength = 13
  • Filters:
    • Category: Breakout
    • Direction: Both
    • Indicators: ATR, EMA
    • Stops: ATR
    • Complexity: Intermediate
    • Timeframe: Intraday
    • Seasonality: Yes
    • Neural networks: No
    • Divergence: No
    • Risk level: Medium
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;
		}
	}
}