Category Archives: investing

Backtrader Reference Info

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/1203/is-it-possible-to-retrieve-all-the-open-active-orders-from-interactive-broker

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

22 March Futures Commentary

Disclaimer- The following is presented for entertainment purposes only. Do not attempt to trade using this data! I recommend that you not trade futures at all. Among other things, you can lose more than your initial investment if you do so.

Today we had exit signals in crude oil, fed funds, and sugar. However, we had positions in none of these so no action is required in our hypothetical portfolio. We show duplicate exit signals because anytime exit signals conditions are met the system repeats the signal. Similarly, entry signals are repeated even if the position has already been initiated by the system. These also make for alternative entry places if the initial signal is missed.