FAQ · Getting systematic research right

How do I backtest a strategy that uses the VIX in Python?

Load the VIX as a separate series stamped with the time each value became known, join it to the traded instrument's bars "as of" that time, and trade the instrument, not the index. The common mistake is joining the VIX by date, which lets each day's trade see that day's VIX close before it exists.

Load the VIX as a separate series stamped with the time each value became known, join it to the traded instrument's bars "as of" that time, and trade the instrument, not the index. The common mistake is joining the VIX by date, which lets each day's trade see that day's VIX close before it exists.

What the VIX is

The Cboe Volatility Index is computed from S&P 500 option prices and reflects the market's expected volatility over the next 30 days. It cannot be bought directly. Strategies use it as a signal to time another instrument, such as an S&P 500 ETF or futures, or trade VIX futures and the products built on them, which behave quite differently from the index.

Common rules

  • Level filter. Hold equities while the VIX is below a level such as 20, and step aside when it rises above a higher one such as 30. Two levels avoid trading back and forth around one.
  • Spike reversal. Buy equities after the VIX jumps far above its recent average, betting that fear fades.
  • Term structure. Compare the VIX with a longer-dated measure such as VIX3M; a VIX above it signals stress.

Getting the timing right

  • Stamp each value when it was known. The VIX close is published after the stock market close. A daily file stamped with the date, joined to that day's bars, gives every bar of the day the evening's value.
  • Use the traded instrument's prices for fills. The VIX only decides; fills, costs and results come from the instrument.
  • Mind the history's start. The current VIX methodology dates from 2003; earlier values were back-calculated.

How Reamer Research handles it

Reamer Research attaches a non-price series to a ticker and shows each value only from the first bar at or after its time stamp, as exogenous data. This configured object reads it with d.exogenous:

import json
import reamer_research as rr

class VixTiming:
    lookback = 1
    def __init__(self, ticker='SPY', calm=20.0, stressed=30.0, qty=10):
        self.ticker, self.calm, self.stressed, self.qty = ticker, calm, stressed, qty
    def on_bar(self, d):
        raw = d.exogenous(self.ticker)
        if raw is None:                                  # no VIX value known yet
            return None
        vix = json.loads(raw)['vix']
        if d.position(self.ticker) == 0 and vix < self.calm:
            return rr.buy_market(self.qty, ticker=self.ticker)
        if d.position(self.ticker) > 0 and vix > self.stressed:
            return rr.close_position(ticker=self.ticker)

# {time it became known: value}; VIX closes stamped at 21:15 UTC
vix = {'2024-03-01 21:15': {'vix': 13.1}, '2024-03-04 21:15': {'vix': 13.5}}
result = rr.backtest(VixTiming(), {'SPY': 'data/SPY.bin'}, exogenous={'SPY': vix})

exogenous= takes a dictionary of times and values per ticker, or a sidecar file built once with rr.write_exogenous or the kit's bin/reamer-exo-build. A value stamped after the close reaches the strategy on the next session's first bar, so the order it triggers fills after that. The same rule runs as a function on_bar(data) or a class as well. The VIX here is data attached to one ticker, so the run is a one-ticker run.

Limits to know

  • VIX data is yours to bring. The kit includes fetchers for Alpaca and Databento bars; the VIX history comes from your own source.
  • Evaluation Mode runs 30 days or 50,000 bars of one ticker. Enough to check the join; years of history need a licence.

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

Next questions

How do I backtest a strategy that uses earnings dates or other non-price data?Stamp every record with the moment it became available to you, not the date it describes, then join it to your bars "as of": each bar sees the latest record stamped at or before it, never a later one. Most lookahead in event-driven backtests comes from the timestamps, not the join. Reamer Research attaches a non-price series to each ticker and enforces that rule, so a value appears exactly on the bar carrying its timestamp and never earlier.What is look-ahead bias and how do I find it in my backtest?Look-ahead bias is a backtest using information that was not available when the decision was made, such as filling at the close of the bar that produced the signal or reading data published later. Find it by checking when each value was known, shifting signals by one bar, and seeing whether results collapse.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 a momentum or trend following strategy in Python?Measure trend from past closes only, such as the return over the last 12 months or price above a long average, hold long while it is positive and exit when it turns, and size each position by its recent volatility. Test across many years and several markets, because trend following earns most of its return in a few large moves.

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.