Leanpub Header

Skip to main content

The Quant Reality Check

Minimum price

$9.99

$24.99

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

Share this book

Author

About the Author

Contents

Table of Contents

Disclaimer

How This Book Is Structured

1. Algorithmic Trading as an Engineering Discipline

  1. 1.1 What we actually mean by algorithmic trading
  2. 1.2 Mindset: a strategy is a hypothesis, not a conviction
  3. 1.3 Scope of this book
  4. 1.4 The pipeline in one picture

pipeline.py - the skeleton we will build up across this book

Every stage logs to a single results table for auditability.

  1. 1.5 Why most retail strategies fail
  2. 1.6 A note on realism and on the numbers in this book
  3. Key Takeaways
  4. Action Steps

2. Setting Up a Reproducible Research Environment

  1. 2.1 Why the environment matters
  2. 2.2 Creating a virtual environment

From your project root

  1. 2.3 Freezing dependencies
  2. 2.4 Project layout
  3. 2.5 A small paths and config helper

src/utils/paths.py

  1. 2.6 Determinism: seeds and clocks

src/utils/determinism.py

  1. 2.7 Logging instead of print

src/utils/logging.py

  1. 2.8 Data formats: why parquet beats CSV

src/data/convert.py

  1. 2.9 Testing your assumptions

tests/test_indicators.py

First two windows are NaN; window 3 mean = (1+2+3)/3 = 2.0

  1. 2.10 A minimal runnable script

run_backtest.py

  1. Key Takeaways
  2. Action Steps

3. Market Data: Structure, Adjustment, and Quality

  1. 3.1 The units you are actually working with

src/data/schema.py

You must decide how to handle NaNs explicitly, not let them slide.

  1. 3.2 Time zones and trading calendars

src/data/calendar.py

  1. 3.3 Raw versus adjusted prices

src/data/adjust.py

  1. 3.4 Data quality profiling

src/data/profile.py

  1. 3.5 Handling gaps and missing days
  2. 3.6 Survivorship and universe construction

src/data/universe.py

  1. 3.7 A sample cleaning pipeline

src/data/clean.py

Drop exact duplicate timestamps, keeping the last observed bar.

Coerce prices to numeric; bad prints become NaN and are dropped here.

  1. Key Takeaways
  2. Action Steps

4. Your Indicator Toolbox: SMA, EMA, RSI, MACD, and ATR

  1. 4.1 Why build your own indicators
  2. 4.2 Simple moving average

src/indicators/trend.py

  1. 4.3 Exponential moving average
  2. 4.4 Relative strength index (RSI)

Wilder smoothing is equivalent to EMA with alpha = 1/window.

  1. 4.5 Average true range (ATR)
  2. 4.6 MACD
  3. 4.7 Rolling volatility and returns
  4. 4.8 A reusable indicator frame

src/indicators/frame.py

  1. 4.9 Unit-testing indicators against known values

tests/test_indicators_extra.py

  1. 4.10 The discipline of not over-engineering
  2. Key Takeaways
  3. Action Steps

5. From Idea to Falsifiable Strategy

  1. 5.1 The hypothesis statement
  2. 5.2 Defining the state machine

State machine for a trend-pullback strategy (illustration only)

States: FLAT, LONG

FLAT -> LONG when: trend_up AND pullback_today

LONG -> FLAT when: trend_broken OR trailing_stop OR time_stop

  1. 5.3 Separate signal generation from execution

src/strategies/pullback.py

Expand entry into a hold window via a time stop.

  1. 5.4 Parameter choices: pick values, do not optimize yet

config/params.yaml

  1. 5.5 What to record for every experiment
  2. 5.6 Falsifiability: actively try to break your idea
  3. 5.7 Common idea-destroying questions
  4. Key Takeaways
  5. Action Steps

6. Vectorized Backtesting: Mechanics and Costs

  1. 6.1 What a vectorized backtest actually computes
  2. 6.2 The one-period lag that kills beginners

src/backtest/returns.py

Asset return from t to t+1.

We hold target[t] over that period.

  1. 6.3 Modeling commissions

src/backtest/costs.py

  1. 6.4 Modeling slippage

Cost per trade scales with ATR (volatility) times a slippage coefficient.

  1. 6.5 Assembling the equity curve

