Automated position management for your crypto trading bot
When you are trading with a crypto trading bot, it is important that you are in control of its actions. Of course, you have your trading rules to go long, short, take profit, etc. But how can you be sure that your bot is executing the right trades? And how can you manage your position on your exchange, ensuring that your stop losses, take profits, and positions are actually being executed?
In this blog, I will explain some techniques to help you maintain control over your crypto bot trading, and I will share some of my own experiences.
Let’s dive in!
Why checking your orders & positions is important
Let’s say you have a trend-following trading strategy that goes long and short. It also places a stop-loss order and takes profits when it has entered a trade. When the trade goes in the right direction, it will update the stop-loss and occasionally take profit.
When your trading strategy sends a signal to your middleware, the middleware makes an API connection to your exchange and executes the order.
However, there are several things that could go wrong in this process:
- Technical issues at the exchange preventing the order from being executed.
- Disappearing orders on the exchange (e.g., stop-losses).
- Limit orders that do not get filled.
- Technical issues in your trading strategy or trading platform (e.g., Tradingview) causing it not to send the trading signal to your middleware.
- Excessive time slippage anywhere in the process of getting your trading signals to the right place: Trading strategy -> middleware -> exchange.
There are plenty of other examples of issues that could prevent your orders from being executed. Somebody could manually interfere with the trading account where the trading bot is trading, such as switching the position from long to short or deleting stop-losses.
All these issues could cause significant problems, including:
- A big difference between your backtest and real-life results.
- Causing significant losses.
- Causing liquidations.
So, how could we solve this? Check, check, and double-check!
What to check
When you first start trading, it is important to check your positions and trade manually. However, as your trading strategies become more complex and you run multiple strategies, this can become a difficult task, and there is a high chance that you may overlook some errors that arise.
That’s why it is probably best to automate this process. Create a watchdog process in your crypto trading bot that regularly checks your positions and trades.
A few things to check:
- Is my position in the right direction (long/short) at the right price?
- Is my risk per trade the same as in my algo?
- Is my stop loss placed in the order book at the exchange at the right price?
- Are my take profits placed in the order book at the exchange at the right price?
- Is the account balance as expected based on the last trades?
Technology & middleware
So, those things to check seem pretty logical and simple. However, most middleware solutions and crypto trading bot solutions do not support such checks. Make sure the middleware you are using supports running a watchdog/check function.
If you have the possibility to run such a process, you’d want to run this if/else process regularly. The lower the timeframe you are trading on, the more often you’d want to run this process. For example, you could run this every 15 minutes. Keep in mind that running this process will use some API requests and server resources. Therefore, running it too often, especially in combination with a lot of checks, is probably a bad idea.
Where to start?
First, make sure your trading strategy sends a signal every X minutes to your middleware. This signal should contain some information about the position your trading strategy is in. For example:
- Signal: statuscheck
- Strategy direction: long
- Risk per trade: 4%
- Stoploss: 10000
- TP1: 11000
- TP2: 12000
- Entry: 10500
Starting from here, you could create an if/else script like this:
- Query your exchange about your position.
- If position == long and strategy == short, send an error.
- If position == short and strategy == long, send an error.
You will get the idea, just check as many parameters as possible.
Notifications
If there is something wrong, you’d want to receive an error message. Most middleware solutions do support Telegram, Discord, Email, SMS, or webhooks. However, you only want to receive messages when it is important. If you are receiving false positives every day, you may ignore the important errors when they occur. Make sure to use one channel (e.g., a private Telegram group) for important errors. This way, you will know that when you receive a message there, something important is wrong.
When to take action
When there is something wrong with your trading bot, you’d want to take action. Whatever you do, it is important to be quick because doing nothing can have significant consequences.
Manual actions:
When errors occur, you can manually close your positions, place new stop-losses, or place take profit orders. However, this can be a slow and inaccurate way of managing orders.
Automated actions:
Based on the if/else script, you could also take automated actions. If you know what’s wrong (e.g., the stop-loss is missing), you can automatically place a new stop-loss order. Send an error notification along with a confirmation that the new stop-loss is placed. This way, even if you are sleeping and something goes wrong, your crypto bot will manage the position and errors.
Non regular checks
Okay, so now you have a watchdog process that checks your positions every X minutes. However, you also want to run an extra check when something happens, such as your stop-loss being hit. It’s possible that your stop-loss order disappeared at the exchange for no apparent reason. Therefore, you want to immediately check after hitting the stop-loss if the position is closed. If it’s not closed, you should close the position. You could apply the same approach for take profits, for example.
My experiences
Crypto bot trading can be quite stressful, as there are numerous things that could potentially go wrong. Having automated position management is crucial to maintain a work-life balance alongside bot trading. It’s vital to know that your positions are being managed and stop-losses are in the right place. Changes can occur rapidly, exchanges may experience downtime, and your trading strategy could encounter errors – you get the idea.
This automated position management has prevented many errors and potential significant losses. I have witnessed instances where people interfered with their trading accounts, taking long positions when the trading bot was short, encountering stop-losses that disappeared inexplicably, and facing exchanges that stopped functioning during critical trading times.
Ensure that you double-check everything that occurs while engaged in crypto bot trading. That’s the only way to ensure your trading strategy is being executed correctly. Stay safe and happy trading!