FAQ · Getting systematic research right

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.

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.

The effect

Studies of US stocks, starting with Ariel in 1987 and Lakonishok and Smidt in 1988, found that a large share of the market's return came in a window around the turn of each month: roughly the last trading day of one month and the first three or four of the next. Proposed reasons include month-end salary flows into funds and portfolio rebalancing.

Like any calendar effect, it may weaken once widely known. A backtest should check whether it still shows in recent years, not only in the full history.

Defining the window

  • Trading days, not calendar days. "The last trading day" depends on weekends and holidays. A weekday count gets most of it right; an exchange calendar gets holidays right too.
  • Entry timing. To hold through the last trading day's session, the order goes in at the close of the day before, or at the last day's open.
  • Exit timing. Selling after the third trading day of the new month is a common choice; test a few and keep the window you would have chosen in advance.

What to compare against

The strategy holds for only four or five days a month, so its total return will usually trail buy-and-hold. The useful comparisons are return per day in the market, drawdown, and what the capital earns on the other days.

How Reamer Research handles it

In Reamer Research the rule reads the bar's own time stamp and counts weekdays with NumPy. Run on daily bars, the order placed on one day fills at the next day's open:

import datetime as dt
import numpy as np
import reamer_research as rr

class TurnOfMonth:
    lookback = 1
    def __init__(self, days_before=1, days_after=3, qty=10):
        self.before, self.after, self.qty = days_before, days_after, qty
        self.entry_month = None
    def on_bar(self, d):
        today = dt.datetime.fromtimestamp(int(d.ts), dt.timezone.utc).date()
        next_month = (today.replace(day=28) + dt.timedelta(days=4)).replace(day=1)
        left = np.busday_count(today + dt.timedelta(days=1), next_month)  # weekdays after today
        done = np.busday_count(today.replace(day=1), today + dt.timedelta(days=1))
        if d.position() == 0 and left <= self.before:
            self.entry_month = today.month
            return rr.buy_market(self.qty)               # fills at the next bar's open
        if d.position() > 0 and today.month != self.entry_month and done >= self.after:
            return rr.close_position()

result = rr.backtest(TurnOfMonth(days_before=1, days_after=3), 'data/SPY_daily.bin')

With days_before=1 the decision is made on the second-to-last weekday, so the position opens at the last weekday's open; with days_after=3 it closes at the open after the third weekday of the new month. d.ts is the step's time in epoch seconds UTC. np.busday_count accepts a holidays= list, which turns the weekday count into a trading-day count. The same rule runs as a function on_bar(data) or a class as well.

Limits to know

  • Holidays are yours to supply. The weekday count above treats exchange holidays as trading days until you pass them in.
  • Evaluation Mode runs 30 days or 50,000 bars of one ticker. That covers one month end; a meaningful test needs years, which needs a licence.

A $20 month includes the full kit, so you can test the window on your own market and cancel if it does not fit.

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.