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

Live Algo Trading on the Cloud – AWS – AlgoTrading101 Blog


Kraken Bot

In this article, we will want to explore the AWS Lightsail cloud service that offers affordable servers on which we can run our trading strategies. The simple trading strategy that we will use will feature the Kraken exchange.

Before we set up a server and let the bot go crazy with trading, 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 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 how 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 and load the API Keys
import time, logging, os
import pandas as pd
import krakenex
from pykrakenapi import KrakenAPI
api = krakenex.API()
kraken = KrakenAPI(api)

api.load_key('KrakenPass.txt')

# Create environment variables
track_ticker = os.environ.get('ticker_to_track')
trade_ticker = os.environ.get('ticker_to_trade')
logname = os.environ.get('my_log_name') # Name of the saved log file

# 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', 
                    filename=logname, filemode='a')
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 completed sucessfully')
            break
        else:
            logging.info('Order rejected')
            break
    else:
        logging.info('--------- End of current 5 minute period ---------')

Notice: The code is stored in a private GitHub repository.

How to sign up for an AWS server?

In order to sign up for an AWS server, you will first need to open an AWS account. So let us do that together. Go to the following link and in the upper right corner click the “Create an AWS Account” button.

https://aws.amazon.com

Then input your email address and create your password and unsername.

After that, you will enter your contact information.

The next screen will require you to enter your credit card information as you will need to pay for the server. AWS also offers some free services and trials. After that, you will verify your account via SMS and it should be done.

Now that you’re in your management console, go to “Services” which is located in the upper left corner, and select “Lighsail” which is found under the “Compute” category.

When there click on the orange “Create instance” button and let us get started with the server creation.

How to pick a good AWS server location?

Picking a good AWS server location is important as you don’t want your trading strategies to lag. In order to make the right choice, we need to align the server location with the exchange location.

Sometimes there aren’t any servers in that particular exchange location, but our goal is to pick the closest one. There are several ways through which you can learn about the server location of your exchange or broker.

For this article, we won’t go into documentation but rather stick to a more “detective-like” approach by going over to this website and inputting the API URL of our exchange.

Now we do a Ping check by clicking the Ping button. The location with the lowest latencies is the best one for our exchange. In this case, we have several good ones like Los Angeles, Singapore, and Moscow.

Now we can change our AWS server location by clicking on the marked link and select a location that is closest with the ones that had the lowest latency. For this article, I’ll go with Singapore.

Now we will change the Blueprint to be OS only as we are not creating a website or an app.

As Python and our requirements for the trading strategy don’t require much space and computing power, we can go with the cheapest instance. You can name your server too.

After that, at the bottom, you will find a create instance button that will complete the AWS server creation. After that, you will wait for a few seconds until your server comes online.

How to install Python and Git in an AWS Server?

Python and Git can be easily installed in an AWS server by utilizing the terminal commands. As our instance is based on Linux we will use the sudo yum install command that is the equivalent of the pip install command in Python.

In order to do this, we will open up our server instance by clicking the run button.

Now that we are in our terminal we will commence the installation of Python3 and Git. But first, let’s check if we have Python already installed in it.

Great! We have the latest version of Python3 installed. If you received that python3 wasn’t found you would need to write the following command:

After that, you will agree for it to be downloaded and installed by passing a “y” letter when the terminal prompts you.

Now that Python is ready, it is time to turn our focus to Git. Git can be seen as a program that allows us to communicate with GitHub and send (push) or take (pull) code from it.

To install Git write the following command:

As you can see, on the last line the Terminal has prompted us to agree to the installation by passing the “y” letter. After you do that the installation will be complete and we will have our Git ready for use.

How to connect AWS to GitHub?

In order to connect your AWS instance to GitHub, you will need to utilize the SSH (Secure Shell) method and Git.

This process can be viewed as being consisted of 3 main steps:

  1. Create your special “keys” (like the API and secret key)
  2. Upload the public key to GitHub
  3. Use the keys when interacting with GitHub

Let’s create the keys. Write the following command in your AWS instance terminal

Then give it a name and the passphrase can be left blank. I’ll name it “algotrading”.

Now that your key is created, you can find it by writing the following commands. The first one is ls that will list all the folders that are in your AWS instance.

here you can see our two keys. The “algotrading” key can be viewed as our secret while the “algotrading.pub” key is the public aka API key. For us to import the key to our GitHub we need to read it first.

To do this write the following command and copy the output:

Now it’s time to set this SSH key in our GitHub profile. Go over to your GitHub account -> Setting -> SSH and GPG keys.

When there, click on the green “New SSH key button”. Then give it a name and input the key. Then add the key and input your password when prompted. If you’ve done everything correctly the SSH should be ready.

Now we will instantiate the private key.

That’s it, our AWS instance is ready. The two code snippets above need to be typed in your Terminal every time you run the AWS instance. Now, let’s test it out by pulling our trading strategy from a private repository.

Go over to your repository on GitHub, click on the green “Code” dropdown button, select SSH and copy the URL.

