Like many i used to manually watch and trade stocks, ETFs. Its non-productive and involves our emotions in trading decisions.
So I wanted to automate trading. Initially used Trailing Stop and Limit orders. But they can automate only in a prescribed way.
I started thinking about a way to trade programatically, where we have more control.
My quest pointed to Zipline, a python algorithmic trading library. It support backtesting (testing algorithm with historic data) and most importantly supports live trading
In this blog, I will walk you through my algorithmic trading journey.
Zipline Installation
From https://www.zipline.io/index.html, I see it only supports python 2.7 or Python 3.5
Since python 2 was sun set on Jan 1 2020, there is no point in trying it. I went ahead with Python 3.
For programming, installed latest Ubuntu 19.10 in a virtual machine . Ubuntu 19.10 comes bundled with Python 3.7.5.
You can check your python version by typing
python3 -V
in terminal.
Since we needed python 3.5, I installed it using
sudo apt-get install python3.5
It installed python 3.5 but broken some of the system processes as it overridden system default version of 3.7.5
There is a way to install safely using altinstall method, but that doesnt keep dependencies messing up system down the line.
Later figured out that we can use pyenv to safely install multiple version of python and use python virtual environments to make sure dependencies of one project dont mess up with other project.
Installing pyenv
In a terminal, execute below commands one after other
1. sudo
apt update && sudo apt upgrade
2. sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl
3. sudo apt install git
4. curl https://pyenv.run | bash
Last step should give a warning to update .bashrc file.
As per instructions edit your .bashrc and append new statements
# Load pyenv automatically by adding
# the following to ~/.bashrc:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"
Next close and restart shell to make the settings taken into effect.
Now pyenv is installed in your home directory named .pyenv
you can verify it using
which pyenv
This should show full path to pyenv location where its installedInstalling Python 3.5 (or any version)
Using pyenv, we can safely install multiple python versions.
pyenv install -v 3.5.9
This will take some time as pyenv installs 3.5.9 from the source code. Once installation is done, you can verify it using
pyenv versions
This command will list system version and the newly installed 3.5.9
Each newly created version of python can be located at ~/.pyenv/versions/
Creating a virtual environment for Zipline
Using a virtual environment, we can make sure a project is executed using a specific version of Python and use specific dependencies, with out messing up Operating system or other projects.
pyenv virtualenv 3.5.9 AlgTrd_zipline
This will create a folder under ~/.pyenv/versions/3.5.9/envs/AlgTrd_zipline
next navigate to this folder and type
pyenv local AlgTrd_zipline
This will make sure any code executed from this folder uses python 3.5.9 only.
We now completed proper installation of python 3.5 and created a virtual environment for zipline.
if you want to understand why we done what we done, refer tutorial on pyenv at https://realpython.com/intro-to-pyenv/
update pip using pip install --upgrade pip
Installing Zipline
Installing zipline requires some dependencies.
In the terminal navigate to your home directory and use below code
sudo apt-get install libatlas-base-dev gfortran pkg-config libfreetype6-dev libhdf5-serial-dev python3-pip
Next, in terminal navigate to your virtual environment
cd ~/.pyenv/versions/3.5.9/envs/AlgTrd_zipline
next use below commands to install zipline
p
pip3 install numpy
pip3 install cython
pip3 install -U setuptools
pip3 install zipline
It will take few minutes. Once done you can verify it by starting python terminal and importing zipline
python3
import zipline
Here if there are no errors, you successfully installed Zipline
using
exit()
to exit from python interpreter.
Comments