Rolling & calendar returns

Rolling diagnostics that make regime changes visible, and year-by-month calendar return tables. These code cells are executed live by Quarto every time the docs are built.

Real return streams

We use qrt’s bundled sample datasets — AAPL as the “strategy” and SPY as the benchmark — loaded offline via q.data.datasets.load, no network dependency:

import pandas as pd

import qrt as q

aapl = q.data.datasets.load("aapl")
spy = q.data.datasets.load("spy")

returns = pd.concat(
    {
        "AAPL": aapl["close"].pct_change(),
        "SPY": spy["close"].pct_change(),
    },
    axis=1,
).dropna()
strategy = returns["AAPL"]
benchmark = returns["SPY"]

returns.tail()
AAPL SPY
datetime
2026-07-13 0.006311 -0.007656
2026-07-14 -0.007721 0.003551
2026-07-15 0.040145 0.003964
2026-07-16 0.017588 -0.005419
2026-07-17 0.001440 -0.009897

Rolling diagnostics

Rolling metrics make regime changes visible. A 63-business-day window is roughly one quarter. The four diagnostics are combined into one interactive figure with q.plot.col:

window = 63
rolling = pd.concat(
    {
        "Volatility": q.stats.rolling_volatility(strategy, window),
        "Sharpe": q.stats.rolling_sharpe(strategy, window),
        "Beta": q.stats.rolling_beta(strategy, benchmark, window),
        "Alpha": q.stats.rolling_alpha(strategy, benchmark, window),
    },
    axis=1,
)

fig = q.plot.col(
    rolling,
    title=f"{window}-day rolling risk and benchmark diagnostics",
    ylabel="Value",
    height=550,
)
fig.show()

Calendar returns

q.stats.monthly_returns compounds daily returns within each calendar month into a year-by-month table, with a trailing EOY (end-of-year) column of compounded annual returns by default (eoy=False to omit it); q.plot.monthly_heatmap renders the monthly grid as an interactive heatmap:

display(q.stats.monthly_returns(strategy).style.format("{:.1%}"))

fig = q.plot.monthly_heatmap(strategy, title="Strategy monthly returns")
fig.show()
Month 1 2 3 4 5 6 7 8 9 10 11 12 EOY
Year                          
2000 -7.3% 10.5% 18.5% -8.7% -32.3% 24.7% -3.0% 19.9% -57.7% -24.0% -15.7% -9.8% -73.4%
2001 45.4% -15.6% 20.9% 15.5% -21.7% 16.5% -19.2% -1.3% -16.4% 13.2% 21.3% 2.8% 47.2%
2002 12.9% -12.2% 9.1% 2.5% -4.0% -23.9% -13.9% -3.3% -1.7% 10.8% -3.5% -7.5% -34.6%
2003 0.2% 4.5% -5.8% 0.6% 26.2% 6.2% 10.6% 7.3% -8.4% 10.5% -8.7% 2.2% 49.1%
2004 5.6% 6.0% 13.0% -4.7% 8.8% 16.0% -0.6% 6.6% 12.4% 35.2% 28.0% -4.0% 201.4%
2005 19.4% 16.7% -7.1% -13.5% 10.3% -7.4% 15.9% 9.9% 14.3% 7.4% 17.8% 6.0% 123.3%
2006 5.0% -9.3% -8.4% 12.2% -15.1% -4.2% 18.7% -0.2% 13.5% 5.3% 13.0% -7.4% 18.0%
2007 1.0% -1.3% 9.8% 7.4% 21.4% 0.7% 8.0% 5.1% 10.8% 23.8% -4.1% 8.7% 133.5%
2008 -31.7% -7.6% 14.8% 21.2% 8.5% -11.3% -5.1% 6.7% -33.0% -5.3% -13.9% -7.9% -56.9%
2009 5.6% -0.9% 17.7% 19.7% 7.9% 4.9% 14.7% 2.9% 10.2% 1.7% 6.1% 5.4% 146.9%
2010 -8.9% 6.5% 14.8% 11.1% -1.6% -2.1% 2.3% -5.5% 16.7% 6.1% 3.4% 3.7% 53.1%
2011 5.2% 4.1% -1.3% 0.5% -0.7% -3.5% 16.3% -1.4% -0.9% 6.2% -5.6% 6.0% 25.6%
2012 12.7% 18.8% 10.5% -2.6% -1.1% 1.1% 4.6% 9.4% 0.3% -10.8% -1.2% -9.1% 32.6%
2013 -14.4% -2.5% 0.3% 0.0% 2.2% -11.8% 14.1% 8.4% -2.1% 9.6% 7.0% 0.9% 8.1%
2014 -10.8% 5.8% 2.0% 9.9% 7.9% 2.8% 2.9% 7.8% -1.7% 7.2% 10.6% -7.2% 40.6%
2015 6.1% 10.1% -3.1% 0.6% 4.5% -3.7% -3.3% -6.6% -2.2% 8.3% -0.6% -11.0% -3.0%
2016 -7.5% -0.1% 12.7% -14.0% 7.2% -4.3% 9.0% 2.4% 6.6% 0.4% -2.2% 4.8% 12.5%
2017 4.8% 13.4% 4.9% -0.0% 6.8% -5.7% 3.3% 10.7% -6.0% 9.7% 2.0% -1.5% 48.5%
2018 -1.1% 6.8% -5.8% -1.5% 13.5% -0.9% 2.8% 20.0% -0.8% -3.0% -18.1% -11.7% -5.4%
2019 5.5% 4.5% 9.7% 5.6% -12.4% 13.1% 7.6% -1.6% 7.3% 11.1% 7.8% 9.9% 89.0%
2020 5.4% -11.5% -7.0% 15.5% 8.5% 14.7% 16.5% 21.7% -10.3% -6.0% 9.5% 11.5% 82.3%
2021 -0.6% -8.0% 0.7% 7.6% -5.0% 9.9% 6.5% 4.2% -6.8% 5.9% 10.5% 7.4% 34.6%
2022 -1.6% -5.4% 5.7% -9.7% -5.4% -8.1% 18.9% -3.1% -12.1% 11.0% -3.3% -12.2% -26.4%
2023 11.1% 2.3% 11.9% 2.9% 4.6% 9.4% 1.3% -4.2% -8.9% -0.3% 11.4% 1.4% 49.0%
2024 -4.2% -1.9% -5.1% -0.7% 13.0% 9.6% 5.4% 3.2% 1.7% -3.0% 5.2% 5.5% 30.7%
2025 -5.8% 2.6% -8.2% -4.3% -5.4% 2.2% 1.2% 12.0% 9.7% 6.2% 3.2% -2.5% 9.1%
2026 -4.6% 1.9% -3.9% 6.9% 15.1% -7.3% 15.3% nan% nan% nan% nan% nan% 23.0%
Back to top