issue:integrate backtrader with IBKR for trading
-
when I start the programme data status is stuck at "DELEYED".
Anyone know why ?
import backtrader as bt class StockStrategy(bt.Strategy): def __init__(self): print("initializing strategy") self.data_ready = False self.highest = bt.ind.Highest(self.data.high, period=5) self.lowest = bt.ind.Lowest(self.data.low, period=5) def notify_data(self, data, status): print('Data Status =>', data._getstatusname(status)) if status == data.LIVE: self.data_ready = True def log_data(self): ohlcv = [] ohlcv.append(str(self.data.datetime.datetime())) ohlcv.append(str(self.data.open[0])) ohlcv.append(str(self.data.high[0])) ohlcv.append(str(self.data.low[0])) ohlcv.append(str(self.data.close[0])) ohlcv.append(str(self.data.volume[0])) print(",".join(ohlcv)) def next(self): self.log_data() if not self.data_ready: return print("current highest high", self.highest[0]) print("current lowest low", self.lowest[0]) print("previous highest high", self.highest[-1]) print("previous lowest low", self.lowest[-1]) previous_highest_high = self.highest[-1] if self.data.close[0] > (previous_highest_high - 0.50): print(f"closed at {self.data.close[0]}, which is above previous high of {previous_highest_high}, let's buy!") # uncomment this if you want to buy # self.buy_bracket(limitprice=self.data.close[0]+1.00, price=self.data.close[0], stopprice=self.data.close[0]-0.50) def start(): print("starting backtrader") cerebro = bt.Cerebro() store = bt.stores.IBStore(port=7497) data = store.getdata(dataname='AAPL', sectype='STK', exchange='ISLAND', timeframe=bt.TimeFrame.Minutes) cerebro.resampledata(data, timeframe=bt.TimeFrame.Minutes, compression=1) cerebro.broker = store.getbroker() cerebro.addstrategy(StockStrategy) cerebro.run() start()
-
I just made a through post for this
The answer is an update to IB API that doesn't accept GMT timezone for backfilling.
currently the only fix is to remove backfilling.
setting the data args
backfill_start=False, backfill=False
fixes the hanging problem but creates other problems if backfilling data is required for indicators -
@Lennie said in issue:integrate backtrader with IBKR for trading:
backfill_start=False, backfill=False
thank you.
Where should I pass backfill and backill_start ? -
@Lennie I made the changes, but it is not working
data = store.getdata(dataname='AAPL', sectype='STK', exchange='ISLAND', timeframe=bt.TimeFrame.Minutes,backfill_start = False,backfill=False)
-
double check your codes for IB instruments.
I see others' using long format id eg.
data = store.getdata(dataname='AAPL-STK-SMART-USD' timeframe=bt.TimeFrame.Minutes, compression=15, backfill_start=False, backfill=False)
You can check the API log files for error messages wrt stock symbols, etc
-
This post is deleted! -
@Lennie said in issue:integrate backtrader with IBKR for trading:
data = store.getdata(dataname='AAPL-STK-SMART-USD' timeframe=bt.TimeFrame.Minutes, compression=15, backfill_start=False, backfill=False)
Thank you but not working.
I guess backtrader no long integrates with IBKR -
@Loop-loop keep trying... working for me live using no backfilling.
only thing I see different than mine is I have
store = bt.stores.IBStore(port=7496,clientId=None)