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
DELTA-X (fictional market) : The vertical line marks the current bar. With each passing bar, an Exposition observer adds a point: 0% before the purchase, around 95% once in position, back to 0% after the sale.
Nasdaq 2000–2002: what a DrawDown observer would have drawn : Between March 2000 and October 2002, the Nasdaq Composite index lost about 78% of its value. An observer such as bt.observers.DrawDown, applied to a portfolio permanently invested in that index, would have drawn a fall from the peak of that size: a figure to face squarely before choosing a strategy.
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