- Advertisement -Newspaper WordPress Theme
Algorithm tradingLive Algo Trading on the Cloud - Microsoft Azure - AlgoTrading101 Blog

Live Algo Trading on the Cloud – Microsoft Azure – AlgoTrading101 Blog


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.

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:

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.

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:

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:

To get your registry credentials, run:

az acr show --name krakenexample101 --query loginServer

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.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Subscribe Today

GET EXCLUSIVE FULL ACCESS TO PREMIUM CONTENT

SUPPORT NONPROFIT JOURNALISM

EXPERT ANALYSIS OF AND EMERGING TRENDS IN CHILD WELFARE AND JUVENILE JUSTICE

TOPICAL VIDEO WEBINARS

Get unlimited access to our EXCLUSIVE Content and our archive of subscriber stories.

Exclusive content

- Advertisement -Newspaper WordPress Theme

Latest article

More article

- Advertisement -Newspaper WordPress Theme