ml-finance-python
python scripts for finance machine learning
git clone https://9o.is/git/ml-finance-python.git
notebook.ipynb
(11841B)
1 {
2 "cells": [
3 {
4 "cell_type": "markdown",
5 "metadata": {
6 "deletable": true,
7 "editable": true
8 },
9 "source": [
10 "#Exercises: Universe Selection\n",
11 "\n",
12 "## Lecture Link\n",
13 "\n",
14 "This exercise notebook refers to this lecture. Please use the lecture for explanations and sample code.\n",
15 "\n",
16 "https://www.quantopian.com/lectures#Universe-Selection\n",
17 "\n",
18 "Part of the Quantopian Lecture Series:\n",
19 "\n",
20 "* [www.quantopian.com/lectures](https://www.quantopian.com/lectures)\n",
21 "* [github.com/quantopian/research_public](https://github.com/quantopian/research_public)"
22 ]
23 },
24 {
25 "cell_type": "code",
26 "execution_count": null,
27 "metadata": {
28 "collapsed": true,
29 "deletable": true,
30 "editable": true
31 },
32 "outputs": [],
33 "source": [
34 "import numpy as np\n",
35 "import pandas as pd\n",
36 "import matplotlib.pyplot as plt\n",
37 "from quantopian.pipeline.classifiers.morningstar import Sector\n",
38 "from quantopian.pipeline import Pipeline\n",
39 "from quantopian.pipeline.data.builtin import USEquityPricing\n",
40 "from quantopian.pipeline.filters import QTradableStocksUS, AtLeastN\n",
41 "from quantopian.research import run_pipeline\n",
42 "from quantopian.pipeline.data import morningstar\n",
43 "from quantopian.pipeline.factors import CustomFactor, AverageDollarVolume"
44 ]
45 },
46 {
47 "cell_type": "markdown",
48 "metadata": {
49 "deletable": true,
50 "editable": true
51 },
52 "source": [
53 "## Helper Functions"
54 ]
55 },
56 {
57 "cell_type": "code",
58 "execution_count": null,
59 "metadata": {
60 "collapsed": true,
61 "deletable": true,
62 "editable": true
63 },
64 "outputs": [],
65 "source": [
66 "def calculate_daily_turnover(unstacked):\n",
67 " return (unstacked\n",
68 " .diff() # Get True/False showing where values changed from previous day.\n",
69 " .iloc[1:] # Drop first row, which is meaningless after diff().\n",
70 " .astype(bool) # diff() coerces from bool -> object :(. Undo that.\n",
71 " .groupby(axis=1, level=0) \n",
72 " .sum()) "
73 ]
74 },
75 {
76 "cell_type": "markdown",
77 "metadata": {
78 "deletable": true,
79 "editable": true
80 },
81 "source": [
82 "#Exercise 1: Examining the QTradableStocksUS Universe\n",
83 "\n",
84 "##a. Initializing the Universe\n",
85 "\n",
86 "Set the QTradableStocksUS as your universe by using the `QTradableStocksUS()` function."
87 ]
88 },
89 {
90 "cell_type": "code",
91 "execution_count": null,
92 "metadata": {
93 "collapsed": true,
94 "deletable": true,
95 "editable": true
96 },
97 "outputs": [],
98 "source": [
99 "# Your code goes here"
100 ]
101 },
102 {
103 "cell_type": "markdown",
104 "metadata": {
105 "deletable": true,
106 "editable": true
107 },
108 "source": [
109 "## b. Finding Asset Composition\n",
110 "\n",
111 "Use the pipeline API with the QTradableStocksUS as a screen to find and print the list of equities included in the QTradableStocksUS on 2016-07-01."
112 ]
113 },
114 {
115 "cell_type": "code",
116 "execution_count": null,
117 "metadata": {
118 "collapsed": false,
119 "deletable": true,
120 "editable": true,
121 "scrolled": false
122 },
123 "outputs": [],
124 "source": [
125 "# Your code goes here"
126 ]
127 },
128 {
129 "cell_type": "markdown",
130 "metadata": {
131 "deletable": true,
132 "editable": true
133 },
134 "source": [
135 "## c. Sector Exposure\n",
136 "\n",
137 "Use the pipeline API with the QtradableStocksUS as a screen to find and print the sector composition of the universe on 2016-07-01."
138 ]
139 },
140 {
141 "cell_type": "code",
142 "execution_count": null,
143 "metadata": {
144 "collapsed": false,
145 "deletable": true,
146 "editable": true
147 },
148 "outputs": [],
149 "source": [
150 "SECTOR_CODE_NAMES = {\n",
151 " Sector.BASIC_MATERIALS: 'Basic Materials',\n",
152 " Sector.CONSUMER_CYCLICAL: 'Consumer Cyclical',\n",
153 " Sector.FINANCIAL_SERVICES: 'Financial Services',\n",
154 " Sector.REAL_ESTATE: 'Real Estate',\n",
155 " Sector.CONSUMER_DEFENSIVE: 'Consumer Defensive',\n",
156 " Sector.HEALTHCARE: 'Healthcare',\n",
157 " Sector.UTILITIES: 'Utilities',\n",
158 " Sector.COMMUNICATION_SERVICES: 'Communication Services',\n",
159 " Sector.ENERGY: 'Energy',\n",
160 " Sector.INDUSTRIALS: 'Industrials',\n",
161 " Sector.TECHNOLOGY: 'Technology',\n",
162 " -1 : 'Misc'\n",
163 "}\n",
164 "\n",
165 "# Your code goes here"
166 ]
167 },
168 {
169 "cell_type": "markdown",
170 "metadata": {
171 "deletable": true,
172 "editable": true
173 },
174 "source": [
175 "## d. Turnover Rate\n",
176 "\n",
177 "Use the pipeline API with the QtradableStocksUS as a screen and the `calculate_daily_turnover` helper function to find and plot the turnover of the universe during 2016."
178 ]
179 },
180 {
181 "cell_type": "code",
182 "execution_count": null,
183 "metadata": {
184 "collapsed": false,
185 "deletable": true,
186 "editable": true,
187 "scrolled": false
188 },
189 "outputs": [],
190 "source": [
191 "# Your code goes here"
192 ]
193 },
194 {
195 "cell_type": "markdown",
196 "metadata": {
197 "deletable": true,
198 "editable": true
199 },
200 "source": [
201 "# Exercise 2: Examining Tradability\n",
202 "\n",
203 "## a. NetIncome 1500\n",
204 "\n",
205 "Create a universe consisting of the top 1500 equities by net income then find and print the list of equities included in the universe on 2016-07-01."
206 ]
207 },
208 {
209 "cell_type": "code",
210 "execution_count": null,
211 "metadata": {
212 "collapsed": false,
213 "deletable": true,
214 "editable": true
215 },
216 "outputs": [],
217 "source": [
218 "# Your code goes here"
219 ]
220 },
221 {
222 "cell_type": "markdown",
223 "metadata": {
224 "deletable": true,
225 "editable": true
226 },
227 "source": [
228 "## b. Measuring Tradability\n",
229 "\n",
230 "\n",
231 "Find the average 200 day average dollar volume of the NetIncome 1500 universe using the `AverageDollarVolume` built in factor and compare to that of the QTradableStocksUS."
232 ]
233 },
234 {
235 "cell_type": "code",
236 "execution_count": null,
237 "metadata": {
238 "collapsed": false,
239 "deletable": true,
240 "editable": true
241 },
242 "outputs": [],
243 "source": [
244 "# Your code goes here"
245 ]
246 },
247 {
248 "cell_type": "markdown",
249 "metadata": {
250 "deletable": true,
251 "editable": true
252 },
253 "source": [
254 "# Exercise 3: Sector Balance\n",
255 "\n",
256 "## a. Dividend 1500\n",
257 "\n",
258 "Create a universe consisting of the top 1500 equities by dividend yield then find and print the list of equities included in the this universe on 2016-07-01."
259 ]
260 },
261 {
262 "cell_type": "code",
263 "execution_count": null,
264 "metadata": {
265 "collapsed": false,
266 "deletable": true,
267 "editable": true
268 },
269 "outputs": [],
270 "source": [
271 "# Your code goes here"
272 ]
273 },
274 {
275 "cell_type": "markdown",
276 "metadata": {
277 "deletable": true,
278 "editable": true
279 },
280 "source": [
281 "## b. Dividend 1500 Sector Composition\n",
282 "\n",
283 "Find and print the sector composition of the universe on 2016-07-01."
284 ]
285 },
286 {
287 "cell_type": "code",
288 "execution_count": null,
289 "metadata": {
290 "collapsed": false,
291 "deletable": true,
292 "editable": true
293 },
294 "outputs": [],
295 "source": [
296 "SECTOR_CODE_NAMES = {\n",
297 " Sector.BASIC_MATERIALS: 'Basic Materials',\n",
298 " Sector.CONSUMER_CYCLICAL: 'Consumer Cyclical',\n",
299 " Sector.FINANCIAL_SERVICES: 'Financial Services',\n",
300 " Sector.REAL_ESTATE: 'Real Estate',\n",
301 " Sector.CONSUMER_DEFENSIVE: 'Consumer Defensive',\n",
302 " Sector.HEALTHCARE: 'Healthcare',\n",
303 " Sector.UTILITIES: 'Utilities',\n",
304 " Sector.COMMUNICATION_SERVICES: 'Communication Services',\n",
305 " Sector.ENERGY: 'Energy',\n",
306 " Sector.INDUSTRIALS: 'Industrials',\n",
307 " Sector.TECHNOLOGY: 'Technology',\n",
308 " -1 : 'Misc'\n",
309 "}\n",
310 "\n",
311 "# Your code goes here"
312 ]
313 },
314 {
315 "cell_type": "markdown",
316 "metadata": {
317 "deletable": true,
318 "editable": true
319 },
320 "source": [
321 "# Exercise 4: Turnover Smoothing\n",
322 "\n",
323 "## a. PE 1500\n",
324 "\n",
325 "Create a universe consisting of the top 1500 equities by price to earnings ratio then find and print the list of equities included in the this universe on 2016-07-01."
326 ]
327 },
328 {
329 "cell_type": "code",
330 "execution_count": null,
331 "metadata": {
332 "collapsed": false,
333 "deletable": true,
334 "editable": true
335 },
336 "outputs": [],
337 "source": [
338 "# Your code goes here"
339 ]
340 },
341 {
342 "cell_type": "markdown",
343 "metadata": {
344 "deletable": true,
345 "editable": true
346 },
347 "source": [
348 "## b. PE 1500 Turnover\n",
349 "\n",
350 "Use the `calculate_daily_turnover` helper function to find and plot the turnover of the PE 1500 universe during 2016. Compare the average to that of the QTradableStocksUS."
351 ]
352 },
353 {
354 "cell_type": "code",
355 "execution_count": null,
356 "metadata": {
357 "collapsed": false,
358 "deletable": true,
359 "editable": true
360 },
361 "outputs": [],
362 "source": [
363 "# Your code goes here"
364 ]
365 },
366 {
367 "cell_type": "markdown",
368 "metadata": {
369 "deletable": true,
370 "editable": true
371 },
372 "source": [
373 "## c. Smoothing the PE 1500\n",
374 "\n",
375 "Using `AtLeastN`, apply a smoothing function to the PE 1500 to reduce turnover noise and find the new mean turnover."
376 ]
377 },
378 {
379 "cell_type": "code",
380 "execution_count": null,
381 "metadata": {
382 "collapsed": false,
383 "deletable": true,
384 "editable": true
385 },
386 "outputs": [],
387 "source": [
388 "# Your code goes here"
389 ]
390 },
391 {
392 "cell_type": "markdown",
393 "metadata": {},
394 "source": [
395 "---\n",
396 "\n",
397 "Congratulations on completing the Universe Selection exercises!\n",
398 "\n",
399 "As you learn more about writing trading models and the Quantopian platform, enter a daily [Quantopian Contest](https://www.quantopian.com/contest). Your strategy will be evaluated for a cash prize every day.\n",
400 "\n",
401 "Start by going through the [Writing a Contest Algorithm](https://www.quantopian.com/tutorials/contest) tutorial."
402 ]
403 },
404 {
405 "cell_type": "markdown",
406 "metadata": {
407 "deletable": true,
408 "editable": true
409 },
410 "source": [
411 "*This presentation is for informational purposes only and does not constitute an offer to sell, a solicitation to buy, or a recommendation for any security; nor does it constitute an offer to provide investment advisory or other services by Quantopian, Inc. (\"Quantopian\"). Nothing contained herein constitutes investment advice or offers any opinion with respect to the suitability of any security, and any views expressed herein should not be taken as advice to buy, sell, or hold any security or as an endorsement of any security or company. In preparing the information contained herein, Quantopian, Inc. has not taken into account the investment needs, objectives, and financial circumstances of any particular investor. Any views expressed and data illustrated herein were prepared based upon information, believed to be reliable, available to Quantopian, Inc. at the time of publication. Quantopian makes no guarantees as to their accuracy or completeness. All information is subject to change and may quickly become unreliable for various reasons, including changes in market conditions or economic circumstances.*"
412 ]
413 }
414 ],
415 "metadata": {
416 "kernelspec": {
417 "display_name": "Python 2",
418 "language": "python",
419 "name": "python2"
420 },
421 "language_info": {
422 "codemirror_mode": {
423 "name": "ipython",
424 "version": 2
425 },
426 "file_extension": ".py",
427 "mimetype": "text/x-python",
428 "name": "python",
429 "nbconvert_exporter": "python",
430 "pygments_lexer": "ipython2",
431 "version": "2.7.12"
432 }
433 },
434 "nbformat": 4,
435 "nbformat_minor": 0
436 }