- The AI Finance Frontier
- Posts
- How to use Spatiotemporal Transformers for Stock Prediction?
How to use Spatiotemporal Transformers for Stock Prediction?
Spatiotemporal transformers and clustering methods for developing stock trading strategies
Hi! Here's Iván with this week's exciting newsletter, brimming with insights and discoveries on building robust risk models and trading strategies using Machine Learning. This edition focuses on using spatiotemporal transformers and clustering methods for developing stock trading strategies.
🕹️ 2 Academic Articles: Dive into groundbreaking research featuring actionable ideas that are reshaping our understanding of how to apply ML/DL in creating successful investment and trading strategies.
💊 Video Insight : A user-friendly guide on how to use PCA and RSI for effective trading strategies.
✔️ Market Insight: Best market reflection shared on my LinkedIn/Twitter during the last week: the financial market is always first!
🥐 Book Recommendations: New Year Book Recommendations for Algorithmic Trading.
Academic Insights
“How to use Spatiotemporal Transformers for Stock Prediction?”
Let me summarize the main takeaways of the paper: 👇
💡The Model
The authors propose STST, a novel approach that utilizes a Spatiotemporal Transformer LSTM model for stock movement prediction in two steps:
First, the model uses a transformer-based encoder to capture contextualized embeddings for individual stocks within a specific context window (W).
Second, these contextual embeddings are then processed by a multi-layered LSTM, paired with a dense neural network for final predictions.
💡 Data
For the empirical tests, the authors use two well-known datasets (tick data).
The ACL18 dataset includes historical price data from 88 high-volume stocks on NASDAQ and NYSE, spanning January 1, 2014, to January 1, 2016, divided into training, validation, and test sets. The KDD17 dataset covers January 1, 2007, to January 1, 2017, from the 50 highest volume stocks, also segmented into training, validation, and test phases. Both datasets encompass daily open, close, adjusted close, high, low prices, and trading volumes, excluding weekends.
💡 Results
The authors test the STST model in trading simulations using ACL18 and KDD17 datasets. The simulated agent, starting with $10,000 USD, adjusts its portfolio daily based on predictions. The model's effectiveness is gauged by comparing its returns against the S&P 500 index's performance.
For the ACL18 test range:
STST achieved a return of 15.21%.
S&P 500 index had a return of 0.66%, leading to an annualized STST return of 199%.
For the KDD17 dataset:
STST had a return of 26.78%.
S&P 500 index showed a return of 16.36%, resulting in an annualized STST return of 31.24%.
“Correlation Matrix Clustering for Statistical Arbitrage Portfolios”
Let me summarize the main takeaways of the paper: 👇
The authors of the paper follow a simple two step process;
--> Firstly, the authors identify a group of similar (correlated) assets.
--> Secondly, they construct arbitrage portfolios within those groups of assets.
Let's go into more details:
💡 First Step: Clustering:
The authors calculate market residual returns by subtracting the product of the stock's CAPM beta and market return from the stock returns.
Then, they create a correlation matrix of these residual returns, treat it as a weighted signed network, and use graph clustering algorithms to divide stocks into groups with high intra-group and low inter-group correlation.
In the paper, five clustering algorithms, including two versions of SPONGE clustering, a modified Spectral clustering, and two variants of Signed Laplacian clustering, are used to build statistical arbitrage portfolios.
💡 Second Step: Arbitrage Portfolios
The authors use a rolling window to identify stocks whose returns are above or below the mean returns of their cluster, labeling them as "previous winners" and "previous losers," respectively.
They then construct a contrarian portfolio comprising long positions on previous losers and short positions on previous winners within each cluster, using this portfolio to evaluate if the stocks in each cluster exhibit mean-reversion patterns, i.e., if their returns revert to the mean return of the cluster.
Note that they use all stocks in each identified cluster to construct mean-variance Markowitz portfolios.
💡 Data & Results
The sample period is from January 2000 to December 2022. They include stocks listed on the NYSE, Amex, and NASDAQ exchanges.
The first benchmark used in the paper is the SPY ETF, which serves as a standard market comparison.
The second benchmark is an arbitrage portfolio based on the Fama-French 12 (FF12) industry classifications, constructed similarly to the cluster-driven portfolios, comparing intra-cluster mean-reversion effects to those found by the cluster-driven portfolios.
💣 The best clustering strategy (SPONGE clustering) is able to obtain Sharpe ratios between 1.03 and 1.10.
Finally, note that they found clustering, simply by following the FF12, yields similar results.
AI-Essentials: Step-by-Step Tutorial
🚀 A user-friendly guide on how to use PCA (Principal Component Analysis) and RSI (Relative Strength Index) for effective trading strategies. The tutorial includes easy-to-follow code examples, making it great for those looking to improve their trading methods with these techniques.
The post: Market Ideas
Is inflation in the rearview mirror?
The amount of loans & leases (YoY) is decelerating.
Check the latest update: December 27th. 👇
Books: Recommendation
“New Year Book Recommendations for Algorithmic Trading”
🔔 From Beginners to Experienced Quants!
(1) Hands-On Financial Trading with Python by Jiri Pik & Sourav Ghosh
Ideal for Python beginners in trading.
Academic: ⭐⭐ | Practical: ⭐⭐⭐
(2) Python for Algorithmic Trading by Yves Hilpisch
Basic introduction, practical for intermediate users.
Academic: ⭐⭐ | Practical: ⭐⭐⭐⭐
(3) Mastering Python for Finance by James Ma Weiming
Blends finance principles with Python.
Academic: ⭐⭐⭐⭐ | Practical: ⭐⭐⭐
(4) Machine Learning for Algorithmic Trading by Stefan Jansen
Extensive coverage on varied topics.
Academic: ⭐⭐⭐⭐ | Practical: ⭐⭐⭐
(5) In Pursuit of the Perfect Portfolio by Andrew Lo & Stephen Foerster
Focus on trading theories, based on Nobel Laureates' stories.
Academic: ⭐⭐ | Practical: ⭐⭐
(6) Machine Learning in Finance by Matthew Dixon, Igor Halperin, & Paul Bilokon
In-depth, graduate-level analysis of ML in finance.
Academic: ⭐⭐⭐⭐⭐ | Practical: ⭐⭐
(7) Day Trade With AI by Shunyu Tang
Theoretical and practical aspects of AI in trading.
Academic: ⭐⭐⭐ | Practical: ⭐⭐⭐⭐⭐
(8) Advances in Financial Machine Learning by Marcos Lopez de Prado
Cutting-edge exploration of ML applications in finance.
Academic: ⭐⭐⭐⭐⭐ | Practical: ⭐⭐⭐⭐
If you're enjoying our newsletter and want to support us, please recommend it to anyone you know who's interested in AI and Finance. Your referrals are the biggest compliment and help us grow! 🌟🤖💼