A Comparative Analysis of XGBoost and Random Forest for Time Series Based Stock Price Prediction with Directional Movement Evaluation


  • Andri Fahmi * Mail Universitas Pamulang, Tangerang Selatan, Indonesia
  • Nur Rofiq Universitas Pamulang, Tangerang Selatan, Indonesia
  • (*) Corresponding Author
Keywords: Stock Price Prediction; XGBoost; Random Forest; Time Series; Yahoo Finance

Abstract

Stock price prediction remains a complex task due to the dynamic nature of financial time series and the difficulty of extracting informative patterns from historical price movements. This study addresses the need to better understand whether the choice of model or the design of time series features plays a more dominant role in prediction performance. The objective of this research is to comparatively evaluate Extreme Gradient Boosting (XGBoost) and Random Forest for stock price prediction using engineered time series features, while also assessing their ability to capture directional price movements. The proposed approach applies a structured pipeline involving data preprocessing, extraction of time series features (lag, moving average, and volatility), and evaluation using a time-aware data split to preserve temporal order. Unlike conventional studies that focus solely on prediction accuracy, this research integrates both regression-based evaluation (RMSE, MAE, and R²) and directional movement analysis using confusion matrix, along with feature importance interpretation to understand model behavior. The experimental results, based on 1,258 daily stock price records, show that XGBoost achieved an RMSE of 457.97, MAE of 345.28, and R² of 0.884, while Random Forest obtained an RMSE of 462.01, MAE of 351.02, and R² of 0.882. The difference in R² (0.002 or 0.2%) indicates that both models perform comparably, with no substantial performance gap. Directional evaluation further reveals that both models are more accurate in predicting upward trends than downward movements. These findings suggest that feature engineering plays a more critical role than model selection in this context, providing a practical contribution to the development of stock prediction systems.

Downloads

Download data is not yet available.

References

Assaduddiari, I. G., Kurdhi, N. A., Siri, Z., & Aruchunan, E. (2025). Forecasting Tesla Stock Price Using XGBoost, Random Forest, and CatBoost: A Comparative Study with TreeSHAP Interpretation of the Best Model. 2025 International Conference on Artificial Intelligence and Technological Solutions (ICAITech), 309–314. https://doi.org/10.1109/ICAITech66481.2025.11387624

B, Y. Z. (2023). Stock Price Prediction Method Based on XGboost Algorithm. Atlantis Press International BV. https://doi.org/10.2991/978-94-6463-030-5

Bettahi, A., Belouadha, F. Z., & Harroud, H. (2025). A Modular and Explainable Machine Learning Pipeline for Student Dropout Prediction in Higher Education. Algorithms, 18(10), 1–31. https://doi.org/10.3390/a18100662

Dehvari, M., Farzaneh, S., & Forootan, E. (2025). Forecasting rainfall events based on zenith wet delay time series utilizing eXtreme gradient boosting (XGBoost). Advances in Space Research, 75(3), 2584–2598. https://doi.org/https://doi.org/10.1016/j.asr.2024.11.013

Ferrouhi, E. M., & Bouabdallaoui, I. (2024). A comparative study of ensemble learning algorithms for high-frequency trading. Scientific African, 24, e02161. https://doi.org/https://doi.org/10.1016/j.sciaf.2024.e02161

Gono, D. N., Napitupulu, H., & Firdaniza. (2023). Silver Price Forecasting Using Extreme Gradient Boosting (XGBoost) Method. Mathematics, 11(18). https://doi.org/10.3390/math11183813

Hossain, S., & Kaur, G. (2024). Stock Market Prediction: XGBoost and LSTM Comparative Analysis. 2024 3rd International Conference on Artificial Intelligence For Internet of Things (AIIoT), 1–6. https://doi.org/10.1109/AIIoT58432.2024.10574794

