Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Interactive brokers never finishes the script, even there is no error

    General Discussion
    1
    3
    106
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • MislavSag
      MislavSag last edited by

      I 0ma using IB data feed in my strategy. Strategy works till end, when it hangs and doesn't show anything. Before, when I was using one asset the strategy IB feed works, but know, with 2 asset, it doesn't. It doesn't return any error, just hangs and I have to stop the process in my command line.

      I had similar problem before: https://community.backtrader.com/topic/3021/interactive-brokers-hang-and-plot/3, but I solved it by removing cerebro.plot (I used quantstats instead).

      Here is the cerebro part of my code (I don't thing the problem is in strategy):

      
      def run(args=None):
      
          # parse args
          args = parse_args(args)
          
          # Create a cerebro entity
          cerebro = bt.Cerebro(stdstats=False, live=False, preload=False)
         
          # 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) 
          """
          broker = args.broker
          IS_BACKTEST = args.isbacktest
          IS_LIVE = args.islive
          if broker == 'alpaca':
              symbol = "SPY"
          elif broker == 'ib':
              symbol = "SPY-STK-SMART-USD"
      
          # Add a strategy
          cerebro.addstrategy(ExuberStrategy)
          
          # Data API store
          if broker == 'alpaca':
              store = alpaca_backtrader_api.AlpacaStore(
                  key_id=ALPACA_API_KEY,
                  secret_key=ALPACA_SECRET_KEY,
                  paper=IS_LIVE,
                  usePolygon=USE_POLYGON
              )
              DataFactory = store.getdata  # or use alpaca_backtrader_api.AlpacaData
          elif broker == 'ib':
              # IB store
              store=bt.stores.IBStore(host="127.0.0.1", port=7496, clientId=14)
      
          # Data feed args
          from_date = args.fromdate
          from_date = datetime.datetime.strptime(from_date, '%Y-%m-%d')
          to_date = args.todate
          to_date = datetime.datetime.strptime(to_date, '%Y-%m-%d')
          timeframe = bt.TimeFrame.TFrame(args.timeframedata)
          compression = args.compression
          compression_resamaple = args.cpresample
          timezone = pytz.timezone("America/New_York")
          
          # Data feed
          if IS_BACKTEST:
              stockkwargs = dict(
                  timeframe=bt.TimeFrame.Minutes,
                  historical=True,
                  fromdate=from_date,
                  todate=to_date,
                  compression=compression,
                  tz=timezone
              )
              if broker == 'alpaca':
                  data0 = DataFactory(dataname=symbol, **stockkwargs)
              elif broker == 'ib':
                  data0 = store.getdata(dataname=symbol, **stockkwargs)
                  data1 = store.getdata(dataname='VIX-IND-CBOE-USD', **stockkwargs)
                  cerebro.resampledata(data1, timeframe=bt.TimeFrame.Minutes, compression=compression_resamaple)
              cerebro.resampledata(data0, timeframe=bt.TimeFrame.Minutes, compression=compression_resamaple)
          else:
              stockkwargs = dict(
                  timeframe=bt.TimeFrame.Seconds,
                  historical=False,  # only historical download
                  qcheck=2.0,  # timeout in seconds (float) to check for events
                  # fromdate=from_date,  # get data from..
                  # todate=datetime.datetime(2020, 9, 22),  # 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_resamaple)
              # or just alpaca_backtrader_api.AlpacaBroker()
              if IS_LIVE:
                  broker = store.getbroker()
                  cerebro.setbroker(broker)
          
          # set cash if backtest
          if IS_BACKTEST:
              # backtrader broker set initial simulated cash
              cerebro.broker.setcash(50000.0)       
      
          # Set sizer
          cerebro.addsizer(bt.sizers.AllInSizer)
          
          # Set the commission
          cerebro.broker.setcommission(commission=0.01, leverage=2, commtype=bt.commissions.CommInfoBase.COMM_FIXED)    
          
          # Analyzer
          cerebro.addanalyzer(bt.analyzers.PyFolio, _name='PyFolio')
      
          # 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])
      
          # save data
          file_name = f'single_{symbol}_{from_date.strftime("%Y-%m-%d")}_{datetime.datetime.now().strftime("%Y-%m-%d-%H-%M-%S")}'
          # cerebro.addwriter(bt.WriterFile, csv=True, out='log_' + file_name + '.csv')
      
          # Run over everything
          cerebro.run()
          print('Krsj')
          #strat = results[0]
      
          # ---- Format the values from results ----
          #portfolio_stats = strat.analyzers.getbyname('PyFolio')
          #returns, positions, transactions, gross_lev = portfolio_stats.get_pf_items()
          #returns.index = returns.index.tz_convert(None)
          # ----------------------------------------
          
          # Benchmark
          # raw data
          # dtime = [bt.utils.date.num2date(x) for x in strat.data_datetime.array]
          # dtime = pd.to_datetime(dtime)
          # close = pd.Series(strat.data_close.array.tolist(), index=dtime)
          # close = close.resample('D').last().dropna()
          # # returns_benchmark = close.pct_change().dropna()
          # returns_bm = quantstats.utils.to_returns(close)    
          
          # Quantstats report
          #quantstats.reports.html(
          #   returns, benchmark='SPY',
          #  output='stats_' + file_name + '.html',
          # title='BackCUSUM Volatility strategy for ' + symbol)
      
      
      
      def parse_args(pargs=None):
          parser = argparse.ArgumentParser(
              formatter_class=argparse.ArgumentDefaultsHelpFormatter,
              description=(
                  'Backtrader exuber'
              )
          )
              
          # Defaults for dates
          parser.add_argument('--broker', 
                              default='ib',
                              choices=['ib', 'alpaca'],
                              required=True,
                              action='store',
                              help='Broker to use: alpaca or IB'                        
                              )
          
          parser.add_argument('--compression', default=1, type=int,
                              required=False, action='store',
                              help='Compression')
             
          parser.add_argument('--timeframedata', default='Minutes',
                              choices=bt.TimeFrame.Names,
                              required=False, action='store',
                              help='TimeFrame for Resample/Replay')
          
          parser.add_argument('--cpresample', default=5, type=int,
                              required=False, action='store',
                              help='Compression for Resample/Replay')
          
          parser.add_argument('--isbacktest',
                              required=False, action='store_true',
                              help='Do only historical download')
          
          parser.add_argument('--islive',
                              required=False, action='store_true',
                              help='Do only historical download')
      
          # parser.add_argument('--historical',
          #                     required=False, action='store_true',
          #                     help='Do only historical download')
              
          parser.add_argument('--fromdate', required=False, default='2020-05-01',
                              help='Date[time] in YYYY-MM-DD[THH:MM:SS] format')
          
          parser.add_argument('--todate', required=False, default='2020-11-30',
                              help='Date[time] in YYYY-MM-DD[THH:MM:SS] format')
          
          parser.add_argument('--benchdata1', required=False, action='store_true',
                              help=('Benchmark against data1'))
              
          parser.add_argument('--timereturn', required=False, action='store_true',
                              default=None,
                              help=('Use TimeReturn observer instead of Benchmark'))
      
          parser.add_argument('--timeframe', required=False, action='store',
                              default=None, choices=TIMEFRAMES.keys(),
                              help=('TimeFrame to apply to the Observer'))
          
          return parser.parse_args(pargs)
      
      
      if __name__ == '__main__':
          run()
      
      
      

      I commented many parts because I tried to figure out where is the problem, but I can't find it.

      1 Reply Last reply Reply Quote 0
      • MislavSag
        MislavSag last edited by

        The problem was in time zone. When I removed timezone from store it worked.

        1 Reply Last reply Reply Quote 0
        • MislavSag
          MislavSag last edited by

          I was wrong!!!

          The problem is not in timezone. I solved the problem by using oldsync=True in cerebro!

          But if I use this option, new problems arise I will explain in next topic.

          1 Reply Last reply Reply Quote 1
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors