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/

    Extract historical expired futures contract data from IB

    Indicators/Strategies/Analyzers
    2
    2
    701
    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.
    • L
      lch81592 last edited by lch81592

      Hello - I am trying to extract the historical 1 minute data for an expired futures contract from interactive broker using backtrader. In the contract specification, it seems there is no arguments to enter for "expired" contracts.

      For example, when i run

      # code block
          stockkwargs = dict(
              historical=True,
              fromdate=datetime.datetime(2018, 12, 1),  # get data from
          )
          data0 = store.getdata(dataname="HSI-FUT-HKFE-HKD-201901-50", timeframe=bt.TimeFrame.Minutes, **stockkwargs)
          cerebro.adddata(data0)
      
      

      I have no problem getting the spot HSI futures contract (i.e. the one expiring on 2019 Jan). However when I try to get the 2018 Nov expired HSI futures contract, I do not receive any values.

          stockkwargs = dict(
              historical=True,
              fromdate=datetime.datetime(2018, 11, 1),  # get data from
          )
          data0 = store.getdata(dataname="HSI-FUT-HKFE-HKD-201811-50", timeframe=bt.TimeFrame.Minutes, **stockkwargs)
          cerebro.adddata(data0)
      

      Based on my experience with getting data from IB, I believe there is an argument to specify that the contract is already expired - bool IncludeExpired. (see: https://interactivebrokers.github.io/tws-api/classIBApi_1_1Contract.html). However I do not see that in backtrader's setting.

      Can someone please advise? Many thanks!

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        That's not included.

        You could do something like this:

        • Subclass IBData

        • Override parsecontract

          • With a flag that you set in the dataname or by checking the date of the contract, set m_includedExpired to True for the contract that the store (via makecontract) returns
        • Replace DataCls in IBStore with your subclass

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