Backtrader Community

    • 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/

    Resampling and plotting multiple data: Value Error: x and y must have same first dimension

    General Code/Help
    2
    3
    5155
    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.
    • dimitar-petrov
      dimitar-petrov last edited by

      Hello,

      I am trying to work with multiple data with different sample rates but I am facing an issue with plotting.

      ~/develop/python/py-envs/py3env/lib/python3.6/site-packages/matplotlib/axes/_base.py in _xy_from_xy(self, x, y)
          217         y = _check_1d(y)
          218         if x.shape[0] != y.shape[0]:
      --> 219             raise ValueError("x and y must have same first dimension")
          220         if x.ndim > 2 or y.ndim > 2:
          221             raise ValueError("x and y can be no greater than 2-D")
      
      ValueError: x and y must have same first dimension
      
      

      Here is a minimal example:

      import backtrader as bt
      import sys
      import datetime as dt
      import backtrader.indicators as btind
      # %matplotlib inline
      
      datafile="/tmp/example.csv"
      
      class CompEma(bt.Strategy):
          params = (
              ('ema_period', 21),
              ('mult_period', 60),
          )
      
          def __init__(self):
              self.ema = btind.ExponentialMovingAverage(
                  self.data0,
                  period=self.p.ema_period * self.p.mult_period)
      
              self.ema = btind.ExponentialMovingAverage(
                  self.data1,
                  period=self.p.ema_period)
      
      
      if __name__ == '__main__':
      
          cerebro = bt.Cerebro(runonce=False)
          st_kwargs = dict()
          st_kwargs['ema_period'] = 21
          st_kwargs['mult_period'] = 60
          cerebro.addstrategy(CompEma, **st_kwargs)
      
          data0 = bt.feeds.GenericCSVData(dataname=datafile,
                        timeframe=bt.TimeFrame.Minutes,
                        compression=1,
                        sessionstart=dt.time(8,30),
                        sessionend=dt.time(15,15),
                        openinterest=-1)
      
          data1 = bt.feeds.GenericCSVData(dataname=datafile,
                        timeframe=bt.TimeFrame.Minutes,
                        compression=1,
                        sessionstart=dt.time(8,30),
                        sessionend=dt.time(15,15),
                        openinterest=-1)
      
          cerebro.adddata(data0)
          cerebro.resampledata(data1,
                               timeframe=bt.TimeFrame.Minutes,
                               compression=60)
      
          cerebro.addwriter(bt.WriterFile, csv=True)
          cerebro.run()
      
          cerebro.plot(style="candle")
      
      

      Contents of data file

       cat /tmp/example.csv 
      2004-01-12 08:31:00,1122.53,1123.18,1122.53,1123.18,0
      2004-01-12 08:32:00,1123.25,1123.65,1123.25,1123.65,0
      2004-01-12 08:33:00,1123.83,1123.92,1123.83,1123.86,0
      2004-01-12 08:34:00,1123.73,1123.99,1123.73,1123.87,0
      2004-01-12 08:35:00,1123.9,1123.9,1123.74,1123.74,0
      2004-01-12 08:36:00,1123.64,1123.7,1123.56,1123.7,0
      2004-01-12 08:37:00,1123.86,1123.9,1123.76,1123.8,0
      2004-01-12 08:38:00,1123.92,1124.07,1123.92,1124.07,0
      2004-01-12 08:39:00,1124.05,1124.74,1124.04,1124.74,0
      2004-01-12 08:40:00,1124.76,1124.94,1124.76,1124.89,0
      2004-01-12 08:41:00,1124.94,1125.1,1124.94,1125.1,0
      2004-01-12 08:42:00,1124.84,1124.84,1124.58,1124.58,0
      2004-01-12 08:43:00,1124.51,1124.51,1124.32,1124.32,0
      2004-01-12 08:44:00,1124.19,1124.19,1123.72,1123.72,0
      2004-01-12 08:45:00,1123.68,1123.74,1123.62,1123.62,0
      2004-01-12 08:46:00,1123.8,1123.87,1123.8,1123.84,0
      2004-01-12 08:47:00,1123.66,1123.66,1123.56,1123.56,0
      2004-01-12 08:48:00,1123.58,1123.58,1123.41,1123.41,0
      2004-01-12 08:49:00,1123.34,1123.34,1122.8,1122.8,0
      2004-01-12 08:50:00,1122.78,1122.78,1122.65,1122.65,0
      2004-01-12 08:51:00,1122.8,1122.8,1122.56,1122.56,0
      2004-01-12 08:52:00,1122.42,1122.42,1122.02,1122.02,0
      2004-01-12 08:53:00,1121.91,1121.91,1121.73,1121.73,0
      2004-01-12 08:54:00,1121.86,1121.87,1121.65,1121.72,0
      2004-01-12 08:55:00,1121.68,1122.13,1121.68,1121.92,0
      2004-01-12 08:56:00,1121.95,1122.13,1121.94,1122.12,0
      2004-01-12 08:57:00,1122.23,1122.42,1122.23,1122.42,0
      2004-01-12 08:58:00,1122.27,1122.27,1122.2,1122.2,0
      2004-01-12 08:59:00,1122.08,1122.15,1122.08,1122.15,0
      2004-01-12 09:00:00,1122.12,1122.38,1122.03,1122.35,0
      2004-01-12 09:01:00,1122.29,1122.29,1122.03,1122.03,0
      2004-01-12 09:02:00,1121.83,1121.83,1121.76,1121.76,0
      2004-01-12 09:03:00,1121.44,1121.93,1121.44,1121.93,0
      2004-01-12 09:04:00,1122.03,1122.03,1121.62,1121.62,0
      2004-01-12 09:05:00,1121.7,1121.7,1121.54,1121.54,0
      2004-01-12 09:06:00,1121.66,1121.86,1121.66,1121.77,0
      2004-01-12 09:07:00,1121.67,1121.67,1121.43,1121.43,0
      2004-01-12 09:08:00,1121.4,1121.4,1121.2,1121.2,0
      2004-01-12 09:09:00,1121.16,1121.16,1120.9,1120.9,0
      2004-01-12 09:10:00,1121.16,1121.2,1121.1,1121.2,0
      2004-01-12 09:11:00,1121.23,1121.23,1121.19,1121.19,0
      2004-01-12 09:12:00,1121.13,1121.14,1121.05,1121.05,0
      2004-01-12 09:13:00,1121.1,1121.38,1121.1,1121.38,0
      2004-01-12 09:14:00,1121.4,1121.44,1121.4,1121.44,0
      2004-01-12 09:15:00,1121.45,1121.73,1121.45,1121.73,0
      2004-01-12 09:16:00,1121.63,1121.76,1121.63,1121.76,0
      2004-01-12 09:17:00,1121.81,1122.03,1121.81,1122.01,0
      2004-01-12 09:18:00,1122.03,1122.03,1121.95,1121.95,0
      2004-01-12 09:19:00,1121.97,1122.45,1121.97,1122.45,0
      2004-01-12 09:20:00,1122.48,1122.61,1122.48,1122.61,0
      
      

      I have found couple of threads regarding issues with resampling and plotting and there was recommendation to initialize Cerebro with runonce=False, but that did not help in this particular case.

      BR,
      Dimitar

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

        This is probably done with the latest release, including your own PR and the additional fixes. Is it so?

        dimitar-petrov 1 Reply Last reply Reply Quote 0
        • dimitar-petrov
          dimitar-petrov @backtrader last edited by

          @backtrader, yes fixed

          1 Reply Last reply Reply Quote 0
          • 1 / 1
          • First post
            Last post
          Copyright © 2016, 2017, 2018, 2019, 2020, 2021 NodeBB Forums | Contributors