src/backtest/simulate.py

  1. 6.6 Compounding versus fixed fractional size
  2. 6.7 What vectorized backtests cannot do
  3. 6.8 Sanity checks every backtest must pass
  4. Key Takeaways
  5. Action Steps

7. Position Sizing and Risk Budgeting

  1. 7.1 Size is a decision, not an afterthought
  2. 7.2 Fixed fractional sizing

src/risk/sizing.py

  1. 7.3 Volatility-targeted sizing
  2. 7.4 Portfolio-level risk: the heat limit

src/risk/portfolio_heat.py

  1. 7.5 The Kelly intuition, used conservatively

win_loss_ratio = average win / average loss

  1. 7.6 Drawdown control: the circuit breaker

src/risk/drawdown.py

  1. 7.7 Sizing constraints you must respect
  2. 7.8 Why this matters more than the signal
  3. Key Takeaways
  4. Action Steps

8. Stop-Loss and Exit Rules as Risk Devices

  1. 8.1 Exits are risk controls, not predictions
  2. 8.2 The hard stop

src/risk/exits.py

  1. 8.3 The volatility (ATR) stop
  2. 8.4 The trailing stop
  3. 8.5 The time stop
  4. 8.6 Combining exits with a small state machine

src/risk/position_state.py

  1. 8.7 The gap-down problem
  2. 8.8 What stops do and do not do
  3. Key Takeaways
  4. Action Steps

9. Order Types and Execution Realism

  1. 9.1 Why execution belongs in the backtest
  2. 9.2 Market orders
  3. 9.3 Limit orders

src/backtest/fills.py

  1. 9.4 Stop and stop-limit orders
  2. 9.5 The open-versus-close decision
  3. 9.6 Volume and participation limits
  4. 9.7 Borrow and short constraints
  5. 9.8 Choosing one fill convention and documenting it
  6. Key Takeaways
  7. Action Steps

10. Paper Trading and Reconciliation

  1. 10.1 Why paper trading still matters
  2. 10.2 What paper trading actually is
  3. 10.3 Logging the paper book

src/paper/log.py

  1. 10.4 Reconciliation: sim versus live
  2. 10.5 How long to paper trade
  3. 10.6 The temptation to tune mid-test
  4. 10.7 When paper trading says stop
  5. Key Takeaways
  6. Action Steps

11. The Bias Zoo: Look-Ahead, Survivorship, and Data Snooping

  1. 11.1 Why this chapter matters more than the signal
  2. 11.2 Look-ahead bias

src/audit/lookahead.py

  1. 11.3 Survivorship bias
  2. 11.4 Data snooping and multiple testing

src/audit/multiple_testing.py

  1. 11.5 Overfitting

src/audit/parameter_robustness.py

  1. 11.6 Selection bias in reporting
  2. 11.7 A pre-production checklist
  3. Key Takeaways
  4. Action Steps

12. Performance Measurement That Does Not Lie

  1. 12.1 Measure the right things
  2. 12.2 Total return and CAGR

src/metrics/performance.py

  1. 12.3 Volatility and Sharpe ratio
  2. 12.4 Sortino ratio
  3. 12.5 Maximum drawdown
  4. 12.6 Win rate, profit factor, and turnover
  5. 12.7 Putting a report together
  6. Key Takeaways
  7. Action Steps

13. Capstone: A Complete Rule-Based System Walkthrough

  1. 13.1 What this chapter is and is not
  2. 13.2 The hypothesis, restated
  3. 13.3 The full pipeline code

src/capstone/system.py

Mark to market.

Exit on trailing stop.

Enter on signal, sized by volatility risk.

  1. 13.4 Reading the report
  2. 13.5 What to vary and what to hold constant
  3. 13.6 Common failure modes you will see
  4. 13.7 When to stop developing
  5. Key Takeaways
  6. Action Steps

14. From Research to Production: A Roadmap

  1. 14.1 The research-to-production gap
  2. 14.2 Stage 1: Research validation
  3. 14.3 Stage 2: Paper trading
  4. 14.4 Stage 3: Small live deployment
  5. 14.5 Stage 4: Monitoring and alerting

