Machine Learning Trading bots
This is my 3rd article and I’m writing from Auch in France. Don’t hesitate to look for the article about gridbots or breakout bots.
Why Machine Learning ?
The two last articles referred to algorithm-based trading bots. The gridbot is efficient when the market ranges, while the breakout bot is efficient in very low volatility markets.
Since the FTX collapse, the market evolved into a new phase : a powerful bull market nurtured by ETF approval expectations, crazy outflows from big institutions, the upcoming halving, etc. In those conditions, the algorithm-based strategies are short-lived ; They only work a couple of months, then you have to find a new algorithm that fits the market phase.
This is where machine learning brings a huge benefit : no matter how the market behaves, the machine learning helps the trading bot to continuously fit and extract value.
Machine Learning V1
The first version of the machine learning bot is based on the build proposed by Justin Dehorty (@jdehorty). He managed to build a machine learning system (=indicator+testing tools) in PineScript directly into TradingView, which awarded him to be selected in the editor’s picks. The whole article can be found here.
He proposes to use a K-Nearest Neighbours (KNN) model, populated with indicators values (not the price directly), in a Lorentzian space. For those looking for a TL;DR, let’s say that the KNN helps the bot to predict the short-term future direction based on historical situations that had similar indicators values. The Lorentzian space helps to better handle what looks like erratic values such as FOMC pumps/dumps, macro events, exchanges collapsing, ETF announcements, etc.
From there, I replicated the PineScript code and translated it into Python(3) to fully leverage the performance of the language, the efficiency of the data processing libs such as pandas, numpy, sklearn, etc. The main differences between his code and mine are:
- I have a “more accurate” KNN coding, as Dehorty’s is an approximation
- I only take 1 trade after the other, while Dehorty’s code can handle multiple trades at the same time, this is for simpler size/money/trade management
- I only look for the direction of the next candle while Dehorty looks 4 candles beyond.
One question that might be asked is how to learn Machine Learning. I had the chance to find a great Youtube channel that taught me almost everything. It is in French, but I’m confident that there are plenty of similar great content in english. At worst, you can put the english subs on Youtube.
Starting from there, it took me around 10 full days (of holidays+weekends) to completely build, test and deploy the MLv1 bot. Here is a simplified structure of the bot:
Of course, I didn’t go all-in in this bot at start, I wanted to make a dry run to assess its performance. And here are the results:
What can be said from this chart ?
The blue line represents the perfect behavior. When a signal is triggered, the ordre is instantly placed and filled. The dark orange line represents the results in TradingView. The grew line represents the results in the testing environment of the exchange. The light orange line represents the worst scenario : entering the trade on the market and paying the market fees.
The takeaway here is that there are factors that prevent the ML algorithm to perfectly extract the value. The 3 main ones are the time, the orderbook, the fees. Between the time when a candle closes and the time where the order is placed, there is enough time for price to move and those slight movements, when cumulated can reduce the expected performance. Placing an order is one thing, but having it executed is something else. I still need a counterpart to quickly fill my order, which is almost never the case in standard market conditions, simply with the best bid/best ask spread. Finally, I must enter in limit order to benefits from free execution fees, the market fees are bigger than the expected profits by trade.
Which leaves me with one problematic : How can I modify the program so my equity curve gets closer to the blue curve ? It can be rephrase into : How can I instantly submit my order, at the perfect price, without paying the fees ?
Machine Learning v2 — PEWD
Simple answer : it is not possible, or I did not find out.
However, I can make this problematic way more simply by removing the most complex & unalterable factor : time. How ? Simply by focusing on submitting limit orders at the perfect price and wait until the order is filled. Thus come the PEWD.
PEWD is an acronym that stands for Perfect Execution With Delay. More pragmatically, it is a layer of code that slips between the API layer and the main trading bot. Instead of trying to push an order, a bullish/bearish signal is pushed into a stack with its price. Then, the PEWD will only process one order at the time in a First In First Out (FIFO) logic. In other words, as long as an order is not filled, the next one is not push and waits, while other signals might be triggered. Here is how the structure looks like:
Another benefit here is that the stack of signals can be read. As the ML kernel does not provide perfect trades (the average winrate is around 70%), it is possible to delete the signals that would lead to a losing trade or a trade that is close to break even, to only keep the ones that provide value. Then, my winrate move from 70% to guaranteed 100%, with better gains per trade. This function is ensured by the “Sniff” bloc.
Later on, multiple minor upgrades have been brought which moved the bot to V2.8. Here are the main changes:
- Having multiple kernel handling multiple UT, pairs (inverse and USDC based)
- Moving the bot from my laptop to a Virtual Private Server
- Handling errors coming both from client & server
- Adding more indicators : SuperTrend, Chop, RSI derivations
- Adding a drawdown manager that open contract in the opposite direction to reduce the delta value of the account
- Finetuning the hyper parameters
The best setup I found was:
- Indicators : RSI_9, RSI_14, RSI_14 derivative, WaveTrend_10_11, SuperTrend_10_3
- Neighbours = 8
- No filter
When volatility came back, I reduced the leverage and increased the UT but the parameters remained unchanged.
Get Pooki_’s stories in your inbox
Join Medium for free to get updates from this writer.
One surprisingly discovery : I miscoded the derivative calculations and got better results with it. More specifically, all indicators values were normalized between [0;1] but the derivative was [-1;1]. So, whenever the derivative was negative, the signal was always bearish, because of the “oversensitivity” to the downside. It triggered more trades and granted +30% more gains compared to the “correctly coded” version.
Because of the gap between the bot execution and the trade execution on the exchange, I couldn’t simulate the performance and had to be skin in the game. Here is below the representation of my equity curve:
As you can see, it is not lifting off and always have the “eat like a bird, shit like an elephant” pattern. Despite multiple behaviors that push my equity curve down, there is one technical that is related to the ML program : the phase change.
Basically, the market has a pattern A and when it moves to pattern B, the ML program is not able to play it, because it never learnt what pattern B is and continue to play along pattern A. So, there is a transition phase that the bot is not able to handle, until it learns to trade the phase B.
Also, the drawback of PEWD is that a position can be stuck in the opposite direction of the market trend. Worse, it can be stuck shorting a bottom or longing a top.
So, I have to decide whether I want to increase my gains and freeze my drawdowns, or freeze my gains and reduce my drawdowns. As the gains are growing exponentially with a decent performance (between 3% and 5% per day), I prefer to find a way to reduce my drawdowns. Also, drawdowns is not something nice to witness, so let’s get rid of it. Finally, in the worst case scenario, a flash crash/pump can liquidate me.
Multiple Paths for drawdown reduction
4 paths have been identified:
- Use a different financial tool to offset the drawdowns
- Run MLv2 on multiple UT to emulate a LTSM
- Apply ML onto ML
- Research into Deep Learning
Research into Deep Learning
Deep Learning is a sub-domain of Machine Learning, who uses more advanced and adaptative solutions, thanks to neural networks. This doesn’t look successful in my case for 2 reasons:
1) It requires way more ressources in terms of storage and calculation. A 10k€ budget might be required, on top of deep learning expertise that I do not have.
2) Deep learning on time series has not brought a lot of successes based on the research documentation I found. It is less performant that ML.
This path is given up.
Apply ML onto ML
The idea is to extract some data from the ML Traders to put a ML manager on top to detect potential market phase changes. This approach will be parked as it requires a precise definition of the data to extract or days of data collection will be wasted.
Run MLv2 on multiple UT to emulate a LSTM
Long Short-Term Memory is a method to maintain the “learnings” of the model on the short term and long term as well in Deep Learning. This method would help to manager the phase transitions.
However, this is already done by my multi-UT setup and it didn’t help, so it is not a solution here.
Use a different financial tool to offset the drawdowns
Thanks to Deribit and Rogue Academy, I had a great introduction to the world of options via their weekly webinars. I learned that there is a way to have asymetric trades, which is exactly what I needed.
MLv3–0DTE Options
Instead of trading futures, my bot will focus on specific options : 0DTE (0 Days Till Expiry). The benefit of 0DTE is that their theta value (time decay) reaches its peak, meaning that the closer we are from the expiry, the more benefits we can expect. 0DTE requires less time in front of the screen because they only need to make 1 trade a day. However, it requires more capital, we can even say that it has the worst gain/capital_required ratio.
On top of it, there are 3 option combinations that I will focus on : the bull/call spread for uptrends, the bear/put spread for downtrends, the iron condor for ranges.
In the exemple above, you can see that multiple scenarii are bringing benefits on the table, except if I underestimated how right I am. This completely answers the problems met in MLv2. If the market pivot to the direction that I’m not playing, I’m not losing money. Also, as the expiration is in less than 24h, the exit plan is guaranteed to be executed, no shenanigans possible.
It is now a matter of optimisation on which strikes to pick.
For the strike of the long option, it is simple, it just picks the one that it At The Money (ATM, in other words, the closer to the actual price).
For the strike of the short options, it is more complicated. At the time where I write this article, I simply calculates an average of the daily % variation over the last month and found 3% from open price. I’m working on a forecasting model that can anticipate the volatility. Here is an example below:
Here are the performances below so far.
One important thing to precise : the values are anormaly increased. As the Deribit options are used as an inverse contract, the benefits earned are in BTC. So, if the bot earns BTC via a bull call spread and the price goes up, you can consider that the gain is almost multiplied by 2. The test phase has been led through an intense pumping phase, so you can expect twice less in a ranging market and 4x less in a downtrend market.
In other words:
+5% of daily gain in a bullish phase
+2,5% of daily gain in a ranging phase
+ 1,25% of daily gain in a bearish phase
NB : Those results take the executions fees into account, which are not free.
Takeaways
Machine learning is truly a key enabler to automate a trading bot regardless of the market behavior, which will always make it better than simple algo-based trading bots. Don’t look for the amazing super easy indicator that will make your portfolio x1000 in Youtube. However, it needs something else to manage the change of phases and ensure a successful trade execution.
Here, I choose to work with options that grant an asymmetrical trade everyday, no matter how the market behaves. The ML Manager is still in my backlog, but I’m prioritising options until I reach a dead-end.
This opens a very large world of possibilities and optimisations that keep be busy every week-end. At the moment, the optimisations are focuses on the increase of the confidence factor of predictions and better option price selection. A hands-off solution can be established as it is close to be systematic.
I’m looking forward different market phases to assess the resilience of the MLv3 0DTE trading bot.








