Quantitative trading — backtest, drawdown, factor exposure

30 minutes. Two CSVs. Run a momentum strategy on 20 tickers over 500 trading days and see how it would have done.

Updated 2026-04-17

Industry: Quant trading Difficulty: Advanced Time: 30 min Plan: Free or Plus

The scenario

You’re a solo quant running a small book. You want to evaluate a simple idea: own stocks whose close is more than 2% above their 20-day moving average, short the ones more than 2% below, equal-weight the rest. Lots of papers have tested it. You want to run it yourself, on your own data, in a notebook that anyone on your team can read.

This tutorial is the Tablize version of a Jupyter session. Four questions:

  1. What would the strategy have returned over the last 500 trading days?
  2. What was the drawdown profile — and was it tolerable?
  3. What factor exposures does the strategy end up with — was it actually long/short, or just long-biased?
  4. Can we run it forward as a daily Script against live data?

Note: the data is synthetic. It’s realistic in shape (cross-sectional noise, 2022-style drawdown injected around days 300-340) but do not draw real-world conclusions from it. For live data, connect a provider — see Next steps.

Download the sample data

10,000 rows · 500 days × 20 tickers
prices.csv
Daily OHLCV for 20 large-cap US tickers. Synthetic, shared market factor + idiosyncratic noise.
9,600 rows
signals.csv
20-day momentum signal computed from the price data, ready to feed into a backtest.
01

Backtest the strategy

Long top-momentum, short bottom-momentum, equal weight, daily rebalance.
Prompt
Backtest this strategy: each day, go long tickers with signal='long', short tickers with signal='short', flat the rest. Equal-weight across positions. Daily rebalance. Compute cumulative return, annualized return, annualized volatility, and Sharpe ratio (assuming 4% risk-free rate). Compare to an equal-weight long-only benchmark on the same 20 tickers.
quant · momentum backtest
step 1 · expected answer
Tablize
python.analyze · daily-rebalance P&L with pandas 1.8 s
Over 500 trading days the momentum strategy returned +32.4% cumulative vs +41.8% for the long-only benchmark. The momentum strategy had lower volatility (11.2% vs 18.6%) and a higher Sharpe (2.21 vs 1.95) — better risk-adjusted return, but lower absolute.
MetricMomentumLong-only benchmark
Total return+32.4%+41.8%
Annualized return14.9%18.4%
Annualized vol11.2%18.6%
Sharpe (rf=4%)2.211.95
Max drawdown-8.2%-22.4%
· Step 1 — backtest output, momentum vs long-only

Keep it: + Save as Script named “momentum backtest” — rerunnable with different parameter values (window size, threshold, rebalance frequency).

02

Drawdown profile

Equity curve + drawdown chart. Was there a period where you'd have lost faith?
Prompt
Plot the equity curve of the momentum strategy. Below it, plot the rolling drawdown (equity relative to all-time high). Identify the worst drawdown period — duration, depth, time to recover. Compare to the long-only.

Expected answer: the Agent finds the drawdown around days 300-340 (the injected market stress period). The long-only fell ~22% and took ~50 trading days to recover. The momentum fell ~8% and recovered in ~25 days. Plotted side by side.

Keep it: + Save as Report — attach to your risk narrative.

03

Factor exposure

Is this strategy actually long/short, or does it drift net-long?
Prompt
For each day, compute the gross and net exposure of the momentum strategy (number of longs, shorts, and the net). Plot net exposure over time. Also compute the average sector tilt if I group tickers by sector — tech-heavy? Financial-heavy? Any persistent bias?

The Agent shows net exposure hovering around zero (as intended) but with brief periods of strong net-long (during uptrends, most tickers trigger “long”). Sector tilt analysis shows the strategy spent significant time net-long Tech and net-short Consumer Staples — not surprising but worth flagging.

Keep it: + Save as Script — feeds a weekly risk review.

04

Run it live

Turn the backtest into a daily signal generator.
Prompt
Turn the strategy into a daily Script: fetch the latest prices, compute today's signals, produce a list of positions to open / close vs yesterday. Schedule for every trading day at 16:15 US Eastern. Output as a CSV I can paste into my broker.

The Agent creates the Script with a daily schedule. Each run produces a trade list. You review the list before executing — automation for the analysis, human-in-the-loop for the execution.

What you built in 30 minutes

  • 2 Reports — backtest performance, drawdown analysis.
  • 2 Scripts — backtest rerunnable with new parameters; daily live signal generator.
  • A foundation — for adding more signals, more instruments, more sophisticated risk.

Next steps in this industry

  • Connect real data — Alpaca, Interactive Brokers, Polygon, Refinitiv — all via Web API. Daily prices are free/cheap; intraday gets pricier.
  • Extend to multiple signals — momentum × value × quality. Rank-combine. Standard factor model.
  • Paper trade first — run the daily Script for a month, log the paper trades, compare to live would-have-been. Rarely does research PnL match live; discovering that 10% miss is the whole point.
  • Read the Quant Trading industry page — deeper scenarios: transaction cost modeling, walk-forward validation, ensemble strategies.

Nearby tutorials

  • Finance — if you’re managing cash, not positions.
  • SaaS — if your brain hurts from vol numbers and you want something simpler to reset with.