Now write the following function in your AWS instance:

We successfully cloned out the GitHub repository to our AWS instance. In order to confirm this write ls

How to install Python packages in an AWS Server?

Python packages can be installed in an AWS server by using the sudo pip3 install command. This can be tiresome if your code uses many dependencies so I’ll show you how to automate this process.

But as our code only uses the Kraken API let’s install it in our AWS instance with the following command:

sudo pip3 install pykrakenapi

If you have many dependencies (Libraries) you can utilize the pipreqs library that will export all the libraries your script is using into a .txt file that can be read by our AWS instance.

You can use this library in two ways, the first way would be to install the pipreqs library on your local machine and generate a .txt file there, upload it to GitHub, and then pull it to your AWS instance.

The second way is to install the library in your AWS instance and generate the .txt file there. I’m more of a fan of the second way as it is more efficient, so let’s go with it.

Type the following command to install the pipreqs library:

sudo pip3 install pipreqs

Then list all of the documents that are located in our AWS instance with ls and hone into the file you want the dependencies to be generated from. In our case, it is the Kraken-AWS file and we write this:

Then create the requirements file:

As you can see we have our requirements.txt file inside of the Kraken-AWS folder. Now that we have it you can install the requirements with the following command:

sudo pip3 install -r requirements.txt

That’s it! Notice: To go back in your file hierarchy just type the following line that will take you a step back

How to use environment variables?

In order to use the environment variables with in you AWS instance you will need to use the export command and state the values for those variables.

Let’s do it with our AWS instance and see how the code runs. We’ll start with a fresh AWS instance connection and go through all the steps.

Now we can set up the environmental variables by typing export followed by the variable name and the value that we will assign it. In our case we have three environmental variables which are the following:

track_ticker = os.environ.get('ticker_to_track')
trade_ticker = os.environ.get('ticker_to_trade')
logname = os.environ.get('my_log_name') # Name of the saved log file

No we will assign them and launch our script:

export ticker_to_track='BTCUSD'
export ticker_to_trade='ETHUSD'
export my_log_name='log_kraken'

We have basically stated that we want to track BTC and trade ETH (as per our trade logic) and store the longs in a file named ‘log_kraken’. Now we can run the strategy with the following command:

And our script is operational and running. You can confirm this by the logs that are appearing. But how do we export these logs so that they can be read later on?

How to read and download logs from an AWS instance?

To read the logs from your AWS instance you can use the cat command followed by the name of your log file. To download the logs, you can push them to your GitHub repo from the AWS server instance.

Let’s check out the logs that are situated in the log_kraken file and read them with the following command:

Now, we will push this log file to our GitHub. For this be sure to be in the right repo. Then type the following command that will prepare the changes to be added to our GitHub repo:

After that let’s confirm the changes by commiting them:

git commit -m "Add Log files"

Now that the log files are ready we can push them to our GitHub with this command:

As you can see, the log files has been added to our GitHub repo.

How to make a trading Telegraph bot?

Now that your strategy is running live. You want to be notified of significant events.

If you don’t do this, you are blind to whatever happens to your strategy and money! You should always know what is happening to your trading robot.

The usual significant events are:

  • On strategy start-up and shut down.
  • On trade entry or exit.
  • On current positions and prices on selected assets every X hours. This ensures we know that the strategy is still running.
  • When available margin drops before a certain level
  • When volatility rises above a certain threshold
  • When prices hit a certain level
  • When prices move more than X% in the last Y minutes/hours/days
  • Every place in Aiur where we have a logging message.

To do this, we use Telegram Chat Bots.

Step 1:

Search for ‘BotFather’ in the chat search bar.

Open a chat with the ‘BotFather’ from within Telegram.

Step 2:

Type in the command /newbot

It will prompt you to enter a bot name and send you an access token. You need this access token to use your bot. Don’t lose it or give it away!

Step 3

Search for your newly created bot

Open a new chat with your newly created bot. Click start or type something (this is important for Step 4).

Step 4

Go to the following URL – https://api.telegram.org/botxxxxxxxxxxxxxxxx/getUpdates – replacing the XXX with your access token. Here you should see a JSON structure if you had started a chat with your bot.

Note down the id (not to be confused with update_id or message_id). See picture below. Grab the number in the red rectangle.

Test your bot

At this point, the bot is created and messages can be sent to it. Here is a code snippet to test if everything is working:

import requests
def send(text):
	token = 'your_access_token_goes_here'
  	your_id = 'your_id_goes_here'
  	params = {'chat_id': your_id, 'text': text, 'parse_mode': 'HTML'}
  	resp = requests.post('https://api.telegram.org/bot{}/sendMessage'.format(token), params)
  	resp.raise_for_status()
send('Profit: $1,000,000,000')

Example on a notebook:

Make your bot private.

This means that no one else can chat with your bots.

Normally, bots created by BotFather are accessible to all.

Here is the workaround to make your bots private: https://sarafian.github.io/low-code/2020/03/24/create-private-telegram-chatbot.html

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