{"action":"created","issue":{"url":"https://api.github.com/repos/verybadsoldier/backtrader_plotting/issues/11","repository_url":"https://api.github.com/repos/verybadsoldier/backtrader_plotting","labels_url":"https://api.github.com/repos/verybadsoldier/backtrader_plotting/issues/11/labels{/name}","comments_url":"https://api.github.com/repos/verybadsoldier/backtrader_plotting/issues/11/comments","events_url":"https://api.github.com/repos/verybadsoldier/backtrader_plotting/issues/11/events","html_url":"https://github.com/verybadsoldier/backtrader_plotting/issues/11","id":321008488,"number":11,"title":"UnboundLocalError: local variable 'height_set' referenced before assignment","user":{"login":"aaron8tang","id":136689,"avatar_url":"https://avatars1.githubusercontent.com/u/136689?v=4","gravatar_id":"","url":"https://api.github.com/users/aaron8tang","html_url":"https://github.com/aaron8tang","followers_url":"https://api.github.com/users/aaron8tang/followers","following_url":"https://api.github.com/users/aaron8tang/following{/other_user}","gists_url":"https://api.github.com/users/aaron8tang/gists{/gist_id}","starred_url":"https://api.github.com/users/aaron8tang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaron8tang/subscriptions","organizations_url":"https://api.github.com/users/aaron8tang/orgs","repos_url":"https://api.github.com/users/aaron8tang/repos","events_url":"https://api.github.com/users/aaron8tang/events{/privacy}","received_events_url":"https://api.github.com/users/aaron8tang/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":1,"created_at":"2018-05-08T01:04:58Z","updated_at":"2018-05-09T01:15:44Z","closed_at":null,"author_association":"NONE","body":"I'm working on the develop branch.Maybe height_set should be set a default value,such as 800.\r\n\r\nTraceback (most recent call last):\r\n  File \"crossover_strategy.py\", line 131, in <module>\r\n    cerebro.plot(b)\r\n  File \"D:\\devhome\\anaconda3\\lib\\site-packages\\backtrader\\cerebro.py\", line 991, in plot\r\n    start=start, end=end, use=use)\r\n  File \"backtrader_plotting\\backtrader_plotting\\bokeh\\bokeh.py\", line 144, in plot\r\n    self._plot_strategy(obj, start, end, **kwargs)\r\n  File \"backtrader_plotting\\backtrader_plotting\\bokeh\\bokeh.py\", line 187, in _plot_strategy\r\n    bf.plot(master, None)\r\n  File \"backtrader_plotting\\backtrader_plotting\\bokeh\\figure.py\", line 126, in plot\r\n    self.figure.plot_height = height_set\r\nUnboundLocalError: local variable 'height_set' referenced before assignment"},"comment":{"url":"https://api.github.com/repos/verybadsoldier/backtrader_plotting/issues/comments/387590035","html_url":"https://github.com/verybadsoldier/backtrader_plotting/issues/11#issuecomment-387590035","issue_url":"https://api.github.com/repos/verybadsoldier/backtrader_plotting/issues/11","id":387590035,"user":{"login":"aaron8tang","id":136689,"avatar_url":"https://avatars1.githubusercontent.com/u/136689?v=4","gravatar_id":"","url":"https://api.github.com/users/aaron8tang","html_url":"https://github.com/aaron8tang","followers_url":"https://api.github.com/users/aaron8tang/followers","following_url":"https://api.github.com/users/aaron8tang/following{/other_user}","gists_url":"https://api.github.com/users/aaron8tang/gists{/gist_id}","starred_url":"https://api.github.com/users/aaron8tang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/aaron8tang/subscriptions","organizations_url":"https://api.github.com/users/aaron8tang/orgs","repos_url":"https://api.github.com/users/aaron8tang/repos","events_url":"https://api.github.com/users/aaron8tang/events{/privacy}","received_events_url":"https://api.github.com/users/aaron8tang/received_events","type":"User","site_admin":false},"created_at":"2018-05-09T01:15:44Z","updated_at":"2018-05-09T01:15:44Z","author_association":"NONE","body":"Sorry..Just like this:\r\n\r\nimport datetime\r\nimport backtrader as bt\r\nfrom backtrader_plotting import Bokeh\r\nfrom backtrader_plotting.schemes import PlotMode\r\n\r\n\r\n# Create a Stratey\r\nclass CrossOver(bt.Strategy):\r\n\t\"\"\"A simple moving average crossover strategy,\r\n\tat SMA 50/200 a.k.a. the \"Golden Cross Strategy\"\r\n\t\"\"\"\r\n\tparams = (('fast', 5),\r\n\t\t\t  ('slow', 60),\r\n\t\t\t  ('order_pct', 0.95),\r\n\t\t\t  ('market', 'A/CNY')\r\n\t\t\t )\r\n\r\n\tdef __init__(self):\r\n\t\tsma = bt.indicators.SimpleMovingAverage\r\n\t\tcross = bt.indicators.CrossOver\r\n\t\tself.fastma = sma(self.data.close,\r\n\t\t\t\t\t\t  period=self.p.fast,\r\n\t\t\t\t\t\t  plotname='FastMA')\r\n\t\tsma = bt.indicators.SimpleMovingAverage\r\n\t\tself.slowma = sma(self.data.close,\r\n\t\t\t\t\t\t  period=self.p.slow,\r\n\t\t\t\t\t\t  plotname='SlowMA')\r\n\t\tself.crossover = cross(self.fastma, self.slowma)\r\n\r\n\tdef start(self):\r\n\t\tself.size = None\r\n\r\n\tdef log(self, txt, dt=None):\r\n\t\t\"\"\" Logging function for this strategy\r\n\t\t\"\"\"\r\n\t\tdt = dt or self.datas[0].datetime.date(0)\r\n\t\ttime = self.datas[0].datetime.time()\r\n\t\tprint('%s - %s, %s' % (dt.isoformat(), time, txt))\r\n\r\n\tdef next(self):\r\n\t\tif self.position.size == 0:\r\n\t\t\tif self.crossover > 0:\r\n\t\t\t\tamount_to_invest = (self.p.order_pct *\r\n\t\t\t\t\t\t\t\t\tself.broker.cash)\r\n\t\t\t\tself.size = round(amount_to_invest / self.data.close,0)\r\n\t\t\t\tmsg = \"*** MKT: {} BUY: {}\"\r\n\t\t\t\tself.log(msg.format(self.p.market, self.size))\r\n\t\t\t\tself.buy(size=self.size)\r\n\r\n\t\tif self.position.size > 0:\r\n\t\t\t# we have an open position or made it to the end of backtest\r\n\t\t\tlast_candle = (self.data.close.buflen() == len(self.data.close) + 1)\r\n\t\t\tif (self.crossover < 0) or last_candle:\r\n\t\t\t\tmsg = \"*** MKT: {} SELL: {}\"\r\n\t\t\t\tself.log(msg.format(self.p.market, self.size))\r\n\t\t\t\t#self.close()\r\n\t\t\t\tself.sell(size=self.size)\r\n\r\n\r\nif __name__ == '__main__':\r\n    cerebro = bt.Cerebro()\r\n\r\n    cerebro.addstrategy(CrossOver)\r\n\r\n    data = bt.feeds.YahooFinanceCSVData(\r\n        dataname=\"data/orcl-1995-2014.txt\",\r\n        # Do not pass values before this date\r\n        fromdate=datetime.datetime(2000, 1, 1),\r\n        # Do not pass values after this date\r\n        todate=datetime.datetime(2001, 2, 28),\r\n        reverse=False,\r\n        )\r\n    cerebro.adddata(data)\r\n\r\n    cerebro.run()\r\n\r\n    b = Bokeh(style='bar', plot_mode=PlotMode.Single)\r\n    cerebro.plot(b)"}}
{"actor":{"display_login":"aaron8tang"}}