Photo by Paul Csogi on Unsplash

Why the Cloud?

The basic idea of algorithmic trading is that you can set rules to perform all of the functions of stock trading: analyzing candidates, portfolio construction, trade sizing, and setting the actual buy orders and sell orders. While you can run these commands from your own personal computer running python, there are potential problems:

The local computer can crash

The local computer can have a power outage

The local computer can have an internet outage

Your kid can pour orange juice on the local computer.

These problems can be catastrophic to a portfolio if you cannot issue buy and especially sell commands at the appropriate time.

Moving the algorithm to the cloud means that the above problems are someone else’s headache.

Heroku

Heroku is an online application platform. It will allow you to move the python app to the cloud, most often for free.

These are the steps I used to successfully get Heroku to trade for me.

Sign up for Heroku.

The first step is to create a Free Heroku account and Log in.

In the Dashboard, click “New” then “Create new app”

Supply a unique name for your app:

Click “Create app.” Next open a Command line. For this example, I’m using Windows so type cmd in the Windows search bar and open Command Prompt. (You may need to open Command Prompt as an administrator.)

Make a folder.

>mkdir Algorithms

>cd Algorithms

Then download pylivetrader into the folder (You need to have GIT installed)

git clone https://github.com/alpacahq/pylivetrader \Algorithms>

Some files need to be moved around. Windows File explorer is probably the easiest way of doing this:

Navigate to the pylivetrader/examples/q01 directory and move algo.py , Pipfile , Procfile and Dockerfile to the main pylivetrader directory.

From a text editor or IDE, edit the Procfile in the main pylivetrader directory.

Set contents to:

worker: pylivetrader run -f algo.py --backend-config config.yaml

Next, you have to add your unique keys from Alpaca.

Get these keys from your Alpaca account. Copy and paste these values.

You obtain your unique key_id and secretkey from your Alpaca Account Dashboard

Create a file called config.yaml Set the contents to:

Be sure to replace the <> with the supplied keys leaving just the characters of the keys in their place. Now, the above is for Alpaca’s paper trading API. The Live trading market has a different address for base_url and will have distinct key_id and secret key values from your paper trading keys.

algo.py contains the main algorithm — the heart of the actual trading code. Replace this algorithm with your own recipe.

Now that the files are ready, the next step is to ship them to Heroku and then turn on the app. Heroku CLI will need to be installed for these commands to work. The Heroku Getting Started documentation will walk through how to install the Heroku CLI.

Once Heroku CLI is installed, in the Command Prompt type:

\pylivetrader>heroku login

\pylivetrader>heroku git:remote -a happy-man

\pylivetrader>git add .

\pylivetrader>git commit -am "initial commit"

\pylivetrader>git push heroku master

Where happy-man is the name of the app supplied to Heroku above.

There is a bug in pylivetrader so that it will not build correctly unless files are moved from /dockerfiles into the main pylivetrader folder at this point.

Move all the contents of /dockerfiles into the main pylivetrader and replace any copies.

Update Heroku

\pylivetrader>git add --all

\pylivetrader>git commit -m "docker"

\pylivetrader>git push heroku master

Now, set the key_id, secret key, and base_url as environmental variables for inside Heroku.

\pylivetrader>heroku config:set APCA_API_KEY_ID=<ReplaceWithSuppliedKey> \pylivetrader>heroku config:set APCA_API_SECRET_KEY=<ReplaceWithSuppliedSecKey> \pylivetrader>heroku config:set APCA_API_base_url=https://paper-api.alpaca.markets

There should be no spaces before or after the = . Now the app is built and pushed onto the Heroku server and the environmental variables are set. Just need to turn it on. Heroku calls this “scaling.”

\pylivetrader>heroku ps:scale worker=1

Congratulations.

Check on the status with:

\pylivetrader>heroku logs --tail

Updates

To make updates to the program, save the modified algo.py file inside the pylivetrader folder.

Then go back to the Command Prompt. (make sure you are inside the pylivetrader folder.)