src/prod/monitor.py

  1. 14.6 Stage 5: Iteration without contamination
  2. 14.7 Tax and record keeping
  3. 14.8 Knowing when to walk away
  4. Key Takeaways
  5. Action Steps

15. Worked Example: Step-by-Step on Synthetic Data

  1. 15.1 Why a synthetic example
  2. 15.2 The input series
  3. 15.3 Compute the SMA by hand

Expected: [nan, nan, 101.17, 101.77, 102.43, 103.27, 105.0, 106.0, 107.17, 108.17]

  1. 15.4 Compute ATR
  2. 15.5 Walk the position
  3. 15.6 Reconstruct a real trade from your log
  4. 15.7 The danger of a single example
  5. Key Takeaways
  6. Action Steps

16. A Catalog of Common Code Mistakes

  1. 16.1 Why this chapter exists
  2. 16.2 The missing shift
  3. 16.3 Partial-window averages
  4. 16.4 Adjustment factor leak
  5. 16.5 NaN propagation
  6. 16.6 Off-by-one in the time stop
  7. 16.7 Inconsistent cost scaling
  8. 16.8 Survivorship in a quick universe
  9. 16.9 Tuning on the test set
  10. 16.10 Equity curve compounding error

src/audit/mistakes.py

  1. 16.11 The “too clean” equity curve
  2. 16.12 A self-audit script
  3. Key Takeaways
  4. Action Steps

17. From One Instrument to a Small Portfolio

  1. 17.1 Why one instrument is not enough
  2. 17.2 The weights matrix

src/portfolio/weights.py

  1. 17.3 Volatility-targeted weights
  2. 17.4 Portfolio-level returns

Lag weights by one period to avoid look-ahead.

  1. 17.5 Correlation and the hidden risk
  2. 17.6 The cash buffer
  3. 17.7 Rebalancing rules
  4. 17.8 When to stop adding instruments
  5. Key Takeaways
  6. Action Steps

18. A Repeatable Research SOP and Final Checklists

  1. 18.1 Why an SOP
  2. 18.2 Stage zero: hypothesis
  3. 18.3 Stage one: data
  4. 18.4 Stage two: signal
  5. 18.5 Stage three: simulation
  6. 18.6 Stage four: validation
  7. 18.7 Stage five: paper trading
  8. 18.8 Stage six: small live deployment
  9. 18.9 The pre-production checklist
  10. 18.10 The anti-patterns list
  11. 18.11 Closing the loop
  12. Key Takeaways
  13. Action Steps

19. Choosing and Comparing Data Sources

  1. 19.1 Data is the foundation; pick it deliberately
  2. 19.2 The dimensions that matter
  3. 19.3 Free sources and their limits
  4. 19.4 Paid retail vendors
  5. 19.5 When free data is good enough
  6. 19.6 Auditing a new source

src/data/audit_source.py

  1. 19.7 Storing and versioning data
  2. Key Takeaways
  3. Action Steps

20. Rolling Analytics and Benchmarking

  1. 20.1 A single Sharpe hides more than it reveals
  2. 20.2 Rolling Sharpe

src/metrics/rolling.py

  1. 20.3 Calendar-year returns
  2. 20.4 Benchmark-relative returns
  3. 20.5 Rolling correlation to benchmark
  4. 20.6 Monthly heatmap
  5. 20.7 Drawdown underwater curve
  6. Key Takeaways
  7. Action Steps

21. Regime Analysis: When Does Your Strategy Actually Work?

  1. 21.1 Every strategy lives in a regime
  2. 21.2 Defining a simple regime label

src/regime/labels.py

  1. 21.3 Segmenting returns by regime
  2. 21.4 Why this changes risk management
  3. 21.5 Stress periods
  4. 21.6 Drift over time
  5. Key Takeaways
  6. Action Steps

22. A 30-Day Learning Plan

  1. 22.1 How to use this chapter
  2. 22.2 Week one: environment and data
  3. 22.3 Week two: signal and simulation
  4. 22.4 Week three: validation and bias
  5. 12.4 Sortino ratio
  6. 12.5 Maximum drawdown
  7. 12.6 Win rate, profit factor, and turnover
  8. 12.7 Putting a report together
  9. Key Takeaways
  10. Action Steps

