Adding individual futures data with rolloverdata feed - Import #158


  • administrators

    From https://github.com/mementum/backtrader/issues/158 - for reference purposes

    Hi,

    The below is an excerpt of your sample code in the doc:

    cerebro = bt.Cerebro()
    data0 = bt.feeds.MyFeed(dataname='Expiry0')
    data1 = bt.feeds.MyFeed(dataname='Expiry1')
    ...
    dataN = bt.feeds.MyFeed(dataname='ExpiryN')
    
    drollover = cerebro.rolloverdata(data0, data1, ..., dataN, name='MyRoll', **kwargs)
    
    cerebro.run()
    

    Is there a way to add separate futures feed in a loop? Maybe something conceptually like below:

    cerebro = bt.Cerebro()
    for i in range(0, len(data_DB)):
        data_temp = bt.feeds.MyFeed(dataname=data_DB.ix[:,i])
        drollover = cerebro.rolloverdata(data_temp, name='MyRoll', **kwargs)
    cerebro.run()
    

    Thanks for your attention in advance. Please allow me to let you know I have been having great fun with your platform and I am greatly thankful for your work.


  • administrators

    I probably misunderstand your question and therefore my simple answer. This seems to be a matter of *args expansion.

    cerebro = bt.Cerebro()
    
    dtemp = [bt.feeds.MyFeed(dataname=data_DB.ix[:,i]) for i in range(0, len(data_DB))]
    drollover = cerebro.rolloverdata(*dtemp, name='MyRoll', **kwargs)
    cerebro.run()
    

    rolloverdata has actually no name argument. It will look into the **kwargs to allow the behavior from the small snippet.


  • administrators

    Thanks - will give a try.

    One slightly irrelevant question to this specific data feeding issue, please:
    Would an existing position be rolled automatically (i.e. sold/bought for a long position) once the futures time series is rolled to the next contract?


  • administrators

    Would an existing position be rolled automatically (i.e. sold/bought for a long position) once the futures time series is rolled to the next contract?

    That's an easy one: No. But not because of what you express in the question.

    The cerebro.rolloverdata creates a data feed named MyRoll (in the example) and that's the trading instrument for the internal broker and this instrument trades continuously without interruptions. The broker has no notion of expirations.

    There is a real data feed/broker (Visual Chart) for which there is support: The data feed can reference the continuous future, but takes an additional parameter to understand which is the current underlying asset to trade on.


  • administrators

    Understood - thanks for the swift response on this.

    What I am trying to achieve here is to simulate the pnl of the actual futures trading where I am likely to hold multiple futures positions over multiple rollovers (i.e. holding period of 3-6 months). It is not the crucial part of the pnl attribution but I am just seeking to get the realistic aspect of the algo to certain degree.

    What do you think it would be my best approach using backtrader? I don't have access to Visual Chart... if I understood correctly it's like a 3rd party broker like IB?


  • administrators

    VisualChart is a real data provider (with an associated broker mostly like TradeStation) with footprint in Europe. It was only meant as an example. It shouldn't apply to your case for simulation purposes.

    There are 2 keys for long-term futures that span over several expirations:

    • Commissions. Since there are no automatic roll-overs with the continuous feed, the commissions have to be accounted for ... manually. From a practical point of view this is for sure not a major

    • Price difference: In some instances the price difference between the current future and the next is HUGE. This is not the regular case, but the EuroStoxx50 (well traded) happens to have this sometimes (2%-3% difference)

      Your code sample seems to indicate that you use raw futures data and not backadjusted data, which already flattens/reduces the gap.

    If the price differences are small amongst the different assets, then the simulation shall be good. But if not, the results will not be accurate. Your system may even jump into buying/selling with the price difference spike.

    At the moment the RollOverData data feed offers no direct way of knowing when the expiration has happened. There is information on which is the currently expired future, but any order issued would be evaluated against the new data.

    Some thinking needs to go into this.


  • administrators

    Thanks for looking into this.

    Yes - tricky issue once one wants to backtest something over a long time span in the futures spaces. I am happy with my workaround for now where I use the average adjusted futures time series to generate signals and use roll-adjusted futures time series separately to enter/exit and calculate pnl.

    Like you mentioned, brokerage consideration is another factor but I generally know the average holding period of the trades and can assume 2-3 trips per trades manually.


Log in to reply
 

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