Quantformer: From attention to profit with a quantitative transformer trading strategy

Zhaofeng Zhang, Banghao Chen, Shengxin Zhu, Nicolas Langrené

TL;DR

This paper introduces "Quantformer," a novel transformer-based architecture designed to process numerical financial data for quantitative trading. By adapting the transformer's attention mechanism to time-series financial data and incorporating market sentiment, Quantformer aims to improve the accuracy of stock prediction and generate profitable trading signals, outperforming traditional factor-based strategies.

What Problem Does This Paper Address?

The financial market is notoriously complex and dynamic, making it challenging for traditional quantitative trading strategies to consistently capture market variables and identify profitable signals. Existing machine learning approaches often struggle with the inherent characteristics of financial data, such as its numerical nature and the need to capture long-range dependencies and market sentiment simultaneously. This paper addresses the gap in applying transformer architectures, which have excelled in natural language processing, to the domain of quantitative finance, specifically for stock prediction and factor generation.

Methods

The core innovation is Quantformer, a modified transformer architecture. Key adaptations include:

  • Numerical Input Handling: Unlike standard transformers that rely on word embeddings for text data, Quantformer uses a linear layer to directly process numerical time-series data (stock prices, turnover rates, etc.). This bypasses the need for word embeddings and allows for direct input of financial features.
  • Simplified Decoder: The decoder is simplified by removing the autoregressive prediction mechanism and masking operations. Instead, it directly outputs a probability distribution over future price movements, rather than generating sequences of tokens. This is more suitable for direct price prediction tasks.
  • Market Sentiment Integration: The model leverages transfer learning from sentiment analysis and incorporates market sentiment as an input feature, aiming to capture a broader range of market influences.
  • Attention Mechanism Adaptation: The self-attention mechanism is adapted to handle the specific structure of financial time-series data, focusing on capturing dependencies between different time points for each stock.

Datasets

The study utilizes a large dataset comprising over 5,000,000 rolling data points from 4,601 stocks in the Chinese capital market, spanning from January 2010 to May 2023. This data was sourced from AKShare and Tushare, reputable quantitative finance terminals. The dataset includes adjusted closing prices, dividends, and stock splits.

Key Insights and Ideas

  • Transformers are Adaptable: The paper demonstrates that the powerful attention mechanism of transformers can be effectively adapted to numerical time-series data in finance, not just text.
  • Direct Numerical Processing is Key: Replacing word embeddings with linear layers is crucial for efficiently processing financial data, which is inherently numerical.
  • Sentiment Matters: Integrating market sentiment alongside price and volume data significantly enhances the predictive power of trading signals.
  • Simplified Output for Prediction: For direct price prediction, a simplified decoder that outputs a probability distribution is more effective than sequence-to-sequence generation.

Key Results

The Quantformer-based trading strategy demonstrated strong performance in backtests:

  • Superior Returns: Quantformer-based factors consistently outperformed traditional price-volume factors and the CSI 300 benchmark. For instance, QF_Month_1 achieved an annual return (AR) of 17.35% and an annual excess return (AER) of 19.43%.
  • Robust Risk-Adjusted Performance: The strategy showed favorable Sharpe Ratios, indicating sound risk-adjusted returns. QF_Month_1 achieved a Sharpe Ratio of 0.915, significantly higher than other factors.
  • Positive Alpha: The positive alpha values suggest that the strategy consistently outperformed expected returns after accounting for risk factors.
  • Low Downside Risk: The 99% Value at Risk (VaR) for Quantformer strategies was among the best 10% of factors, indicating a lower potential for extreme losses.
  • Robustness Across Frequencies: The model performed well across different time frequencies (monthly, weekly, daily) and training scales, suggesting its robustness.

Limitations and Future Work

  • Short Selling Restriction: The current trading strategy does not allow for short selling, which is a limitation in markets where it is permitted.
  • Market Specificity: While tested on Chinese market data, further validation on diverse global markets would be beneficial.
  • Future Work: The authors suggest further analysis and investigations into ML-based quantitative methods, implying potential for exploring more complex market dynamics and integrating additional data sources. The implementation code is made publicly available, encouraging further research.

Summary updated

BibTeX
@article{zhang2026quantformer,
  author = {Zhaofeng Zhang and Banghao Chen and Shengxin Zhu and Nicolas Langrené},
  title = {{Quantformer: From attention to profit with a quantitative transformer trading strategy}},
  journal = {Expert Systems with Applications},
  year = {2026},
  url = {https://openreview.net/forum?id=N2AdlqCgK2},
}
Figure