Navigation

    Backtrader Community

    • Register
    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    For code/output blocks: Use ``` (aka backtick or grave accent) in a single line before and after the block. See: http://commonmark.org/help/

    Plotting of resample data

    General Code/Help
    2
    3
    280
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • V
      vaclavku last edited by

      dear all, I try to plot the original and resampled data, however there's the following issue:

      Traceback (most recent call last):
        File "_initial.py", line 158, in <module>
          scan()
        File "_initial.py", line 120, in scan
          fcommission
        File "C:\Users\XXX\master.py", line 258, in runstrat
          cerebro.plot(style='candlestick')
        File "C:\Users\XXX\Local\Programs\Python\Python37\lib\site-packages\backtrader\cerebro.py", line 991, in plot
          start=start, end=end, use=use)
        File "C:\Users\XXX\Local\Programs\Python\Python37\lib\site-packages\backtrader\plot\plot.py", line 189, in plot
          self.plotind(None, ptop, subinds=self.dplotsover[ptop])
        File "C:\Users\XXX\Local\Programs\Python\Python37\lib\site-packages\backtrader\plot\plot.py", line 472, in plotind
          plottedline = pltmethod(xdata, lplotarray, **plotkwargs)
        File "C:\Users\XXX\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes\_axes.py", line 1666, in plot
          lines = [*self._get_lines(*args, data=data, **kwargs)]
        File "C:\Users\XXX\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes\_base.py", line 225, in __call__
          yield from self._plot_args(this, kwargs)
        File "C:\Users\XXX\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes\_base.py", line 391, in _plot_args
          x, y = self._xy_from_xy(x, y)
        File "C:\Users\XXX\Local\Programs\Python\Python37\lib\site-packages\matplotlib\axes\_base.py", line 270, in _xy_from_xy
          "have shapes {} and {}".format(x.shape, y.shape))
      ValueError: x and y must have same first dimension, but have shapes (690,) and (689,)
      

      The code of the resample is:

          namedf = tic
          df = pd.read_sql(namedf, conn)
      
          data0 = bt.feeds.PandasData(dataname=df, 
                                     timeframe=bt.TimeFrame.Minutes,
                                     compression=1)#, fromdate=start, todate=end)
          cerebro.adddata(data0)
      
          data1 = bt.feeds.PandasData(dataname=df, 
                                     timeframe=bt.TimeFrame.Minutes,
                                     compression=1)
          
          cerebro.resampledata(data1, timeframe = bt.TimeFrame.Minutes, compression = 60) 
      

      The resampling itself as well as plotting work well.

      Many thanks.

      1 Reply Last reply Reply Quote 0
      • B
        backtrader administrators last edited by

        Yes, that's something which happens on some scenario when mixing different timeframes. matplotlib is not forgiving at all when putting things on the same axis is something is slightly misaligned.

        @vaclavku said in Plotting of resample data:

            data0 = bt.feeds.PandasData(dataname=df, 
                                       timeframe=bt.TimeFrame.Minutes,
                                       compression=1)#, fromdate=start, todate=end)
            cerebro.adddata(data0)
        
            data1 = bt.feeds.PandasData(dataname=df, 
                                       timeframe=bt.TimeFrame.Minutes,
                                       compression=1)
            
            cerebro.resampledata(data1, timeframe = bt.TimeFrame.Minutes, compression = 60) 
        

        Although it won't cure the issue, you can actually do this

            data0 = bt.feeds.PandasData(dataname=df, 
                                       timeframe=bt.TimeFrame.Minutes,
                                       compression=1)#, fromdate=start, todate=end)
            cerebro.adddata(data0) 
            cerebro.resampledata(data0, timeframe = bt.TimeFrame.Minutes, compression = 60) 
        
        1 Reply Last reply Reply Quote 0
        • V
          vaclavku last edited by

          Many thanks for confirming.

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018 NodeBB Forums | Contributors
          $(document).ready(function () { app.coldLoad(); }); }