{"action":"opened","issue":{"url":"https://api.github.com/repos/mementum/backtrader/issues/133","repository_url":"https://api.github.com/repos/mementum/backtrader","labels_url":"https://api.github.com/repos/mementum/backtrader/issues/133/labels{/name}","comments_url":"https://api.github.com/repos/mementum/backtrader/issues/133/comments","events_url":"https://api.github.com/repos/mementum/backtrader/issues/133/events","html_url":"https://github.com/mementum/backtrader/issues/133","id":173720808,"number":133,"title":"example (Quickstart-Optimize) does not work","user":{"login":"grinboy","id":7887304,"avatar_url":"https://avatars.githubusercontent.com/u/7887304?v=3","gravatar_id":"","url":"https://api.github.com/users/grinboy","html_url":"https://github.com/grinboy","followers_url":"https://api.github.com/users/grinboy/followers","following_url":"https://api.github.com/users/grinboy/following{/other_user}","gists_url":"https://api.github.com/users/grinboy/gists{/gist_id}","starred_url":"https://api.github.com/users/grinboy/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/grinboy/subscriptions","organizations_url":"https://api.github.com/users/grinboy/orgs","repos_url":"https://api.github.com/users/grinboy/repos","events_url":"https://api.github.com/users/grinboy/events{/privacy}","received_events_url":"https://api.github.com/users/grinboy/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2016-08-29T08:19:19Z","updated_at":"2016-08-29T08:19:19Z","closed_at":null,"body":"from __future__ import (absolute_import, division, print_function,\r\n                        unicode_literals)\r\n\r\nimport datetime  # For datetime objects\r\nimport os.path  # To manage paths\r\nimport sys  # To find out the script name (in argv[0])\r\n\r\n\r\n# Import the backtrader platform\r\nimport backtrader as bt\r\n\r\n\r\n# Create a Stratey\r\nclass TestStrategy(bt.Strategy):\r\n    params = (\r\n        ('maperiod', 15),\r\n        ('printlog', False),\r\n    )\r\n\r\n    def log(self, txt, dt=None, doprint=False):\r\n        ''' Logging function fot this strategy'''\r\n        if self.params.printlog or doprint:\r\n            dt = dt or self.datas[0].datetime.date(0)\r\n            print('%s, %s' % (dt.isoformat(), txt))\r\n\r\n    def __init__(self):\r\n        # Keep a reference to the \"close\" line in the data[0] dataseries\r\n        self.dataclose = self.datas[0].close\r\n\r\n        # To keep track of pending orders and buy price/commission\r\n        self.order = None\r\n        self.buyprice = None\r\n        self.buycomm = None\r\n\r\n        # Add a MovingAverageSimple indicator\r\n        self.sma = bt.indicators.SimpleMovingAverage(\r\n            self.datas[0], period=self.params.maperiod)\r\n\r\n    def notify_order(self, order):\r\n        if order.status in [order.Submitted, order.Accepted]:\r\n            # Buy/Sell order submitted/accepted to/by broker - Nothing to do\r\n            return\r\n\r\n        # Check if an order has been completed\r\n        # Attention: broker could reject order if not enougth cash\r\n        if order.status in [order.Completed, order.Canceled, order.Margin]:\r\n            if order.isbuy():\r\n                self.log(\r\n                    'BUY EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %\r\n                    (order.executed.price,\r\n                     order.executed.value,\r\n                     order.executed.comm))\r\n\r\n                self.buyprice = order.executed.price\r\n                self.buycomm = order.executed.comm\r\n            else:  # Sell\r\n                self.log('SELL EXECUTED, Price: %.2f, Cost: %.2f, Comm %.2f' %\r\n                         (order.executed.price,\r\n                          order.executed.value,\r\n                          order.executed.comm))\r\n\r\n            self.bar_executed = len(self)\r\n\r\n        # Write down: no pending order\r\n        self.order = None\r\n\r\n    def notify_trade(self, trade):\r\n        if not trade.isclosed:\r\n            return\r\n\r\n        self.log('OPERATION PROFIT, GROSS %.2f, NET %.2f' %\r\n                 (trade.pnl, trade.pnlcomm))\r\n\r\n    def next(self):\r\n        # Simply log the closing price of the series from the reference\r\n        self.log('Close, %.2f' % self.dataclose[0])\r\n\r\n        # Check if an order is pending ... if yes, we cannot send a 2nd one\r\n        if self.order:\r\n            return\r\n\r\n        # Check if we are in the market\r\n        if not self.position:\r\n\r\n            # Not yet ... we MIGHT BUY if ...\r\n            if self.dataclose[0] > self.sma[0]:\r\n\r\n                # BUY, BUY, BUY!!! (with all possible default parameters)\r\n                self.log('BUY CREATE, %.2f' % self.dataclose[0])\r\n\r\n                # Keep track of the created order to avoid a 2nd order\r\n                self.order = self.buy()\r\n\r\n        else:\r\n\r\n            if self.dataclose[0] < self.sma[0]:\r\n                # SELL, SELL, SELL!!! (with all possible default parameters)\r\n                self.log('SELL CREATE, %.2f' % self.dataclose[0])\r\n\r\n                # Keep track of the created order to avoid a 2nd order\r\n                self.order = self.sell()\r\n\r\n    def stop(self):\r\n        self.log('(MA Period %2d) Ending Value %.2f' %\r\n                 (self.params.maperiod, self.broker.getvalue()), doprint=True)\r\n        \r\n\r\nif __name__ == '__main__':\r\n    \r\n    cerebro=bt.Cerebro()\r\n    \r\n    strats = cerebro.optstrategy(TestStrategy,maperiod=range(10, 12))\r\n    \r\n    cerebro.addstrategy(TestStrategy)\r\n    cerebro.broker.setcommission(0.001)\r\n\r\n    \r\n    modpath = \"D:\\\\python\\\\\"\r\n    datapath = os.path.join(modpath, 'datas/orcl-1995-2014.txt')\r\n\r\n    # Create a Data Feed\r\n    data = bt.feeds.YahooFinanceCSVData(\r\n        dataname=datapath,\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(2000, 12, 31),\r\n        reverse=False)\r\n    \r\n    cerebro.adddata(data)\r\n    \r\n    cerebro.broker.set_cash(10000.00)\r\n    cerebro.addsizer(bt.sizers.FixedSize, stake=10)\r\n    cerebro.broker.setcommission(commission=0.0)\r\n    \r\n    cerebro.run()\r\n    \r\n    \r\nCopied example, I launched and does not work\r\n\r\nPython 3.4.4 (v3.4.4:737efcadf5a6, Dec 20 2015, 20:20:57) [MSC v.1600 64 bit (AMD64)]\r\n\r\nrunfile('D:/Downloads/WinPython-64bit-3.4.4.2/notebooks/back_trader.py', wdir='D:/Downloads/WinPython-64bit-3.4.4.2/notebooks')\r\nException in thread Thread-8:\r\nTraceback (most recent call last):\r\n  File \"D:\\Downloads\\WinPython-64bit-3.4.4.2\\python-3.4.4.amd64\\lib\\threading.py\", line 911, in _bootstrap_inner\r\n    self.run()\r\n  File \"D:\\Downloads\\WinPython-64bit-3.4.4.2\\python-3.4.4.amd64\\lib\\threading.py\", line 859, in run\r\n    self._target(*self._args, **self._kwargs)\r\n  File \"D:\\Downloads\\WinPython-64bit-3.4.4.2\\python-3.4.4.amd64\\lib\\multiprocessing\\pool.py\", line 429, in _handle_results\r\n    task = get()\r\n  File \"D:\\Downloads\\WinPython-64bit-3.4.4.2\\python-3.4.4.amd64\\lib\\multiprocessing\\connection.py\", line 251, in recv\r\n    return ForkingPickler.loads(buf.getbuffer())\r\nAttributeError: Can't get attribute 'TestStrategy' on <module '__main__' (<_frozen_importlib.SourceFileLoader object at 0x0000000002298240>)>\r\n\r\n\r\n\r\n"}}
{"actor":{"display_login":"grinboy"}}