1DES logo
Cognitrade
arrow_forward_ios

How to Design a Machine Learning Trading Bot - Part 5: Running an Automation

Bahman

Published on Apr 28, 2021

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

We started with “ Collecting Data ”.

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

Then we continued with “ Data Analysis ”.

We saw how important data cleaning and feature engineering are. To make a stable ML model, we must prepare the data correctly and visualize it to reach our goals.

Next, we explored finding a pattern and noted how easy it is to be “horoscopist-trapped” when searching for patterns—so we follow scientific methods like astronomers. We identified a simple pattern, “SMA20,” and discussed labeling it to [0,1].

Then we built a model. After building an ML model, you should evaluate it with a backtest. At this point, you need a Buy/Sell strategy—meaning we already have a signal to open a Long/Short position.

Now, it’s time to pass this signal to automation (a bot) that will buy/sell automatically.

Step 5: Running an automation

The automation controls buys, sells, take-profits, and stop-losses based on model signals.

Before building a crypto bot, let’s review a few terms.

Cryptocurrency Exchange:

A cryptocurrency exchange (DCE) lets customers trade cryptocurrencies for other assets, such as fiat or other digital currencies. (Wikipedia)

For example, Binance is a popular exchange platform.

By registering with the following link, you may receive 10% back on commissions: Register on Binance

Spot Trading vs. Margin Trading:

In spot trading, you buy/sell the actual crypto/fiat pair (e.g., sell BTC to receive ETH or EUR). In margin trading, you take leveraged positions on price movements; your assets may be locked as collateral. Each exchange has its own rules and protocols for margin trading.

Automation overview

API connections:

Most exchanges provide secure API access, typically using two keys: Public Key and Secret Key. With these, you can place orders from outside the exchange.

API helps developers connect to the exchange

What is a crypto bot or automation?

A crypto bot places orders on an exchange via secure APIs. Protocols vary across exchanges, and leverage (margin markets) adds complexity. Following our “start simple” approach, we begin with spot trading (moderate complexity). In the next season, we’ll implement a crypto bot.

Crypto Bot

Actions:

Our automation should support at least:

  • Place an order in the marketplace
  • Retrieve/query an order from the exchange
  • Cancel an order on the exchange
  • Get the asset’s balance on the exchange
  • Get online prices in a marketplace

Now, let’s review the whole process from receiving a model signal to placing an order.

If you need more background on the model, see the previous episode on building a model.

Model output: Open a Long position on BTC/USDT
Place an order
  • Set side and type: In our example, side is “Buy” and type is “Market”. (Depending on your signal/strategy, you might use Sell/Buy and Limit/Market or advanced types like Iceberg.)
  • Set the size: You may fetch your balance and calculate size per strategy (e.g., use half of USDT to buy).
  • Place your order: You now know side, type, and size—submit the order.
  • Retrieve your order: Check fill status. Orders can be filled in multiple trades at different prices.
Keep calm!
  • Place Stop-loss order: If your signal includes TP/SL, spot trading may block assets when placing TP/SL simultaneously. (This issue typically doesn’t exist in margin trading.) If SL is provided, place an SL order.
  • Cancel order: Be ready to cancel any order (e.g., cancel SL to place a TP).

Conclusion:

Automation complexity depends on the exchange and its APIs. Prefer well-documented exchanges with solid support. There are libraries that unify connections to multiple exchanges. In the next season (automation development), we’ll cover how to use them.

Want a secure, comfortable way to connect your exchange to intelligent automation? Try 1DES.

TRADING MACHINE LEARNING AUTOMATION