Live Algo Trading on the Cloud – Microsoft Azure

12 min read

Get 10-day Free Algo Trading Course

Loading

Last Updated on November 25, 2023

Microsoft-Azure-Logo

Table of contents:

  1. What does live algorithmic trading on the Cloud mean?
  2. Prerequisite – 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 Microsoft Azure?
  9. Why should I use Microsoft Azure?
  10. Why shouldn’t I use Microsoft Azure?
  11. How to get started with Microsoft Azure?
  12. What is the Azure CLI?
  13. How to get started with the Azure CLI?
  14. What is Docker?
  15. Kraken exchange trading bot setup
  16. How to package a trading algorithm with Docker?
  17. What is Azure Container Registry?
  18. How to create Azure Container Registry with Azure CLI?
  19. How to push a Docker image to Azure Container Registry?
  20. How to protect your Azure Container Registry?
  21. What are Azure Container Instances?
  22. How to deploy a docker image to Azure Container Instance?
  23. How to view Docker logs with Azure CLI?
  24. How to remove a Docker container from Azure with the CLI?
  25. Where can I learn more?
  26. 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 easily maintainable.

This is article, we will demonstrate how to deploy a live algorithmic trading strategy to the cloud server using Microsoft Azure 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?

The pros of deploying trading strategies to the Cloud are:

  • Cloud services are easily accessible – 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 minimal effort.
  • 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.

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

The cons of deploying trading strategies to the Cloud are:

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

What is the Cloud Service?

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

What is the Cloud used for? 

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

  • Algorithmic trading – cloud services allow algorithmic traders to run multiple bots that trade the market.
  • Big data storage – clouds 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.
  • Test 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 Microsoft Azure?

Microsoft Azure is a cloud provider service that offers various cloud solutions for things such as algorithmic trading, data storage, development, AI and machine learning, app scaling, system monitoring, and more.

Why should I use Microsoft Azure?

  • Microsoft Azure is cost-effective (pay as you go)
  • Microsoft Azure is scalable
  • Offers many cloud solutions
  • Has SOTA security practices
  • Has a good uptime track record
  • Has good documentation with examples
  • Is one of the largest and most known cloud providers

Why shouldn’t I use Microsoft Azure?

  • Microsoft Azure can be confusing for beginners
  • Microsoft Azure support can take a while to respond
  • It can be hard to migrate off of Azure
  • The interface might be complicated for beginners
  • Might have a steep learning curve
  • Can get pricey depending on the user’s needs

How to get started with Microsoft Azure?

To get started with Microsoft Azure, we will open up a free account. To do this, go over to their website, and click the green “Free account” button in the upper right corner of your screen.

This will open up a new page that will explain what you will get for free in terms of their services. In my case, I’m getting $200 in free credits in addition to all the free services for the next 12 months. We confirm our account creation intent by clicking the green “Start free” button.

This will open up a Microsoft sign-up prompt where you will need to use your email, phone, or skype. Signing up with GitHub is also an option. I’ll go the GitHub route. After you agree to authorize Microsoft Corp with your Github, you will need to agree to their terms and similar.

Now, a signup form will be presented to us where you will be asked for a lot of things such as your name, country, phone, address, and more. After that, you will also be asked to provide your card for payment verification. Even if you are using the free account they will ask you for this.

It is good to keep the information in the picture below in mind:

After your card has been verified, you should be ready to go from the account side of things. The way we want to interact with Azure in this guide is through their CLI.

In the following headers of the article, we’ll explore how to deploy our trading algorithms by using the Azure CLI, Docker, and the Kraken exchange. The code used will be available at the end of the article in a GitHub repository.

If you would want a more beginner and manual way of deploying your algorithm to a cloud service, check out our AWS example. If you have read the AWS version before, you can also apply this method to AWS as it also supports all the services we need. They just have a slightly different name.

What is the Azure CLI?

