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



  • 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!


  • administrators

    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


 

});