For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

Question on Article: Interactive Brokers in Python with backtrader



  • Hi Folks,

    Greetings from Asia.

    This is my first post, so kindly be patient with me if I have asked anything that seems simple.

    I have been using backtrader for backtesting and am moving on to use it for Paper Trading and have read this article written by Daniel, https://medium.com/@danjrod/interactive-brokers-in-python-with-backtrader-23dea376b2fc

    The following code is from the above mentioned article and I have made the following changes

    1. used GBPUSD instead of TWTR
    2. imported pytz to synchronize the timezone to US/Eastern

    My questions are

    1. Why are the Live Resampled Data and the Delayed Resampled Data for the same code and same instrument (GBPUSD) showing different values for the same timings?
    1. I also notice that the OHLC values of the Live Data 5 minutes resampled bar are available almost at the start of the bar formation, eg the closing price of 1.300370 for Live Bar 08:45:00 was available at approximately 08:45:00, whereas the OHLC values of the Delayed Data 5 minutes resampled bar follow IB charting convention, eg closing price of 1.299480 for Delayed Bar 08:45:00 shows the closing price of the bar for the period 08:45:00 to 08:49:99. Shouldn't both live data and delayed data be using the same convention?
    1. What should I do to make the OHLC values of the Live Data resampled bar be available only at the end of the bar formation so that the Live Resampled Data and Delayed Resampled Data for the same instrument show the same values for the same timings?

    02af8152-469c-4fd6-acad-6bd4b8f88aa5-image.png

    6c1d0129-3e02-47e2-9d2c-0b606341a8ef-image.png cb02bcca-7d2d-4ef1-b198-51ed44a3f79f-image.png

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    import backtrader as bt
    import pytz
    class St(bt.Strategy):
        def logdata(self):
            txt = []
            txt.append('{}'.format(len(self)))
               
            txt.append('{}'.format(
                self.data.datetime.datetime(0).isoformat())
            )
            txt.append('{:.6f}'.format(self.data.open[0]))
            txt.append('{:.6f}'.format(self.data.high[0]))
            txt.append('{:.6f}'.format(self.data.low[0]))
            txt.append('{:.6f}'.format(self.data.close[0]))
            txt.append('{:.2f}'.format(self.data.volume[0]))
            print(','.join(txt))
            
        data_live = False
        
        def notify_data(self, data, status, *args, **kwargs):
            print('*' * 5, 'DATA NOTIF:', data._getstatusname(status),
                  *args)
            if status == data.LIVE:
                self.data_live = True
        def next(self):
            self.logdata()
            
    def run(args=None):
        cerebro = bt.Cerebro(stdstats=False)
        store = bt.stores.IBStore(port=7497)
        data = store.getdata(dataname='GBP.USD-CASH-IDEALPRO',
                             tz=pytz.timezone('US/Eastern'),      
                             timeframe=bt.TimeFrame.Ticks)    
        cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=5)       
        cerebro.addstrategy(St)
        cerebro.run()
    if __name__ == '__main__':
        run()
    

    I have also read the forum on related issues and have also tried to vary the values for parameters, rightedge and boundoff. However, I still cannot find the answers to the questions posted above.

    Thank you for reading my post and I hope someone can help me out here.



});