How to use this page
Read it top to bottom once. The order is deliberate: returns feed volatility, volatility feeds sizing and risk, and the last two sections (backtesting and costs) decide whether any of it makes money live.
Every snippet is plain NumPy, SciPy, pandas, statsmodels, scikit-learn or arch, and every one was run before publishing. They are deliberately small: building blocks for your own bot, not a trading system.
The 50-formula one-sheet by hft.md (adapted from the MIT Sloan "Quant Bible") inspired this page. Here the math is written out as text, so you can search it, link to each formula and copy the code.
FAQ
What math do you need for algorithmic trading?
Statistics (means, variance, regression, hypothesis tests), linear algebra (covariance matrices), basic time series (stationarity, autocorrelation) and probability. Stochastic calculus only matters for options pricing, and order-book math only for high-frequency strategies.
Which formula matters most for a trading bot?
Position sizing and costs. A small edge with good sizing (volatility targeting, fractional Kelly) and realistic costs survives; a big backtest edge with no cost model usually does not.
Is a Sharpe ratio of 1 good?
For a live strategy after costs, an annual Sharpe around 1 is respectable and above 2 is rare. In a backtest it means little on its own: check the probabilistic and deflated Sharpe ratio, especially after trying many variants.
Why use log returns instead of simple returns?
Log returns add up over time, so multi-period returns and statistics are simpler. Simple returns add up across assets, so use them for portfolio P&L.
Is this financial advice?
No. It is educational material. The formulas are standard textbook versions and real markets break their assumptions.
Education only, not financial advice. Formulas are the textbook versions; real markets break their assumptions (fat tails, regime changes, costs). Test everything on paper before risking money.