Building a Google Compute Engine interface from start to finish.

Photo by Yuvy Dhaliah on Unsplash

Why Google Compute Engine?

In my previous post about using Heroku to host your Alpaca algorithm, I explained some of the advantages of using a Cloud solution over using a local computer.

Sometimes, the algorithm needs more power or memory than one can get with Heroku. Google’s Compute Engine will provide you with a separate virtual machine (VM) which can be set up to run algorithm code even as it becomes more complex. Additionally, having a virtual machine on the cloud means that you can access your virtual machine and have full control from anywhere you have an internet connection or from your phone using the Cloud Console app.

Sign up for an account

Go to https://cloud.google.com/compute/ . Click “TRY IT FREE”

Then log in to your Google account. (If you have a Gmail account it’s the same) Enter your Country and Accept the Terms of Service.

Next, enter your account information. The service will require a credit card to verify that you are a real person but will not charge you.

After clicking Start my free trial, you will see the main console.

Select Compute Engine.

Next, select Enable billing.

At this point the service will need some time to set up the workspace. But when it is ready click Create.

Next, enter a name for the app. Select a region for the host. Under Machine type I am going for a g1-small type but individual needs may require other types.

For this tutorial I am selecting Ubuntu 18.04 LTS for the Boot disk.

After the instance builds, click the SSH button next to your new instance to launch a terminal inside your new virtual machine.

The terminal may take a moment to load

Alright, now that the virtual machine is created, the next step is to build the interface to Alpaca. For this example, I am building an interface for my Alpaca Paper account. The process would be the same for the Live account. Make a folder for the files:

$ mkdir AlpacaPaper

$ cd AlpacaPaper

Next, load in pylivetrader from the git repository.

Move in the example algorithm that comes with pylivetrader. (After building a working example here, replace this algorithm, algo.py , with your own recipe.)

$ cd pylivetrader

$ cd examples

$ cd q01

$ mv algo.py ~/AlpacaPaper/pylivetrader

$ cd ..

$ cd ..

The next step is to get Docker installed on the virtual machine:

$ sudo apt-get update

$ sudo apt install docker.io

Enter [Y] for yes to continue

Alpaca will require your key ID and secret ID. You can obtain these in your Alpaca Dashboard.

Edit: Because of a quirk with how Compute Engine does environmental variables, the best option is to embed these keys within your algorithm script. Add the appropriate keys to algo.py by using the command line text editor Vim which is pre-loaded with the virtual machine.

$ vim algo.py

In Vim, press the i button to enter “Insert Mode”. Enter your Alpaca key ID, secret Key, and base URL somewhere near to top of the script like this:

After entering this data, press the Escape key to exit editing and type

:wq

Which will save the contents of algo.py and exit Vim.

Now, launch Docker and run the algo.py . This is all one line.

$ sudo docker run -d --restart unless-stopped -v $PWD:/work -w /work alpacamarkets/pylivetrader pylivetrader run -f algo.py

The use of -d will daemonize the docker container, meaning that this command will run in the background meaning that even if we now close the terminal, the program will continue to run on the virtual machine. The use of --restart unless-stopped will automatically restart the container if there are any unexpected crashes.

Congratulations. The virtual machine is built, pylivetrader is loaded, and your unique key ID and secret ID have been set up in the machine, and the program is running.

You can check on the logs of your script by accessing the log from docker:

$ sudo docker logs $(sudo docker ps -aq)

Updates

Because docker is running the container as a daemon, the process is running in the background now. Making changes means that the old process needs to be aborted prior to launching a new process to avoid having multiple processes each talking to the account.

Stop the docker and remove any instances:

$ sudo docker stop $(sudo docker ps -aq)

$ sudo docker rm $(sudo docker ps -aq)

Now you can update your algorithm. Replace algo.py in the pylivetrader folder with your own recipe then use the same line from above to launch the update.