ml-finance-python
python scripts for finance machine learning
git clone https://9o.is/git/ml-finance-python.git
notebook.ipynb
(18073B)
1 {
2 "cells": [
3 {
4 "cell_type": "code",
5 "execution_count": 1,
6 "metadata": {
7 "collapsed": true
8 },
9 "outputs": [],
10 "source": [
11 "from quantopian.pipeline import Pipeline\n",
12 "from quantopian.research import run_pipeline\n",
13 "from quantopian.pipeline.data.builtin import USEquityPricing\n",
14 "from quantopian.pipeline.factors import SimpleMovingAverage, AverageDollarVolume"
15 ]
16 },
17 {
18 "cell_type": "markdown",
19 "metadata": {},
20 "source": [
21 "###Combining Filters\n",
22 "Like factors, filters can be combined. Combining filters is done using the `&` (and) and `|` (or) operators. For example, let's say we want to screen for securities that are in the top 10% of average dollar volume and have a latest close price above $20. To start, let's make a high dollar volume filter using an `AverageDollarVolume` factor and `percentile_between`:"
23 ]
24 },
25 {
26 "cell_type": "code",
27 "execution_count": 2,
28 "metadata": {
29 "collapsed": true
30 },
31 "outputs": [],
32 "source": [
33 "dollar_volume = AverageDollarVolume(window_length=30)\n",
34 "high_dollar_volume = dollar_volume.percentile_between(90, 100)"
35 ]
36 },
37 {
38 "cell_type": "markdown",
39 "metadata": {},
40 "source": [
41 "Note: `percentile_between` is a `Factor` method returning a `Filter`."
42 ]
43 },
44 {
45 "cell_type": "markdown",
46 "metadata": {},
47 "source": [
48 "Next, let's create a `latest_close` factor and define a filter for securities that closed above $20:"
49 ]
50 },
51 {
52 "cell_type": "code",
53 "execution_count": 3,
54 "metadata": {
55 "collapsed": true
56 },
57 "outputs": [],
58 "source": [
59 "latest_close = USEquityPricing.close.latest\n",
60 "above_20 = latest_close > 20"
61 ]
62 },
63 {
64 "cell_type": "markdown",
65 "metadata": {},
66 "source": [
67 "Now we can combine our `high_dollar_volume` filter with our `above_20` filter using the `&` operator:"
68 ]
69 },
70 {
71 "cell_type": "code",
72 "execution_count": 4,
73 "metadata": {
74 "collapsed": true
75 },
76 "outputs": [],
77 "source": [
78 "tradeable_filter = high_dollar_volume & above_20"
79 ]
80 },
81 {
82 "cell_type": "markdown",
83 "metadata": {},
84 "source": [
85 "This filter will evaluate to `True` for securities where both `high_dollar_volume` and `above_20` are `True`. Otherwise, it will evaluate to `False`. A similar computation can be made with the `|` (or) operator.\n",
86 "\n",
87 "If we want to use this filter as a screen in our pipeline, we can simply pass `tradeable_filter` as the `screen` argument."
88 ]
89 },
90 {
91 "cell_type": "code",
92 "execution_count": 5,
93 "metadata": {
94 "collapsed": true
95 },
96 "outputs": [],
97 "source": [
98 "def make_pipeline():\n",
99 "\n",
100 " mean_close_10 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=10)\n",
101 " mean_close_30 = SimpleMovingAverage(inputs=[USEquityPricing.close], window_length=30)\n",
102 "\n",
103 " percent_difference = (mean_close_10 - mean_close_30) / mean_close_30\n",
104 "\n",
105 " dollar_volume = AverageDollarVolume(window_length=30)\n",
106 " high_dollar_volume = dollar_volume.percentile_between(90, 100)\n",
107 "\n",
108 " latest_close = USEquityPricing.close.latest\n",
109 " above_20 = latest_close > 20\n",
110 "\n",
111 " tradeable_filter = high_dollar_volume & above_20\n",
112 "\n",
113 " return Pipeline(\n",
114 " columns={\n",
115 " 'percent_difference': percent_difference\n",
116 " },\n",
117 " screen=tradeable_filter\n",
118 " )"
119 ]
120 },
121 {
122 "cell_type": "markdown",
123 "metadata": {},
124 "source": [
125 "When we run this, our pipeline output now only includes ~700 securities."
126 ]
127 },
128 {
129 "cell_type": "code",
130 "execution_count": 6,
131 "metadata": {
132 "collapsed": false
133 },
134 "outputs": [
135 {
136 "name": "stdout",
137 "output_type": "stream",
138 "text": [
139 "Number of securities that passed the filter: 737\n"
140 ]
141 },
142 {
143 "data": {
144 "text/html": [
145 "<div style=\"max-height:1000px;max-width:1500px;overflow:auto;\">\n",
146 "<table border=\"1\" class=\"dataframe\">\n",
147 " <thead>\n",
148 " <tr style=\"text-align: right;\">\n",
149 " <th></th>\n",
150 " <th></th>\n",
151 " <th>percent_difference</th>\n",
152 " </tr>\n",
153 " </thead>\n",
154 " <tbody>\n",
155 " <tr>\n",
156 " <th rowspan=\"61\" valign=\"top\">2015-05-05 00:00:00+00:00</th>\n",
157 " <th>Equity(24 [AAPL])</th>\n",
158 " <td>0.016905</td>\n",
159 " </tr>\n",
160 " <tr>\n",
161 " <th>Equity(62 [ABT])</th>\n",
162 " <td>0.014385</td>\n",
163 " </tr>\n",
164 " <tr>\n",
165 " <th>Equity(67 [ADSK])</th>\n",
166 " <td>-0.003921</td>\n",
167 " </tr>\n",
168 " <tr>\n",
169 " <th>Equity(76 [TAP])</th>\n",
170 " <td>-0.008759</td>\n",
171 " </tr>\n",
172 " <tr>\n",
173 " <th>Equity(114 [ADBE])</th>\n",
174 " <td>0.009499</td>\n",
175 " </tr>\n",
176 " <tr>\n",
177 " <th>Equity(122 [ADI])</th>\n",
178 " <td>0.009271</td>\n",
179 " </tr>\n",
180 " <tr>\n",
181 " <th>Equity(128 [ADM])</th>\n",
182 " <td>0.015760</td>\n",
183 " </tr>\n",
184 " <tr>\n",
185 " <th>Equity(154 [AEM])</th>\n",
186 " <td>0.026035</td>\n",
187 " </tr>\n",
188 " <tr>\n",
189 " <th>Equity(161 [AEP])</th>\n",
190 " <td>0.010405</td>\n",
191 " </tr>\n",
192 " <tr>\n",
193 " <th>Equity(168 [AET])</th>\n",
194 " <td>0.005853</td>\n",
195 " </tr>\n",
196 " <tr>\n",
197 " <th>Equity(185 [AFL])</th>\n",
198 " <td>-0.002239</td>\n",
199 " </tr>\n",
200 " <tr>\n",
201 " <th>Equity(216 [HES])</th>\n",
202 " <td>0.036528</td>\n",
203 " </tr>\n",
204 " <tr>\n",
205 " <th>Equity(239 [AIG])</th>\n",
206 " <td>0.012322</td>\n",
207 " </tr>\n",
208 " <tr>\n",
209 " <th>Equity(270 [AKRX])</th>\n",
210 " <td>-0.024963</td>\n",
211 " </tr>\n",
212 " <tr>\n",
213 " <th>Equity(300 [ALK])</th>\n",
214 " <td>0.015147</td>\n",
215 " </tr>\n",
216 " <tr>\n",
217 " <th>Equity(301 [ALKS])</th>\n",
218 " <td>-0.033228</td>\n",
219 " </tr>\n",
220 " <tr>\n",
221 " <th>Equity(328 [ALTR])</th>\n",
222 " <td>0.012284</td>\n",
223 " </tr>\n",
224 " <tr>\n",
225 " <th>Equity(357 [TWX])</th>\n",
226 " <td>0.000365</td>\n",
227 " </tr>\n",
228 " <tr>\n",
229 " <th>Equity(368 [AMGN])</th>\n",
230 " <td>0.008860</td>\n",
231 " </tr>\n",
232 " <tr>\n",
233 " <th>Equity(438 [AON])</th>\n",
234 " <td>0.002327</td>\n",
235 " </tr>\n",
236 " <tr>\n",
237 " <th>Equity(448 [APA])</th>\n",
238 " <td>0.035926</td>\n",
239 " </tr>\n",
240 " <tr>\n",
241 " <th>Equity(455 [APC])</th>\n",
242 " <td>0.049153</td>\n",
243 " </tr>\n",
244 " <tr>\n",
245 " <th>Equity(460 [APD])</th>\n",
246 " <td>-0.006999</td>\n",
247 " </tr>\n",
248 " <tr>\n",
249 " <th>Equity(624 [ATW])</th>\n",
250 " <td>0.014957</td>\n",
251 " </tr>\n",
252 " <tr>\n",
253 " <th>Equity(630 [ADP])</th>\n",
254 " <td>-0.002134</td>\n",
255 " </tr>\n",
256 " <tr>\n",
257 " <th>Equity(679 [AXP])</th>\n",
258 " <td>-0.011809</td>\n",
259 " </tr>\n",
260 " <tr>\n",
261 " <th>Equity(693 [AZO])</th>\n",
262 " <td>0.002395</td>\n",
263 " </tr>\n",
264 " <tr>\n",
265 " <th>Equity(698 [BA])</th>\n",
266 " <td>-0.016685</td>\n",
267 " </tr>\n",
268 " <tr>\n",
269 " <th>Equity(734 [BAX])</th>\n",
270 " <td>0.009414</td>\n",
271 " </tr>\n",
272 " <tr>\n",
273 " <th>Equity(739 [BBBY])</th>\n",
274 " <td>-0.027796</td>\n",
275 " </tr>\n",
276 " <tr>\n",
277 " <th>...</th>\n",
278 " <td>...</td>\n",
279 " </tr>\n",
280 " <tr>\n",
281 " <th>Equity(45269 [EVHC])</th>\n",
282 " <td>-0.004877</td>\n",
283 " </tr>\n",
284 " <tr>\n",
285 " <th>Equity(45451 [FEYE])</th>\n",
286 " <td>0.042108</td>\n",
287 " </tr>\n",
288 " <tr>\n",
289 " <th>Equity(45558 [BURL])</th>\n",
290 " <td>-0.053654</td>\n",
291 " </tr>\n",
292 " <tr>\n",
293 " <th>Equity(45570 [JNUG])</th>\n",
294 " <td>0.053977</td>\n",
295 " </tr>\n",
296 " <tr>\n",
297 " <th>Equity(45618 [AR])</th>\n",
298 " <td>0.091085</td>\n",
299 " </tr>\n",
300 " <tr>\n",
301 " <th>Equity(45769 [WUBA])</th>\n",
302 " <td>0.234141</td>\n",
303 " </tr>\n",
304 " <tr>\n",
305 " <th>Equity(45804 [ASHR])</th>\n",
306 " <td>0.082573</td>\n",
307 " </tr>\n",
308 " <tr>\n",
309 " <th>Equity(45815 [TWTR])</th>\n",
310 " <td>-0.077268</td>\n",
311 " </tr>\n",
312 " <tr>\n",
313 " <th>Equity(45971 [AAL])</th>\n",
314 " <td>0.008087</td>\n",
315 " </tr>\n",
316 " <tr>\n",
317 " <th>Equity(45978 [ATHM])</th>\n",
318 " <td>0.063568</td>\n",
319 " </tr>\n",
320 " <tr>\n",
321 " <th>Equity(45993 [HLT])</th>\n",
322 " <td>-0.000895</td>\n",
323 " </tr>\n",
324 " <tr>\n",
325 " <th>Equity(46015 [ALLY])</th>\n",
326 " <td>0.009605</td>\n",
327 " </tr>\n",
328 " <tr>\n",
329 " <th>Equity(46308 [ASPX])</th>\n",
330 " <td>0.054145</td>\n",
331 " </tr>\n",
332 " <tr>\n",
333 " <th>Equity(46631 [GOOG])</th>\n",
334 " <td>0.004730</td>\n",
335 " </tr>\n",
336 " <tr>\n",
337 " <th>Equity(46693 [GRUB])</th>\n",
338 " <td>-0.016904</td>\n",
339 " </tr>\n",
340 " <tr>\n",
341 " <th>Equity(46979 [JD])</th>\n",
342 " <td>0.058362</td>\n",
343 " </tr>\n",
344 " <tr>\n",
345 " <th>Equity(47169 [KITE])</th>\n",
346 " <td>-0.049366</td>\n",
347 " </tr>\n",
348 " <tr>\n",
349 " <th>Equity(47208 [GPRO])</th>\n",
350 " <td>0.061078</td>\n",
351 " </tr>\n",
352 " <tr>\n",
353 " <th>Equity(47230 [NSAM])</th>\n",
354 " <td>-0.037879</td>\n",
355 " </tr>\n",
356 " <tr>\n",
357 " <th>Equity(47430 [MBLY])</th>\n",
358 " <td>0.050288</td>\n",
359 " </tr>\n",
360 " <tr>\n",
361 " <th>Equity(47740 [BABA])</th>\n",
362 " <td>-0.008354</td>\n",
363 " </tr>\n",
364 " <tr>\n",
365 " <th>Equity(47777 [CFG])</th>\n",
366 " <td>0.025703</td>\n",
367 " </tr>\n",
368 " <tr>\n",
369 " <th>Equity(47779 [CYBR])</th>\n",
370 " <td>0.101844</td>\n",
371 " </tr>\n",
372 " <tr>\n",
373 " <th>Equity(48065 [AXTA])</th>\n",
374 " <td>0.034600</td>\n",
375 " </tr>\n",
376 " <tr>\n",
377 " <th>Equity(48317 [JUNO])</th>\n",
378 " <td>-0.103370</td>\n",
379 " </tr>\n",
380 " <tr>\n",
381 " <th>Equity(48384 [QRVO])</th>\n",
382 " <td>-0.050578</td>\n",
383 " </tr>\n",
384 " <tr>\n",
385 " <th>Equity(48892 [IGT])</th>\n",
386 " <td>0.005591</td>\n",
387 " </tr>\n",
388 " <tr>\n",
389 " <th>Equity(48934 [ETSY])</th>\n",
390 " <td>-0.030142</td>\n",
391 " </tr>\n",
392 " <tr>\n",
393 " <th>Equity(48962 [CSAL])</th>\n",
394 " <td>0.000000</td>\n",
395 " </tr>\n",
396 " <tr>\n",
397 " <th>Equity(48972 [EVA])</th>\n",
398 " <td>0.000000</td>\n",
399 " </tr>\n",
400 " </tbody>\n",
401 "</table>\n",
402 "<p>737 rows × 1 columns</p>\n",
403 "</div>"
404 ],
405 "text/plain": [
406 " percent_difference\n",
407 "2015-05-05 00:00:00+00:00 Equity(24 [AAPL]) 0.016905\n",
408 " Equity(62 [ABT]) 0.014385\n",
409 " Equity(67 [ADSK]) -0.003921\n",
410 " Equity(76 [TAP]) -0.008759\n",
411 " Equity(114 [ADBE]) 0.009499\n",
412 " Equity(122 [ADI]) 0.009271\n",
413 " Equity(128 [ADM]) 0.015760\n",
414 " Equity(154 [AEM]) 0.026035\n",
415 " Equity(161 [AEP]) 0.010405\n",
416 " Equity(168 [AET]) 0.005853\n",
417 " Equity(185 [AFL]) -0.002239\n",
418 " Equity(216 [HES]) 0.036528\n",
419 " Equity(239 [AIG]) 0.012322\n",
420 " Equity(270 [AKRX]) -0.024963\n",
421 " Equity(300 [ALK]) 0.015147\n",
422 " Equity(301 [ALKS]) -0.033228\n",
423 " Equity(328 [ALTR]) 0.012284\n",
424 " Equity(357 [TWX]) 0.000365\n",
425 " Equity(368 [AMGN]) 0.008860\n",
426 " Equity(438 [AON]) 0.002327\n",
427 " Equity(448 [APA]) 0.035926\n",
428 " Equity(455 [APC]) 0.049153\n",
429 " Equity(460 [APD]) -0.006999\n",
430 " Equity(624 [ATW]) 0.014957\n",
431 " Equity(630 [ADP]) -0.002134\n",
432 " Equity(679 [AXP]) -0.011809\n",
433 " Equity(693 [AZO]) 0.002395\n",
434 " Equity(698 [BA]) -0.016685\n",
435 " Equity(734 [BAX]) 0.009414\n",
436 " Equity(739 [BBBY]) -0.027796\n",
437 "... ...\n",
438 " Equity(45269 [EVHC]) -0.004877\n",
439 " Equity(45451 [FEYE]) 0.042108\n",
440 " Equity(45558 [BURL]) -0.053654\n",
441 " Equity(45570 [JNUG]) 0.053977\n",
442 " Equity(45618 [AR]) 0.091085\n",
443 " Equity(45769 [WUBA]) 0.234141\n",
444 " Equity(45804 [ASHR]) 0.082573\n",
445 " Equity(45815 [TWTR]) -0.077268\n",
446 " Equity(45971 [AAL]) 0.008087\n",
447 " Equity(45978 [ATHM]) 0.063568\n",
448 " Equity(45993 [HLT]) -0.000895\n",
449 " Equity(46015 [ALLY]) 0.009605\n",
450 " Equity(46308 [ASPX]) 0.054145\n",
451 " Equity(46631 [GOOG]) 0.004730\n",
452 " Equity(46693 [GRUB]) -0.016904\n",
453 " Equity(46979 [JD]) 0.058362\n",
454 " Equity(47169 [KITE]) -0.049366\n",
455 " Equity(47208 [GPRO]) 0.061078\n",
456 " Equity(47230 [NSAM]) -0.037879\n",
457 " Equity(47430 [MBLY]) 0.050288\n",
458 " Equity(47740 [BABA]) -0.008354\n",
459 " Equity(47777 [CFG]) 0.025703\n",
460 " Equity(47779 [CYBR]) 0.101844\n",
461 " Equity(48065 [AXTA]) 0.034600\n",
462 " Equity(48317 [JUNO]) -0.103370\n",
463 " Equity(48384 [QRVO]) -0.050578\n",
464 " Equity(48892 [IGT]) 0.005591\n",
465 " Equity(48934 [ETSY]) -0.030142\n",
466 " Equity(48962 [CSAL]) 0.000000\n",
467 " Equity(48972 [EVA]) 0.000000\n",
468 "\n",
469 "[737 rows x 1 columns]"
470 ]
471 },
472 "execution_count": 6,
473 "metadata": {},
474 "output_type": "execute_result"
475 }
476 ],
477 "source": [
478 "result = run_pipeline(make_pipeline(), '2015-05-05', '2015-05-05')\n",
479 "print 'Number of securities that passed the filter: %d' % len(result)\n",
480 "result"
481 ]
482 },
483 {
484 "cell_type": "markdown",
485 "metadata": {},
486 "source": [
487 "In the next lesson, we'll look at masking factors and filters."
488 ]
489 }
490 ],
491 "metadata": {
492 "kernelspec": {
493 "display_name": "Python 2",
494 "language": "python",
495 "name": "python2"
496 },
497 "language_info": {
498 "codemirror_mode": {
499 "name": "ipython",
500 "version": 2
501 },
502 "file_extension": ".py",
503 "mimetype": "text/x-python",
504 "name": "python",
505 "nbconvert_exporter": "python",
506 "pygments_lexer": "ipython2",
507 "version": "2.7.12"
508 }
509 },
510 "nbformat": 4,
511 "nbformat_minor": 0
512 }