class_docstr: Interactive Brokers Data Feed.
  
  Supports the following contract specifications in parameter ``dataname``:
  
        - TICKER  # Stock type and SMART exchange
        - TICKER-STK  # Stock and SMART exchange
        - TICKER-STK-EXCHANGE  # Stock
        - TICKER-STK-EXCHANGE-CURRENCY  # Stock
  
        - TICKER-CFD  # CFD and SMART exchange
        - TICKER-CFD-EXCHANGE  # CFD
        - TICKER-CDF-EXCHANGE-CURRENCY  # Stock
  
        - TICKER-IND-EXCHANGE  # Index
        - TICKER-IND-EXCHANGE-CURRENCY  # Index
  
        - TICKER-YYYYMM-EXCHANGE  # Future
        - TICKER-YYYYMM-EXCHANGE-CURRENCY  # Future
        - TICKER-YYYYMM-EXCHANGE-CURRENCY-MULT  # Future
        - TICKER-FUT-EXCHANGE-CURRENCY-YYYYMM-MULT # Future
  
        - TICKER-YYYYMM-EXCHANGE-CURRENCY-STRIKE-RIGHT  # FOP
        - TICKER-YYYYMM-EXCHANGE-CURRENCY-STRIKE-RIGHT-MULT  # FOP
        - TICKER-FOP-EXCHANGE-CURRENCY-YYYYMM-STRIKE-RIGHT # FOP
        - TICKER-FOP-EXCHANGE-CURRENCY-YYYYMM-STRIKE-RIGHT-MULT # FOP
  
        - CUR1.CUR2-CASH-IDEALPRO  # Forex
  
        - TICKER-YYYYMMDD-EXCHANGE-CURRENCY-STRIKE-RIGHT  # OPT
        - TICKER-YYYYMMDD-EXCHANGE-CURRENCY-STRIKE-RIGHT-MULT  # OPT
        - TICKER-OPT-EXCHANGE-CURRENCY-YYYYMMDD-STRIKE-RIGHT # OPT
        - TICKER-OPT-EXCHANGE-CURRENCY-YYYYMMDD-STRIKE-RIGHT-MULT # OPT
  
  Params:
  
    - ``sectype`` (default: ``STK``)
  
      Default value to apply as *security type* if not provided in the
      ``dataname`` specification
  
    - ``exchange`` (default: ``SMART``)
  
      Default value to apply as *exchange* if not provided in the
      ``dataname`` specification
  
    - ``currency`` (default: ``''``)
  
      Default value to apply as *currency* if not provided in the
      ``dataname`` specification
  
    - ``historical`` (default: ``False``)
  
      If set to ``True`` the data feed will stop after doing the first
      download of data.
  
      The standard data feed parameters ``fromdate`` and ``todate`` will be
      used as reference.
  
      The data feed will make multiple requests if the requested duration is
      larger than the one allowed by IB given the timeframe/compression
      chosen for the data.
  
    - ``what`` (default: ``None``)
  
      If ``None`` the default for different assets types will be used for
      historical data requests:
  
        - 'BID' for CASH assets
        - 'TRADES' for any other
  
      Use 'ASK' for the Ask quote of cash assets
      
      Check the IB API docs if another value is wished
  
    - ``rtbar`` (default: ``False``)
  
      If ``True`` the ``5 Seconds Realtime bars`` provided by Interactive
      Brokers will be used as the smalles tick. According to the
      documentation they correspond to real-time values (once collated and
      curated by IB)
  
      If ``False`` then the ``RTVolume`` prices will be used, which are based
      on receiving ticks. In the case of ``CASH`` assets (like for example
      EUR.JPY) ``RTVolume`` will always be used and from it the ``bid`` price
      (industry de-facto standard with IB according to the literature
      scattered over the Internet)
  
      Even if set to ``True``, if the data is resampled/kept to a
      timeframe/compression below Seconds/5, no real time bars will be used,
      because IB doesn't serve them below that level
  
    - ``qcheck`` (default: ``0.5``)
  
      Time in seconds to wake up if no data is received to give a chance to
      resample/replay packets properly and pass notifications up the chain
  
    - ``backfill_start`` (default: ``True``)
  
      Perform backfilling at the start. The maximum possible historical data
      will be fetched in a single request.
  
    - ``backfill`` (default: ``True``)
  
      Perform backfilling after a disconnection/reconnection cycle. The gap
      duration will be used to download the smallest possible amount of data
  
    - ``backfill_from`` (default: ``None``)
  
      An additional data source can be passed to do an initial layer of
      backfilling. Once the data source is depleted and if requested,
      backfilling from IB will take place. This is ideally meant to backfill
      from already stored sources like a file on disk, but not limited to.
  
    - ``latethrough`` (default: ``False``)
  
      If the data source is resampled/replayed, some ticks may come in too
      late for the already delivered resampled/replayed bar. If this is
      ``True`` those ticks will bet let through in any case.
  
      Check the Resampler documentation to see who to take those ticks into
      account.
  
      This can happen especially if ``timeoffset`` is set to ``False``  in
      the ``IBStore`` instance and the TWS server time is not in sync with
      that of the local computer
  
    - ``tradename`` (default: ``None``)
      Useful for some specific cases like ``CFD`` in which prices are offered
      by one asset and trading happens in a different onel
  
      - SPY-STK-SMART-USD -> SP500 ETF (will be specified as ``dataname``)
  
      - SPY-CFD-SMART-USD -> which is the corresponding CFD which offers not
        price tracking but in this case will be the trading asset (specified
        as ``tradename``)
  
  The default values in the params are the to allow things like ```TICKER``,
  to which the parameter ``sectype`` (default: ``STK``) and ``exchange``
  (default: ``SMART``) are applied.
  
  Some assets like ``AAPL`` need full specification including ``currency``
  (default: '') whereas others like ``TWTR`` can be simply passed as it is.
  
    - ``AAPL-STK-SMART-USD`` would be the full specification for dataname
  
      Or else: ``IBData`` as ``IBData(dataname='AAPL', currency='USD')``
      which uses the default values (``STK`` and ``SMART``) and overrides
      the currency to be ``USD``
class_docstr_tok: ["Interactive","Brokers","Data","Feed",".","Supports","the","following","contract","specifications","in","parameter","``","dataname","``",":","-","TICKER","#","Stock","type","and","SMART","exchange","-","TICKER-STK","#","Stock","and","SMART","exchange","-","TICKER-STK-EXCHANGE","#","Stock","-","TICKER-STK-EXCHANGE-CURRENCY","#","Stock","-","TICKER-CFD","#","CFD","and","SMART","exchange","-","TICKER-CFD-EXCHANGE","#","CFD","-","TICKER-CDF-EXCHANGE-CURRENCY","#","Stock","-","TICKER-IND-EXCHANGE","#","Index","-","TICKER-IND-EXCHANGE-CURRENCY","#","Index","-","TICKER-YYYYMM-EXCHANGE","#","Future","-","TICKER-YYYYMM-EXCHANGE-CURRENCY","#","Future","-","TICKER-YYYYMM-EXCHANGE-CURRENCY-MULT","#","Future","-","TICKER-FUT-EXCHANGE-CURRENCY-YYYYMM-MULT","#","Future","-","TICKER-YYYYMM-EXCHANGE-CURRENCY-STRIKE-RIGHT","#","FOP","-","TICKER-YYYYMM-EXCHANGE-CURRENCY-STRIKE-RIGHT-MULT","#","FOP","-","TICKER-FOP-EXCHANGE-CURRENCY-YYYYMM-STRIKE-RIGHT","#","FOP","-","TICKER-FOP-EXCHANGE-CURRENCY-YYYYMM-STRIKE-RIGHT-MULT","#","FOP","-","CUR1.CUR2-CASH-IDEALPRO","#","Forex","-","TICKER-YYYYMMDD-EXCHANGE-CURRENCY-STRIKE-RIGHT","#","OPT","-","TICKER-YYYYMMDD-EXCHANGE-CURRENCY-STRIKE-RIGHT-MULT","#","OPT","-","TICKER-OPT-EXCHANGE-CURRENCY-YYYYMMDD-STRIKE-RIGHT","#","OPT","-","TICKER-OPT-EXCHANGE-CURRENCY-YYYYMMDD-STRIKE-RIGHT-MULT","#","OPT","Params",":","-","``","sectype","``","(","default",":","``","STK","``",")","Default","value","to","apply","as","*","security","type","*","if","not","provided","in","the","``","dataname","``","specification","-","``","exchange","``","(","default",":","``","SMART","``",")","Default","value","to","apply","as","*","exchange","*","if","not","provided","in","the","``","dataname","``","specification","-","``","currency","``","(","default",":","``","``","``",")","Default","value","to","apply","as","*","currency","*","if","not","provided","in","the","``","dataname","``","specification","-","``","historical","``","(","default",":","``","False","``",")","If","set","to","``","True","``","the","data","feed","will","stop","after","doing","the","first","download","of","data",".","The","standard","data","feed","parameters","``","fromdate","``","and","``","todate","``","will","be","used","as","reference",".","The","data","feed","will","make","multiple","requests","if","the","requested","duration","is","larger","than","the","one","allowed","by","IB","given","the","timeframe\/compression","chosen","for","the","data",".","-","``","what","``","(","default",":","``","None","``",")","If","``","None","``","the","default","for","different","assets","types","will","be","used","for","historical","data","requests",":","-","'BID","'","for","CASH","assets","-","'TRADES","'","for","any","other","Use","'ASK","'","for","the","Ask","quote","of","cash","assets","Check","the","IB","API","docs","if","another","value","is","wished","-","``","rtbar","``","(","default",":","``","False","``",")","If","``","True","``","the","``","5","Seconds","Realtime","bars","``","provided","by","Interactive","Brokers","will","be","used","as","the","smalles","tick",".","According","to","the","documentation","they","correspond","to","real-time","values","(","once","collated","and","curated","by","IB",")","If","``","False","``","then","the","``","RTVolume","``","prices","will","be","used",",","which","are","based","on","receiving","ticks",".","In","the","case","of","``","CASH","``","assets","(","like","for","example","EUR.JPY",")","``","RTVolume","``","will","always","be","used","and","from","it","the","``","bid","``","price","(","industry","de-facto","standard","with","IB","according","to","the","literature","scattered","over","the","Internet",")","Even","if","set","to","``","True","``",",","if","the","data","is","resampled\/kept","to","a","timeframe\/compression","below","Seconds\/5",",","no","real","time","bars","will","be","used",",","because","IB","does","n't","serve","them","below","that","level","-","``","qcheck","``","(","default",":","``","0.5","``",")","Time","in","seconds","to","wake","up","if","no","data","is","received","to","give","a","chance","to","resample\/replay","packets","properly","and","pass","notifications","up","the","chain","-","``","backfill_start","``","(","default",":","``","True","``",")","Perform","backfilling","at","the","start",".","The","maximum","possible","historical","data","will","be","fetched","in","a","single","request",".","-","``","backfill","``","(","default",":","``","True","``",")","Perform","backfilling","after","a","disconnection\/reconnection","cycle",".","The","gap","duration","will","be","used","to","download","the","smallest","possible","amount","of","data","-","``","backfill_from","``","(","default",":","``","None","``",")","An","additional","data","source","can","be","passed","to","do","an","initial","layer","of","backfilling",".","Once","the","data","source","is","depleted","and","if","requested",",","backfilling","from","IB","will","take","place",".","This","is","ideally","meant","to","backfill","from","already","stored","sources","like","a","file","on","disk",",","but","not","limited","to",".","-","``","latethrough","``","(","default",":","``","False","``",")","If","the","data","source","is","resampled\/replayed",",","some","ticks","may","come","in","too","late","for","the","already","delivered","resampled\/replayed","bar",".","If","this","is","``","True","``","those","ticks","will","bet","let","through","in","any","case",".","Check","the","Resampler","documentation","to","see","who","to","take","those","ticks","into","account",".","This","can","happen","especially","if","``","timeoffset","``","is","set","to","``","False","``","in","the","``","IBStore","``","instance","and","the","TWS","server","time","is","not","in","sync","with","that","of","the","local","computer","-","``","tradename","``","(","default",":","``","None","``",")","Useful","for","some","specific","cases","like","``","CFD","``","in","which","prices","are","offered","by","one","asset","and","trading","happens","in","a","different","onel","-","SPY-STK-SMART-USD","-",">","SP500","ETF","(","will","be","specified","as","``","dataname","``",")","-","SPY-CFD-SMART-USD","-",">","which","is","the","corresponding","CFD","which","offers","not","price","tracking","but","in","this","case","will","be","the","trading","asset","(","specified","as","``","tradename","``",")","The","default","values","in","the","params","are","the","to","allow","things","like","``","`","TICKER","``",",","to","which","the","parameter","``","sectype","``","(","default",":","``","STK","``",")","and","``","exchange","``","(","default",":","``","SMART","``",")","are","applied",".","Some","assets","like","``","AAPL","``","need","full","specification","including","``","currency","``","(","default",":","``",")","whereas","others","like","``","TWTR","``","can","be","simply","passed","as","it","is",".","-","``","AAPL-STK-SMART-USD","``","would","be","the","full","specification","for","dataname","Or","else",":","``","IBData","``","as","``","IBData","(","dataname='AAPL","'",",","currency='USD","'",")","``","which","uses","the","default","values","(","``","STK","``","and","``","SMART","``",")","and","overrides","the","currency","to","be","``","USD","``"]
code: def _load_rtbar(self, rtbar, hist=False):
      # A complete 5 second bar made of real-time ticks is delivered and
      # contains open/high/low/close/volume prices
      # The historical data has the same data but with 'date' instead of
      # 'time' for datetime
      dt = date2num(rtbar.time if not hist else rtbar.date)
      if dt < self.lines.datetime[-1] and not self.p.latethrough:
          return False  # cannot deliver earlier than already delivered
  
      self.lines.datetime[0] = dt
      # Put the tick into the bar
      self.lines.open[0] = rtbar.open
      self.lines.high[0] = rtbar.high
      self.lines.low[0] = rtbar.low
      self.lines.close[0] = rtbar.close
      self.lines.volume[0] = rtbar.volume
      self.lines.openinterest[0] = 0
  
      return True
  
imports: from __future__ import absolute_import, division, print_function, unicode_literals
  import datetime
  import backtrader
  from backtrader.feed import DataBase
  from backtrader import TimeFrame, date2num, num2date
  from backtrader.utils.py3 import integer_types, queue, string_types, with_metaclass
  from backtrader.metabase import MetaParams
  from backtrader.stores import ibstore
label_desc: Use image node_id 14 for calling the IBData obj's underlying member method code with example usage: obj._load_rtbar(rtbar, hist) and returns: True, False