I found the following to be useful when working with the Backtrader testing/trading engine.
To get “self” variables from a strategy for inspection after the run is over:
...
strategies = cerebro.run()
...
strat = strategies[0]
# the strategy uses self.hypo_long_orders as a variable
strat.hypo_long_orders
To view orders from the system this approach should work:
# orders
orders = [str(order).splitlines() for order in strat._orders]
df = pd.DataFrame(orders)
df
#trades
trades = [str(trade).splitlines() for trade in list(strat._trades.values())[0][0]]
df1 = pd.DataFrame(trades)
df1
#raw data
# raw data
open = strat.data_open.array
high = strat.data_high.array
I adapted some of this from the following place in the backtrader community:
https://community.backtrader.com/topic/809/accessing-data-after-finished-run/6
With respect to using backtrader with IB the following appears to be useful:
https://community.backtrader.com/topic/1146/is-there-way-to-track-open-orders-with-ib-broker
https://medium.com/@danjrod/interactive-brokers-in-python-with-backtrader-23dea376b2fc
https://community.backtrader.com/user/richard-o-regan
https://pypi.org/project/ibapi/
https://medium.com/codex/using-python-to-send-telegram-messages-in-3-simple-steps-419a8b5e5e2