backtrader.observers.trades module

Trades Observer Module - Trade tracking and PnL visualization.

This module provides the Trades observer for tracking completed trades and plotting their profit/loss.

Classes:

Trades: Observer that tracks trades and plots PnL.

Example

>>> cerebro = bt.Cerebro()
>>> cerebro.addobserver(bt.observers.Trades)
class backtrader.observers.trades.Trades[source]

Bases: Observer

This observer keeps track of full trades and plots the PnL level achieved when a trade is closed.

A trade is open when a position goes from 0 (or crossing over 0) to X and is then closed when it goes back to 0 (or crosses over 0 in the opposite direction)

Params:
  • pnlcomm (def: True)

    Show net/profit and loss, i.e.: after commission.If set to False, it will show the result of trades before commission

plotinfo = <backtrader.metabase.plotinfo_obj object>
plotlines = <backtrader.metabase.plotlines_obj object>
__init__(*args, **kwargs)

Wrapped __init__ that properly handles observer initialization

next()[source]

Update trade statistics based on closed trades.

Processes pending trades and updates PnL lines.

frompackages = ()
packages = ()
class backtrader.observers.trades.DataTrades[source]

Bases: Observer

DataTrades observer that has been refactored to remove metaclass usage and dynamic class creation. Uses fixed line definitions for common cases.

plotinfo = <backtrader.metabase.plotinfo_obj object>
plotlines = <backtrader.metabase.plotlines_obj object>
__init__(*args, **kwargs)

Wrapped __init__ that properly handles observer initialization

next()[source]

Update data-specific trade PnL values.

Records closed trade PnL for each data feed.

frompackages = ()
packages = ()