Random forest algorithm is a kind of integrated learning algorithm with strong universality, high prediction accuracy and not easy to overfitting, and strong stability in stock index prediction application. This study constructs a stock index prediction model based on the random forest algorithm, and predicts the stock index futures price state according to the iteration of the decision tree in the random forest algorithm. Then we propose to use the regular term and ARMA-GARCH time series forecasting model to optimize the overfitting and large forecasting errors in the Random Forest model to achieve the construction of stock index forecasting optimization model. It is verified that the average absolute error of the random forest optimization model proposed in this paper is only 0.0316 in stock index forecasting, and the robustness in stock index forecasting is excellent. The empirical application results of stock index forecasting show that the accuracy of this paper’s model for CSI 300 and CSI 500 indexes is above 90%, and the total return of the strategy during the backtesting period is relatively high. The practical application of the stock index forecasting model proposed in this study has the value of further research, which can provide reference and guidance for investors.