The Azure CLI (Command-Line Interface) is a way for us to interact with Microsoft Azure via code. This way, we don’t need to go over to their website to manually create, start and stop our cloud services.

How to get started with the Azure CLI?

To get started with the Azure CLI, we will need to install it. The approach may differ depending on your OS, so we won’t waste time going over that step-by-step. Instead, here are the instructions from Microsoft on how to install it for your OS.

I’ll be using VS Code as my code editor and Azure CLI has a useful extension if you plan to use Azure CLI more often. To install it, we navigate to the extensions at the left bar of your editor and search for “Azure CLI Tools”. When you find it, click the blue “Install” button if you wish to have it.

What is Docker?

Docker is an open source containerization platform. It allows us to package our code into containers that can be easily 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.

Kraken exchange trading bot setup

In this article, we will explore a simple trading strategy that will feature the Kraken exchange, Azure CLI, and Docker. Before we set up anything, we need to explain how this strategy works and where you can learn how to code a similar 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 around. 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 a quick guide about Kraken and how to code a similar strategy by yourself.

Take note that the intent is to fit everything in one file to get us going quicker, you’ll probably want to clean up the code as your strategies will be more complex and have more moving parts to them.

# 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 ---------")

For our local testing purposes, we will have a .env file that looks like this:

KRAKEN_API_KEY = 'YOUR_KRAKEN_API_KEY'
KRAKEN_API_SECRET = 'YOUR_KRAKEN_API_SECRET'
TRACK_TICKER  = 'BTCUSDT'
TRADE_TICKER = 'ETHUSDT'

Now, let’s set up this algorithm for Docker.

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 features 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 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
CMD ["python", "/code/main.py"]

In the code above we specify what version of python we’re using, then we create a folder /code to hold our script. Then, we copy over the two files we need to the newly created folder and set it as our working directory. We then provide environment variables that will be set when we build the docker image.

The ARG is your placeholder that is only accessible during the build time and then it vanishes, while the ENV will be accessible through your whole image process. We use the ARG parameter to dynamically pass our environment variables on docker image builds.

There is also a way to pass the environment variables via Azure.

At the end of the file, we install the dependencies and start the main.py script. Have in mind that you should double-check what ports are exposed in your instance as the code might not run. To open a specific port via Docker you can add EXPOSE <PORT_NUMBER>.

Before we build the image, it is good practice to have a .gitignore and .dockerignore files where we place files (.env) that we don’t want to push into GitHub or the Docker Container.

Now that we have everything ready, make sure to start your Docker Desktop so that we can build our docker container image. To build the image, we run the following command to our terminal:

docker build -t azure-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
azure-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 azure-algotrading101

The next step is to push our container image to the Azure Container Registry.

What is Azure Container Registry?

Azure Container Registry is a service that allows us to build, store, secure, scan, replicate, and manage container images and artifacts. You can think of it as GitHub but private and for containers.

How to create Azure Container Registry with Azure CLI?

To create an Azure Container Registry with the Azure CLI, we will need to have Azure CLI installed and authenticate our account through it. Then we will create our registry and push the container image to it.

To make sure that your Azure CLI is ready to go, write the following command in your terminal emulator:

az --version
azure-cli                         2.40.0

core                              2.40.0
telemetry                          1.0.8

Dependencies:
msal                            1.18.0b1
azure-mgmt-resource             21.1.0b1

Now, let us login into our account by writing az login. This will open up your browser and ask you to log in. After you do that, you can close the browser.

Before creating the Azure Container Registry we will need a resource group. To create a resource group we write the following :

az group create --name algotrading101Group --location eastus
{
  "id": "/subscriptions/...",
  "location": "eastus",
  "managedBy": null,
  "name": "algotrading101Group",
  "properties": {
    "provisioningState": "Succeeded"
  },
  "tags": null,
  "type": "Microsoft.Resources/resourceGroups"
}

