For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See:

Plotting An Extended Data Feed on a different Timeframe?

  • Hello All,
    Just wondering if anyone has experience with extending a datafeed and plotting the newly extended lines on a subplot (like an indicator)

    I have some COT data coming in on the weekly and would like to plot the commercial / noncommmercial lines under the daily price chart.

    I have played around trying to add an indicator that simply assigns the extended datafeed lines to the lines of the indicator. However, I am running into a couple of speed bumps...

    • Because my COT data has no OHLCV values, I think I need turn plotting of the data feed off using plotinfo.plot so I avoid getting the error: ValueError Axis limits cannot be NaN or Inf.
    • My guess (could be wrong) is that this now means any indicator assigned to that datafeed will not plot (because I turned plotting off - That would seem logical!).

    My current thinking is to merge the weekly COT data with some dummy OHLCV data or resampled daily data so there are some values to plot and create subplots from. However, it won't look so nice on the final chart and I am wondering if anyone can think of a better way?

    Or perhaps I am extending the data feed incorrectly? Here is my extended code. It works with a pandas dataframe downloaded directly from Quandl

    class COT_Feed(bt.feeds.PandasData):
        # Add a 'pe' line to the inherited ones from the base class
        lines = (
            'ncl',  # Noncommercial long
            'ncs',  # Noncommercial Short
            'cl',   # Commercial Long
            'cs',   # Commercial Short
        params = (
            ('open', -1),
            ('high', -1),
            ('low', -1),
            ('close', -1),
            ('volume', -1),
            ('openinterest', 0),
            ('ncl', 1),
            ('ncs', 2),
            ('cl', 4),
            ('cs', 5)

    The feed works in a simple strategy where I can access the values from the feed using d.ncl[0]... It is only the plotting part I am having some trouble with.

    Maybe I am over thinking this and there is a simple solution?

  • administrators

    No idea if your ncl, ncs, cl and cs values are close to each other, but you could always:

    • Create a 2nd data feed and assign the four components to open, high, low and close
    • And then hide the 1st data feed

    For indicators you can try changing the plotinfo.plotmaster to the new data feed made up of ncl and friends.

    Alternatively: create an Observer which inspects the data feed during each iteration and outputs the value of ncl and friends (like the Broker observer inspects the value of the portfolio). This will always be plotted.

  • @backtrader Awesome. Thank you for the suggestions. I will give them a try.

Log in to reply