FRENESDE

Custom observers

An observer records a value at each bar to follow the backtest: cash, portfolio value, trades. By default Cerebro adds three (Broker, Trades, BuySell): they draw the panels of cerebro.plot().
A custom observer inherits from bt.Observer, declares its lines and fills them in next(). It reaches the strategy through self._owner, for example self._owner.broker.getvalue().
Example: exposure = (value − cash) ÷ value × 100. With a value of 10,000 and 3,000 in cash, 70% of the capital is invested. An indicator could not compute it: it only sees prices.
You add it with cerebro.addobserver(Exposition). To remove the default observers, create bt.Cerebro(stdstats=False). The observer watches without acting: it never sends an order.

Examples

Key terms

Observer
Component that records a value at each bar during the backtest
stdstats
Cerebro option that adds the default observers
Exposure
Share of the portfolio value invested in the market
self._owner
The observer’s reference to the strategy it watches
Take the mission on Doji Academy

← Back to the index