Skip to main content

My algorithmic Trading journey


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 installed

Installing 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.

NEXT UP.. Running a sample algorithm to back test my theory

Comments

Popular posts from this blog

Why Chiru?

I don't want to say Chiru will do wonders. But he is not mastered in looting the people like others. He is not an experienced Politician .Its a great Qualification. One of my friend raised an objection here saying that " being in-experienced politican how its going to be a great qualification and do you thinking that all current politicans are bad" i am accepting his comments and removing a few words from my article.... He is a new comer into this field, so by the time he learns how to do all these things, it will take some time. As a fresher, he will may try to do some thing good for till he get defeated. By the track record of Chiru, i don't think, he will get defeated. Just because he became No.1 in a field which similarly ruled on the base of caste. But he conquered it not just with Charishma and acting. He know how to win. Another comment is how chiru's past track record in Movie field will guarentee success in completely different "Political field...

Investing in Gold

Like many, I keep hearing about looming recession and Gold as a hedge. But how exactly gold is going to help me and if yes, how to invest is something running behind my mind.  So here is my work on it and hope it helps you too. Why Invest in Gold ?  Fiat currency we are holding is loosing its value at the rate of inflation. But before fiat currency, gold is the standard. Based on current technology, its difficult to extract gold. so in the demand vs supply equation, supply cant balloon like central banks printing money at their will. so i dont need to worry about Gold loosing its value over time. On Supply part, As per World Gold Council estimate 190,040 metric tones extracted and another 54000 metric can be extracted from ground. Refer :  https://www.gold.org/about-gold/gold-supply/gold-mining/how-much-gold On the Demand part, Below are the key drivers of Gold demand Source: Thomson Reuters GFMS, World Gold Council As an investment, From individu...

Manual to living a Human Life - How to poop properly

Years ago, I had a question "is there a manual explaining how to use my (Human) body? " Pursuit of this question send me in different directions. In this post, I will talk about "How to poop properly?" what is the right way ? Before get into it, Why now ? My daughter going through potty training. She naturally prefers Indian style full squat. This initially puzzled me as she newer knew about this way and also this is against what she is seeing / observing in Potty training videos and what is being taught at school. Being born in India, crouching is the way for us. Now being in western world, forcing my daughter against her natural will, makes me feel guilt. So i started searching about what is the right thing to do. Came across  Dov Sikirov who researched on Human Digestive diseases. In his 2013 article (refer  https://link.springer.com/article/10.1023/A:1024180319005 ) he studied straining forces applied when sitting or squatting dur...