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 (self.data.close, period = self.params.maperiod)
def next (self):
self.log (str(self.data.close) + '(Closing Price) ' + str(self.sma) + '(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 (self.data.datetime.datetime(0),": ", text)
if __name__ == '__main__':
ibstore = bt.stores.IBStore (host = '127.0.0.1', 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()
cerebro.broker = ibstore.getbroker() ###
cerebro.resampledata (data, timeframe = bt.TimeFrame.Minutes, compression = 5)
cerebro.addstrategy (Test_Strategy, maperiod = 2)