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

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:

    1. 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?

    2. 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
        # IB store
        store=bt.stores.IBStore(host="", 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)
            data0 = DataFactory(dataname=symbol,
            # or just alpaca_backtrader_api.AlpacaBroker()
            broker = store.getbroker()
        # Add dat to cerebro
        # set cash if backtest
        if IS_BACKTEST:
            # backtrader broker set initial simulated cash
        # Set sizer
        # Set our desired cash start
        # 35305 / 10000
        # (352 - 115) / 115
        # Set sizer
        # Set the commission
        # check returns and banchmarks
        if args.timereturn:
            benchdata = data0
            if args.benchdata1:
                data1 = DataFactory(dataname='SPY', historical=True,
                                    timeframe=bt.TimeFrame.Minutes, compression=60)
                cerebro.adddata(data1, name='Data1')
                benchdata = data1
        # Run over everything
        # 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:

  • Unfortunately, setting live=False, preload=False didn't help. It still hangs in the end. Code in stop method and plot doesn't execute.

Log in to reply