View on GitHub

ORB 15m – First 15min Breakout (Long/Short)

This strategy enters at the close of the first 15‑minute bar after the session open in Stockholm time. A bullish first bar triggers a long trade, a bearish bar triggers a short. Position size is calculated from risk percentage and the distance to the stop.

Details

  • Entry Criteria: trade on the first 15-minute bar after session open; long if the bar closes above its open, short if below.
  • Exit Criteria: stop-loss at the opposite extreme of the reference bar; optional take profit at RMultiple times risk or otherwise at session end.
  • Long/Short: both.
  • Stops: yes.
  • Default Values:
    • RiskPct = 1
    • TpTenR = true
    • RMultiple = 10
    • SessionOpenHour = 15
    • SessionOpenMinute = 30
    • SessionEndHour = 22
    • SessionEndMinute = 0
  • Filters:
    • Category: Breakout
    • Direction: Both
    • Indicators: None
    • Stops: Yes
    • Complexity: Beginner
    • Timeframe: Intraday
using System;

using Ecng.Common;

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

namespace StockSharp.Samples.Strategies;

public class Orb15mFirst15minBreakoutStrategy : Strategy
{
	private readonly StrategyParam<DataType> _candleType;

	private decimal _orHigh;
	private decimal _orLow;
	private bool _tradeTakenToday;
	private bool _wasInOr;
	private DateTime _currentDay;
	private bool _orEstablished;

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

	public Orb15mFirst15minBreakoutStrategy()
	{
		_candleType = Param(nameof(CandleType), TimeSpan.FromMinutes(5).TimeFrame());
	}

	/// <inheritdoc />
	protected override void OnReseted()
	{
		base.OnReseted();
		_orHigh = 0;
		_orLow = 0;
		_tradeTakenToday = false;
		_wasInOr = false;
		_currentDay = default;
		_orEstablished = false;
	}

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

		_orHigh = 0;
		_orLow = 0;
		_tradeTakenToday = false;
		_wasInOr = false;
		_currentDay = default;
		_orEstablished = false;

		var sma = new SimpleMovingAverage { Length = 20 };
		var subscription = SubscribeCandles(CandleType);
		subscription
			.Bind(sma, (candle, smaVal) =>
			{
				if (candle.State != CandleStates.Finished)
					return;

				if (!sma.IsFormed)
					return;

				var day = candle.OpenTime.Date;
				if (_currentDay != day)
				{
					_currentDay = day;
					_orHigh = 0;
					_orLow = 0;
					_tradeTakenToday = false;
					_orEstablished = false;
				}

				var hour = candle.OpenTime.TimeOfDay.TotalHours;
				var inOr = hour < 1;

				if (inOr)
				{
					_orHigh = _orHigh > 0 ? Math.Max(_orHigh, candle.HighPrice) : candle.HighPrice;
					_orLow = _orLow > 0 ? Math.Min(_orLow, candle.LowPrice) : candle.LowPrice;
				}

				if (_wasInOr && !inOr && _orHigh > 0 && _orLow > 0 && _orHigh - _orLow > 0)
					_orEstablished = true;

				if (!_tradeTakenToday && _orEstablished && !inOr)
				{
					if (candle.ClosePrice > _orHigh && candle.ClosePrice > smaVal && Position <= 0)
					{
						BuyMarket();
						_tradeTakenToday = true;
					}
					else if (candle.ClosePrice < _orLow && candle.ClosePrice < smaVal && Position >= 0)
					{
						SellMarket();
						_tradeTakenToday = true;
					}
				}

				_wasInOr = inOr;
			})
			.Start();

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