Kulkarni, M. S., Vijayakumar Bharathi, S., Perdana, A., & Kilari, D. (2025). A Quest for Context-Specific Stock Price Prediction: A Comparison Between Time Series, Machine Learning and Deep Learning Models. SN Computer Science, 6(4), 335. https://doi.org/10.1007/s42979-025-03848-y

Lina, S., Sitio, M., Samudra, Y., Informatika, T., Inggris, S., & Pamulang, U. (2026). Comparative Analysis Of Bagging And Boosting Models In. 11(3), 979–986. https://doi.org/10.33480/jitk.v11i3.7579.COMPARATIVE

Liu, J., Zhang, S., & Fan, H. (2022). A two-stage hybrid credit risk prediction model based on XGBoost and graph-based deep neural network. Expert Systems with Applications, 195, 116624. https://doi.org/https://doi.org/10.1016/j.eswa.2022.116624

Mostafavi, S. M., & Hooman, A. R. (2025). Key technical indicators for stock market prediction. Machine Learning with Applications, 20, 100631. https://doi.org/https://doi.org/10.1016/j.mlwa.2025.100631

Nabilah Selayanti, Dwi Amalia Putri, Trimono Trimono, & Mohammad Idhom. (2025). Prediksi Harga Penutupan Saham Bbri Dengan Model Hybrid Lstm-Xgboost. Informatika: Jurnal Teknik Informatika Dan Multimedia, 5(1), 52–64. https://doi.org/10.51903/informatika.v5i1.1011

Noorunnahar, M., Chowdhury, A. H., & Mila, F. A. (2023). A tree based eXtreme Gradient Boosting (XGBoost) machine learning model to forecast the annual rice production in Bangladesh. PLOS ONE, 18(3), 1–15. https://doi.org/10.1371/journal.pone.0283452

Nugroho, W. A., Rachman, F. D., Siahu, B. K., Iswanto, I. A., & Joddy, S. (2025). Hybrid Ensemble Model Approaches for Stock Price Forecasting Using LSTM, Random Forest, ARIMA, and Linear Regression as Meta-Learner. Procedia Computer Science, 269, 901–910. https://doi.org/https://doi.org/10.1016/j.procs.2025.09.033

Ritonga, A., Ma, A., & Suwarno, I. (2024). Stock Price Forecasting with Multivariate Time Series Long Short-Term Memory : A Deep Learning Approach. 5(5), 1322–1335. https://doi.org/10.18196/jrc.v5i5.22460

Sharma, A. K., Li, L. H., & Ahmad, R. (2023). Default Risk Prediction Using Random Forest and XGBoosting Classifier. In Smart Innovation, Systems and Technologies (Vol. 314). Springer International Publishing. https://doi.org/10.1007/978-3-031-05491-4_10

Vuong, P. H., Dat, T. T., Mai, T. K., Uyen, P. H., & Bao, P. T. (2022). Stock-Price Forecasting Based on XGBoost and LSTM. https://doi.org/10.32604/csse.2022.017685

Yang, T. (2023). Sales Prediction of Walmart Sales Based on OLS, Random Forest, and XGBoost Models. Highlights in Science, Engineering and Technology, 49. https://doi.org/10.54097/hset.v49i.8513

Yi, S. (2023). Walmart Sales Prediction Based on Machine Learning. Highlights in Science, Engineering and Technology, 47. https://doi.org/10.54097/hset.v47i.8170

Yun, K. K., Yoon, S. W., & Won, D. (2021). Prediction of stock price direction using a hybrid GA-XGBoost algorithm with a three-stage feature engineering process. Expert Systems with Applications, 186, 115716. https://doi.org/https://doi.org/10.1016/j.eswa.2021.115716


Bila bermanfaat silahkan share artikel ini

Berikan Komentar Anda terhadap artikel A Comparative Analysis of XGBoost and Random Forest for Time Series Based Stock Price Prediction with Directional Movement Evaluation

Dimensions Badge
Article History
Published: 2026-05-10
Abstract View: 117 times
PDF Download: 126 times
Issue
Section
Articles