{"action":"created","issue":{"url":"https://api.github.com/repos/mementum/backtrader/issues/277","repository_url":"https://api.github.com/repos/mementum/backtrader","labels_url":"https://api.github.com/repos/mementum/backtrader/issues/277/labels{/name}","comments_url":"https://api.github.com/repos/mementum/backtrader/issues/277/comments","events_url":"https://api.github.com/repos/mementum/backtrader/issues/277/events","html_url":"https://github.com/mementum/backtrader/issues/277","id":212673447,"number":277,"title":"Plotting Indicator synchronization with resampled data","user":{"login":"dimitar-petrov","id":4025446,"avatar_url":"https://avatars1.githubusercontent.com/u/4025446?v=3","gravatar_id":"","url":"https://api.github.com/users/dimitar-petrov","html_url":"https://github.com/dimitar-petrov","followers_url":"https://api.github.com/users/dimitar-petrov/followers","following_url":"https://api.github.com/users/dimitar-petrov/following{/other_user}","gists_url":"https://api.github.com/users/dimitar-petrov/gists{/gist_id}","starred_url":"https://api.github.com/users/dimitar-petrov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimitar-petrov/subscriptions","organizations_url":"https://api.github.com/users/dimitar-petrov/orgs","repos_url":"https://api.github.com/users/dimitar-petrov/repos","events_url":"https://api.github.com/users/dimitar-petrov/events{/privacy}","received_events_url":"https://api.github.com/users/dimitar-petrov/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2017-03-08T08:52:29Z","updated_at":"2017-03-13T13:01:34Z","closed_at":null,"body":"Hello mementum,\r\n\r\nGreat work with the backtesting framework and congrats on the responsive community.\r\n\r\nThere is case where indicator plotting is breaking in matplotlib due to data resampling.\r\nThe first datetime in data.datetime.plot() is smaller than self.pinf.xreal[0] and off by one error is happening in the loop that is counting.\r\n\r\n```\r\nipdb> l\r\n    184             for data in strategy.datas:\r\n    185                 if not data.plotinfo.plot:\r\n    186                     continue\r\n    187 \r\n    188                 import ipdb; ipdb.set_trace()\r\n--> 189                 self.pinf.xdata = self.pinf.x\r\n    190                 if len(data) < self.pinf.xlen:\r\n    191                     self.pinf.xdata = xdata = []\r\n    192                     xreal = self.pinf.xreal\r\n    193                     dts = data.datetime.plot()\r\n    194                     for dt in (x for x in dts if dt0 <= x <= dt1):\r\n\r\nipdb> len(self.pinf.xdata)\r\n23004\r\nipdb> len(data)\r\n413\r\nipdb> c\r\n> /home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/backtrader/plot/plot.py(199)plot()\r\n    198                 import ipdb; ipdb.set_trace()\r\n--> 199                 for ind in self.dplotsup[data]:\r\n    200                     self.plotind(\r\n\r\nipdb> l\r\n    194                     for dt in (x for x in dts if dt0 <= x <= dt1):\r\n    195                         dtidx = bisect.bisect_left(xreal, dt)\r\n    196                         xdata.append(dtidx)\r\n    197 \r\n    198                 import ipdb; ipdb.set_trace()\r\n--> 199                 for ind in self.dplotsup[data]:\r\n    200                     self.plotind(\r\n    201                         data,\r\n    202                         ind,\r\n    203                         subinds=self.dplotsover[ind],\r\n    204                         upinds=self.dplotsup[ind],\r\n\r\nipdb> len(self.pinf.xdata)\r\n412\r\nipdb> data.num2date(dt0)\r\ndatetime.datetime(2003, 1, 2, 8, 32)\r\nipdb> data.num2date(dt1)\r\ndatetime.datetime(2003, 3, 27, 14, 59)\r\nipdb> data.num2date(dts[0])\r\ndatetime.datetime(2003, 1, 2, 9, 0)\r\nipdb> c\r\nn> /home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/backtrader/plot/plot.py(422)plotind()\r\n    421             import ipdb; ipdb.set_trace()\r\n--> 422             plottedline = pltmethod(self.pinf.xdata, lplot, **plotkwargs)\r\n    423             try:\r\n\r\nipdb> n\r\nValueError: x and y must have same first dimension\r\n> /home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/backtrader/plot/plot.py(422)plotind()\r\n    421             import ipdb; ipdb.set_trace()\r\n--> 422             plottedline = pltmethod(self.pinf.xdata, lplot, **plotkwargs)\r\n    423             try:\r\n\r\nipdb> len(self.pinf.xdata)\r\n412\r\nipdb> len(lplot)\r\n413\r\nipdb> \r\n```\r\nHopefully my explanation was understandable.\r\nI can set a minimal example if needed.\r\n\r\n**Stack Trace:**\r\n```\r\nTraceback (most recent call last):\r\n  File \"./stester.py\", line 1173, in <module>\r\n    btrun()\r\n  File \"./stester.py\", line 203, in btrun\r\n    cerebro.plot(**pkwargs)\r\n  File \"/home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/backtrader/cerebro.py\", line 659, in plot\r\n    start=start, end=end)\r\n  File \"/home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/backtrader/plot/plot.py\", line 207, in plot\r\n    self.plotdata(data, self.dplotsover[data])\r\n  File \"/home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/backtrader/plot/plot.py\", line 671, in plotdata\r\n    self.plotind(data, ind, subinds=self.dplotsover[ind], masterax=ax)\r\n  File \"/home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/backtrader/plot/plot.py\", line 422, in plotind\r\n    plottedline = pltmethod(self.pinf.xdata, lplot, **plotkwargs)\r\n  File \"/home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/matplotlib/__init__.py\", line 1819, in inner\r\n    return func(ax, *args, **kwargs)\r\n  File \"/home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/matplotlib/axes/_axes.py\", line 1382, in plot\r\n    for line in self._get_lines(*args, **kwargs):\r\n  File \"/home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/matplotlib/axes/_base.py\", line 381, in _grab_next_args\r\n    for seg in self._plot_args(remaining, kwargs):\r\n  File \"/home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/matplotlib/axes/_base.py\", line 359, in _plot_args\r\n    x, y = self._xy_from_xy(x, y)\r\n  File \"/home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/matplotlib/axes/_base.py\", line 219, in _xy_from_xy\r\n    raise ValueError(\"x and y must have same first dimension\")\r\nValueError: x and y must have same first dimension\r\n\r\n```"},"comment":{"url":"https://api.github.com/repos/mementum/backtrader/issues/comments/286100341","html_url":"https://github.com/mementum/backtrader/issues/277#issuecomment-286100341","issue_url":"https://api.github.com/repos/mementum/backtrader/issues/277","id":286100341,"user":{"login":"dimitar-petrov","id":4025446,"avatar_url":"https://avatars1.githubusercontent.com/u/4025446?v=3","gravatar_id":"","url":"https://api.github.com/users/dimitar-petrov","html_url":"https://github.com/dimitar-petrov","followers_url":"https://api.github.com/users/dimitar-petrov/followers","following_url":"https://api.github.com/users/dimitar-petrov/following{/other_user}","gists_url":"https://api.github.com/users/dimitar-petrov/gists{/gist_id}","starred_url":"https://api.github.com/users/dimitar-petrov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimitar-petrov/subscriptions","organizations_url":"https://api.github.com/users/dimitar-petrov/orgs","repos_url":"https://api.github.com/users/dimitar-petrov/repos","events_url":"https://api.github.com/users/dimitar-petrov/events{/privacy}","received_events_url":"https://api.github.com/users/dimitar-petrov/received_events","type":"User","site_admin":false},"created_at":"2017-03-13T13:01:34Z","updated_at":"2017-03-13T13:01:34Z","body":"Hello mementum,\r\n\r\nYes commit f3bba12026521121d9fe4ab61603cde8d4033f08 is correcting the plotting multiple data with different sample-rates.\r\n\r\nBut once we introduce numfigs parameter it is breaking again:\r\n\r\nHere is a debugging session:\r\n\r\n```\r\n> /home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/backtrader/plot/plot.py(212)plot()\r\n    211 \r\n--> 212                 for ind in self.dplotsup[data]:\r\n    213                     self.plotind(\r\n\r\nipdb> l 200\r\n    195 \r\n    196                 self.pinf.xdata = self.pinf.x\r\n    197                 if len(data) < self.pinf.xlen:\r\n    198                     self.pinf.xdata = xdata = []\r\n    199                     xreal = self.pinf.xreal\r\n    200                     dts = data.datetime.plot()\r\n    201                     for dt in (x for x in dts if dt0 <= x <= dt1):\r\n    202                         dtidx = bisect.bisect_left(xreal, dt)\r\n    203                         xdata.append(dtidx)\r\n    204 \r\n    205                     import ipdb; ipdb.set_trace()\r\n    206                     if False:\r\n\r\nipdb> b 434\r\nBreakpoint 1 at /home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/backtrader/plot/plot.py:434\r\nipdb> args\r\nself = <backtrader.plot.plot.Plot_OldSync object at 0x7f5472d738d0>\r\nstrategy = <9J1G81MNN3.JohnDoe_C object at 0x7f546ca1fba8>\r\nfigid = 0\r\nnumfigs = 3\r\niplot = True\r\nstart = 0\r\nend = 31587\r\nkwargs = {}\r\nipdb> len(strategy.data0)\r\n31587\r\nipdb> len(strategy.data1)\r\n567\r\nipdb> c\r\n> /home/dpetrov/develop/python/py-envs/py3env/lib/python3.6/site-packages/backtrader/plot/plot.py(434)plotind()\r\n    433             pltmethod = getattr(ax, lineplotinfo._get('_method', 'plot'))\r\n1-> 434             plottedline = pltmethod(self.pinf.xdata, lplot, **plotkwargs)\r\n    435             try:\r\n\r\nipdb> len(lplot)\r\n567\r\nipdb> len(self.pinf.xdata)\r\n189\r\nipdb> \r\n```"}}
{"actor":{"display_login":"dimitar-petrov"}}