How to design a machine learning trading bot: Running an automation

Bahman

Bahman in How to

Apr 28, 2021

Previously on “How to design a machine learning trading bot”

We have started with “Collecting Data”:

We found out what is OHLCV data, and we learned why we need historical data and online data both together.

Then we continued with “Data Analysis

We have seen how cleaning the data including features engineering is important. We learned that to make a stable machine learning model we need to prepare data in the right way and finally, we found out how visualizing data can help us to reach the goal.s

Then we continued with finding a pattern and we noted you can be trapped like a horoscopist to find a pattern in data, and always follow the scientific ways and act as an astronomer. Then we had found a very simple pattern which was “SMA20” and we discussed how to label them to [0,1].

Then, we build a model. We explained after you made an ML (Machine Learning) model, you should evaluate it with a backtest. And at this point, you need a strategy to Buy and Sell. It means, we already have a signal to open a Long/Short position.

Now, it’s time to pass this signal to automation or a bot and this bot is going to buy/sell automatically.

Step 5: Running an automation

The automation part is going to control buys, sells, take profits, and stop losses placed by the model’s signals.

Running an automation

Before we start to build a crypto bot or automation we need to review a couple of terms together.

Cryptocurrency Exchange:

A cryptocurrency exchange, or digital currency exchange (DCE), is a business that allows customers to trade cryptocurrencies or digital currencies for other assets, such as conventional fiat money or other digital currencies. (Wikipedia)

For example, Binance and are two papular exchanges platform.

By registering on Binance with the following link, you will receive 10% back on your commissions.

Register On Binance

 

Spot Trading Vs. Margin Trading:

On spot trading, you sell and buy the actual crypto/Fiat pairs. For example, you sell real BTC and receive real ETH or EURO. But in margin trading, you bet on the prices of the pairs with some leverage. In margin trading based on the leverage you use, your assets get locked on the exchange. Typically, each exchange has different rules and protocols for margin trading.

automation

API connections:

Most of the exchanges are providing a secure API connection. Usually, the API has two parameters: Publick Key and Secret Key.

By having these two parameters you’re able to place an order on the exchange from the outside of the exchange.

API helps developers to connect to the exchange

So, now the question is:

What is a crypto bot or automation?

With a crypto bot, you can place an order in the exchange. Managing the communication between the bot and exchange is with a secure API. Depending on the exchanges and their API protocols, it could be different ways to place an order on the exchange. And also in some cases (depending on the exchange), it brings more complexity when we want to place an order with leverage (Margin Market). So, if you follow us so far you have seen that we always start with a simple sample, and at this level, we start with spot trading which has moderate complexity. In our next season, we will see how to implement a crypto bot.

Crypto Bot

Actions:

So, our automation at least should support these actions:

  • Place an order in the marketplace
  • Retrieve or query an order from the exchange
  • Cancel an order in the exchange
  • Get the asset’s balance in the exchange
  • Get Online prices in a marketplace

Now, let’s dig into the whole process once and review what we need to do step by step since we got the signal from our model to placing an order.

Just as a sample, consider we have the following signal which is the output of our model.

If you don’t have any idea about the model, please review the last episode about building a model.

Model output: Open a Long position on BTC/USDT

Here you need to follow step by step. (Kindly, remind you that I always simplify the method to make the concept more understandable)

place an order

  • Set aside and type of your order.

On our sample the side is “Buy” and the type is “Market order”. (Depending on your signal, the side could be Sell/Buy, and also based on your strategy, the order type could be limit order or market order or also some other advanced orders like Iceberg Order)

  • Set the size

Before you place the order you need to set the size of your order. Also, based on your strategy, in some cases, you need to get your balance and calculate the size you want to dedicate to buy or sell. For example, in our sample, we want to dedicate half of our USDT to buy.

Accordingly, we get the balance of USDT, calculate the half, and set the size to the determined value.

  • Place your order.

So far you know the size of the order, the side, and the type of your order. Then it’s ready to place your order.

  • Retrieve your order

Depending on the exchange API you need to retrieve your order to find out if it is filled or not. Also, it could possible that your order is filled with more than one trade with different prices.

Keep calm!

  • Place Stop-loss order:

If your signal provides “Take-profit” (TP) and “Stop-loss” (SL) then most probably you have a challenge in spot trading to place an order for TP and SL together. Because in many exchanges on spot trading when you place an order for TP/SL your asset will be blocked by the exchange to make sure that the order has been supported with an asset. (You won’t face this problem in margin trading)

Let’s consider your signal comes with stop-loss. So, it means you need to place an order with the type of stop-loss.

  • Cancel order:

In the end, you should be ready to cancel your order at any time. Consider, you want to cancel the stop-loss order and place a take-profit order.

Conclusion:

The automation section could be very complicated depending on the exchange you’re working with. So, make sure the API is well-documented and has proper support from the exchange. There are some packages and libraries which help us to connect to several exchanges. In the next season the automation development part, we will discuss how to work with them.

But if you want a secure, comfortable, and easy way to connect your exchange to intelligent automation and make some profit, try 1DES.


This post was originally published on 1des.com