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

An issue with plotting in case of multiple datastreams



  • Hello all,

    I am new to backtrader and in my attempt to get familiar with the system, I ran into a strange issue, which might be down to a small oversight or some misconfiguration, but I am hoping to find some help here.

    I have prepared a small datafile

    Dates,Open,High,Low,Close,Volume,Openinterest
    2000-01-04,25.2,25.69,24.71,25.55,1,2
    2000-01-05,25.5,25.61,24.86,24.91,1,2
    2000-01-06,24.8,25.34,24.51,24.78,1,2
    2000-01-07,24.65,25,24.15,24.22,1,2
    2000-01-10,24.15,24.75,24.02,24.67,1,2
    2000-01-11,24.71,25.8,24.67,25.77,1,2
    2000-01-12,25.73,26.55,25.38,26.28,1,2
    2000-01-13,26.28,27.12,26.2,26.69,1,2
    2000-01-14,26.8,28.1,26.61,28.02,1,2
    2000-01-18,28.03,29,27.96,28.85,1,2
    2000-01-19,28.8,29.68,28.55,29.54,1,2
    2000-01-20,29.25,29.95,28.8,29.66,1,2
    2000-01-21,27.85,29.1,27.7,28.2,1,2
    2000-01-24,28.23,28.69,27.51,27.83,1,2
    2000-01-25,27.78,28.55,27.35,28.28,1,2
    2000-01-26,28.29,28.77,27.8,27.84,1,2
    2000-01-27,27.78,28.7,27.19,27.32,1,2
    2000-01-28,27.22,27.55,26.95,27.22,1,2
    2000-01-31,27.1,27.9,26.7,27.64,1,2
    2000-02-01,27.6,28.4,27.6,28.22,1,2
    2000-02-02,28.22,28.65,27.45,27.55,1,2
    2000-02-03,27.56,28.4,27.41,28.03,1,2
    2000-02-04,28.17,28.88,27.75,28.82,1,2
    2000-02-07,28.71,28.9,28.25,28.45,1,2
    2000-02-08,28.44,28.49,27.9,28.02,1,2
    2000-02-09,28,28.84,27.71,28.77,1,2
    

    and the python script I am running is:

    from __future__ import (absolute_import, division, print_function,
                            unicode_literals)
    import backtrader as bt
    class TestStrategy(bt.Strategy):
    
        def log(self, txt, dt=None):
            ''' Logging function for this strategy'''
            dt = dt or self.datas[0].datetime.date(0)
            print('%s, %s' % (dt.isoformat(), txt))
    
        def __init__(self):
            # Keep a reference to the "close" line in the data[0] dataseries
            self.datetime  = self.datas[0].datetime
            self.dataclose = self.datas[0].close
            self.dataopen  = self.datas[0].open
            self.datahigh  = self.datas[0].high
            self.datalow   = self.datas[0].low
    
        def next(self):
            self.log('Close, %.2f' % self.dataclose[0])
    
    if __name__ == '__main__':
        # Create a cerebro entity
        cerebro = bt.Cerebro()
    
        DataPath = 'C:\\dev\\datafolder\\'
        DataFile = 'SampleError.csv'
        data = bt.feeds.GenericCSVData(
            dataname = DataPath + DataFile,
            timeframe=bt.TimeFrame.Days,
            reverse  = False,
            dtformat ='%Y-%m-%d',
            datetime = 0,
            time = -1,
            open = 1,
            high = 2,
            low = 3,
            volume = 5,
            close = 4,
            openinterest = 6,
        )
        cerebro.adddata(data)
    
        data2 = cerebro.resampledata(data, timeframe=bt.TimeFrame.Weeks, compression=1 )
        #cerebro.adddata(data2)
    
        # Add a strategy
        cerebro.addstrategy(TestStrategy)
    
    
        print('Starting Portfolio Value: %.2f' % cerebro.broker.getvalue())
    
        thestrats = cerebro.run()  # (stdstats=False, runonce=False)
    
        cerebro.plot()  
    
    

    The output looks like this (and my questions below)

    Python 3.6.4 |Anaconda, Inc.| (default, Jan 16 2018, 10:22:32) [MSC v.1900 64 bit (AMD64)] on win32
    
    Backend Qt5Agg is interactive backend. Turning interactive mode on.
    Starting Portfolio Value: 10000.00
    2000-01-10, Close, 24.67
    2000-01-11, Close, 25.77
    2000-01-12, Close, 26.28
    2000-01-13, Close, 26.69
    2000-01-14, Close, 28.02
    2000-01-18, Close, 28.85
    2000-01-19, Close, 29.54
    2000-01-20, Close, 29.66
    2000-01-21, Close, 28.20
    2000-01-24, Close, 27.83
    2000-01-25, Close, 28.28
    2000-01-26, Close, 27.84
    2000-01-27, Close, 27.32
    2000-01-28, Close, 27.22
    2000-01-31, Close, 27.64
    2000-02-01, Close, 28.22
    2000-02-02, Close, 27.55
    2000-02-03, Close, 28.03
    2000-02-04, Close, 28.82
    2000-02-07, Close, 28.45
    2000-02-08, Close, 28.02
    2000-02-09, Close, 28.77
    2000-02-09, Close, 28.77
    Traceback (most recent call last):
      File "C:\ProgramData\Anaconda3\lib\site-packages\IPython\core\interactiveshell.py", line 2910, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-2-e33710618050>", line 58, in <module>
        cerebro.plot()
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\cerebro.py", line 991, in plot
        start=start, end=end, use=use)
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\plot\plot.py", line 189, in plot
        self.plotind(None, ptop, subinds=self.dplotsover[ptop])
      File "C:\ProgramData\Anaconda3\lib\site-packages\backtrader\plot\plot.py", line 452, in plotind
        plottedline = pltmethod(xdata, lplotarray, **plotkwargs)
    
    

    So my questions are:

    • What is this warning about the interactive backend? And do I care or not care?
    • What am I doing wrong that I cannot have this resampled code as a second feed?

    I tried also to load another datafile and left a gap in the bars. Same kind of error. Have not tried on a different system (was planning to do this on a fresh PC)

    Hope to find some clarity.


  • administrators

    @crystalet said in An issue with plotting in case of multiple datastreams:

    What is this warning about the interactive backend? And do I care or not care?

    That's something in your configuration for matplotlib

    @crystalet said in An issue with plotting in case of multiple datastreams:

    What am I doing wrong that I cannot have this resampled code as a second feed?

    You need to tell us something more. Why cannot you? There is no indication of an error. Your snippet runs.

    The error you have is generated by interactiveshell.py from IPython and not by backtrader. You probably have to back to your configuration for matplotlib and disable Qt5Agg

    Or else you run in the shell and see how things work out of the box when not hijacked by wrapping environments.



  • That's something in your configuration for matplotlib

    ok i will have a look and report back on this one.

    You need to tell us something more. Why cannot you? There is no indication of an error. Your snippet runs.
    the code runs indeed, but the plot fails to be generated... again, will try the above and see.

    just wanted to stop by and say thank you already. Have not had time to look at it properly yet