In part 1 of this series, we introduced the process of training a neural network to identify uptrends in prices. We went over the difference between regressors and classifiers and why we prefer the latter. Then, we built a CNN-LSTM and trained it to identify trends in the SPDR Gold Trust (GLD).
Neural networks are the backbone of the current generation of artificial intelligence, including LLM’s like GPT and Gemini, so we can confidently say that our humble CNN-LSTM is a kind of AI.
What we will do here is illustrate how this AI model can be deployed to form an actual trading strategy and perform a backtest using the FX major pairs. Our model actually did pretty well and shows some promise!
Quick Recap
We are training a CNN-LSTM model to identify uptrends in the prices of a stock, FX pair, or other security. Because neural networks need labelled data in order to “learn” its complex relationships, we used a trend signal to label historical time series. The signal is simple - it uses hindsight to identify when a hypothetical trader could have bought or sold in order to avoid drawdowns beyond a given threshold.
This neural network, then, is trying to learn how to be this hypothetical perfect trader. It takes the contexts for the optimal buy and sell points and tries to predict if a new context means that there is an uptrend in the security.
What contexts did we use? For this round, we used technical indicators on the price series - SMA, EMA, RSI, MACD, and Bollinger Bands. These formed the feature set, in machine learning parlance, for each series. The idea is that we want to see if our AI could make perfect trades using those indicators for each FX pair.
Identifying a Trend
We trained a model for each FX pair using historical daily data for 2006-2023, with 2022-2023 used as the validation set for training. Then, 2023-Jan 2024 formed our test set or holdout set, which totaled 252 bars.
Let’s look at the the model we trained on CADCHF:
There’s a lot going on in this graph, so let’s break it down.
The left y-axis plots the CADCHF price for the duration of our test period. It was mostly ranging during this period and didn’t form an actual trend. When the price line is red, our actual signal (what the model is trying to learn) has a value of 0 for no trend. When the price line is blue, the actual signal is signaling that a trend is occurring. Our signal treats the tail end of the series as a trend because we have not hit a 10% drawdown after the previous drawdown.
The right y-axis plots the models raw output for each day. Because we are using a binary classifier, the outputs range form 0 to 1 (outputs of a sigmoid activation function). We can roughly interpret these values as the model’s predicted probability that we are in an uptrend.
With CADCHF, our model is never expressing much confidence that a trend is occurring. Its most certain predictions first occur in July 2023, when the price was dropping. If we were forming a trading strategy on CADCHF alone, we would need to decide a model output threshold that would trigger a buy signal. If we wanted our model to be “80% sure” of a trend, for example, we would ignore signals below 0.8 and would not hold CADCHF at any point during this period.
So even though the model expressed its highest certainty at the wrong time, it never had a lot of conviction. I would interpret those as “IDK, maybe?”
Let’s look at a completely different case:
Both the actual prices and model predictions for CHFJPY form a completely different picture. Here, CHFJPY was actually in an uptrend the entire period. It never had a drawdown of over 10%.
And look at our model’s predictions. I would interpret that output as our robot trader holding a big neon sign saying “BUY THIS OR YOU’RE STUPID”.
Forming an AI Trading Strategy
We can use our model outputs for the 28 FX major pairs to simulate a trading strategy.
For this test, we wanted to long the FX pair with the highest conviction output from its model. We wanted to get into the most likely trend every 28 days (roughly a month). So each month, we ask our robot trader which FX pair it thinks is in an uptrend. Then, we buy that for 28 days and repeat.
What happened here was interesting. Our model had more conviction with CHFJPY than with any other pair. This strategy would have been long CHFJPY for the entire period, in other words.
And assuming buying and holding, that would have actually worked. CHFJPY outperformed the 27 other pairs for the period.
But was this the optimal pair for each rebalancing period? This graph illustrates:
The blue line in this chart shows a hypothetical portfolio over time. At each rebalancing period, we would have closed our previous position and opened another in the FX pair with the highest trend probability. The blue line shows what this strategy actually did.
An actual perfect strategy would have gone long the FX pair with the highest performance for the period. Because no other model had such high conviction, we did not buy any alternatives. CHFJPY was at or near the top of the pack in many periods, but not all. We left some outperformance on the table, in other words.
Still, these results are encouraging! We would not consider this a comprehensive backtest by any means, but it is a cause for some optimism about this modeling approach.
In future posts, we would like to explore other security sets, other feature sets, and other model architectures. There is lot more ground we can cover with AI.
Until next time, stay on the cutting edge, everyone.
Resources
Trend Following and Deep Learning, Part 1
References
Eggebrecht, Patrik & Lütkebohmert, Eva. (2023). A Deep Trend-Following Trading Strategy for Equity Markets. The Journal of Financial Data Science. 5. jfds.2023.1.120. 10.3905/jfds.2023.1.120.
Disclaimers
The content on this page is for educational and informational purposes only. Any views and opinions expressed belong only to the writer and do not represent views and opinions of people, institutions, or organizations that the writer may or may not be associated with.
No material in this page should be construed as buy/sell recommendations, investment advice, determinations of suitability, or solicitations. Securities investment and trading involve risks, and not all risks are disclosed or discussed here. Loss of principal is possible. You are encouraged to seek financial advice from a licensed professional prior to making transaction decisions.
Further, you should not assume that the future performance of any specific investment or investment strategy will be profitable or equal to corresponding past performance levels. Past performance does not guarantee future results.