genrec_blog
Panfeng Cao/

StockFormer: Stock Price Prediction with Transformer

5 min read

Predicting stock prices has always been a complex and challenging task due to market volatility and the influence of diverse factors. In this blog, we introduce a novel Transformer-based approach for stock price prediction, integrating multimodal data such as stock prices, trading volumes, and financial sentiment analysis.

Highlights

  • Leverages the Transformer encoder architecture for time series modeling.
  • Incorporates multimodal features, including sentiment analysis using FinBERT.
  • Achieves satisfactory performance on key metrics like RMSE, MAE, and MAPE.

Introduction

Stock price prediction has long been a critical focus for investors and researchers. The chaotic nature of the stock market, driven by factors such as macroeconomic trends, geopolitical events, and company fundamentals, makes accurate forecasting difficult. Our model, StockFormer, addresses these challenges by integrating multimodal data to capture complex relationships and dependencies.

Datasets Overview

Our experiments utilized data from three major stocks: Apple (AAPL), Google (GOOG), and Amazon (AMZN).

datasets
Datasets Overview

Model Architecture

The StockFormer architecture builds upon the Transformer encoder framework, incorporating the following components:

  • Input Features: Combines stock prices, trading volumes, and sentiment scores extracted using FinBERT.
  • Positional Encoding: Uses sinusoidal encoding for efficient sequence alignment.
  • Multi-head Self Attention: Captures both short-term and long-term dependencies in the data.
  • Feed Forward Network: Enhances the learning capacity and generalization of the model.

Experimental Results

We conducted extensive experiments to evaluate the effectiveness of StockFormer on multimodal data. Below are the key metrics:

metrics
Metrics (P: Price, V: Volume, S: Sentiment)

Ablation Study

To understand the impact of each feature on the model's performance, we conducted an ablation study. The results demonstrated that incorporating multimodal data (price, volume, and sentiment) significantly improved prediction accuracy.

ablation
Ablation Study (P: Price, V: Volume, S: Sentiment)

Visualization

Below is the visualization comparing predicted and actual Amazon stock prices.

amzn_perf
Prediction on Amazon Stocks

Conclusion

StockFormer reveals the potential of Transformer models in financial data analysis. By integrating multimodal features, it provides a robust framework for stock price prediction, with applications extending to trend prediction and market analysis.

Future work includes exploring additional features such as macroeconomic indicators and company fundamentals, as well as adapting the model for classification tasks like predicting stock movement directions.

author-profile-imageAbout the article

This was my final project of the online education program provided by Professor Mark Vogelsberger.

Presentation of StockFormer