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

Last Bar of Delayed Data from Interactive Brokers

  • Re: Backtest vs live bars "off-by-1" discrepancy?


    For those of you who use Interactive Brokers, are you all seeing the last bar of delayed data being formed even before the time period is over?

    I am aware that rightedge = False for Delayed data and rightedge = True for Live Data.

    For example, using the below screenshot as an example
    At 08:05:00, Closing Price of 1.28926 is the closing price for the period 08:05:00 to 08:10:00
    At 08:10:00, Closing Price of 1.28935 is the closing price for the period 08:10:00 to 08:13:33 (the time when connection to IB server was established)
    At 08:15:00, Closing Price of 1.2891 is the closing price for the period 08:10:00 to 08:15:00

    If the last bar of delayed data is indeed formed before time period is over, aren't indicators, such as 2 periods SMA in the below screenshot being skewed by this "Last Bar of Delayed Data"?


    If you want to replicate the results, pls use the below code.

    import backtrader as bt
    import datetime as dt
    import pytz as pytz
    class Test_Strategy (bt.Strategy):
        params = (('maperiod',10),
        def __init__ (self):
            self.data_live = False
            self.sma = bt.indicators.SimpleMovingAverage (, period = self.params.maperiod)
        def next (self):
            self.log (str([0]) + '(Closing Price) ' + str(self.sma[0]) + '(2 periods SMA)')
        def notify_data(self, data, status):
            print('*' * 5, 'DATA NOTIF:', data._getstatusname(status))
            if status == data.LIVE:
                self.data_live = True
        def log (self, text):
            print (,": ", text)
    if __name__ == '__main__':
        ibstore = bt.stores.IBStore (host = '', clientId = 111, port = 7497)
        data = ibstore.getdata (dataname = 'GBP.USD-CASH-IDEALPRO',       #GBP.USD-CASH-IDEALPRO, BARC-STK-LSE-GBP
                                rtbar = False,
                                timeframe = bt.TimeFrame.Minutes,
                                compression = 1,
        cerebro = bt.Cerebro() = ibstore.getbroker()                                                          ###
        cerebro.resampledata (data, timeframe = bt.TimeFrame.Minutes, compression = 5)
        cerebro.addstrategy (Test_Strategy, maperiod = 2)

Log in to reply