13. Capstone: A Complete Rule-Based System Walkthrough

  1. 13.1 What this chapter is and is not
  2. 13.2 The hypothesis, restated
  3. 13.3 The full pipeline code

src/capstone/system.py

Mark to market.

Exit on trailing stop.

Enter on signal, sized by volatility risk.

  1. 13.4 Reading the report
  2. 13.5 What to vary and what to hold constant
  3. 13.6 Common failure modes you will see
  4. 13.7 When to stop developing
  5. Key Takeaways
  6. Action Steps

14. From Research to Production: A Roadmap

  1. 14.1 The research-to-production gap
  2. 14.2 Stage 1: Research validation
  3. 14.3 Stage 2: Paper trading
  4. 14.4 Stage 3: Small live deployment
  5. 14.5 Stage 4: Monitoring and alerting

src/prod/monitor.py

  1. 14.6 Stage 5: Iteration without contamination
  2. 14.7 Tax and record keeping
  3. 14.8 Knowing when to walk away
  4. Key Takeaways
  5. Action Steps

15. Worked Example: Step-by-Step on Synthetic Data

  1. 15.1 Why a synthetic example
  2. 15.2 The input series
  3. 15.3 Compute the SMA by hand

Expected: [nan, nan, 101.17, 101.77, 102.43, 103.27, 105.0, 106.0, 107.17, 108.17]

  1. 15.4 Compute ATR
  2. 15.5 Walk the position
  3. 15.6 Reconstruct a real trade from your log
  4. 15.7 The danger of a single example
  5. Key Takeaways
  6. Action Steps

16. A Catalog of Common Code Mistakes

  1. 16.1 Why this chapter exists
  2. 16.2 The missing shift
  3. 16.3 Partial-window averages
  4. 16.4 Adjustment factor leak
  5. 16.5 NaN propagation
  6. 16.6 Off-by-one in the time stop
  7. 16.7 Inconsistent cost scaling
  8. 16.8 Survivorship in a quick universe
  9. 16.9 Tuning on the test set
  10. 16.10 Equity curve compounding error

src/audit/mistakes.py

  1. 16.11 The “too clean” equity curve
  2. 16.12 A self-audit script
  3. Key Takeaways
  4. Action Steps

17. From One Instrument to a Small Portfolio

  1. 17.1 Why one instrument is not enough
  2. 17.2 The weights matrix

src/portfolio/weights.py

  1. 17.3 Volatility-targeted weights
  2. 17.4 Portfolio-level returns

Lag weights by one period to avoid look-ahead.

  1. 17.5 Correlation and the hidden risk
  2. 17.6 The cash buffer
  3. 17.7 Rebalancing rules
  4. 17.8 When to stop adding instruments
  5. Key Takeaways
  6. Action Steps

18. A Repeatable Research SOP and Final Checklists

  1. 18.1 Why an SOP
  2. 18.2 Stage zero: hypothesis
  3. 18.3 Stage one: data
  4. 18.4 Stage two: signal
  5. 18.5 Stage three: simulation
  6. 18.6 Stage four: validation
  7. 18.7 Stage five: paper trading
  8. 18.8 Stage six: small live deployment
  9. 18.9 The pre-production checklist
  10. 18.10 The anti-patterns list
  11. 18.11 Closing the loop
  12. Key Takeaways
  13. Action Steps

19. Choosing and Comparing Data Sources

  1. 19.1 Data is the foundation; pick it deliberately
  2. 19.2 The dimensions that matter
  3. 19.3 Free sources and their limits
  4. 19.4 Paid retail vendors
  5. 19.5 When free data is good enough
  6. 19.6 Auditing a new source

src/data/audit_source.py

  1. 19.7 Storing and versioning data
  2. Key Takeaways
  3. Action Steps

20. Rolling Analytics and Benchmarking

  1. 20.1 A single Sharpe hides more than it reveals
  2. 20.2 Rolling Sharpe

src/metrics/rolling.py

  1. 20.3 Calendar-year returns
  2. 20.4 Benchmark-relative returns
  3. 20.5 Rolling correlation to benchmark
  4. 20.6 Monthly heatmap
  5. 20.7 Drawdown underwater curve
  6. Key Takeaways
  7. Action Steps

