View on GitHub

London BreakOut Classic Strategy

This strategy trades breakouts of the London session using the Asian range. The high and low between 00:00 and 06:55 UTC form a box. After 07:00 UTC a breakout above the high opens a long position and a breakout below the low opens a short position. Stop loss is placed at the midpoint of the box and take profit uses a configurable risk-reward factor.

Details

  • Entry Criteria:
    • Long: price crosses above the Asian session high.
    • Short: price crosses below the Asian session low.
  • Exit Criteria:
    • Stop loss or take profit.
    • End of trading window.
  • Stops: Yes.
  • Default Values:
    • Asian session: 00:00–06:55 UTC.
    • Trading session: 07:00–16:00 UTC.
    • CRV = 1.
  • Filters:
    • Category: Breakout
    • Direction: Both
    • Indicators: None
    • Stops: Yes
    • Complexity: Medium
    • Timeframe: Intraday
    • Seasonality: No
    • Neural networks: No
    • Divergence: No
    • Risk level: Medium
using System;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

/// <summary>
/// London session breakout strategy using Highest/Lowest channels.
/// </summary>
public class LondonBreakOutClassicStrategy : Strategy
{
	private readonly StrategyParam<int> _channelLength;
	private readonly StrategyParam<DataType> _candleType;
	private readonly StrategyParam<int> _cooldownBars;

	private Highest _highest;
	private Lowest _lowest;
	private decimal _prevHigh;
	private decimal _prevLow;
	private int _barsSinceSignal;

	public int ChannelLength { get => _channelLength.Value; set => _channelLength.Value = value; }
	public DataType CandleType { get => _candleType.Value; set => _candleType.Value = value; }
	public int CooldownBars { get => _cooldownBars.Value; set => _cooldownBars.Value = value; }

	public LondonBreakOutClassicStrategy()
	{
		_channelLength = Param(nameof(ChannelLength), 20)
			.SetDisplay("Channel Length", "Period for breakout channel", "General");
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(15).TimeFrame())
			.SetDisplay("Candle Type", "Candles", "General");
		_cooldownBars = Param(nameof(CooldownBars), 20)
			.SetDisplay("Cooldown Bars", "Min bars between signals", "General");
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();

		_highest = null;
		_lowest = null;
		_prevHigh = 0;
		_prevLow = 0;
		_barsSinceSignal = 0;
	}

	protected override void OnStarted2(DateTime time)
	{
		base.OnStarted2(time);

		_highest = new Highest { Length = ChannelLength };
		_lowest = new Lowest { Length = ChannelLength };
		_prevHigh = 0;
		_prevLow = 0;
		_barsSinceSignal = 0;

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

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

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

		_barsSinceSignal++;

		if (!_highest.IsFormed || !_lowest.IsFormed)
		{
			_prevHigh = highVal;
			_prevLow = lowVal;
			return;
		}

		if (_barsSinceSignal < CooldownBars)
		{
			_prevHigh = highVal;
			_prevLow = lowVal;
			return;
		}

		// Breakout above channel high
		if (candle.ClosePrice > _prevHigh && Position <= 0)
		{
			BuyMarket(Volume + Math.Abs(Position));
			_barsSinceSignal = 0;
		}
		// Breakout below channel low
		else if (candle.ClosePrice < _prevLow && Position >= 0)
		{
			SellMarket(Volume + Math.Abs(Position));
			_barsSinceSignal = 0;
		}

		_prevHigh = highVal;
		_prevLow = lowVal;
	}
}