FRENESDE

Installing and launching Cerebro

Backtrader is an open-source Python backtesting library. You install it once with pip install backtrader, then every script starts with import backtrader as bt.
The conductor is called Cerebro: you create cerebro = bt.Cerebro(), give it data with adddata(), a strategy with addstrategy(), then launch cerebro.run().
During run(), Cerebro replays history bar after bar: at each bar your strategy only sees the past, just like in real conditions.
run() returns a list of the strategies that ran: strat = cerebro.run()[0] gives you your strategy after the test, for example to read its analyzers.

Examples

Key terms

Cerebro
Backtrader’s engine that brings together data, strategy and broker
Backtest
Simulation of a strategy on past data
pip
The tool that installs Python packages
run()
The method that launches the simulation
Take the mission on Doji Academy

← Back to the index