Among the influential elements in the national economy is the stock market. The stock market is a multifaceted system that combines economics, investor psychology, and other market mechanics. The objective of the financial market investment is to maximize profits; but, due to the market's complexity and the multitude of factors that might impact it, it is challenging to predict its future behavior. The challenging process of stock price prediction requires the analysis of a wide range of social, political, and economic factors. These variables include market trends, financial statements, earnings reports, and other data. The goal of this project is to develop an accurate hybrid stock price forecasting model using Random Forest which is combined with the optimization. Random Forest is one type of machine learning that is often used in time series analysis. This study provides stock price forecasting using the Hang Seng index market, which consists of the largest and most liquid corporations that are publicly traded on the Hong Kong Stock Exchange, data from 2015 to 2023. The Dow Jones and KOSPI were evaluated as two additional indices. This study demonstrates some optimization approaches including genetic algorithm, grey wolf optimization, and biogeography-based optimization, which drew inspiration from the phenomenon of species migrating between islands in search of a suitable habitat. Biogeography-based optimization has shown the best result among these optimizations. The proposed hybrid model obtained the values 0.992, 0.997, and 0.9937 for the coefficient of determination for HSI, Dow Jones, and KOSPI markets, respectively. These results indicate the ability of the model in order to predict the stock market with a high degree of accuracy.