When picking a location, it is best to find to try one that is the closest to the main servers of your exchange for the best latency. If you want to learn how to do that, check out our AWS article here.

Now, we create our Container Registry with this command:

az acr create --resource-group algotrading101Group --name krakenexample101 --sku Basic

Your Registry name should be all lowercase, unique within Azure, and contain 5-50 alphanumeric characters.

Now that the Registry is set, we will log into it with this command:

az acr login --name krakenexample101

-> Login Succeeded

How to push a Docker image to Azure Container Registry?

To push a Docker image to the Azure Container Registry, we must first tag the image with the full name of the registry’s login server. Let’s find that full name by writing the following command:

az acr show --name krakenexample101 --query loginServer --output table
Result
---------------------------
krakenexample101.azurecr.io

Now we tag the image, I’ll add :awesome as its tag. To recall the image name, run docker images.

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

To make sure the tag was set, run docker images again.

REPOSITORY                                         TAG      
krakenexample101.azurecr.io/azure-algotrading101   awesome

Finally, we can push our Docker image to the Registry by writing the following:

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

The speed of the push will depend on your internet connection and the size of your image. As this is a small image it shouldn’t take long in most cases. To make sure that the image has been properly pushed, we can inspect the registry for its images:

az acr repository list --name krakenexample101 --output table
Result
--------------------
azure-algotrading101

How to protect your Azure Container Registry?

To protect your Azure Container Registry from cybersecurity attacks, it is best practice to set up a feature named Azure Active Directory. The Active Directory is a service that provides single sign-on, multifactor authentication, and conditional access to guard against cybersecurity attacks.

In the case of our Container Registry, the best practice for many scenarios is to create and configure an Azure Active Directory service principal with pull permissions to the registry. Azure has us covered with an example of how to do this.

You can find the bash script for it in the GitHub repo linked at the bottom of the article. Be sure to edit the script to your liking by changing the first two parameters indicated in the script.

To run the script go to the directory and do:

.\active_directory.sh

To get your registry credentials, run:

az acr show --name krakenexample101 --query loginServer

What are Azure Container Instances?

Azure Container Instances is a service that enables users to deploy containers on the Microsoft Azure public cloud without having to provision or manage any underlying infrastructure.

How to deploy a docker image to Azure Container Instance?

To deploy a docker image to Azure Container Instance, use the az container create command to deploy the container.

Replace <acrLoginServer> with the value, you obtained previously. Replace <service-principal-ID> and <service-principal-password> with the service principal ID and password that you created to access the registry. Replace <aciDnsLabel> with the desired DNS name.

az container create --resource-group algotrading101Group \
	--name azure-algotrading101 --image <acrLoginServer>/azure-algotrading101:awesome \
	--cpu 1 --memory 1 --registry-login-server <acrLoginServer> \
	--registry-username <service-principal-ID> \
	--registry-password <service-principal-password> \
	--ip-address Public --dns-name-label <aciDnsLabel> --ports 80

To verify our deployment we run the following command:

az container show --resource-group algotrading101Group \
   --name krakenexample101 --query instanceView.state

Until you get “Running” as a response, don’t proceed to the next step.

How to view Docker logs with Azure CLI?

To view the output logs of your Docker container with Azure CLI, run the following command:

az container logs --resource-group algotrading101Group --name azure-algotrading101
2022-09-13 21:13:32,340: INFO: --------- Start of current 5 minute period ---------
2022-09-13 21:13:32,343: INFO: 2022-09-13 21:13:32.342519

How to remove a Docker container from Azure with the CLI?

To remove a Docker container from Azure and all other things we created with the Azure CLI, we only need to delete the resource group which will take care of everything else. To delete the group we do the following:

az group delete --name algotrading101Group

Where can I learn more?

To learn more about Microsoft Azure, I advise checking out their documentation and visiting their learning paths and tutorials which are structured nicely. There are also good code samples to explore and learn from.

Full code

GitHub Link

Igor Radovanovic