The present paper deals with optimizing the stock portfolio of active companies listed on the Tehran Stock Exchange based on the forecast price. This paper is based on a combination of different filtering methods such as optimization of trading rules based on technical analysis (ROC, SMA, EMA, WMA, and MACD at six levels—Very Very Weak (VVW), Very Weak (VW), Weak (W), Strong (S), Very Strong (VS), and Very Very Strong (VVS)), Markov Chains, and Machine Learning (Random Forest and Support Vector Machine) Filter stock exchanges and provide buy signals between 2011 and 2020. In proportion to each combination of filtering methods, a buy signal is issued and based on the mean-variance (M-V) model, the stock portfolio is optimized based on increasing the portfolio return and minimizing the stock portfolio risk. Based on this, out of 480 companies listed on the Tehran Stock Exchange, 85 active companies have been selected and stock portfolio optimization is based on two algorithms, MOGWO and NSGA II. The analysis results show that the use of SVM learning machine leads to minor correlation error than the random forest method. Therefore, this method was used to predict stock prices. Based on the results, it was observed that if the shares of companies are filtered, the risk of transactions decreases, and the return on the stock portfolio increases. Also, if two filtering methods are applied simultaneously, the stock portfolio returns slightly and the risk increases. In the analysis, MOGWO algorithm has obtained 133.13% stock return rate with a risk of 3.346%, while the stock portfolio returns in NSGA II algorithm 107.73, with a risk of 1.459%. Comparison of solution methods shows that the MOGWO algorithm has high efficiency in stock portfolio optimization.