GitHub PullRequestReviewCommentEvent at 2014-03-13 17:22:18
Repository: zipline
Actor: richafrank
Payload: {"comment":{"url":"https://api.github.com/repos/quantopian/zipline/pulls/comments/10575240","id":10575240,"diff_hunk":"@@ -0,0 +1,156 @@\n+#\n+# Copyright 2013 Quantopian, Inc.\n+#\n+# Licensed under the Apache License, Version 2.0 (the \"License\");\n+# you may not use this file except in compliance with the License.\n+# You may obtain a copy of the License at\n+#\n+#     http://www.apache.org/licenses/LICENSE-2.0\n+#\n+# Unless required by applicable law or agreed to in writing, software\n+# distributed under the License is distributed on an \"AS IS\" BASIS,\n+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n+# See the License for the specific language governing permissions and\n+# limitations under the License.\n+\n+from copy import copy\n+import six\n+\n+import numpy as np\n+from datetime import timedelta\n+\n+from zipline.sources.data_source import DataSource\n+from zipline.utils import tradingcalendar as calendar_nyse\n+from zipline.gens.utils import hash_args\n+\n+\n+class RandomWalkSource(DataSource):\n+    \"\"\"RandomWalkSource that emits events with prices that follow a\n+    random walk. Will generate valid datetimes that match market hours\n+    of the supplied calendar and can generate emit events with\n+    user-defined frequencies (e.g. minutely).\n+\n+    \"\"\"\n+    def __init__(self, start_prices=None, freq='minute', start=None,\n+                 end=None, calendar=calendar_nyse):\n+        \"\"\"\n+        :Arguments:\n+            start_prices : dict\n+                 sid -> starting price.\n+                 Default: {0: 100, 1: 500}\n+            freq : str <default='minute'>\n+                 Emits events according to freq.\n+                 Can be 'day' or 'minute'\n+            start : datetime <default=start of calendar>\n+                 Start dt to emit events.\n+            end : datetime <default=end of calendar>\n+                 End dt until to which emit events.\n+            calendar : calendar object <default: NYSE>\n+                 Calendar to use.\n+                 See zipline.utils for different choices.\n+\n+        :Example:\n+            # Assumes you have instantiated your Algorithm\n+            # as myalgo.\n+            myalgo = MyAlgo()\n+            source = RandomWalkSource()\n+            myalgo.run(source)\n+\n+        \"\"\"\n+        # Hash_value for downstream sorting.\n+        self.arg_string = hash_args(start_prices, freq, start, end,\n+                                    calendar)\n+\n+        self.freq = freq\n+        if start_prices is None:\n+            self.start_prices = {0: 100,\n+                                 1: 500}\n+        else:\n+            self.start_prices = start_prices\n+\n+        self.calendar = calendar\n+        if start is None:\n+            self.start = calendar.start\n+        else:\n+            self.start = start\n+        if end is None:\n+            self.end = calendar.end_base\n+        else:\n+            self.end = end\n+\n+        self.drift = .1\n+        self.sd = .1\n+\n+        self.open_and_closes = \\\n+            calendar.open_and_closes[self.start:self.end]\n+\n+        if self.freq == 'minute':\n+            self.raw_data_gen = self.raw_data_gen_minute\n+        elif self.freq == 'day':\n+            self.raw_data_gen = self.raw_data_gen_day\n+        else:\n+            raise ValueError('Frequency %s not supported.' % self.freq)\n+\n+        self._raw_data = None\n+\n+    @property\n+    def instance_hash(self):\n+        return self.arg_string\n+\n+    @property\n+    def mapping(self):\n+        return {\n+            'dt': (lambda x: x, 'dt'),\n+            'sid': (lambda x: x, 'sid'),\n+            'price': (float, 'price'),\n+            'volume': (int, 'volume'),\n+        }\n+\n+    def _gen_next_step(self, x):\n+        x += np.random.randn() * self.sd + self.drift\n+        if x <= 0.1:\n+            return 0.1\n+        else:\n+            return x\n+\n+    def raw_data_gen_minute(self):\n+        cur_prices = copy(self.start_prices)\n+        for _, (open_dt, close_dt) in self.open_and_closes.iterrows():\n+            # Emit minutely trade signals from open to close\n+            current_dt = copy(open_dt)\n+            while current_dt < close_dt:\n+                for sid, price in six.iteritems(cur_prices):\n+                    cur_prices[sid] = self._gen_next_step(cur_prices[sid])\n+\n+                    event = {\n+                        'dt': current_dt,\n+                        'sid': sid,\n+                        'price': cur_prices[sid],\n+                        'volume': 1000,\n+                    }\n+\n+                    yield event\n+\n+                current_dt += timedelta(minutes=1)\n+\n+    def raw_data_gen_day(self):","path":"zipline/sources/simulated.py","position":136,"original_position":136,"commit_id":"e0859965c6608fe09fe13aa38b22493166bf73f7","original_commit_id":"e0859965c6608fe09fe13aa38b22493166bf73f7","user":{"login":"richafrank","id":2123660,"avatar_url":"https://gravatar.com/avatar/adcb4bc889c27f8a36039678a3ce4fd2?d=https%3A%2F%2Fidenticons.github.com%2F4f03be1dc2ef04825fcba44ef5267c7c.png&r=x","gravatar_id":"adcb4bc889c27f8a36039678a3ce4fd2","url":"https://api.github.com/users/richafrank","html_url":"https://github.com/richafrank","followers_url":"https://api.github.com/users/richafrank/followers","following_url":"https://api.github.com/users/richafrank/following{/other_user}","gists_url":"https://api.github.com/users/richafrank/gists{/gist_id}","starred_url":"https://api.github.com/users/richafrank/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/richafrank/subscriptions","organizations_url":"https://api.github.com/users/richafrank/orgs","repos_url":"https://api.github.com/users/richafrank/repos","events_url":"https://api.github.com/users/richafrank/events{/privacy}","received_events_url":"https://api.github.com/users/richafrank/received_events","type":"User","site_admin":false},"body":"This looks very similar to ```raw_data_gen_minute```.  I wonder if we could de-duplicate any of it.","created_at":"2014-03-13T17:22:18Z","updated_at":"2014-03-13T17:22:18Z","html_url":"https://github.com/quantopian/zipline/pull/283#discussion_r10575240","pull_request_url":"https://api.github.com/repos/quantopian/zipline/pulls/283","_links":{"self":{"href":"https://api.github.com/repos/quantopian/zipline/pulls/comments/10575240"},"html":{"href":"https://github.com/quantopian/zipline/pull/283#discussion_r10575240"},"pull_request":{"href":"https://api.github.com/repos/quantopian/zipline/pulls/283"}}}}