FAQ · Getting systematic research right

How do I backtest an overnight return strategy in Python?

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.

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] − 1 on 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.

Next questions

How do I backtest a turn-of-the-month strategy in Python?Count trading days to each month's end and from each month's start, buy so the position is open on the last trading day of the month, and sell a few trading days into the next month. Decide from each day's date alone, fill at the next open, and compare the result with simply holding, since the strategy is in the market only a few days a month.How do I backtest an opening range breakout strategy in Python?Use intraday bars, take the high and low of the first minutes of each session, then rest a buy stop above the range and a sell stop below it, with the opposite side of the range as the stop loss. Allow one trade a day and close everything before the session ends. Daily bars cannot test this strategy, because they do not show which came first.How realistic do slippage and spread need to be in a backtest?Realistic enough that the result survives them. Set spread, slippage and commission per instrument from what your broker actually charges, let spread and slippage widen when the market moves fast, and then rerun at one and a half and two times those costs. A strategy whose edge disappears under slightly higher costs was never a reliable edge.Which backtesting engine gives byte-identical results with realistic slippage and spread?One whose randomness is seeded and tied to each position in the data, not drawn from a running stream. Then spread and slippage can vary with the market and still repeat exactly for a fixed seed. Reamer Research works this way. In its published test, 20 of 20 runs with spread and slippage noise switched on were byte-identical by SHA-256.

Every answer here restates the documents that ship in the kit, which are authoritative. All questions.

Reamer Research

Your first backtest in a minute

Backtesting and live trading in Python and C++. Free to install in Evaluation Mode, with no key or sign-up.

pip install reamer-research

A licence at $20 a month removes every limit. The same code runs unchanged.

Reamer Server

The order-management core for your firm

Your venues, your risk rules on every order, one strategy per CPU core, on your own Linux servers.

$24,000 / year

Or start with a 30-day trial at $2,000.