quanttoolbox.backtest¶
Python alternatives
Full backtesting frameworks like vectorbt, bt, backtrader, and zipline exist, but are a different paradigm (event-driven or much heavier) than this module's lightweight, transparent, vectorized style. vectorbt specifically is Numba-accelerated and worth knowing about if performance on very large universes becomes a bottleneck — but it's not a drop-in replacement. Keep this module for its scope.
backtest.returns¶
quanttoolbox.backtest.returns
¶
Price/return series conversion: simple returns, cumulative price indices, funded/unfunded conversions, and capitalized LIBOR indices.
Ported from QuantToolBox/backtest/{price2return,return2price, price2unfunded,unfunded2price,capitalized_libor,capitalized_libor_plus}.m
Translation notes:
price2return2.min the original is dead/incomplete code (it references undefined variablesDates/day_of_weekand shares its function name withprice2return.m, so it could never have been called under normal MATLAB name resolution) -- it is NOT ported here.lagn/lag1(n-period lag) are replaced by array slicing (arr[:-n]) orpandas.Series.shift(n)where a NaN-padded result is wanted.findnomiss/fillmiss(..., 4)(MATLAB: find first/last non-missing row per column, forward/back-fill gaps) map to pandas'.ffill()/.bfill()plusfirst_valid_index()/last_valid_index().- All functions accept and return 2-D arrays (rows = dates, columns = assets) to match the original's column-oriented convention, even for a single series -- pass a (n, 1) array or reshape as needed.
capitalized_libor(dates, libor_rates, method=1)
¶
Build a capitalized (compounding) index from a LIBOR-style annualized rate series, using actual/365.25 day-count compounding.
Original: backtest/capitalized_libor.m
Source code in src/quanttoolbox/backtest/returns.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
capitalized_libor_plus(dates, libor_index, plus)
¶
Build a capitalized "LIBOR + spread" index from an existing LIBOR index and a per-series spread (in annualized terms).
Original: backtest/capitalized_libor_plus.m
Source code in src/quanttoolbox/backtest/returns.py
242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
price_to_return(x, n_lags=1)
¶
Simple (arithmetic) returns over n_lags periods: x[t]/x[t-n] - 1.
Original: backtest/price2return.m
Source code in src/quanttoolbox/backtest/returns.py
30 31 32 33 34 35 36 37 38 39 40 | |
price_to_unfunded(funded_prices, libor_index, method=1)
¶
Convert funded (total-return) prices to unfunded (excess-return) prices by subtracting the LIBOR return each period.
method=1 (default): mask output using the funded series' own first/last valid range. method=2: mask output using the input's exact NaN pattern instead.
Original: backtest/price2unfunded.m
Source code in src/quanttoolbox/backtest/returns.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 | |
return_to_price(x, keep_interior_nan=False)
¶
Convert a return series back into a cumulative price index starting at 100, handling leading/trailing NaN gaps per column.
Original: backtest/return2price.m
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
keep_interior_nan
|
if True, interior single-period gaps in the input
|
(immediately after the first valid observation) are preserved as NaN in the output rather than treated as a zero return. |
False
|
Source code in src/quanttoolbox/backtest/returns.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | |
unfunded_to_price(unfunded_prices, libor_index, method=1)
¶
Convert unfunded (excess-return) prices to funded (total-return) prices by adding back the LIBOR return each period.
Original: backtest/unfunded2price.m
Source code in src/quanttoolbox/backtest/returns.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | |
Examples¶
Flat transaction cost under a realistic rebalancing schedule — backtest/backtest5.py
"""Translated from Examples/backtest/backtest5.m -- generate_backtest with
a flat transaction cost (0.01) under monthly rebalancing (the original
builds weekly and daily rebalancing schedules too, but overwrites both
before use, so only the monthly one -- `RB_Dates = ones(nDates,1)`, i.e.
every date is a rebalance -- is actually exercised; reproduced as-is)."""
import numpy as np
from quanttoolbox.backtest.reporting import generate_backtest
from quanttoolbox.backtest.returns import return_to_price
from quanttoolbox.dates.convert import parse_date_serial
from quanttoolbox.dates.rebalancing import generate_trading_dates
# See backtest2.py's translation note: generate_trading_dates needs an
# actual pd.Timestamp, not a bare YYYYMMDD int, hence parse_date_serial.
d1, d2 = parse_date_serial([20160101, 20161231])
_, dates = generate_trading_dates(d1, d2, business_days_only=True)
n_dates = len(dates)
n_assets = 3
sigma = 0.20 / np.sqrt(260)
rng = np.random.default_rng(123456789)
r = sigma * rng.standard_normal((n_dates, n_assets))
indices = return_to_price(r)
weights = np.ones((n_dates, n_assets))
weights = weights / np.sum(weights, axis=1, keepdims=True)
rb_dates = np.ones(
n_dates
) # "monthly rebalancing" per the original's final overwrite -> every date
result1 = generate_backtest(dates, weights, indices, rb_dates)
tc = 0.01
result2 = generate_backtest(dates, weights, indices, rb_dates, tc_bid_ask=tc)
print("backtest without / with transaction costs, turnover, TC (first 5 rows):")
print(
np.round(
np.column_stack(
[result1.backtest, result2.backtest, result2.turnover, result2.transaction_costs]
)[:5],
4,
)
)
to_total = np.nansum(result2.turnover)
cost1 = 1 - result2.backtest[-1] / result1.backtest[-1]
cost2 = to_total * tc
print("\nTotal turnover:", round(to_total, 4))
print("Cost1 (1 - final wealth ratio):", round(cost1, 5))
print("Cost2 (total turnover * tc):", round(cost2, 5))
Funded vs. unfunded backtest formulations cross-checked — backtest/unfunded1.py
"""Translated from Examples/backtest/unfunded1.m -- compares a funded
(price-return) backtest against economically equivalent unfunded
(excess-return) formulations, three different ways:
Backtest1 -- pure funded weights on funded prices.
Backtest2 -- funded weights plus an explicit cash/LIBOR leg, funded prices.
Backtest3 -- generate_backtest_funded_unfunded with the LIBOR leg funded
and the risky assets unfunded (price_to_unfunded'd), which should match
Backtest1/Backtest2 economically since unfunding + re-funding is neutral.
The original draws R from MATLAB's unseeded `randn`; a fixed seed
(`np.random.default_rng(0)`) is substituted here. Plotting is dropped."""
import numpy as np
from quanttoolbox.backtest.reporting import generate_backtest, generate_backtest_funded_unfunded
from quanttoolbox.backtest.returns import price_to_unfunded
n_dates = 3000
dates = np.arange(1, n_dates + 1)
rb = np.zeros(n_dates)
rb[np.arange(1, n_dates, 5) - 1] = 1.0
sigma = 0.20 * np.sqrt(1 / 260)
rng = np.random.default_rng(0)
r = sigma * rng.standard_normal((n_dates, 2))
prices_funded = 100 * np.cumprod(1 + r, axis=0)
rate = 0.03
r_libor = rate * np.sqrt(1 / 260) * np.ones((n_dates, 1))
libor_index = 100 * np.cumprod(1 + r_libor, axis=0)
prices_unfunded = price_to_unfunded(prices_funded, libor_index, method=1)
weights = np.tile([0.5, 0.5], (n_dates, 1))
w1 = weights
backtest1 = generate_backtest(dates, w1, prices_funded, rb).backtest
w2 = np.column_stack([weights, 1 - np.sum(weights, axis=1)])
backtest2 = generate_backtest(dates, w2, np.column_stack([prices_funded, libor_index]), rb).backtest
weights_libor = np.ones((n_dates, 1))
w3 = weights
backtest3 = generate_backtest_funded_unfunded(
dates, weights_libor, libor_index, w3, prices_unfunded, rb
).backtest
print(
"Backtest1 (funded), Backtest2 (funded + cash leg), Backtest3 (unfunded formulation) -- first/last 5 rows:"
)
print(np.round(np.column_stack([backtest1, backtest2, backtest3])[:5], 3))
print(np.round(np.column_stack([backtest1, backtest2, backtest3])[-5:], 3))
Maximum drawdown in relative mode — backtest/mdd1.py
"""Translated from Examples/backtest/mdd1.m -- maximum_drawdown on 3
simulated price indices (numeric core only; the original's plot marking
the drawdown peak/trough on each series is dropped).
The original explicitly seeds MATLAB's RNG (`rng(1234567)`); NumPy's
generator is seeded the same way for a comparable (not bit-identical)
run."""
import numpy as np
from quanttoolbox.backtest.returns import return_to_price
from quanttoolbox.backtest.stats import maximum_drawdown
from quanttoolbox.dates.convert import parse_date_serial
from quanttoolbox.dates.rebalancing import generate_trading_dates
# See backtest2.py's translation note: generate_trading_dates needs an
# actual pd.Timestamp, not a bare YYYYMMDD int, hence parse_date_serial.
d1, d2 = parse_date_serial([20160101, 20161231])
_, dates = generate_trading_dates(d1, d2, business_days_only=True)
n_dates = len(dates)
n_assets = 3
sigma = 0.20 / np.sqrt(260)
rng = np.random.default_rng(1234567)
r = sigma * rng.standard_normal((n_dates, n_assets))
indices = return_to_price(r)
max_dd, start_dd, end_dd, tau_dd = maximum_drawdown(indices, relative=True)
print("Maximum drawdown per asset:", np.round(max_dd, 4))
print("Start date (peak):", [str(dates[i].date()) for i in start_dd])
print("End date (trough):", [str(dates[i].date()) for i in end_dd])
print("Duration (trading days):", tau_dd)
Per-asset bid/ask transaction costs vs. turnover — backtest/backtest4.py
"""Translated from Examples/backtest/backtest4.m -- generate_backtest with
per-asset bid/ask transaction costs, comparing turnover computed inside
the backtest against `static_turnover` computed independently from the
rebalance-date weights.
The original explicitly seeds MATLAB's RNG (`rng(123456789)`); NumPy's
generator is seeded the same way for a comparable (not bit-identical --
different RNG algorithms) run."""
import numpy as np
from quanttoolbox.backtest.reporting import generate_backtest
from quanttoolbox.backtest.returns import return_to_price
from quanttoolbox.backtest.stats import static_turnover
from quanttoolbox.dates.convert import parse_date_serial
from quanttoolbox.dates.rebalancing import generate_trading_dates
# See backtest2.py's translation note: generate_trading_dates needs an
# actual pd.Timestamp, not a bare YYYYMMDD int, hence parse_date_serial.
d1, d2 = parse_date_serial([20160101, 20160304])
_, dates = generate_trading_dates(d1, d2, business_days_only=True)
n_dates = len(dates)
n_assets = 3
sigma = 0.20 / np.sqrt(260)
rng = np.random.default_rng(123456789)
r = sigma * rng.standard_normal((n_dates, n_assets))
indices = return_to_price(r)
indices = np.ones_like(indices) # matches the original: prices reset to a flat 1.0
weights = rng.random((n_dates, n_assets))
weights = weights / np.sum(weights, axis=1, keepdims=True)
rb_dates = np.arange(5, 30, 5) - 1 # seqa(5,5,5) -> positions 5,10,15,20,25 (1-indexed)
result1 = generate_backtest(dates, weights, indices, rb_dates)
result2 = generate_backtest(
dates, weights, indices, rb_dates, tc_bid_ask=np.array([0.01, 0.01, 0.05])
)
print("backtest without / with transaction costs, and turnover/TC (first 8 rows):")
print(
np.round(
np.column_stack(
[result1.backtest, result2.backtest, result2.turnover, result2.transaction_costs]
)[:8],
4,
)
)
rb_mask = result2.rebalancing[:, 0] == 1
w_rb = weights[rb_mask]
to_from_backtest = result2.turnover[rb_mask]
# static_turnover gives one turnover per consecutive pair of rebalance
# weights (n_rb - 1 values), vs. the backtest's own per-rebalance turnover
# series (n_rb values, first entry 0 at the initial rebalance) -- printed
# separately since they don't align row-for-row.
to_static = static_turnover(w_rb)
print("\n100*weights at rebalance dates, turnover (from backtest):")
print(np.round(np.column_stack([100 * w_rb, to_from_backtest]), 4))
print("\nturnover between consecutive rebalances (static_turnover, independent cross-check):")
print(np.round(to_static, 4))
Small hand-traceable funded/unfunded round-trip (n=8) — backtest/unfunded2.py
"""Translated from Examples/backtest/unfunded2.m -- a tiny (n=8),
hand-traceable version of unfunded1.py's funded/unfunded round-trip:
checks that price_to_unfunded -> unfunded_to_price recovers the original
funded prices, and that the three backtest formulations agree, on a
deterministic constant-return series (no randomness involved, so no
fixed-seed substitution is needed here)."""
import numpy as np
from quanttoolbox.backtest.reporting import generate_backtest, generate_backtest_funded_unfunded
from quanttoolbox.backtest.returns import price_to_return, price_to_unfunded, unfunded_to_price
n_dates = 8
dates = np.arange(1, n_dates + 1)
rb = np.ones(n_dates) # every date is a rebalance
r = 0.03 * np.ones((n_dates, 1))
prices_funded = 100 * np.cumprod(1 + r, axis=0)
prices_funded = 100 * prices_funded / prices_funded[0]
r1 = price_to_return(prices_funded, 1)
r_libor = 0.01 * np.ones((n_dates, 1))
libor_index = 100 * np.cumprod(1 + r_libor, axis=0)
prices_unfunded = price_to_unfunded(prices_funded, libor_index, method=1)
r2 = price_to_return(prices_unfunded, 1)
r3 = price_to_return(libor_index, 1)
p3 = unfunded_to_price(prices_unfunded, libor_index)
print("returns: r (input), r1 (from prices_funded), r2 (unfunded), r3 (libor):")
print(np.round(np.column_stack([r, r1, r2, r3]), 5))
print("\nprices: funded, round-tripped-back-to-funded (p3), libor:")
print(np.round(np.column_stack([prices_funded, p3, libor_index]), 4))
weights = np.ones((n_dates, 1))
w1 = weights
backtest1 = generate_backtest(dates, w1, prices_funded, rb).backtest
w2 = np.column_stack([weights, 1 - np.sum(weights, axis=1)])
backtest2 = generate_backtest(dates, w2, np.column_stack([prices_funded, libor_index]), rb).backtest
weights_libor = np.ones((n_dates, 1))
w3 = weights
backtest3 = generate_backtest_funded_unfunded(
dates, weights_libor, libor_index, w3, prices_unfunded, rb
).backtest
print("\nBacktest1 (funded), Backtest2 (funded + cash leg), Backtest3 (unfunded formulation):")
print(np.round(np.column_stack([backtest1, backtest2, backtest3]), 4))
Three rebalancing schedules on a small backtest — backtest/backtest2.py
"""Translated from Examples/backtest/backtest2.m -- generate_backtest at
three rebalancing schedules (single rebalance at t0, four fixed
positional rebalances, and a schedule mixing an actual date subset with
two out-of-range placeholder dates) on a simulated 3-asset equal-weight
portfolio, with one price set to NaN in the third test.
The original draws R from MATLAB's unseeded `randn`; a fixed seed
(`np.random.default_rng(0)`) is substituted here for reproducibility,
same convention used elsewhere in this port. Plotting is dropped (see
`docs/migration_map.md`'s example translation tracker Notes column
convention)."""
import numpy as np
from quanttoolbox.backtest.reporting import generate_backtest
from quanttoolbox.backtest.returns import return_to_price
from quanttoolbox.dates.convert import parse_date_serial
from quanttoolbox.dates.rebalancing import generate_trading_dates
# generate_trading_dates takes pd.Timestamp (or a bare int, which it feeds
# straight to pd.Timestamp(int) -- interpreted as a nanosecond epoch, NOT
# a YYYYMMDD date). parse_date_serial(...) is used here to get an actual
# 2016-01-01/2016-12-31 pair instead.
d1, d2 = parse_date_serial([20160101, 20161231])
_, dates = generate_trading_dates(d1, d2, business_days_only=True)
n_dates = len(dates)
n_assets = 3
sigma = 0.20 / np.sqrt(260)
rng = np.random.default_rng(0)
r = sigma * rng.standard_normal((n_dates, n_assets))
indices = return_to_price(r)
weights = np.full((n_dates, n_assets), 1.0 / n_assets)
# Test 1: single rebalance at t0
rb_dates1 = dates[:1]
result1 = generate_backtest(dates, weights, indices, rb_dates1)
y1 = np.sum(indices * weights, axis=1)
y1 = 100 * y1 / y1[0]
print("Test 1 (single rebalance): backtest vs. buy&hold, first/last 3 rows")
print(np.column_stack([result1.backtest, y1])[:3])
print(np.column_stack([result1.backtest, y1])[-3:])
# Test 2: four fixed positional rebalances (MATLAB 1-indexed [1,50,150,200])
rb_dates2 = np.array([1, 50, 150, 200]) - 1
result2 = generate_backtest(dates, weights, indices, rb_dates2)
y2 = np.sum(indices * weights, axis=1)
y2 = 100 * y2 / y2[0]
print("\nTest 2 (4 fixed rebalances): backtest vs. buy&hold, first/last 3 rows")
print(np.column_stack([result2.backtest, y2])[:3])
print(np.column_stack([result2.backtest, y2])[-3:])
# Test 3: an actual date subset plus two out-of-range placeholder dates
# (20150101/20170101, both before/after the calendar -- they simply won't
# match anything in `dates`), and a NaN price injected on day 3.
rb_idx = np.array([10, 50, 150, 200]) - 1
placeholder_before, placeholder_after = parse_date_serial([20150101, 20170101])
rb_dates3 = dates[rb_idx].insert(0, placeholder_before)
rb_dates3 = rb_dates3.insert(len(rb_dates3), placeholder_after)
indices3 = indices.copy()
indices3[2, 0] = np.nan
result3 = generate_backtest(dates, weights, indices3, rb_dates3)
y3 = np.sum(indices3 * weights, axis=1)
y3 = 100 * y3 / y3[0]
print("\nTest 3 (date subset + NaN price): backtest vs. buy&hold, first/last 3 rows")
print(np.column_stack([result3.backtest, y3])[:3])
print(np.column_stack([result3.backtest, y3])[-3:])
backtest.stats¶
quanttoolbox.backtest.stats
¶
Drawdown, turnover, average-return, and monthly/yearly performance statistics.
Ported from QuantToolBox/backtest/{maximum_drawdown,static_turnover, annualized_turnover,average_return,index_repeated_data,monthly_statistics, yearly_statistics}.m
Translation notes:
maximum_drawdownreturns 0-indexed start/end row positions (not MATLAB's 1-indexed), so downstream code should adjust index arithmetic accordingly (e.g. when mapping back to a Dates array).monthly_statistics/yearly_statisticsreproduce the originals' "reindex onto a full trading calendar, forward-fill gaps" pattern using pandas (reindex+ffill) instead of MATLAB's manualindnv/fillmisscombination.- Both statistics functions accept a
begin_date/end_dateas either apandas.Timestampor an integer YYYYMMDD (0 meaning "use the full available range"), matching the original's dual calling convention.
annualized_turnover(dates, turnover, by_year=False)
¶
Annualize a turnover series, either as a single average-per-year figure over the whole sample, or broken out year by year.
Original: backtest/annualized_turnover.m
Source code in src/quanttoolbox/backtest/stats.py
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
average_return(r, n_lags)
¶
Trailing n_lags-period moving average of a return series (NaN-filled as zero before averaging).
Original: backtest/average_return.m
Source code in src/quanttoolbox/backtest/stats.py
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
index_repeated_data(x, precision)
¶
Identify rows that repeat the previous row's value (after rounding to
precision decimal places) -- useful for spotting stale/non-trading
price data.
Original: backtest/index_repeated_data.m
Returns:
| Name | Type | Description |
|---|---|---|
idx_repeated |
0-indexed positions where the value repeats the prior row.
|
|
idx_changed |
0-indexed positions where the value differs from the prior row.
|
|
is_repeated |
boolean mask, same length as x.
|
|
Source code in src/quanttoolbox/backtest/stats.py
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 | |
maximum_drawdown(x, relative=False)
¶
Maximum drawdown of each column of a price/index series.
Original: backtest/maximum_drawdown.m
Returns:
| Name | Type | Description |
|---|---|---|
max_dd |
maximum drawdown per column (negative or zero).
|
|
start_dd |
0-indexed row where the drawdown period started (peak).
|
|
end_dd |
0-indexed row where the maximum drawdown was reached (trough).
|
|
tau_dd |
drawdown duration in rows (end_dd - start_dd + 1).
|
|
Source code in src/quanttoolbox/backtest/stats.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 | |
monthly_statistics(dates, backtests, begin_date=0, end_date=0)
¶
Monthly and yearly returns/volatility/drawdown statistics for one or more backtest series.
Original: backtest/monthly_statistics.m
Source code in src/quanttoolbox/backtest/stats.py
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
price_to_return_1d(x)
¶
1-D convenience wrapper around price_to_return for internal use here.
Source code in src/quanttoolbox/backtest/stats.py
364 365 366 367 368 369 | |
return_to_price_1d(r)
¶
1-D convenience wrapper around return_to_price for internal use here.
Source code in src/quanttoolbox/backtest/stats.py
372 373 374 375 | |
static_turnover(x, y=None)
¶
Turnover between consecutive rows of x (or between x and y if given).
Original: backtest/static_turnover.m
Source code in src/quanttoolbox/backtest/stats.py
74 75 76 77 78 79 80 81 82 83 84 85 86 | |
yearly_statistics(dates, backtests, benchmark, begin_date=0, end_date=0)
¶
Yearly return/volatility/tracking-error/beta/correlation statistics for one or more backtest series against a benchmark.
Original: backtest/yearly_statistics.m
Source code in src/quanttoolbox/backtest/stats.py
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
Examples¶
Maximum drawdown in relative mode — backtest/mdd1.py
"""Translated from Examples/backtest/mdd1.m -- maximum_drawdown on 3
simulated price indices (numeric core only; the original's plot marking
the drawdown peak/trough on each series is dropped).
The original explicitly seeds MATLAB's RNG (`rng(1234567)`); NumPy's
generator is seeded the same way for a comparable (not bit-identical)
run."""
import numpy as np
from quanttoolbox.backtest.returns import return_to_price
from quanttoolbox.backtest.stats import maximum_drawdown
from quanttoolbox.dates.convert import parse_date_serial
from quanttoolbox.dates.rebalancing import generate_trading_dates
# See backtest2.py's translation note: generate_trading_dates needs an
# actual pd.Timestamp, not a bare YYYYMMDD int, hence parse_date_serial.
d1, d2 = parse_date_serial([20160101, 20161231])
_, dates = generate_trading_dates(d1, d2, business_days_only=True)
n_dates = len(dates)
n_assets = 3
sigma = 0.20 / np.sqrt(260)
rng = np.random.default_rng(1234567)
r = sigma * rng.standard_normal((n_dates, n_assets))
indices = return_to_price(r)
max_dd, start_dd, end_dd, tau_dd = maximum_drawdown(indices, relative=True)
print("Maximum drawdown per asset:", np.round(max_dd, 4))
print("Start date (peak):", [str(dates[i].date()) for i in start_dd])
print("End date (trough):", [str(dates[i].date()) for i in end_dd])
print("Duration (trading days):", tau_dd)
Per-asset bid/ask transaction costs vs. turnover — backtest/backtest4.py
"""Translated from Examples/backtest/backtest4.m -- generate_backtest with
per-asset bid/ask transaction costs, comparing turnover computed inside
the backtest against `static_turnover` computed independently from the
rebalance-date weights.
The original explicitly seeds MATLAB's RNG (`rng(123456789)`); NumPy's
generator is seeded the same way for a comparable (not bit-identical --
different RNG algorithms) run."""
import numpy as np
from quanttoolbox.backtest.reporting import generate_backtest
from quanttoolbox.backtest.returns import return_to_price
from quanttoolbox.backtest.stats import static_turnover
from quanttoolbox.dates.convert import parse_date_serial
from quanttoolbox.dates.rebalancing import generate_trading_dates
# See backtest2.py's translation note: generate_trading_dates needs an
# actual pd.Timestamp, not a bare YYYYMMDD int, hence parse_date_serial.
d1, d2 = parse_date_serial([20160101, 20160304])
_, dates = generate_trading_dates(d1, d2, business_days_only=True)
n_dates = len(dates)
n_assets = 3
sigma = 0.20 / np.sqrt(260)
rng = np.random.default_rng(123456789)
r = sigma * rng.standard_normal((n_dates, n_assets))
indices = return_to_price(r)
indices = np.ones_like(indices) # matches the original: prices reset to a flat 1.0
weights = rng.random((n_dates, n_assets))
weights = weights / np.sum(weights, axis=1, keepdims=True)
rb_dates = np.arange(5, 30, 5) - 1 # seqa(5,5,5) -> positions 5,10,15,20,25 (1-indexed)
result1 = generate_backtest(dates, weights, indices, rb_dates)
result2 = generate_backtest(
dates, weights, indices, rb_dates, tc_bid_ask=np.array([0.01, 0.01, 0.05])
)
print("backtest without / with transaction costs, and turnover/TC (first 8 rows):")
print(
np.round(
np.column_stack(
[result1.backtest, result2.backtest, result2.turnover, result2.transaction_costs]
)[:8],
4,
)
)
rb_mask = result2.rebalancing[:, 0] == 1
w_rb = weights[rb_mask]
to_from_backtest = result2.turnover[rb_mask]
# static_turnover gives one turnover per consecutive pair of rebalance
# weights (n_rb - 1 values), vs. the backtest's own per-rebalance turnover
# series (n_rb values, first entry 0 at the initial rebalance) -- printed
# separately since they don't align row-for-row.
to_static = static_turnover(w_rb)
print("\n100*weights at rebalance dates, turnover (from backtest):")
print(np.round(np.column_stack([100 * w_rb, to_from_backtest]), 4))
print("\nturnover between consecutive rebalances (static_turnover, independent cross-check):")
print(np.round(to_static, 4))
backtest.reporting¶
quanttoolbox.backtest.reporting
¶
Backtest simulation engines and comprehensive performance reporting.
Ported from QuantToolBox/backtest/{generate_backtest,generate_backtest2, backtest_reporting}.m
Translation notes:
generate_backtest(buy-and-hold-between-rebalances simulator, with optional per-asset bid/ask transaction costs) is ported preserving its loop-over-rebalancing-periods structure, since each period's asset universe (nonzero weights, valid prices) can differ and doesn't vectorize cleanly across periods.backtest_reportingis entirely numeric in the original (it builds up a results struct); the MATLABdisp/printing that would normally accompany a report script isn't part of this function, so nothing was dropped here -- the returnedBacktestReportdataclass holds every field the original struct held.- Frequency auto-detection (daily/weekly/monthly, from the average gap between dates) is preserved exactly, including the original's hard failure (empty result) for irregular calendars.
backtest_reporting(dates, backtest, b_index=None, r_index=None, begin_date=0, end_date=0)
¶
Comprehensive performance report: annualized return/vol, Sharpe and information ratios, beta/correlation to a benchmark, maximum drawdown, and monthly/yearly breakdowns.
Original: backtest/backtest_reporting.m
Returns None if the input calendar's frequency can't be classified as daily/weekly/monthly (matching the original's early-return behavior).
Source code in src/quanttoolbox/backtest/reporting.py
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 | |
generate_backtest(dates, weights, prices, rb_dates, tc_bid_ask=None)
¶
Simulate a buy-and-hold-between-rebalances portfolio backtest.
On each rebalancing date, the portfolio is reset to the target
weights for that date (using prices at that date), then held
(drifting with asset prices) until the next rebalancing date.
Assets with NaN price or NaN/zero weight at a rebalance are excluded
for that period. If tc_bid_ask is given, per-unit bid/ask
transaction costs are charged on turnover at each rebalance, and the
simulation tracks actual unit holdings rather than pure weights.
Original: backtest/generate_backtest.m
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dates
|
full daily calendar of the backtest.
|
|
required |
weights
|
(n_dates, n_assets) target weights (only rows at rebalancing
|
dates are used). |
required |
prices
|
(n_dates, n_assets) asset price levels.
|
|
required |
rb_dates
|
rebalancing dates, either as a DatetimeIndex (subset of
|
|
required |
tc_bid_ask
|
optional (n_assets,) or (n_assets, 2) [bid, ask] per-unit
|
transaction cost rates. |
None
|
Source code in src/quanttoolbox/backtest/reporting.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
generate_backtest_funded_unfunded(dates, weights_funded, prices_funded, weights_unfunded, prices_unfunded, rb_dates)
¶
Simulate a backtest combining a "funded" (price-return) leg and an "unfunded" (excess-return, i.e. financed) leg, rebalanced together.
Pass weights_funded=0 (or weights_unfunded=0) to disable that
leg entirely, matching the original's scalar-zero sentinel convention.
Original: backtest/generate_backtest2.m
Source code in src/quanttoolbox/backtest/reporting.py
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
price_to_return_1d_columns(x)
¶
Column-wise version of price_to_return_1d for a 2-D (n_dates, n_cols) array.
Source code in src/quanttoolbox/backtest/reporting.py
454 455 456 457 458 | |
Examples¶
Flat transaction cost under a realistic rebalancing schedule — backtest/backtest5.py
"""Translated from Examples/backtest/backtest5.m -- generate_backtest with
a flat transaction cost (0.01) under monthly rebalancing (the original
builds weekly and daily rebalancing schedules too, but overwrites both
before use, so only the monthly one -- `RB_Dates = ones(nDates,1)`, i.e.
every date is a rebalance -- is actually exercised; reproduced as-is)."""
import numpy as np
from quanttoolbox.backtest.reporting import generate_backtest
from quanttoolbox.backtest.returns import return_to_price
from quanttoolbox.dates.convert import parse_date_serial
from quanttoolbox.dates.rebalancing import generate_trading_dates
# See backtest2.py's translation note: generate_trading_dates needs an
# actual pd.Timestamp, not a bare YYYYMMDD int, hence parse_date_serial.
d1, d2 = parse_date_serial([20160101, 20161231])
_, dates = generate_trading_dates(d1, d2, business_days_only=True)
n_dates = len(dates)
n_assets = 3
sigma = 0.20 / np.sqrt(260)
rng = np.random.default_rng(123456789)
r = sigma * rng.standard_normal((n_dates, n_assets))
indices = return_to_price(r)
weights = np.ones((n_dates, n_assets))
weights = weights / np.sum(weights, axis=1, keepdims=True)
rb_dates = np.ones(
n_dates
) # "monthly rebalancing" per the original's final overwrite -> every date
result1 = generate_backtest(dates, weights, indices, rb_dates)
tc = 0.01
result2 = generate_backtest(dates, weights, indices, rb_dates, tc_bid_ask=tc)
print("backtest without / with transaction costs, turnover, TC (first 5 rows):")
print(
np.round(
np.column_stack(
[result1.backtest, result2.backtest, result2.turnover, result2.transaction_costs]
)[:5],
4,
)
)
to_total = np.nansum(result2.turnover)
cost1 = 1 - result2.backtest[-1] / result1.backtest[-1]
cost2 = to_total * tc
print("\nTotal turnover:", round(to_total, 4))
print("Cost1 (1 - final wealth ratio):", round(cost1, 5))
print("Cost2 (total turnover * tc):", round(cost2, 5))
Funded vs. unfunded backtest formulations cross-checked — backtest/unfunded1.py
"""Translated from Examples/backtest/unfunded1.m -- compares a funded
(price-return) backtest against economically equivalent unfunded
(excess-return) formulations, three different ways:
Backtest1 -- pure funded weights on funded prices.
Backtest2 -- funded weights plus an explicit cash/LIBOR leg, funded prices.
Backtest3 -- generate_backtest_funded_unfunded with the LIBOR leg funded
and the risky assets unfunded (price_to_unfunded'd), which should match
Backtest1/Backtest2 economically since unfunding + re-funding is neutral.
The original draws R from MATLAB's unseeded `randn`; a fixed seed
(`np.random.default_rng(0)`) is substituted here. Plotting is dropped."""
import numpy as np
from quanttoolbox.backtest.reporting import generate_backtest, generate_backtest_funded_unfunded
from quanttoolbox.backtest.returns import price_to_unfunded
n_dates = 3000
dates = np.arange(1, n_dates + 1)
rb = np.zeros(n_dates)
rb[np.arange(1, n_dates, 5) - 1] = 1.0
sigma = 0.20 * np.sqrt(1 / 260)
rng = np.random.default_rng(0)
r = sigma * rng.standard_normal((n_dates, 2))
prices_funded = 100 * np.cumprod(1 + r, axis=0)
rate = 0.03
r_libor = rate * np.sqrt(1 / 260) * np.ones((n_dates, 1))
libor_index = 100 * np.cumprod(1 + r_libor, axis=0)
prices_unfunded = price_to_unfunded(prices_funded, libor_index, method=1)
weights = np.tile([0.5, 0.5], (n_dates, 1))
w1 = weights
backtest1 = generate_backtest(dates, w1, prices_funded, rb).backtest
w2 = np.column_stack([weights, 1 - np.sum(weights, axis=1)])
backtest2 = generate_backtest(dates, w2, np.column_stack([prices_funded, libor_index]), rb).backtest
weights_libor = np.ones((n_dates, 1))
w3 = weights
backtest3 = generate_backtest_funded_unfunded(
dates, weights_libor, libor_index, w3, prices_unfunded, rb
).backtest
print(
"Backtest1 (funded), Backtest2 (funded + cash leg), Backtest3 (unfunded formulation) -- first/last 5 rows:"
)
print(np.round(np.column_stack([backtest1, backtest2, backtest3])[:5], 3))
print(np.round(np.column_stack([backtest1, backtest2, backtest3])[-5:], 3))
generate_backtest at four rebalancing frequencies — backtest/backtest3.py
"""Translated from Examples/backtest/backtest3.m -- generate_backtest at
four different rebalancing frequencies (buy-and-hold, every period, every
2 periods, every 3 periods) on a fixed 7-date, 2-asset price series."""
import numpy as np
import pandas as pd
from quanttoolbox.backtest.reporting import generate_backtest
indices = np.array(
[
[100, 100],
[150, 50],
[250, 100],
[100, 150],
[110, 125],
[160, 140],
[150, 150],
],
dtype=float,
)
weights = np.full((7, 2), 0.50)
dates = pd.bdate_range("2023-01-02", periods=7)
# buy & hold (single rebalance at t=0)
rb_mask = np.zeros(7, dtype=bool)
rb_mask[0] = True
r1 = generate_backtest(dates, weights, indices, dates[rb_mask])
print("buy & hold:", np.round(r1.backtest, 3))
# rebalance every period
r2 = generate_backtest(dates, weights, indices, dates)
print("every period:", np.round(r2.backtest, 3))
# rebalance every 2 periods (indices 0,2,4,6 -> 1st,3rd,5th,7th)
rb_mask3 = np.zeros(7, dtype=bool)
rb_mask3[[0, 2, 4, 6]] = True
r3 = generate_backtest(dates, weights, indices, dates[rb_mask3])
print("every 2 periods:", np.round(r3.backtest, 3))
# rebalance every 3 periods (indices 0,3,6 -> 1st,4th,7th)
rb_mask4 = np.zeros(7, dtype=bool)
rb_mask4[[0, 3, 6]] = True
r4 = generate_backtest(dates, weights, indices, dates[rb_mask4])
print("every 3 periods:", np.round(r4.backtest, 3))
Per-asset bid/ask transaction costs vs. turnover — backtest/backtest4.py
"""Translated from Examples/backtest/backtest4.m -- generate_backtest with
per-asset bid/ask transaction costs, comparing turnover computed inside
the backtest against `static_turnover` computed independently from the
rebalance-date weights.
The original explicitly seeds MATLAB's RNG (`rng(123456789)`); NumPy's
generator is seeded the same way for a comparable (not bit-identical --
different RNG algorithms) run."""
import numpy as np
from quanttoolbox.backtest.reporting import generate_backtest
from quanttoolbox.backtest.returns import return_to_price
from quanttoolbox.backtest.stats import static_turnover
from quanttoolbox.dates.convert import parse_date_serial
from quanttoolbox.dates.rebalancing import generate_trading_dates
# See backtest2.py's translation note: generate_trading_dates needs an
# actual pd.Timestamp, not a bare YYYYMMDD int, hence parse_date_serial.
d1, d2 = parse_date_serial([20160101, 20160304])
_, dates = generate_trading_dates(d1, d2, business_days_only=True)
n_dates = len(dates)
n_assets = 3
sigma = 0.20 / np.sqrt(260)
rng = np.random.default_rng(123456789)
r = sigma * rng.standard_normal((n_dates, n_assets))
indices = return_to_price(r)
indices = np.ones_like(indices) # matches the original: prices reset to a flat 1.0
weights = rng.random((n_dates, n_assets))
weights = weights / np.sum(weights, axis=1, keepdims=True)
rb_dates = np.arange(5, 30, 5) - 1 # seqa(5,5,5) -> positions 5,10,15,20,25 (1-indexed)
result1 = generate_backtest(dates, weights, indices, rb_dates)
result2 = generate_backtest(
dates, weights, indices, rb_dates, tc_bid_ask=np.array([0.01, 0.01, 0.05])
)
print("backtest without / with transaction costs, and turnover/TC (first 8 rows):")
print(
np.round(
np.column_stack(
[result1.backtest, result2.backtest, result2.turnover, result2.transaction_costs]
)[:8],
4,
)
)
rb_mask = result2.rebalancing[:, 0] == 1
w_rb = weights[rb_mask]
to_from_backtest = result2.turnover[rb_mask]
# static_turnover gives one turnover per consecutive pair of rebalance
# weights (n_rb - 1 values), vs. the backtest's own per-rebalance turnover
# series (n_rb values, first entry 0 at the initial rebalance) -- printed
# separately since they don't align row-for-row.
to_static = static_turnover(w_rb)
print("\n100*weights at rebalance dates, turnover (from backtest):")
print(np.round(np.column_stack([100 * w_rb, to_from_backtest]), 4))
print("\nturnover between consecutive rebalances (static_turnover, independent cross-check):")
print(np.round(to_static, 4))
Small hand-traceable funded/unfunded round-trip (n=8) — backtest/unfunded2.py
"""Translated from Examples/backtest/unfunded2.m -- a tiny (n=8),
hand-traceable version of unfunded1.py's funded/unfunded round-trip:
checks that price_to_unfunded -> unfunded_to_price recovers the original
funded prices, and that the three backtest formulations agree, on a
deterministic constant-return series (no randomness involved, so no
fixed-seed substitution is needed here)."""
import numpy as np
from quanttoolbox.backtest.reporting import generate_backtest, generate_backtest_funded_unfunded
from quanttoolbox.backtest.returns import price_to_return, price_to_unfunded, unfunded_to_price
n_dates = 8
dates = np.arange(1, n_dates + 1)
rb = np.ones(n_dates) # every date is a rebalance
r = 0.03 * np.ones((n_dates, 1))
prices_funded = 100 * np.cumprod(1 + r, axis=0)
prices_funded = 100 * prices_funded / prices_funded[0]
r1 = price_to_return(prices_funded, 1)
r_libor = 0.01 * np.ones((n_dates, 1))
libor_index = 100 * np.cumprod(1 + r_libor, axis=0)
prices_unfunded = price_to_unfunded(prices_funded, libor_index, method=1)
r2 = price_to_return(prices_unfunded, 1)
r3 = price_to_return(libor_index, 1)
p3 = unfunded_to_price(prices_unfunded, libor_index)
print("returns: r (input), r1 (from prices_funded), r2 (unfunded), r3 (libor):")
print(np.round(np.column_stack([r, r1, r2, r3]), 5))
print("\nprices: funded, round-tripped-back-to-funded (p3), libor:")
print(np.round(np.column_stack([prices_funded, p3, libor_index]), 4))
weights = np.ones((n_dates, 1))
w1 = weights
backtest1 = generate_backtest(dates, w1, prices_funded, rb).backtest
w2 = np.column_stack([weights, 1 - np.sum(weights, axis=1)])
backtest2 = generate_backtest(dates, w2, np.column_stack([prices_funded, libor_index]), rb).backtest
weights_libor = np.ones((n_dates, 1))
w3 = weights
backtest3 = generate_backtest_funded_unfunded(
dates, weights_libor, libor_index, w3, prices_unfunded, rb
).backtest
print("\nBacktest1 (funded), Backtest2 (funded + cash leg), Backtest3 (unfunded formulation):")
print(np.round(np.column_stack([backtest1, backtest2, backtest3]), 4))
Three rebalancing schedules on a small backtest — backtest/backtest2.py
"""Translated from Examples/backtest/backtest2.m -- generate_backtest at
three rebalancing schedules (single rebalance at t0, four fixed
positional rebalances, and a schedule mixing an actual date subset with
two out-of-range placeholder dates) on a simulated 3-asset equal-weight
portfolio, with one price set to NaN in the third test.
The original draws R from MATLAB's unseeded `randn`; a fixed seed
(`np.random.default_rng(0)`) is substituted here for reproducibility,
same convention used elsewhere in this port. Plotting is dropped (see
`docs/migration_map.md`'s example translation tracker Notes column
convention)."""
import numpy as np
from quanttoolbox.backtest.reporting import generate_backtest
from quanttoolbox.backtest.returns import return_to_price
from quanttoolbox.dates.convert import parse_date_serial
from quanttoolbox.dates.rebalancing import generate_trading_dates
# generate_trading_dates takes pd.Timestamp (or a bare int, which it feeds
# straight to pd.Timestamp(int) -- interpreted as a nanosecond epoch, NOT
# a YYYYMMDD date). parse_date_serial(...) is used here to get an actual
# 2016-01-01/2016-12-31 pair instead.
d1, d2 = parse_date_serial([20160101, 20161231])
_, dates = generate_trading_dates(d1, d2, business_days_only=True)
n_dates = len(dates)
n_assets = 3
sigma = 0.20 / np.sqrt(260)
rng = np.random.default_rng(0)
r = sigma * rng.standard_normal((n_dates, n_assets))
indices = return_to_price(r)
weights = np.full((n_dates, n_assets), 1.0 / n_assets)
# Test 1: single rebalance at t0
rb_dates1 = dates[:1]
result1 = generate_backtest(dates, weights, indices, rb_dates1)
y1 = np.sum(indices * weights, axis=1)
y1 = 100 * y1 / y1[0]
print("Test 1 (single rebalance): backtest vs. buy&hold, first/last 3 rows")
print(np.column_stack([result1.backtest, y1])[:3])
print(np.column_stack([result1.backtest, y1])[-3:])
# Test 2: four fixed positional rebalances (MATLAB 1-indexed [1,50,150,200])
rb_dates2 = np.array([1, 50, 150, 200]) - 1
result2 = generate_backtest(dates, weights, indices, rb_dates2)
y2 = np.sum(indices * weights, axis=1)
y2 = 100 * y2 / y2[0]
print("\nTest 2 (4 fixed rebalances): backtest vs. buy&hold, first/last 3 rows")
print(np.column_stack([result2.backtest, y2])[:3])
print(np.column_stack([result2.backtest, y2])[-3:])
# Test 3: an actual date subset plus two out-of-range placeholder dates
# (20150101/20170101, both before/after the calendar -- they simply won't
# match anything in `dates`), and a NaN price injected on day 3.
rb_idx = np.array([10, 50, 150, 200]) - 1
placeholder_before, placeholder_after = parse_date_serial([20150101, 20170101])
rb_dates3 = dates[rb_idx].insert(0, placeholder_before)
rb_dates3 = rb_dates3.insert(len(rb_dates3), placeholder_after)
indices3 = indices.copy()
indices3[2, 0] = np.nan
result3 = generate_backtest(dates, weights, indices3, rb_dates3)
y3 = np.sum(indices3 * weights, axis=1)
y3 = 100 * y3 / y3[0]
print("\nTest 3 (date subset + NaN price): backtest vs. buy&hold, first/last 3 rows")
print(np.column_stack([result3.backtest, y3])[:3])
print(np.column_stack([result3.backtest, y3])[-3:])