21. Regime Analysis: When Does Your Strategy Actually Work?

  1. 21.1 Every strategy lives in a regime
  2. 21.2 Defining a simple regime label

src/regime/labels.py

  1. 21.3 Segmenting returns by regime
  2. 21.4 Why this changes risk management
  3. 21.5 Stress periods
  4. 21.6 Drift over time
  5. Key Takeaways
  6. Action Steps

22. A 30-Day Learning Plan

  1. 22.1 How to use this chapter
  2. 22.2 Week one: environment and data
  3. 22.3 Week two: signal and simulation
  4. 22.4 Week three: validation and bias
  5. 22.5 Week four: paper trading and deployment
  6. 22.6 The mindset
  7. Key Takeaways
  8. Action Steps

23. Where Strategy Ideas Actually Come From

  1. 23.1 Ideas are cheap; validation is the skill
  2. 23.2 Trend following
  3. 23.3 Mean reversion
  4. 23.4 Breakouts
  5. 23.5 Volatility-based
  6. 23.6 Cross-sectional / relative value
  7. 23.7 The honest assessment
  8. 23.8 How to generate your own ideas
  9. Key Takeaways
  10. Action Steps

24. Maintaining and Iterating a Live Systematic Book

  1. 24.1 A live system is not a finished product
  2. 24.2 The monitoring dashboard

src/prod/dashboard.py

  1. 24.3 What to review weekly
  2. 24.4 When to investigate versus when to wait
  3. 24.5 Iterating without contaminating the live book

src/prod/versioning.py

Promotion rule: paper version must beat live on rolling 60-day net returns

for 20 consecutive trading days before promotion.

  1. 24.6 The retirement decision
  2. 24.7 Keeping the books for tax and audit
  3. 24.8 The sustainable pace
  4. Key Takeaways
  5. Action Steps

25. Statistical Significance for Traders

  1. 25.1 Why a good backtest is not enough
  2. 25.2 The mean and its standard error

src/stats/significance.py

  1. 25.3 Confidence interval on the mean
  2. 25.4 The multiple-testing problem revisited
  3. 25.5 Effect size matters more than p-values
  4. 25.6 Bootstrap for non-normal returns
  5. 25.7 Practical rule of thumb
  6. Key Takeaways
  7. Action Steps

Closing Notes

  1. The method is the product
  2. What to expect
  3. Where to go next
  4. Final disclaimer

Frequently Asked Questions

  1. Do I need a powerful computer to run these strategies?
  2. Which programming language should I use?
  3. How much capital do I need to start?
  4. Can I use these strategies on crypto, futures, or forex?
  5. Why does my backtest show great results but paper trading loses?
  6. How many parameters should my strategy have?
  7. How long should I backtest for?
  8. Should I optimize parameters on the whole dataset?
  9. What is the single most important cost to model?
  10. Is trend following or mean reversion better?
  11. Can I trade these strategies without monitoring?
  12. Why does this book not tell me which stock to buy?
  13. What if I find a strategy with a Sharpe ratio of 3 in backtest?
  14. How do I handle dividends in adjusted data?
  15. When should I stop researching and start trading?
  16. Is algorithmic trading legal for retail traders?
  17. Should I use a backtesting framework instead of building my own?
  18. How do I handle dividends in my backtest?
  19. What if my strategy works in backtest but I cannot stick with it live?
  20. How many instruments should I trade to diversify?
  21. Can I combine trend and mean-reversion signals in one system?
  22. What data frequency do I need?
  23. How do I know if my costs are realistic?
  24. What if I cannot get survivorship-free data?
  25. How often should I review a live strategy?
  26. What is the difference between a backtest and a simulation?
  27. Why does my backtest look great but paper trading is worse?
  28. Can I use leverage in a systematic backtest?
  29. How do I handle corporate actions like spin-offs?
  30. What if two of my signals disagree on the same instrument?
  31. Should I optimize more parameters to improve results?
  32. How do I know if my edge has decayed?
  33. What tax records should I keep?
  34. Is it okay to run several uncorrelated strategies at once?
  35. How long should I paper trade before going live?
  36. What if my broker does not offer programmatic orders?

Glossary

About the Author

FREE RESOURCES

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub