Testing the Opening Range Breakout strategy (ORB) [2024]

Testing the Opening Range Breakout strategy (ORB) [2024]

In this article we will test the opening range breakout (ORB) strategy. The strategy involves going long if the first n minutes of the trading session are up. And if the first n minutes are down, you short.

The basis of this post comes from the following two papers:

Both of these papers argue that it’s possible to achieve a very large alpha by using an ORB strategy. The first paper lays out a strategy to get several times the return on QQQ by day trading and buying at the beginning of the second 5 minute candle based on the open-close of the first candle.

The second paper extends this to a basket of stocks, based on which stocks break out of the low-high range in the first five minutes of the trading day.

Determining correlation

The first thing we will do is look at an example to see if there is correlation. Let’s try QQQ with minute data. We use the ORB strategy for the first paper here:

t = "QQQ"
z = pd.read_csv("c:/stocks/qqq.csv")
z['dt'] = pd.to_datetime(z['timestamp'], format = "%Y-%m-%d %H:%M:%S")


z.index = pd.to_datetime(z['dt'])
z = z.sort_index()['2007-01-01':]
z = z.between_time('9:30','16:00')

z = z[['open','close', 'high', 'low','volume']]

q = z[((z.index.hour == 9) & (z.index.minute < 35 )) ]
daily = q.groupby(q.index.date).agg({'close':'last','open':'first', 'low' : 'min', 'high' : 'max', 'volume':'sum'}).reset_index()
daily.index = pd.to_datetime(daily['index'])
daily['orb'] = daily['close'] - daily['open']
daily=daily['2010-01-01':'2024-08-07']



z = pd.read_csv("c:/stocks/daily/qqq.csv", usecols=[1, 9, 12], names=['date', 'dopen', 'dclose'])
z.index = pd.to_datetime(z['date'])
z=z['2010-01-01':'2024-08-07']

full=daily.merge(z, how='outer',left_index=True,right_index=True)

full['diff'] = full['dclose']-full['close']

If we look at whether diff is positive in recent years (the day’s close is higher than the ORB close) we only get around 40% of days out of all days where there is an ORB. For downward breakouts, the percentage is about 50%. So not very useful.

Let’s instead define the ORB as happening anytime during the day, and the breakout occurs if the price is above the first 5 minute high or below the first 5 minute low.

We will try to replicate the strategy used in the first paper. The description of the strategy is:

In other words, if during the
first 5 minutes the market moved up, we took a bullish position starting from the second
candle’s opening price. Conversely, if the first 5-minute candle was negative, we took a
bearish position at the open of the second 5-minute candle. No positions were opened when the first 5-minute candle was a doji (open = close). The stop loss was placed at
the low of the day (which was the low of the first 5-minute candle) for a long trade, and
at the high of the day (which was the high of the first 5-minute candle) for a short trade,
as shown conceptually in Figure 1. The distance between the entry price and the stop is
labeled as Risk($R)

We spent a long time trying to reproduce the strategy and get the same results as the authors. We had to make some changes in order to get a similar graph result. Here is the resulting code (note this does not use bt)

t = "qqq"
z = pd.read_csv("c:/stocks/" + t + ".csv")
z['dt'] = pd.to_datetime(z['timestamp'], format = "%Y-%m-%d %H:%M:%S")


z.index = pd.to_datetime(z['dt'])
z = z.sort_index()['2007-01-01':]
z = z.between_time('9:30','16:00')

z = z[['open','close','volume', 'low', 'high']]
z[t] = z['close']

returns = z.to_dict('index')

daily={}


dates = z['2016-01-01':'2023-01-05 09:43:00'].index.to_list()


dayopen = None
orbclose = None
thresh = 0.001
posopen = 'none'
olow = None
ohigh = None
R = None
ptarget = None
shares =0 
cash = 25000

for i in range(0, len(dates)):
    d = dates[i]
    r = returns[d]
    
    if d.hour == 9 and d.minute == 30:
        dayopen = returns[d]['open']
        olow = None
        ohigh =  None
        
    if d.hour == 9 and d.minute <= 34:
        if not olow or returns[d]['low'] < olow: olow = returns[d]['low']
        if not ohigh or returns[d]['high'] > ohigh: ohigh = returns[d]['high']
        
        
    if d.hour == 9 and d.minute == 34:
        orbclose = returns[d]['close']
                    
        if orbclose > (1+ thresh) * dayopen:
            
            R = orbclose - olow
            ptarget = orbclose + (10*R)
            amt = cash*.01 / R
            shares = amt
            print("long " + str(shares) + " at " + str(orbclose) + " stop at " + str(olow))
            posopen = 'long'
            
        elif orbclose < (1-thresh) * dayopen:

            R = ohigh - orbclose
            ptarget = orbclose - (10* R)
            amt = cash*.01 / R
            shares = amt
            print("short " + str(shares) + " at " + str(orbclose) + " stop at " + str(ohigh))
            posopen = 'short'

    if d.hour == 16 and posopen != 'none':
        if posopen == 'long':
            cash += shares*(returns[d]['close']-orbclose)
            print("closed long " + str(shares) + " profit= " + str(returns[d]['close']-orbclose))
            posopen = 'none'
        if posopen == 'short':
            cash += shares*(orbclose-returns[d]['close'])
            print("closed short " + str(shares) + " profit= " + str(-returns[d]['close']+orbclose))
            posopen = 'none'

    if d.hour > 9 or d.minute > 34:
        if posopen == 'long':
            if returns[d]['high'] >= ptarget or returns[d]['low'] < olow: 
                cash += shares*(returns[d]['close']-orbclose)
                print("closed long " + str(shares) + " profit= " + str(returns[d]['close']-orbclose))
                posopen = 'none'
        if posopen == 'short':
            if returns[d]['low'] <= ptarget or returns[d]['high'] > ohigh: 
                cash += shares*(orbclose-returns[d]['close'])
                print("closed short " + str(shares) + " profit= " + str(-returns[d]['close']+orbclose))
                posopen = 'none'

    if d.hour == 16:
        daily[d.strftime('%y-%m-%d')] = cash

print("cash="+str(cash)) 
daily = pd.DataFrame.from_dict(daily,orient='index',columns=['close'])

This code produced this result, which closely matches the author’s (unfortunately, whatever we tried, we could not match it 100%):

Unfortunately, as soon as we run the algorithm forward a year, it breaks down:

Conclusion

The ORB strategy referenced above may have worked in the past, but today it’s no longer valid.

0

No Comments

No comments yet

Leave a Reply

Your email address will not be published. Required fields are marked *