How to design a machine learning trading bot — Part 6: Risk Management

Bahman

Bahman in How to

May 06, 2021

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.

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 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.

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 by understanding the concept of exchanges, API, and spot trading.

Now it’s time to take care of the position we have opened and make sure we reach the profit or make our loss minimum as possible.

Step 6: Monitoring the trade and Risk Management

First, let’s start to answer this question:

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

Back to years ago with classic trading, it was so normal when you open a position based on a given signal all information you need has been given to you by the signal maker.

Risk Management

The following example is one of this classic signal:

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

As you can see you have all the information you need for TP1, TP2, and SL. Suppose we are the signal maker and try to understand the meaning of this signal with our data vision.

signal

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

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

It’d be so easy to fix this error. We need to evaluate our trade with the new data from the market and we should 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 time frame)
  2. Update SL and TP on each [defined] period (which is not the period you’re trading)
  3. Update SL and TP on the time frame you’re trading.

So, what does it mean?

Let’s consider we are trading in the 1H (1Hour) time frame.

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

1- Then, updating SL and TP every minute (method 1) means we will decide to change TP and SL on 1minutes OHLCV.

2- Method 2: If we decide to change TP or SL every 15 minutes based on the changes in the last 15 minutes (Lower time frame) or if we update SL or TP every 2 hours (Higher time frame)

3- By following the third method, you will decide about changing SL or TP every hour by looking at the last hour's data.

problem solved

If you’re following me from our first episode, you have seen that I always go for the simplest way to design and later on to develop. This is my trick to estimate the idea. Well, I bet, you know what method we should start!

Yes, that’s right. We are continuing with the third method which is updating SL and TP at the same time frame we are trading.

Machine Learning:

It backs again. Do you remember what we have done so far? Let’s recap it:

 

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

So, I have good news and bad news. The bad news is we have to do it all these steps again. And the good news is, we have done it once then it’s easy to redo it again on a small scale.

First, we collect the last hour data (OHLCV), from the time we opened the position till the time we want to process.

Then, we analyze it and find the proper features. One of the differences between analyzing data at this time and before is the trade position. This means our key feature at this point is our trade entrance.

After that, we need to find a pattern and do the training and testing and also backtesting.

Then, we should make a model.

Finally, we need to run the bot to change the SL or TP on the exchange. For example, in this step maybe you need to cancel the old SL on the exchange and place a new order for the new SL.

pattern

Conclusion:

Risk management and monitoring the position is a separate part of trading with the new input and output. To reach the positive result you need to do all parts from 1 to 5 again. In our development season (Which starts in January 2021) when we want to develop this part we will go deeper and talk more about it.

 


This post was originally published on 1des.com