Quantopian Introduction - AI In Finance?

Today, let's have some fun of AI in finance. We are going to use a tool named Quantopian. As far as I know, functional programming language Haskell has been widely used in quantitative analysis. That's possibly why a dependent package of Quantopian, namely pandoc, requires The Haskell Tool Stack for its installation. Therefore, Haskell and pandoc need to be installed before Quantopian installation.

1. Preparations

1.1 Haskell Installation

Please refer to https://docs.haskellstack.org/en/stable/install_and_upgrade/:

1
➜  ~ curl -sSL https://get.haskellstack.org/ | sh

1.2 pandoc Installation

Please refer to https://pandoc.org/installing.html:

1
2
3
4
➜  ~ git clone https://github.com/jgm/pandoc
➜ ~ cd pandoc
➜ ~ stack setup
➜ ~ stack install

1.3 Zipline Installation with pip (ERRORS)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
➜  ~ pip install -U zipline --user
Collecting zipline
Using cached https://files.pythonhosted.org/packages/be/59/8c5802a7897c1095fdc409fb557f04df8f75c37174e80d2ba58c8d8a6488/zipline-1.3.0.tar.gz
Requirement already satisfied, skipping upgrade: pip>=7.1.0 in ~/.local/lib/python3.6/site-packages (from zipline) (18.1)
Requirement already satisfied, skipping upgrade: setuptools>18.0 in ~/.local/lib/python3.6/site-packages (from zipline) (40.5.0)
Collecting Logbook>=0.12.5 (from zipline)
Using cached https://files.pythonhosted.org/packages/74/fc/3e7557ed1ef1bd4e3ee189fc670416abfc7192b550e8d3c1d858a63f41ab/Logbook-1.4.1.tar.gz
Requirement already satisfied, skipping upgrade: pytz>=2016.4 in ~/.local/lib/python3.6/site-packages (from zipline) (2018.7)
Requirement already satisfied, skipping upgrade: numpy>=1.11.1 in ~/.local/lib/python3.6/site-packages (from zipline) (1.15.4)
Requirement already satisfied, skipping upgrade: requests-file>=1.4.1 in ~/.local/lib/python3.6/site-packages (from zipline) (1.4.3)
Requirement already satisfied, skipping upgrade: scipy>=0.17.1 in ~/.local/lib/python3.6/site-packages (from zipline) (1.1.0)
Collecting pandas<=0.22,>=0.18.1 (from zipline)
Using cached https://files.pythonhosted.org/packages/da/c6/0936bc5814b429fddb5d6252566fe73a3e40372e6ceaf87de3dec1326f28/pandas-0.22.0-cp36-cp36m-manylinux1_x86_64.whl
Collecting pandas-datareader>=0.2.1 (from zipline)
Using cached https://files.pythonhosted.org/packages/cc/5c/ea5b6dcfd0f55c5fb1e37fb45335ec01cceca199b8a79339137f5ed269e0/pandas_datareader-0.7.0-py2.py3-none-any.whl
Requirement already satisfied, skipping upgrade: patsy>=0.4.0 in ~/.local/lib/python3.6/site-packages (from zipline) (0.5.1)
Collecting statsmodels>=0.6.1 (from zipline)
Using cached https://files.pythonhosted.org/packages/85/d1/69ee7e757f657e7f527cbf500ec2d295396e5bcec873cf4eb68962c41024/statsmodels-0.9.0-cp36-cp36m-manylinux1_x86_64.whl
Requirement already satisfied, skipping upgrade: python-dateutil>=2.4.2 in ~/.local/lib/python3.6/site-packages (from zipline) (2.7.5)
Requirement already satisfied, skipping upgrade: six>=1.10.0 in ~/.local/lib/python3.6/site-packages (from zipline) (1.11.0)
Requirement already satisfied, skipping upgrade: requests>=2.9.1 in ~/.local/lib/python3.6/site-packages (from zipline) (2.20.0)
Requirement already satisfied, skipping upgrade: Cython>=0.25.2 in ~/.local/lib/python3.6/site-packages (from zipline) (0.29)
Collecting cyordereddict>=0.2.2 (from zipline)
Using cached https://files.pythonhosted.org/packages/d1/1a/364cbfd927be1b743c7f0a985a7f1f7e8a51469619f9fefe4ee9240ba210/cyordereddict-1.0.0.tar.gz
Collecting bottleneck>=1.0.0 (from zipline)
Using cached https://files.pythonhosted.org/packages/05/ae/cedf5323f398ab4e4ff92d6c431a3e1c6a186f9b41ab3e8258dff786a290/Bottleneck-1.2.1.tar.gz
Requirement already satisfied, skipping upgrade: contextlib2>=0.4.0 in ~/.local/lib/python3.6/site-packages/contextlib2-0.5.5-py3.6.egg (from zipline) (0.5.5)
Requirement already satisfied, skipping upgrade: decorator>=4.0.0 in ~/.local/lib/python3.6/site-packages (from zipline) (4.3.0)
Collecting networkx<2.0,>=1.9.1 (from zipline)
Using cached https://files.pythonhosted.org/packages/d3/2c/e473e54afc9fae58dfa97066ef6709a7e35a1dd1c28c5a3842989322be00/networkx-1.11-py2.py3-none-any.whl
Collecting numexpr>=2.6.1 (from zipline)
Using cached https://files.pythonhosted.org/packages/db/ea/efd9e16283637eb5b6c0042b6cc3521f1b9a5b47767ac463c88bbd37670c/numexpr-2.6.8-cp36-cp36m-manylinux1_x86_64.whl
Collecting bcolz<1,>=0.12.1 (from zipline)
Using cached https://files.pythonhosted.org/packages/6c/8b/1ffa01f872cac36173c5eb95b58c01040d8d25f1b242c48577f4104cd3ab/bcolz-0.12.1.tar.gz
Requirement already satisfied, skipping upgrade: click>=4.0.0 in ~/.local/lib/python3.6/site-packages (from zipline) (7.0)
Requirement already satisfied, skipping upgrade: toolz>=0.8.2 in ~/.local/lib/python3.6/site-packages (from zipline) (0.9.0)
Requirement already satisfied, skipping upgrade: multipledispatch>=0.4.8 in ~/.local/lib/python3.6/site-packages (from zipline) (0.6.0)
Requirement already satisfied, skipping upgrade: MarkupSafe>=0.23 in ~/.local/lib/python3.6/site-packages (from zipline) (1.1.0)
Requirement already satisfied, skipping upgrade: Mako>=1.0.1 in /usr/lib/python3/dist-packages (from zipline) (1.0.7)
Collecting sqlalchemy>=1.0.8 (from zipline)
Using cached https://files.pythonhosted.org/packages/1e/98/4dba86354d271344e25fa01dc38a0bf0e0ba6407ad2d5e8426496a95b568/SQLAlchemy-1.2.13.tar.gz
Collecting alembic>=0.7.7 (from zipline)
Using cached https://files.pythonhosted.org/packages/c0/f3/e60af9a36ae3b8cafabc7e0834d8df6a2965b3feecf27b9b11352dc05dd4/alembic-1.0.2.tar.gz
Collecting sortedcontainers>=1.4.4 (from zipline)
Using cached https://files.pythonhosted.org/packages/be/e3/a065de5fdd5849450a8a16a52a96c8db5f498f245e7eda06cc6725d04b80/sortedcontainers-2.0.5-py2.py3-none-any.whl
Collecting intervaltree>=2.1.0 (from zipline)
Using cached https://files.pythonhosted.org/packages/ca/c1/450d109b70fa58ca9d77972b02f69222412f9175ccf99fdeaf167be9583c/intervaltree-2.1.0.tar.gz
Collecting lru-dict>=1.1.4 (from zipline)
Using cached https://files.pythonhosted.org/packages/00/a5/32ed6e10246cd341ca8cc205acea5d208e4053f48a4dced2b1b31d45ba3f/lru-dict-1.1.6.tar.gz
Collecting empyrical>=0.5.0 (from zipline)
Using cached https://files.pythonhosted.org/packages/7b/55/a01b05162b764830dbbac868462f44cd847a5b6523a01ca9f955721819da/empyrical-0.5.0.tar.gz
Collecting tables>=3.3.0 (from zipline)
Using cached https://files.pythonhosted.org/packages/d7/1b/21f4c7f296b718575c17ef25e61c05742a283c45077b4c8d5a190b3e0b59/tables-3.4.4-cp36-cp36m-manylinux1_x86_64.whl
Collecting trading-calendars>=1.0.1 (from zipline)
Using cached https://files.pythonhosted.org/packages/6e/e9/e9e2aeadacab58580ed9493e6c252f051cd8ee9d7efbe49d2f041371c45d/trading_calendars-1.5.1.tar.gz
Requirement already satisfied, skipping upgrade: wrapt in ~/.local/lib/python3.6/site-packages (from pandas-datareader>=0.2.1->zipline) (1.10.11)
Requirement already satisfied, skipping upgrade: lxml in ~/.local/lib/python3.6/site-packages (from pandas-datareader>=0.2.1->zipline) (4.2.5)
Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in ~/.local/lib/python3.6/site-packages (from requests>=2.9.1->zipline) (2018.10.15)
Requirement already satisfied, skipping upgrade: idna<2.8,>=2.5 in ~/.local/lib/python3.6/site-packages (from requests>=2.9.1->zipline) (2.7)
Requirement already satisfied, skipping upgrade: chardet<3.1.0,>=3.0.2 in /usr/lib/python3/dist-packages (from requests>=2.9.1->zipline) (3.0.4)
Requirement already satisfied, skipping upgrade: urllib3<1.25,>=1.21.1 in ~/.local/lib/python3.6/site-packages (from requests>=2.9.1->zipline) (1.24.1)
Collecting python-editor>=0.3 (from alembic>=0.7.7->zipline)
Using cached https://files.pythonhosted.org/packages/65/1e/adf6e000ea5dc909aa420352d6ba37f16434c8a3c2fa030445411a1ed545/python-editor-1.0.3.tar.gz
Building wheels for collected packages: zipline, Logbook, cyordereddict, bottleneck, bcolz, sqlalchemy, alembic, intervaltree, lru-dict, empyrical, trading-calendars, python-editor
Running setup.py bdist_wheel for zipline ... done
Stored in directory: ~/.cache/pip/wheels/a4/d6/67/f303ab028b004bf8e00c05b5b04fba83d8ec238b6547becdb7
Running setup.py bdist_wheel for Logbook ... done
Stored in directory: ~/.cache/pip/wheels/06/13/e9/88e9e8184d89671ffc754dc80f5eb01dabd72071bdb802c5d1
Running setup.py bdist_wheel for cyordereddict ... done
Stored in directory: ~/.cache/pip/wheels/0b/9d/8b/5bf3e22c1edd59b50f11bb19dec9dfcfe5a479fc7ace02b61f
Running setup.py bdist_wheel for bottleneck ... done
Stored in directory: ~/.cache/pip/wheels/f2/bf/ec/e0f39aa27001525ad455139ee57ec7d0776fe074dfd78c97e4
Running setup.py bdist_wheel for bcolz ... done
Stored in directory: ~/.cache/pip/wheels/c5/cc/1b/2cf1f88959af5d7f4d449b7fc6c9452d0ecbd86fd61a9ee376
Running setup.py bdist_wheel for sqlalchemy ... done
Stored in directory: ~/.cache/pip/wheels/ba/2e/47/b194b03e6aeaa658f521dfba094a42f1d37f4ee4018b9e607e
Running setup.py bdist_wheel for alembic ... done
Stored in directory: ~/.cache/pip/wheels/95/f3/c2/22155963496514f044049f5c96e2757b9838da4a5cae34e76e
Running setup.py bdist_wheel for intervaltree ... done
Stored in directory: ~/.cache/pip/wheels/6b/cf/b0/f7ef2d0f504d26f3e9e70c2369e5725591ccfaf67d528fcbc5
Running setup.py bdist_wheel for lru-dict ... done
Stored in directory: ~/.cache/pip/wheels/b7/ef/06/fbdd555907a7d438fb33e4c8675f771ff1cf41917284c51ebf
Running setup.py bdist_wheel for empyrical ... done
Stored in directory: ~/.cache/pip/wheels/83/14/73/34fb27552601518d28bd0813d75124be76d94ab29152c69112
Running setup.py bdist_wheel for trading-calendars ... done
Stored in directory: ~/.cache/pip/wheels/13/0f/d5/517a7a51d9998c20483d777bf2e35e71ec17445509a9ac4cc1
Running setup.py bdist_wheel for python-editor ... done
Stored in directory: ~/.cache/pip/wheels/36/e0/98/ba386b125a00ea9dd52e2c16aa2ec0adbbd639b84bfe2e001d
Successfully built zipline Logbook cyordereddict bottleneck bcolz sqlalchemy alembic intervaltree lru-dict empyrical trading-calendars python-editor
pyro-ppl 0.2.1+fbaa779 has requirement networkx>=2.0.0, but you'll have networkx 1.11 which is incompatible.
pyro-ppl 0.2.1+fbaa779 has requirement torch==0.4.0, but you'll have torch 1.0.0a0+6c8d47f which is incompatible.
Installing collected packages: Logbook, pandas, pandas-datareader, statsmodels, cyordereddict, bottleneck, networkx, numexpr, bcolz, sqlalchemy, python-editor, alembic, sortedcontainers, intervaltree, lru-dict, empyrical, tables, trading-calendars, zipline
Found existing installation: pandas 0.23.4
Uninstalling pandas-0.23.4:
Successfully uninstalled pandas-0.23.4
Found existing installation: networkx 2.2
Uninstalling networkx-2.2:
Successfully uninstalled networkx-2.2
Successfully installed Logbook-1.4.1 alembic-1.0.2 bcolz-0.12.1 bottleneck-1.2.1 cyordereddict-1.0.0 empyrical-0.5.0 intervaltree-2.1.0 lru-dict-1.1.6 networkx-1.11 numexpr-2.6.8 pandas-0.22.0 pandas-datareader-0.7.0 python-editor-1.0.3 sortedcontainers-2.0.5 sqlalchemy-1.2.13 statsmodels-0.9.0 tables-3.4.4 trading-calendars-1.5.1 zipline-1.3.0
➜ ~

