PurposeIn this study, to maximize returns and minimize investment risk, an attempt was made to form an optimal portfolio under conditions where the capital market has a price bubble. According to the purpose, the research was of the applied type, in terms of data, quantitative and postevent, and in terms of the type of analysis, it was of the descriptive-correlation type. Sequence, skewness and kurtosis tests were used to identify the months with bubbles from 2015 to 2021 in the Tehran Stock Exchange. After identifying the bubble courses, artificial bee colony meta-heuristic and invasive weed algorithms were used to optimize the portfolio. The purpose of this paper is to address these issues.Design/methodology/approachThe existence of bubbles in the market, especially in the capital market, can prevent the participation of investors in the capital market process and the correct allocation of financial resources for the economic development of the country. However, due to the goal of investors to achieve a portfolio of high returns with the least amount of risk, there is need to pay attention to these markets increases.FindingsThe results identify 14 periods of price bubbles during the study period. Additionally, stock portfolios with maximum returns and minimum risk were selected for portfolio optimization. According to the results of using meta-heuristic algorithms to optimize the portfolio, in relation to the obtained returns and risk, no significant difference was observed between the returns and risk of periods with price bubbles in each of the two meta-heuristic algorithms. This study can guide investors in identifying bubble courses and forming an optimal portfolio under these conditions.Research limitations/implicationsOne of the limitations of this research is the non-generalizability of the findings to stock exchanges of other countries and other time periods due to the condition of the price bubble, as well as other companies in the stock market due to the restrictions considered for selecting the statistical sample.Originality/valueThis study intends to form an optimal stock portfolio in a situation where the capital market suffers from a price bubble. This study provides an effective and practical solution for investors in the field of stock portfolio optimization.