Live Algo Trading on the Cloud – Google Cloud Platform

9 min read

Get 10-day Free Algo Trading Course

Loading

Last Updated on November 25, 2023

Table of contents:

  1. What does live algorithmic trading on the Cloud mean?
  2. Rerequisite – Basic Guide
  3. What are the pros of deploying your trading strategies to the Cloud?
  4. What are the cons of deploying your trading strategies to the Cloud?
  5. What is the Cloud Service?
  6. What is the Cloud used for? 
  7. What cloud providers are good?
  8. What is the Google Cloud Platform?
  9. Why should I use Google Cloud?
  10. Why shouldn’t I use Google Cloud?
  11. How to get started with Google Cloud?
  12. Kraken Bot Trading Algorithm
  13. What is Docker?
  14. How to package a trading algorithm with Docker?
  15. What is Google Artifact Registry?
  16. How to push a Docker image to Google Artifact Registry?
  17. What is Google Cloud Run?
  18. How to configure Google Cloud Run?
  19. Where can I learn more?
  20. Full code

What does live algorithmic trading on the Cloud mean?

Live algorithmic trading on the Cloud means that your trading bots can use the cloud provider’s resources to run 24/7 while being efficiently maintainable.

This is article, we will demonstrate how to deploy a live algorithmic trading strategy to the cloud server using Google Cloud Platform and Docker.

Prerequisite – Basic Guide

If you are new to deploying strategies to the cloud, do check out our basic guide using AWS.

We covered how to:

  • Choose a location for your server
  • Input your secret keys
  • Deploy a trading script using Github
  • Set Telegram notifications when trades and key events happen

What are the pros of deploying your trading strategies to the Cloud?

  • Cloud services are easily available– they allow the user to access them at any time, from anywhere, and from almost any device.
  • Can lower costs – cloud computing can lower the costs and provide you with a good connection and hardware that saves you money in the long run.
  • Easy to maintain – you can upload your trading strategies to the cloud and get them running 24/7 with the minimal endeavor.
  • Is scalable – you can store almost limitless amounts of data in the cloud.
  • Easy to set up – setting up your cloud server is quite easy.
  • Has many solutions – the cloud services are upping their game by creating specialized services for almost anything you could need from blockchain to data pipelines and app runners.

What are the cons of deploying your trading strategies to the Cloud?

  • Security risks – your data is being stored by a third-party provider and if a security breach happens your data is at high risk.
  • Depends on the internet connection – even though this is the same case when running trading strategies by yourself, the cloud service provider can also suffer from downtime and other natural risks.
  • Data confidentiality risk – your privacy is of utmost importance and when using a third-party cloud provider you can’t be too sure that you’re the only one accessing your data or being able to see it.
  • It can get costly – sometimes engineers mess up by not exploring the costs and settings alarms, safety nets, and/or writing optimal code which can result in surprising bills.

What is the Cloud Service?

A Cloud Service delivers cloud computing as a service with an intent to provide affordable, easy, and efficient access to diverse resources without the need to have your own hardware or infrastructure.

What is Cloud used for?

Cloud services have many purposes for which they could be used for and here are some of the most common ones:

  • Algorithmic trading – cloud services allow algorithmic traders to run multiple bots that trade the markets.
  • Big data storage – cloud services offer an almost limitless storage capacity which is great when dealing with big data.
  • Backup and recovery – clouds can be frequently backed up and you can easily recover data from them. They also offer good disaster recovery.
  • Testing and Development – cloud services are great when testing and developing your applications and trading strategies.
  • Model training – when doing machine learning work, model training can take up to several months, and running it on a cloud can save you computing power and in some cases even money.

What cloud providers are good?

There are many cloud services and picking a quality one is important. In order to make your decisions process easier I will mention a few that are on top of their cloud game:

What is the Google Cloud Platform?

The Google Cloud Platform is a suite of cloud computing services that runs on the same infrastructure that Google uses internally for its end-user products (i.e. YouTube). It offers a lot of solutions such as cloud-based storage, computing resources, security, and more.

Why should I use Google Cloud?

  • Google Cloud is easy to use
  • Google Cloud has a large community
  • Is in the top 5 cloud service providers
  • Has a good track record
  • Offers easy collaboration between users

Why shouldn’t I use Google Cloud?

  • Google Cloud can be confusing for beginners
  • Google Cloud has fewer data centers when compared to some alternatives
  • It can be messy to move your infrastructure from Google Cloud to another cloud provider
  • Has a complex pricing schema when compared to alternatives
  • Can often be more pricey than its alternatives