You'll possibly meet these ERROR messages:

  • pyro-ppl 0.2.1+fbaa779 has requirement networkx>=2.0.0, but you'll have networkx 1.11 which is incompatible.
  • pyro-ppl 0.2.1+fbaa779 has requirement torch==0.4.0, but you'll have torch 1.0.0a0+6c8d47f which is incompatible.

In such a case, pyro is to be manually installed.

1.4 Manually Install pyro

Please refer to https://github.com/uber/pyro:

1
2
3
4
➜  uber git clone https://github.com/uber/pyro.git
cd pyro
➜ pyro git:(dev) python setup.py build
➜ pyro git:(dev) python setup.py install --user

1.5 Install Zipline Again

Please refer to http://www.zipline.io/install.html:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
➜  ~ pip install -U zipline --user
Collecting zipline
Requirement already satisfied, skipping upgrade: numexpr>=2.6.1 in ./.local/lib/python3.6/site-packages (from zipline) (2.6.8)
Requirement already satisfied, skipping upgrade: cyordereddict>=0.2.2 in ./.local/lib/python3.6/site-packages (from zipline) (1.0.0)
Requirement already satisfied, skipping upgrade: tables>=3.3.0 in ./.local/lib/python3.6/site-packages (from zipline) (3.4.4)
Requirement already satisfied, skipping upgrade: pytz>=2016.4 in ./.local/lib/python3.6/site-packages (from zipline) (2018.7)
Requirement already satisfied, skipping upgrade: scipy>=0.17.1 in ./.local/lib/python3.6/site-packages (from zipline) (1.1.0)
Collecting bcolz<1,>=0.12.1 (from zipline)
Requirement already satisfied, skipping upgrade: patsy>=0.4.0 in ./.local/lib/python3.6/site-packages (from zipline) (0.5.1)
Requirement already satisfied, skipping upgrade: empyrical>=0.5.0 in ./.local/lib/python3.6/site-packages (from zipline) (0.5.0)
Requirement already satisfied, skipping upgrade: setuptools>18.0 in ./.local/lib/python3.6/site-packages (from zipline) (40.5.0)
Requirement already satisfied, skipping upgrade: requests-file>=1.4.1 in ./.local/lib/python3.6/site-packages (from zipline) (1.4.3)
Requirement already satisfied, skipping upgrade: requests>=2.9.1 in ./.local/lib/python3.6/site-packages (from zipline) (2.20.0)
Requirement already satisfied, skipping upgrade: intervaltree>=2.1.0 in ./.local/lib/python3.6/site-packages (from zipline) (2.1.0)
Requirement already satisfied, skipping upgrade: MarkupSafe>=0.23 in ./.local/lib/python3.6/site-packages (from zipline) (1.1.0)
Requirement already satisfied, skipping upgrade: contextlib2>=0.4.0 in ./.local/lib/python3.6/site-packages/contextlib2-0.5.5-py3.6.egg (from zipline) (0.5.5)
Requirement already satisfied, skipping upgrade: sqlalchemy>=1.0.8 in ./.local/lib/python3.6/site-packages (from zipline) (1.2.13)
Requirement already satisfied, skipping upgrade: toolz>=0.8.2 in ./.local/lib/python3.6/site-packages (from zipline) (0.9.0)
Requirement already satisfied, skipping upgrade: six>=1.10.0 in ./.local/lib/python3.6/site-packages (from zipline) (1.11.0)
Requirement already satisfied, skipping upgrade: pandas-datareader>=0.2.1 in ./.local/lib/python3.6/site-packages (from zipline) (0.7.0)
Requirement already satisfied, skipping upgrade: numpy>=1.11.1 in ./.local/lib/python3.6/site-packages (from zipline) (1.15.4)
Collecting pandas<=0.22,>=0.18.1 (from zipline)
Using cached https://files.pythonhosted.org/packages/da/c6/0936bc5814b429fddb5d6252566fe73a3e40372e6ceaf87de3dec1326f28/pandas-0.22.0-cp36-cp36m-manylinux1_x86_64.whl
Requirement already satisfied, skipping upgrade: click>=4.0.0 in ./.local/lib/python3.6/site-packages (from zipline) (7.0)
Requirement already satisfied, skipping upgrade: Mako>=1.0.1 in /usr/lib/python3/dist-packages (from zipline) (1.0.7)
Requirement already satisfied, skipping upgrade: statsmodels>=0.6.1 in ./.local/lib/python3.6/site-packages (from zipline) (0.9.0)
Requirement already satisfied, skipping upgrade: bottleneck>=1.0.0 in ./.local/lib/python3.6/site-packages (from zipline) (1.2.1)
Requirement already satisfied, skipping upgrade: Logbook>=0.12.5 in ./.local/lib/python3.6/site-packages (from zipline) (1.4.1)
Collecting networkx<2.0,>=1.9.1 (from zipline)
Using cached https://files.pythonhosted.org/packages/d3/2c/e473e54afc9fae58dfa97066ef6709a7e35a1dd1c28c5a3842989322be00/networkx-1.11-py2.py3-none-any.whl
Requirement already satisfied, skipping upgrade: alembic>=0.7.7 in ./.local/lib/python3.6/site-packages (from zipline) (1.0.2)
Requirement already satisfied, skipping upgrade: sortedcontainers>=1.4.4 in ./.local/lib/python3.6/site-packages (from zipline) (2.0.5)
Requirement already satisfied, skipping upgrade: lru-dict>=1.1.4 in ./.local/lib/python3.6/site-packages (from zipline) (1.1.6)
Requirement already satisfied, skipping upgrade: multipledispatch>=0.4.8 in ./.local/lib/python3.6/site-packages (from zipline) (0.6.0)
Requirement already satisfied, skipping upgrade: pip>=7.1.0 in ./.local/lib/python3.6/site-packages (from zipline) (18.1)
Requirement already satisfied, skipping upgrade: decorator>=4.0.0 in ./.local/lib/python3.6/site-packages (from zipline) (4.3.0)
Requirement already satisfied, skipping upgrade: Cython>=0.25.2 in ./.local/lib/python3.6/site-packages (from zipline) (0.29)
Requirement already satisfied, skipping upgrade: python-dateutil>=2.4.2 in ./.local/lib/python3.6/site-packages (from zipline) (2.7.5)
Requirement already satisfied, skipping upgrade: trading-calendars>=1.0.1 in ./.local/lib/python3.6/site-packages (from zipline) (1.5.1)
Requirement already satisfied, skipping upgrade: certifi>=2017.4.17 in ./.local/lib/python3.6/site-packages (from requests>=2.9.1->zipline) (2018.10.15)
Requirement already satisfied, skipping upgrade: urllib3<1.25,>=1.21.1 in ./.local/lib/python3.6/site-packages (from requests>=2.9.1->zipline) (1.24.1)
Requirement already satisfied, skipping upgrade: chardet<3.1.0,>=3.0.2 in /usr/lib/python3/dist-packages (from requests>=2.9.1->zipline) (3.0.4)
Requirement already satisfied, skipping upgrade: idna<2.8,>=2.5 in ./.local/lib/python3.6/site-packages (from requests>=2.9.1->zipline) (2.7)
Requirement already satisfied, skipping upgrade: wrapt in ./.local/lib/python3.6/site-packages (from pandas-datareader>=0.2.1->zipline) (1.10.11)
Requirement already satisfied, skipping upgrade: lxml in ./.local/lib/python3.6/site-packages (from pandas-datareader>=0.2.1->zipline) (4.2.5)
Requirement already satisfied, skipping upgrade: python-editor>=0.3 in ./.local/lib/python3.6/site-packages (from alembic>=0.7.7->zipline) (1.0.3)
pyro-ppl 0.2.1+913618a has requirement networkx>=2.0.0, but you'll have networkx 1.11 which is incompatible.
Installing collected packages: bcolz, pandas, networkx, zipline
Found existing installation: bcolz 1.2.1
Uninstalling bcolz-1.2.1:
Successfully uninstalled bcolz-1.2.1
Found existing installation: pandas 0.23.4
Uninstalling pandas-0.23.4:
Successfully uninstalled pandas-0.23.4
Found existing installation: networkx 2.2
Uninstalling networkx-2.2:
Successfully uninstalled networkx-2.2
Found existing installation: zipline 1.0.1+1816.gb7f454b7.dirty
Uninstalling zipline-1.0.1+1816.gb7f454b7.dirty:
Successfully uninstalled zipline-1.0.1+1816.gb7f454b7.dirty
Successfully installed bcolz-0.12.1 networkx-1.11 pandas-0.22.0 zipline-1.3.0
➜ ~ pip show networkx
Name: networkx
Version: 1.11
Summary: Python package for creating and manipulating graphs and networks
Home-page: http://networkx.github.io/
Author: NetworkX Developers
Author-email: networkx-discuss@googlegroups.com
License: BSD
Location: /home/jiapei/.local/lib/python3.6/site-packages
Requires: decorator
Required-by: zipline, scikit-image, pyro-ppl
➜ ~ pip show pyro-ppl
Name: pyro-ppl
Version: 0.2.1+913618a
Summary: A Python library for probabilistic modeling and inference
Home-page: http://pyro.ai
Author: Uber AI Labs
Author-email: pyro@uber.com
License: MIT License
Location: /home/jiapei/.local/lib/python3.6/site-packages/pyro_ppl-0.2.1+913618a-py3.6.egg
Requires: contextlib2, graphviz, networkx, numpy, opt-einsum, six, torch, tqdm
Required-by:
➜ ~

