Open Family Office Datasets Tickers Strategies Portfolios Research Posts About Changelog

Python for Finance

Source

Citation. Yves Hilpisch (The Python Quants). Python for Finance: Analyze Big Financial Data. O'Reilly Media, first edition, December 2014. ISBN 978-1-491-94528-5. A mismatch: the issue names the second edition, Python for Finance: Mastering Data-Driven Finance (2018). The PDF that the maintainer supplied has that title in its file name, but its text is the first edition. So this page reviews the first edition. The second edition's chapters on vectorised backtests, Kelly and trading through a broker aren't in it.

Status. Not applicable. It teaches Python for finance. The repo already has its own versions of what the book builds, with the checks that the book leaves out. Its single backtest and its optimiser are examples of what the harness's checks exist to catch.

Read. Read on 2026-11-28 from a PDF that the maintainer supplied, 605 pages with a text layer. Three subagents read the text in full, one part each, and read the code closely where it concerns finance. I checked the backtest's code and the vega function against the text. The figures are images, and I didn't see them. I didn't run the code, which is Python 2.7 and uses data services that have since closed.

Summary

Claim. Python, with NumPy, pandas, SciPy and their kin, is a good language for finance, from data handling to the valuation of derivatives by simulation.

Method. Nineteen chapters in three parts:

Findings, as the source reports them.

Setting Number Value
S&P 500, 2000 to April 2014, 42- and 252-day moving averages, a threshold of 50 index points parameter sets tried one
The same result a chart only. The strategy "outperforms the market quite significantly", with no costs
A GBM stock, 30 days, 10,000 paths 99.9% VaR 20.175, against 71.833 with jumps
Daily returns, 2006 to 2014 kurtosis, S&P 500 and Yahoo 10.054 and 31.987
AAPL, MSFT, YHOO, DB and GLD, long only, historical means maximum-Sharpe weights 65.9%, 8.8%, 25.3%, 0%, 0%
The same Sharpe ratio at a zero rate 1.06
American put by least squares Monte Carlo, 50,000 paths value 4.470627, against 4.478 in the original paper
VSTOXX October calls, 31 March 2014, nine strikes calibration error, mean squared 0.[removed]

Where the claims go beyond the evidence

The backtest lags its signal, but chooses and judges on the same data. The book lags the signal correctly: "the position is built 'yesterday' and yields 'today's' returns". But it sets the averages and the threshold of 50 points on the 2000–2014 data that it then reports, with no hold-out. It charges no costs, because the rule doesn't "trade 'too often'". It earns no rate on cash, pays nothing to borrow for the short side, and measures buy-and-hold without dividends. It reports no return, Sharpe ratio or drawdown. Its gain comes from two bear markets, 2000–2003 and 2008–2009.

A threshold in index points drifts. Fifty points was about 3.4% of the S&P 500 at the start of 2000, and about 2.7% in April 2014. So the same rule trades on a smaller relative gap as the index rises.

The optimiser takes historical means as forecasts. The book says that "historical mean performance is assumed to be the best estimator for future (expected) performance". The weights put two thirds in Apple, after a decade in which Apple rose fastest. There is no out-of-sample test and no comparison with 1/N.

VaR has no error, and its drift is risk-neutral. The book estimates VaR from 10,000 paths, but reports no standard error. It simulates a risk measure with the risk-free rate as the drift. Its "CVaR" is credit value-at-risk, not expected shortfall, which the book doesn't cover.

Errors in the text.

Out of date. Python 2.7, pd.rolling_mean, pd.ols, .ix and pandas.io.data are gone. So are the Yahoo and Google price services, the Netfonds tick data and the OANDA v1 sandbox.

Credit. The book lags its signal, warns that free data "sometimes does not fulfill professional requirements" and fills gaps forward to avoid "a 'foresight' issue". It says that least squares Monte Carlo gives a lower bound, and that GBM has "known shortcomings".

Use in this repo

The harness already does what the backtest leaves out. The harness charges each venue's costs (costs.py), compares with buy-and-hold, 1/N and inverse volatility, and counts every parameter set as a trial (trials.py). Its oracle checks that a strategy sees no bar after now (oracle.py). The book's crossover would go through as one trial with its threshold, on total returns with costs.

Thresholds should scale with the price. A rule with a fixed gap in points changes as the price changes. A strategy in the registry should state its thresholds as a share of price, or in units of volatility.

Expected shortfall is already reviewed. The repo measures tail loss by expected shortfall with its error (expected-shortfall.md, soundness check M10). The book's VaR by simulation adds no method, and it reports no error.

Mean–variance with historical means loses to 1/N. optimal-versus-naive-diversification.md reviews the evidence, and the PyPortfolioOpt and skfolio pages cover the optimisers with shrinkage. The book's example is the case those reviews warn about.

Option valuation by simulation needs its error. If the options work (#133) values a contract by Monte Carlo, it should report the standard error with each value. Its implied volatilities need a vega from the normal density.