Suggestions on making use of real time futures data?



  • My strategy trades futures ETNs, instead of futures. However it makes use of the futures term structure to generate trading signals. The usage of the signal is rather simplistic, like greater or less than a threshold or so.

    For backtesting I simply added the signal as an extra field to the data feed. For live trading, I use IB, and I have subscriptions for the futures data from it. But I'm not sure how to add it to the strategy.

    Some options I can think of:

    • Fetch the future data outside of backtrader and generate the trading signals there, and then maybe save them in a Redis store, and let backtrader access it;
    • Override the IBData class to fetch futures data, do the calculation, and incorporate an extra field of data;
    • Use another completely different trading platform.

    Which way is the least overwhelming? What do you guys think? Any idea is appreciated.



  • This actually seems straightforward (unless something is not being understood from the post):

    • Data feed 1 is the trading target
    • Data feed 2 is the trading signal generator

    As such it would seem that the obvious approach is:

    • Add Data feed 1
    • Add Data feed 2
    • Create the needed indicators for trading on Data Feed 2
    • When the signals say so: buy/sell on Data Feed 1

    If the trading signals are pre-generated, the same would almos apply:

    1. Add Data feed 1
    2. Add Data feed 2
    3. Create a fake indicator that simply uses the input in Data feed 2 to trigger a signal
    4. When the signals say so: buy/sell on Data Feed 1

    You may of course fuse steps 2. and 3. if the signals aren't a continuous data stream and you can simply load them as an array directly into the indicator.



  • @Paska-Houso Thank you for your suggestion. Normally, it would work like you said.

    The difficulty is that the futures are not a single, perpetual data stream, but rather individual ones with different settlement dates (and different days starting with which the data is available). It's similar to this post. I can't add them to backtrader as-is.

    But since I trade only futures ETNs, which are perpetual contracts, I can get away with it, as long as I somehow manage to feed the data in.



  • Maybe a fourth option:

    • Add each and every individual futures data as a data feed.

    In which case there will be a great lot of them, and I don't know how the backfilling will play out with their different timings?



  • @yangnw I believe IB can give you continuous futures data. At least I can see it in TWS along with actual future contract data, not sure if you can access it via API.



  • @yangnw said in Suggestions on making use of real time futures data?:

    The difficulty is that the futures are not a single, perpetual data stream, but rather individual ones with different settlement dates (and different days starting with which the data is available). It's similar to this post. I can't add them to backtrader as-is.

    The rolloverdata mentioned in the post should then be the solution. There is even an entire chapter in the docs dedicated to it, giving you fine control over the switch over between futures.

    What's missing then?



  • @ab_trader @Paska-Houso

    Thanks, first!

    The futures term structure consists of the prices of multiple futures contracts at the same time, instead of designating any one of them like a continuous futures does.

    However, now that you reminded me, I can probably rewrite the ReplayData class to carry out the calculation there. Thank you for the pointers!

    I'll see how I can share the results when I'm done with it.



  • @ab_trader @Paska-Houso

    No, rolloverdata cannot be rewritten to access multiple contracts at the same time. The way it's implemented, the "new" contract simply skips expired data. The only option with backtrader is to add each contract as a separate data feed.

    this post:

    rolloverdata assume (naively) that the data feeds passed to them are already in order and nowhere else in the system. The latter being what would prevent buying from different targets.
    An approach you may consider:

    • add each contract as a separate data feed,
    • manually manage index of the active contract,
    • override _getminperstatus() to hack the minimum period requirement.

    Apparently those who want to trade continuous futures will also have to add contracts separately.

    It's shouldn't be too hard, I presume. Need to override _getminperstatus() to just respect the minimum period of the first data only.



  • @yangnw I confused a bit. Do you use multiple active contracts at the same time to generate the trade signals? In this case I think they all need to be loaded to backtrader for live trading.


Log in to reply
 

Looks like your connection to Backtrader Community was lost, please wait while we try to reconnect.