1.6 Python Package Compatibility

Let's take a look at the compatibility of python packages.

1
2
3
4
5
6
➜  ~ pip list --outdated
Package Version Latest Type
---------- --------- ------ -----
bcolz 0.12.1 1.2.1 sdist
networkx 1.11 2.2 sdist
pandas 0.22.0 0.23.4 wheel

Clearly, Zipline is NOT compatible with the above 3 LATEST python packages.

2. Register Quandl and Ingest Data

As known, Quantopian is a Python collections of various quantitative analysis algorithms. However, where can we obtain the stock data for our testing? Either the real-time data, or the historical data will do.

We can of course use the real-time Yahoo Finance Data. But here, we are going to test on some historical data downloaded from Quandl, which is JUST the data adopted in Quantopian's Beginner's Tutorial.

After you've successfully registered Quandl and obtained an API key, the following command will help you to ingest Quandl WIKI Bundle data easily, please refer to http://www.zipline.io/bundles.html#ingesting-data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
➜  quantopian QUANDL_API_KEY=<yourkey> zipline ingest -b quandl
[2018-11-07 05:18:37.895463] INFO: zipline.data.bundles.quandl: Downloading WIKI metadata.
Downloading WIKI Prices table from Quandl [####################################] 100%
[2018-11-07 05:18:54.369791] INFO: zipline.data.bundles.quandl: Parsing raw data.
[2018-11-07 05:19:24.118879] INFO: zipline.data.bundles.quandl: Generating asset metadata.
Merging daily equity files: [-----------------------------#------] 1731~/.local/lib/python3.6/site-packages/zipline/data/us_equity_pricing.py:417: UserWarning: Ignoring 1 values because they are out of bounds for uint32: open high low close volume ex_dividend split_ratio
2011-04-11 1.79 1.84 1.55 1.7 6.674913e+09 0.0 1.0
winsorise_uint32(raw_data, invalid_data_behavior, 'volume', *OHLC)
Merging daily equity files: [####################################]
[2018-11-07 05:21:34.367628] INFO: zipline.data.bundles.quandl: Parsing split data.
[2018-11-07 05:21:34.572459] INFO: zipline.data.bundles.quandl: Parsing dividend data.
~/.local/lib/python3.6/site-packages/zipline/data/us_equity_pricing.py:931: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
if not issubdtype(actual, expected):
➜ quantopian

3. First Try

Details can be easily found at http://www.zipline.io/beginner-tutorial.html. Here, we ONLY test the example ./zipline/examples/buyapple.py, which can also be found on Github.

Following the Beginner's Tutorial, by executing the following command,

1
➜  examples git:(master) ✗ zipline run -f ./buyapple.py --start 2016-1-1 --end 2018-1-1 -o buyapple_out.pickle

we should be able to obtain the resultant figure as follows:

Quantopian Example - Buy Apple