Skip to content

Ch 21: AI for Finance - Intermediate

Track: Advanced | Try code in Playground | Back to chapter overview

Read online or run locally

You can read this content here on the web. To run the code interactively, either use the Playground or clone the repo and open chapters/chapter-21-ai-for-finance/notebooks/02_strategy_and_backtest.ipynb in Jupyter.


Chapter 21: AI for Finance — Notebook 02 (Strategy & Backtest)

We turn a simple idea — buy when the fast average crosses above the slow — into a signal, then backtest it honestly with lagged execution and costs.

What you'll learn

Topic Section
Moving-average crossover signals §1
Lagging signals to avoid look-ahead §2
Equity curve and transaction costs §3

Time estimate: 3.5 hours


Key concepts

  • Crossover — fast MA above slow MA signals an uptrend (go long).
  • Look-ahead bias — using information you wouldn't have had in real time.
  • Lagging — act on bar t's signal at bar t+1, never the same bar.
  • Transaction costs — every position change costs money and erodes turnover-heavy strategies.

A crossover signal plus a lagged, cost-aware backtester gives an honest first estimate of a strategy. The lag is what keeps the result from being a fantasy.

Run the full notebook in the chapter folder for code and outputs.


Generated by Berta AI