How to get started with Google Cloud?

To get started with Google Cloud, you will need to open an account to access cloud resources. To do this, go over to their main cloud webpage and click the blue “Get started for free” button in the middle part of your screen.

This will open up a new page where you will be asked from which country you’re from and for what use cases will you use Google Cloud. After answering these questions and agreeing to their terms, click the blue “Continue” button.

The second step will ask you to provide your payment information so that Google can confirm that you aren’t a robot. They won’t charge you anything as we are setting up a free account and getting free $300 worth of credits that last 90 days.

After that, we can choose to upgrade our account to the premium tier after which we start to get charged. When you are done adding in your payment data, click the blue “START MY FREE TRIAL” button to proceed to the Google Cloud Platform.

In the following headers, we will explore how to deploy a live Kraken exchange trading algorithm to Google Cloud. The way that we’ll do this involves the use of Docker and Cloud Run.

For a similar Docker way of doing this, visit our Microsoft Azure article, and for a more manual way, we have the AWS article.

All three ways can be used on all three platforms as all of them have the same services with different, and sometimes similar, names.

Kraken Bot Trading Algorithm

The simple trading strategy that we will use will feature the Kraken exchange, Docker and Cloud Run. Before we set up everything, we need to explain how this strategy works and where you can learn how to code one.

The main idea behind the strategy will be to buy ETH when BTC moves 5% in the last 5 minutes. For this to work, we will pull BTC prices in 5-minute intervals and calculate the percentage difference.

If this difference is more or equal to 5% we will buy ETH, if the requirement isn’t reached we will continue to loop. The strategy also features error management, logging, and environmental variables.

Error management is used to catch errors that might occur, logging is used so we can obtain data about our bot and what it’s been up to while running on the server and environmental variables allow us to interact with the bot.

The code below is what our trading strategy looks like and here you can find an article about Kraken and how to code a similar strategy by yourself.

# Import the libraries we need
import time, logging, os
import pandas as pd
import krakenex
from pykrakenapi import KrakenAPI
from dotenv import load_dotenv

# Grab environment variables
load_dotenv()
API_KEY = os.getenv("KRAKEN_API_KEY")
API_SECRET = os.getenv("KRAKEN_API_SECRET")
TRACK_TICKER = os.getenv("TRACK_TICKER")
TRADE_TICKER = os.getenv("TRADE_TICKER")

api = krakenex.API(key=API_KEY, secret=API_SECRET)
kraken = KrakenAPI(api)

# Set up the logging
for handler in logging.root.handlers[:]:
    logging.root.removeHandler(handler)


logging.basicConfig(
    level=logging.INFO, format="%(asctime)s: %(levelname)s: %(message)s"
)
console = logging.StreamHandler()
console.setLevel(logging.INFO)
formatter = logging.Formatter("%(asctime)s: %(levelname)s: %(message)s")
console.setFormatter(formatter)
logging.getLogger("").addHandler(console)

# Create the main script logic
while True:
    logging.info("--------- Start of current 5 minute period ---------")
    logging.info(pd.Timestamp.now())

    try:
        BTC_old = float((kraken.get_ticker_information(TRACK_TICKER))["b"][0][0])
    except Exception as e:
        logging.warning("Error obtaining data")

    time.sleep(300)

    try:
        BTC_new = float((kraken.get_ticker_information(TRACK_TICKER))["b"][0][0])
    except Exception as e:
        logging.warning("Error obtaining data")

    percent = ((BTC_new - BTC_old) * 100) / BTC_old

    logging.info(f"BTC moved {percent:.2f}% over the last 5 minutes")

    if percent >= 5:

        try:
            ETH = float((kraken.get_ticker_information(TRADE_TICKER))["a"][0][0]) + 2
        except Exception as e:
            logging.warning("Error obtaining data")

        try:
            response = kraken.add_standard_order(
                pair=TRADE_TICKER,
                type="buy",
                ordertype="limit",
                volume="0.007",
                price=ETH,
                validate=False,
            )
        except Exception as e:
            logging.warning("Error placing order")

        logging.info(f"Order details:\n{response}")

        time.sleep(2)

        try:
            check_order = kraken.query_orders_info(response["txid"][0])
        except Exception as e:
            logging.warning("Error checking order")

        if check_order["status"][0] == "open" or "closed":
            logging.info("Order executed sucessfully")
            break
        else:
            logging.info("Order rejected")
            break
    else:
        logging.info("--------- End of current 5 minute period ---------")

