1DES logo
Cognitrade
arrow_forward_ios

How to Design a Machine Learning Trading Bot - Part 6: Risk Management

Bahman

Published on May 06, 2021

Step 6: Monitoring the Trade & Risk Management

We have started with “ Collecting Data ”:

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

Then we continued with “ Data Analysis ”.

We saw how important data cleaning and feature engineering are. To make a stable machine learning model, we need to prepare data the right way and, finally, visualize it to reach the goal.

Then we continued with finding a pattern and noted you can be trapped like a horoscopist when searching for patterns—always follow scientific methods and act as an astronomer. We identified a simple pattern, “SMA20,” and discussed how to label it to [0,1].

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

Finally, we showed how to run the automation, by these actions:

  • Place an order
  • Retrieve an order
  • Cancel an order
  • Get the asset’s balance
  • Get online price

Following that, we understood exchanges, APIs, and spot trading.

Now it’s time to take care of the position we opened and make sure we reach profit—or minimize loss as much as possible.

Step 6: Monitoring the Trade and Risk Management

First, let’s answer this question:

Why do we need to monitor the trade and our position?

Years ago with classic trading, it was normal that when you opened a position based on a given signal, all information you needed was provided by the signal maker.

The following example is one of these classic signals:

  • Market: Whatever ;)
  • Side: Buy
  • Order type: Limit
  • Open position: 100
  • Take-profit (TP) 1: 110
  • Take-profit (TP) 2: 115
  • Stop-loss (SL): 90

Suppose we are the signal maker and try to understand the meaning of this signal with our data vision.

Signal example
Classic signal layout

Signal maker: Based on the data I collected I decided what would be the best trade for me. I don’t care what happens after I open a position because I’m so smart and predicted what could happen; I’ve already set my stop-loss and take-profit.

Can you see the problem behind this kind of prediction and action?

It’s easy to fix: evaluate the trade with new market data and be flexible to change stop-loss or take-profit while the position is still open. With this perspective, we can follow three methods:

  1. Update SL and TP every minute (the minimum timeframe).
  2. Update SL and TP on each defined period (not the one you’re trading).
  3. Update SL and TP on the same timeframe you’re trading.

So, what does it mean?

Let’s consider we are trading in the 1H (1 hour) timeframe.

Trading in the 1H timeframe means our decision to buy and sell is based on the OHLCV in 1H.

1) Updating SL and TP every minute means we decide changes on 1-minute OHLCV.

2) Method 2: change TP/SL every 15 minutes (lower timeframe) or every 2 hours (higher timeframe).

3) Method 3: decide about changing TP/SL every hour by looking at the last hour’s data.

Problem solved illustration
Adapting TP/SL dynamically

If you’re following from the first episode , you’ve seen I go for the simplest way to design and later to develop. That’s my trick to estimate an idea. You know which method we should start with!

Yes—continue with the third method: update SL and TP on the same timeframe we are trading.

Machine Learning

It’s back again. Do you remember what we’ve done so far? Let’s recap:

  1. Data Collection
  2. Analyzing Data
  3. Pattern Recognition
  4. Build a Model
  5. Running Automation

Bad news: we must do all these steps again. Good news: we’ve already done them, so it’s easier to redo on a smaller scale.

First, collect the last hour’s OHLCV—from the time we opened the position to the time we want to process.

Then analyze it and find proper features. A key difference now is the trade position itself—our entry becomes a key feature.

After that, find a pattern, do training/testing and backtesting.

Then, make a model.

Finally, run the bot to change SL/TP on the exchange. For example, you might cancel the old SL and place a new order for the updated SL.

Pattern example
Pattern workflow

Conclusion:

Risk management and monitoring the position are a separate part of trading with new inputs and outputs. To reach positive results, you need to do parts 1 to 5 again. In our development season (which starts in January 2021), when we develop this part, we will go deeper and talk more about it.

TRADING MACHINE LEARNING RISK-MANAGEMENT