# STGAT: Spatial-Temporal Graph Attention Neural Network for Stock Prediction
## 4 Experiments
### Dataset Settings  
This work used the sliding window method to split time series data and construct graph data. The sliding window had a size of 20 trading days and a step size of 5 trading days, chosen to balance short-term market dynamics and computational efficiency. The 20-day window aligns with the typical monthly trading cycle to capture recurring patterns, while the 5-day step ensures sufficient overlap to smooth transient noise without oversampling redundant signals. After dividing the historical stock price data into 20-day periodic segments, STL was applied to extract the trend, seasonal, and residual components of stock data. Subsequently, all stocks' corresponding periodic data and components were combined with the edge weight matrix generated in Section 3.2 to form a periodic graph structure. As a result, the graph dataset containing primary stock relationship was generated with its decomposition features for supplement. In this study, we propose two methods for dividing the dataset. These methods were designed to evaluate the performance of the investment portfolio under different market conditions, taking into account the variability and unpredictability inherent in financial markets. The detailed splitting methods are outlined as follows:  
* Method I: Dividing the original dataset into a 9:1 ratio based on the time range. The test set is defined as the continuous trading days between September 2023 and April 2024. The performance of the portfolio during this fixed time period is compared with the real index and actual performance of each investment portfolio. * Method II: This method uses a simulation environment where 10% of non-continuous trading days are randomly selected from the overall dataset's time range as the testing interval. This approach simulates the unpredictable nature of financial markets and helps evaluate the portfolio in different market conditions, ensuring that the model can handle diverse trends in the stock market. To better evaluate the model's adaptability and risk management capabilities in both the A-share and US stock markets, these methods are applied to simulate the inherent uncertainty of the stock market. Furthermore, this study employed the K-fold time series validation method to better evaluate and enhance the predictive performance of models applied to stock price datasets. Unlike standard cross-validation, K-fold time series validation preserves the temporal order of observations by sequentially partitioning the time series into chronologically ordered folds. This approach ensures the model is trained on historical data and validated on subsequent periods, rigorously simulating real market forecasting scenarios. Therefore, it mitigates overfitting risks inherent in random cross-validation approaches, which could leak future information into training phases when applied to time series data. Figures 7 and 8 show the loss curves under two cross-validation methods. To be noticed, the loss curve of five-fold time series validation exhibits periodic oscillations at every 100-epoch interval (aligned with fold transitions), which can be attributed to \begin{table}
\begin{tabular}{l l l l l} \hline
**Market Index** & **Stock Count** & **Trading Days** & **Train Dataset** & **Test Dataset** \\ \hline CSI500 & 304 & 2170 & 1953 & 217 \\ S\&P500 & 462 & 2330 & 2097 & 233 \\ \hline \end{tabular}
\end{table} 
Table 1: Dataset description. our strategy of resetting the optimizer state at the beginning of each fold. Resetting the optimizer strictly isolates training dynamics between folds, aligning with the independence principle of cross-validation and avoiding overfitting performance. In order to achieve better returns, a dynamic investment strategy is applied to construct portfolios with the highest expected investment values. This strategy aims to optimize capital allocation and ensure that investment decisions are responsive to market dynamics. Based on the STGAT model, predictions generated are used to formulate predictive returns.