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

Netbeans 6.0 - Code generation

The Java editor is capable of generating often used constructs for you automatically. Press Alt+Insert to invoke the code generation menu and pick what you want to generate To add import statements: Use error hints : Click on the error mark with the light bulb or press Alt+Enter . It will offer you a list of possible classes to import. Pick the proper hint and you are done Use the import class dialog : Put the caret into the name of an unimported class and press Alt+Shift+I . A list of possible classes to import will appear. Use the smart fix import : Press Ctrl+Shift+I . You will get a dialog that lists all unresolved identifiers in the source. If there is more than one option for resolving the identifier you may choose using the combo box. Classes shown in gray do not fit for some reason. Method exit points : Putting the caret on the return type of a method definition will highlight all places where the method can exit Exception throwing points : Putting the caret on an...

Artist got automated

Here is my first Tensorflow run to convert images to different styles of famous painters I am using the code that is available at  https://github.com/lengstrom/fast-style-transfer It uses specific versions of below libraries !apt install ffmpeg !pip install tensorflow==0.12.1 !pip install numpy==1.11.2 !pip install scipy==0.18.1 !pip install pillow==3.4.2 I started with latest version and that triggered failure after failure. If you are like me, add below changes to their respective files #This is to make tensorflow behave as if it is in v1 File : evaluate.py and ./src/transform.py import tensorflow.compat.v1 as tf tf.disable_v2_behavior() #Image functions are removed from scipy.misc module. so use imageio instead import imageio scipy.misc.imsave(out_path, img) --> imageio.imwrite(out_path, img) img = scipy.misc.imread(src, mode='RGB') --> img = imageio.imread(src, pilmode='RGB') Refer this post on more details on imageio changes Here is the ...

OnePlus 6T - Android 10 (OOS 10)

Its been more than 3 months since August security patch update. Out of curiosity I checked Oxygen Updater. To my surprise, Oxygen OS v 10 (based on Android 10) is available. Since this is a stable release, i went head and updated it. Its ended up being a buggy release. For those of you, who didnt get update over OTA, its better to wait for OOS 10.1. Here are the list of issue and the workarounds. Issue #1 :  Phone randomly restarts and then goes to boot loop Workaround : when the phone stuck in boot loop (with OnePlus logo animation), Press Power Button and Volume Down button to restart it Issue #2 : Google play wont work Work Around : Booting to recovery and clearing cache is expected to fix this. will update after testing To booting to recovery, Firstly, turn off your phone: press and hold the Power button for a while or press the Power key once and choose ‘power off’. Once the switch off process is completed start pressing and also holding down the Volume Dow...