Interactive brokers hang and plot
-
I have succesfully set up backtesting with Alpaca. Now I am trying to do the same with Interactive brokers and have 2 question:
-
I have 600 step period in my indicator. When using Alpaca, it waits for 600 periods (hours), but in Interactive Brokers it starts earlier than Alpaca. If I set from date to 1.7 it starts at 20.09.2020., but it shouldn't start at all since 600 periods is 600 / 8 hours = 75 days which is 75 /22 = 3 and half months. Does Interactive brokers have more trading days?
-
When backtest finishes it doesn't show the graph and it doesn't stop. Even if I click ctrl + C, it just 'hangs'.
Here is my cerebro part:
def run(args=None): # Create a cerebro entity cerebro = bt.Cerebro() # parse args args = parse_args(args) # Definetrade option """ You have 3 options: - backtest (IS_BACKTEST=True, IS_LIVE=False) - paper trade (IS_BACKTEST=False, IS_LIVE=False) - live trade (IS_BACKTEST=False, IS_LIVE=True) """ IS_BACKTEST = args.isbacktest IS_LIVE = args.islive symbol = 'SPY-STK-SMART-USD' # Add a strategy cerebro.addstrategy(TestStrategy) # IB store store=bt.stores.IBStore(host="127.0.0.1", port=7496, clientId=1) # Set data factory: Alpaca or pandas DataFactory = store.getdata # or use alpaca_backtrader_api.AlpacaData # Data feed if IS_BACKTEST: from_date = args.fromdate from_date = datetime.datetime.strptime(from_date, '%Y-%m-%d') timeframe = bt.TimeFrame.TFrame(args.timeframealpaca) compression = args.compression stockkwargs = dict( # timeframe=timeframe, rtbar=False, # use RealTime 5 seconds bars historical=True, # only historical download qcheck=0.5, # timeout in seconds (float) to check for events fromdate=from_date, # get data from.. # todate=datetime.datetime(2019, 9, 25), # get data from.. latethrough=False, # let late samples through tradename=None # use a different asset as order target ) data0 = store.getdata(dataname=symbol, **stockkwargs) cerebro.resampledata(data0, timeframe=timeframe, compression=compression) else: data0 = DataFactory(dataname=symbol, historical=False, timeframe=timeframe) # or just alpaca_backtrader_api.AlpacaBroker() broker = store.getbroker() cerebro.setbroker(broker) # Add dat to cerebro cerebro.adddata(data0) # set cash if backtest if IS_BACKTEST: # backtrader broker set initial simulated cash cerebro.broker.setcash(100000.0) # Set sizer cerebro.addsizer(bt.sizers.AllInSizer) # cerebro.broker.set_checksubmit(checksubmit=False) # Set our desired cash start cerebro.broker.setcash(10000.0) # 35305 / 10000 # (352 - 115) / 115 # Set sizer cerebro.addsizer(bt.sizers.AllInSizer) cerebro.broker.set_checksubmit(checksubmit=False) # Set the commission cerebro.broker.setcommission(commission=0.0) # check returns and banchmarks if args.timereturn: cerebro.addobserver(bt.observers.TimeReturn, timeframe=TIMEFRAMES[args.timeframe]) else: benchdata = data0 if args.benchdata1: data1 = DataFactory(dataname='SPY', historical=True, fromdate=from_date, timeframe=bt.TimeFrame.Minutes, compression=60) cerebro.adddata(data1, name='Data1') benchdata = data1 cerebro.addobserver(bt.observers.Benchmark, data=benchdata, timeframe=TIMEFRAMES[args.timeframe]) # Run over everything cerebro.run(maxcpus=8) # Plot cerebro.plot()
-
-
@MislavSag said in Interactive brokers hang and plot:
When backtest finishes it doesn't show the graph and it doesn't stop. Even if I click ctrl + C, it just 'hangs'
The same behavior was also observed and discussed in the following post:
https://community.backtrader.com/topic/2988/next-not-triggering-on-ibkr-data-feed/2 -
Unfortunately, setting
live=False, preload=False
didn't help. It still hangs in the end. Code in stop method and plot doesn't execute.