Use intraday bars so the backtest can buy near the close and sell at the next session's open, and charge spread and commission on both. Daily bars alone cannot do it, because the order would fill at the next day's open, not the close. Costs matter more here than in most strategies, because the position trades every day.
The effect
A stock's daily return splits into two parts: overnight, from one close to the next open, and intraday, from the open to the close. Research on US stocks, including Cliff, Cooper and Gulen in 2008, found that for long stretches most of the index's return came overnight. A strategy that holds only overnight tries to capture that part.
How to test it
- Overnight return series first.
open[t] / close[tâ1] â 1on daily data shows whether the effect exists in your market and period, before any trading rules. - Then a trading test on intraday bars. The entry has to fill close to the session's close, and the exit at the next open. On daily bars, an order placed after the close can only fill at the next open, so the buy and the sell would land at the same price.
- Costs on every day. About 250 round trips a year means the spread is paid 500 times. A cost of a few basis points per side can remove the whole effect.
- Open auctions and gaps. The exit fills at the opening price, which on news days is far from the prior close in either direction.
How Reamer Research handles it
Reamer Research fills a market order from the first tick of the next bar. So an order placed when the second-to-last bar of the session closes fills as the last bar opens, and an order placed when the last bar closes fills at the next session's first tick:
import reamer_research as rr
class Overnight:
lookback = 1
second_last, last = (20, 50), (20, 55) # bar start times, UTC
qty = 10
def on_bar(self, d):
t = int(d.ts) % 86400
if t == self.second_last[0] * 3600 + self.second_last[1] * 60 and d.position() == 0:
return rr.buy_market(self.qty) # fills as the last bar opens
if t == self.last[0] * 3600 + self.last[1] * 60 and d.position() > 0:
return rr.close_position() # fills at the next session's open
result = rr.backtest(Overnight, 'data/SPY_5min.bin', spread=0.01, commission_per_unit=0.005)
With 5-minute bars stamped at their start, the last bar of a US session that closes at 21:00 UTC starts at 20:55. d.ts is the step's time in epoch seconds UTC. The same rule runs as a function on_bar(data) or as an object configured with the session times. The trade list shows every entry and exit time, so you can confirm each trade spanned exactly one night.
Limits to know
- Session times are yours. US session times in UTC move by an hour with daylight saving; set the bar times for each part of the year or stamp bars in exchange time.
- Evaluation Mode runs 30 days or 50,000 bars of one ticker. That is about a month of 5-minute bars; years of nights need a licence.
A $20 month includes the full kit, so you can test overnight holding on your own intraday data and cancel if it does not fit.