As we need to provide our API keys for the trading algorithm to work, we will need to find one of the “safer” solutions to perform that.

What is Docker?

Docker is an open-source containerization platform. It lets us package our code into containers that can be smoothly deployed anywhere while being sure that the code in them will work and function in a standardized way.

To install Docker please follow one of these guides depending on your OS. I’ll be using Docker Desktop as I’m on Windows 10.

How to package a trading algorithm with Docker?

To package a trading algorithm with Docker, we will need to have a file named dockerfile that has all the instructions on how to make our algorithm ready by copying code files we need, installing libraries, passing the environment variables, and more.

Because Docker needs to know what dependencies we want to install for our algorithm, it is good practice to have a requirements.txt file that lists all the libraries we need and their versions:

python-dotenv==0.21.0
pandas==1.4.4
krakenex==2.1.0
pykrakenapi==0.3.1

To get your package version you can write pip show <package>.

Now that the requirements are ready, we can move on to specifying our installation and other setup commands inside of the dockerfile. We first create the dockerfile by writing touch dockerfile.

The inside of our dockerfile looks the following way:

FROM python:3.8
RUN mkdir -p /code

COPY ./main.py /code/
COPY ./requirements.txt /code/

WORKDIR /code

# set environment variablesFROM python:3.8
RUN mkdir -p /code

COPY ./main.py /code/
COPY ./requirements.txt /code/

WORKDIR /code

# set environment variables
ARG API_KEY
ARG API_SECRET
ARG TRACK
ARG TRADE

ENV KRAKEN_API_KEY $API_KEY
ENV KRAKEN_API_SECRET $API_SECRET
ENV TRACK_TICKER $TRACK
ENV TRADE_TICKER $TRADE

RUN pip install -r requirements.txt

EXPOSE 8080

CMD ["python", "/code/main.py"]

To build this container image we run the following command:

docker build -t google-algotrading101 \
	--build-arg API_KEY=<YOUR-API-KEY> \
	--build-arg API_SECRET=<YOUR-API-SECRET> \
	--build-arg TRACK=BTCUSDT \
	--build-arg TRADE=ETHUSDT .

Once docker is done, you will see a message similar to this one:

[+] Building 8.2s (11/11) FINISHED
=> [internal] load build definition from Dockerfile ...  

To see your docker images, you can write the following command to the terminal:

docker images
REPOSITORY                       TAG               IMAGE ID       CREATED         SIZE
google-algotrading101             latest            f81336abb1fc   2 minutes ago   1.08GB

If you want to run the container locally, you can write the following command:

docker run -d -p 8080:80 google-algotrading101

The next step is to store this image in the Google Artifact Registry. Prior to exploring that, we will need to install the gcloud CLI.

What is gcloud CLI?

Gcloud (Google Cloud) CLI is a set of tools to create and manage Google Cloud resources. You can use these tools to perform many common platform tasks from the command line or through scripts and other automation.

To install gcloud follow this guide.

How to push a Docker image to Google Artifact Registry?

To store a Docker image on the Google Artifact Registry, navigate to it in your Google Cloud dashboard and enable its usage. Then, click the Create Repository button and set up your repository details. In our case, it is a Docker repository.

When done, click the Create button. The next step is to configure our Docker to work with the Google Artifact Registry. To do this, we will use our previously installed gcloud CLI. Let us open it up and write the following command:

gcloud auth configure-docker \
    northamerica-northeast1-docker.pkg.dev

Take note that your region might be different depending on your choice when creating the Registry. To view your region, you can click the “SETUP INSTRUCTIONS” hyperlink in your Artifact Registry dashboard. This will open an example query you need to run to configure your docker.

Now, we need to tag our docker image with the details from our created Artifact Registry. Have in mind that this will be different for you depending on your registry location and name. I’ll add the “latest” tag to the image.

docker tag google-algotrading101 \
northamerica-northeast1-docker.pkg.dev/gifted-healer-362818/kraken-demo/google-algotrading101:latest

To make sure the tag was set, run docker images again. If everything is good, we can push our Docker image to the Registry by writing the following with the gcloud CLI:

docker push krakenexample101.azurecr.io/azure-algotrading101:awesome

When docker is done, you should see the image in your registry:

If you are facing issues, here is a good tutorial by Google on how to push/pull images properly.

Where can I learn more?

To learn more about the Google Cloud Platform, I advise visiting their webpage and exploring various google tutorials, and reading the documentation. There are also great tutorials on YouTube and other blog pages that you might want to explore.

Full code

GitHub link

Igor Radovanovic