1 Introduction

Today's world is a world of change and knowing what awaits us in the future can greatly contribute to the success of individuals and organizations. In the past, traders took a lot of risks and always looked for a partner to share their profits and losses with, and everyone shared in the profits and losses according to the amount of their capital.

"Stock exchange" means an organized and formal capital market in which shares of companies and participation bonds are traded under certain terms and conditions. The stock exchange is a formal and reliable reference for attracting capital from individuals and the private sector to finance long-term investment projects.

With the continuous development of society's economy, the emergence of capital markets in countries has increased rapidly, so the investor needs powerful and reliable tools to predict stock prices and the ability to buy the stock they need. Stock prices are basically divided into four categories: dynamic, nonlinear, nonparametric and chaotic (Oh & Kim, 2002). Stock price forecasting has been considered by many stock market participants and traders for many years. Even today, buying and selling small stocks of listed companies is attractive to many ordinary people who do not have much capital. Stock market indices fluctuate widely and affect the amount of investment people make. Stock traders are looking for ways to increase their capital gains by predicting future stock prices. Therefore, it seems necessary that appropriate, correct and science-based methods in determining the future price of stocks be presented to investors.

In recent years, various methods for forecasting stock prices have been examined like the artificial neural network (ANN) method, which does not work properly due to many parameters in stock price forecasting and analysis (Tao et al., 2004). Support vector machine (SVM) is also mentioned as one of the efficient algorithms for stock price forecasting, which, unlike artificial neural networks, does not stick to local minimums and seeks optimal solutions globally while artificial neural networks are placed in local optimizations (Cherkassky & Ma, 2004). In support vector machine learning algorithm, regulator parameters and algorithm kernel parameters play an important role therefore, it is necessary to choose these parameters. The impact and importance of these parameters have been discussed in (Alvarez Meza et al., 2012).

In this paper, we try to predict the stock prices of Amazon and several other companies using the support vector machine, which is one of the machine learning (ML) approaches. We also use the radial base function (RBF) for the support vector machine. Finally, we try to use the particle swarm optimization (PSO) algorithm to optimize the parameters in the algorithm so that we can get the best values for the algorithm and thus get the best prediction. Although many studies have been done in the field of stock price forecasting using RBF kernel support vector machine and particle swarm optimization algorithm, in none of them the accuracy of forecasting the proposed method has reached over 90% and at best, they were about 70 percent (Karazmodeh et al., 2013). In this paper, by accurately and step-by-step optimizing the variables C and \(\updelta \), as well as by selecting the appropriate parameters of the PSO algorithm, we were able to achieve above 90% accuracy in forecasting stock prices in all cases.

Our main motivation for doing this research is to provide a reliable way to predict stock prices so that stock market participants and the public can safely invest in the stock market and contribute to the growth and prosperity of their community economy. Our contributions in this paper are as follows. First, we improve the support vector machine algorithm for stock price forecasting using the radial basis function and particle swarm optimization algorithm and second increase the stock price forecasting accuracy and reduce investment risk.

The rest of the paper is as follows: Sect. 2 provides a brief summary of related stock price forecasting work. Section 3 describes and review the support vector machine algorithms, the radial base function, and the particle swarm optimization algorithm. Eventually, we create the proposed model in Sect. 4 and examine it. The results obtained and the evaluation of the model are done in Sect. 5, and finally in Sect. 6 we give a general conclusion of the proposed model.

2 Related Work

Given the significant gains and losses that may occur in the stock market, many methods for predicting stock prices have been explored to date. Hegazy et al. (2014) using the least squares support vector machine algorithm (LS-SVM) and particle swarm optimization algorithm (PSO) were able to increase the accuracy of stock price forecasting compared to methods such as artificial neural network (ANN). They also managed to avoid the over-fitting that happens in ANN'S method but the lack of forecast parameters and lack of explanation of existing parameters is one of the drawbacks of this project. Hafezi et al. (2015) using the bat-neural network multi-agent system (BNNMAS), they tried to pre-process in parallel on the data and increase the accuracy of stock price forecasting compared to ANN and genetic algorithm neural network (GANN) methods. They also proved that this method is suitable for forecasting stock prices in the long run.

Reddy (2018) using support vector machine (SVM) by radial basis function (RBF) kernel tried to develop financial data forecasting. Bruno Miranda Henrique et al. (2018) using support vector regression (SVR) method, they tried to reduce the prediction error compared to the proposed random stage model, efficient market hypothesis (EMH) and zero return random model. Unfortunately, this method does not specify items such as transaction costs or portfolio risk levels and uses a set of fixed tutorials at current prices. Rachna Sable et al. (2019) have presented an article reviewing machine learning algorithms for predicting stock prices. Hernández-Álvarez et al. (2019) used random trees (RT) and multilayer perceptron (MP) algorithms to predict stock prices and were able to increase forecast accuracy and use computers as a tool for automated data processing and data mining. Soroda Basak et al. (2019) provides a model for predicting stock movement in long-term forecasts using random forests (RF) and gradient boosted decision’s trees (GBDT). Mehar Vijh et al. (2020) using artificial neural network (ANN) and random forest (RF), they have predicted the closing price of stocks and have increased the accuracy of the forecast, but unfortunately, they do not take into account financial parameters such as final price, profit and loss, etc. JinShan Yang et al. (2020) used gradient boosting (GBDT) to predict stock prices. They first used natural language processing to deal with the text. Then, they reduce dimension by PCA, finally, with GBDT, they have forecast stock prices. Amanat Ullah et al. (2021) placed a lot of emphasis on feature selection and extracted 28 features from stock data to predict stock prices. They also combined machine learning classifiers for stock trading and were able to prove that group learning in stock trading works better than using a single algorithm. Sohrab Mokhtari et al. (2021) predicted stock prices by incorporating artificial intelligence into machine learning algorithms.

Table 1 shows some of the work done on stock price forecasting using machine learning algorithms and outlines some of the advantages and disadvantages of these methods.

Table 1 A summary of related works is considered

3 Review of SVM and PSO

Considering the importance of the support vector machine algorithm and the role of the kernel in it and the importance of the particle swarm optimization algorithm in the proposed method (SVM-PSO), in this section we explain these algorithms and their application in our proposed method.

3.1 Support Vector Machine (SVM)

The original SVM algorithm was invented in 1963 by Vladimir Vepnik and was generalized by Vapnik and Cortes to the nonlinear mode (1995). Support vector machine is a supervised machine learning model used to categorize data. After giving the SVM dataset to the training tags for each group, they can categorize the new data. SVM is also used in regression.

The purpose of the SVM algorithm is to find a decision boundary called a hyper-plane in a N-dimensional space (N-number of attributes) that specifically classifies data points. Maximizing the distance between the nearest points of each class and the hyper-plane leads to the separation of the hyper-plane. This distance is called the margin. The main purpose of SVM is to find the optimal hyper-plane because it not only categorizes existing data sets but also helps predict unseen data classes. The hyper-plane is the ideal aircraft with the most margins.

The general equation of a hyper-plane is as follows:

$$ F\left( x \right) = W^{T} X + b = 0 $$
(1)

where W is the weight vector, X is the input vector, and \(b \in R\). The formula for calculating the margin is as follows:

$$ m = \frac{2}{\left\| W \right\|} $$
(2)

To increase the margin, we must minimize the denominator of the fraction (\(\left( {\left\| W \right\|} \right)\)). For convenience and to use linear algebra, instead of minimizing \(\left\| W \right\|\), we minimize the following function:

$$ \min \frac{1}{2}\left\| W \right\|^{2} \quad \quad \quad \left( { \frac{1}{2}{\text{is convenient for taking derivative later on}}} \right) $$
(3)

There are many cases in which data cannot be fully categorized through linear segregation. In such cases, the support vector machine seeks out the extraordinary aircraft that maximizes the margin and minimizes incorrect classifications. For this, we introduce the slack variable, \( \varepsilon_{i} \) which allows some objects to fall off the margin but it penalizes them. The slack variables \(\varepsilon_{i} > 1\) for misclassified points, and \(0 < \varepsilon_{i} < 1\) for points close to the decision boundary.

In this scenario, the algorithm tries to keep the slack variable zero while maximizing the margin. However, it minimizes the total distance of incorrect classification from marginal hyper-plane and the number of incorrect classifications. The optimization problem (3) is now as follows:

$$ \min \frac{1}{2}\left\| W \right\|^{2} + C\mathop \sum \limits_{i = 1}^{N} \varepsilon_{i} $$
(4)

Here, the parameter C is the regularization parameter that controls the trade-off between the slack variable penalty (misclassifications) and width of the margin and N is the number of slack variables. Selecting the C-parameter is very important because: Small C makes it easy to ignore constraints, which leads to large margins and contains a lot of unwanted data. Also in this case the training error and the test error are both large and we get under fitting. Large C allows the constraints hard to be ignored which leads to a small margin. In this case, our training error is small while the test error is high. In other words, in this case we suffer from over fitting. There are no rules or solutions for choosing the value of C. In this paper, we use the particle swarm optimization (PSO) algorithm to select the optimal value of parameter C optimally.

3.1.1 Kernel Functions

For SVM, the easiest way to separate two data classes is one line for two-dimensional data and one page for three-dimensional data. But lines or planes cannot always be used, and we sometimes need a nonlinear region to separate these classes. Support vector machine manage such situations using a kernel function that maps data to another space and can be used as a linear hyper-plane to separate classes. This kernel trick is known to turn the data kernel performance into a higher next feature space to allow linear separation.

There are several functions for the kernel K, including:

  • Linear kernel \(K\left( {X,X_{i} } \right) = X_{i}^{T} X\) (5)

  • Polynomial kernel of degree d \(K\left( {X,X_{i} } \right) = \left( {1 + X^{T} X_{i} } \right)^{d} \quad {\text{for}}\;{\text{ any}}\;d > 0\) (6)

  • Multi-layer perceptron (MLP) kernel \(K\left( {X,X_{i} } \right) = \tanh \left( {KX_{i}^{T} X + \theta } \right)\) (7)

  • Radial basis function (RBF) kernel \(K\left( {X,X_{i} } \right) = \frac{{\exp ( - X - X_{i}^{2} }}{{2\delta^{2} }}\quad {\text{for}}\;{\text{ any}}\;\delta > 0\) (8)

In this paper, we use the kernel of the radial basis function for the support vector machine. The choice of parameter \(\delta\) in RBF is of great importance. If the \(\delta\) parameter is selected correctly, the accuracy of the algorithm will increase. So far, unfortunately, there are no exact rules or solutions for choosing parameter \({\updelta }\). In this paper we use the particle swarm optimization (PSO) algorithm to select the optimal value of parameter \(\delta\). The proposed algorithm is implemented in Google Colab using python language. Algorithm1 shows the pseudo-code of the SVM algorithm with the RBF kernel and the selection of parameters by PSO.

figure a

.

3.2 Particle Swarm Optimization (PSO) Algorithm

Particle swarm optimization algorithm is one of the most important intelligent optimization algorithms in the field of collective intelligence. The PSO implements a model for us that is based on sharing information and experiences. This algorithm was introduced by James Kennedy and Eberhart (1995). The PSO algorithm is inspired by the social behaviour of animals such as fish and birds. These animals live together in small and large groups. In the PSO algorithm, the members of the population of answers are directly related to each other and solve the problem by exchanging information with each other, recalling good memories of the past. Like other collective intelligence algorithms, the PSO algorithm uses a set of possible answers. The algorithm continues until an optimal answer is found or the conditions for the end of the algorithm are met. In this method, each x response is represented as a particle, and a group of particles is a set of responses. In PSO, the velocity equation guarantees the movement of particles towards the optimal region. This equation is usually based on three main elements: The speed of stillness, cognitive component (Pbest), collective component (Gbest). In algorithm simulation, the behavior of each particle can be influenced by the best local particle within a given neighborhood or the best general particle.

In general, if \(\overrightarrow {{X_{i} }} \left( t \right)\) represents the position of the \(P_{i}\) particle in the search space at moment t, position \(P_{i}\) changes by adding speed \(\overrightarrow {{V_{i} }} \left( t \right)\) to the current position as follows:

$$ \overrightarrow {{X_{i} }} \left( t \right) = \overrightarrow {{X_{i} }} \left( {t - 1} \right) + \overrightarrow {{V_{i} }} \left( t \right) $$
(9)
$$ \overrightarrow {{V_{i} }} \left( t \right) = \overrightarrow {{V_{i} }} \left( {t - 1} \right) + c_{1} r_{1} \left( {\overrightarrow {{P_{b} }} - \overrightarrow {{X_{i} }} \left( {t - 1} \right)} \right) + c_{2} r_{2} \left( {\overrightarrow {{G_{b} }} - \overrightarrow {{X_{i} }} \left( {t - 1} \right)} \right) $$
(10)

where \(\overrightarrow {{V_{i} }} \left( t \right)\) is the velocity vector in step t, \(c_{1}\) and \(c_{2}\) are positive fixed values and \(r_{1}\) and \(r_{2}\) are random numbers that are normally generated in the interval \(\left[ {0,1} \right]\). Parameters \(\overrightarrow {{P_{b} }}\) and \(\overrightarrow {{G_{b} }}\) also represent the best position for personal and collective experience, respectively. In order to create a better search capability, a parameter called inertial weight is added to the algorithm as a factor in the speed parameter as shown below:

$$ \overrightarrow {{V_{i} }} \left( t \right) = W\overrightarrow {{V_{i} }} \left( {t - 1} \right) + c_{1} r_{1} \left( {\overrightarrow {{P_{b} }} - \overrightarrow {{X_{i} }} \left( {t - 1} \right)} \right) + c_{2} r_{2} \left( {\overrightarrow {{G_{b} }} - \overrightarrow {{X_{i} }} \left( {t - 1} \right)} \right) $$
(11)

The inertial weight determines the effect of the velocity of the particles in the previous step on the velocity now. In this way, with large amounts of inertia weight, the general search ability of the algorithm is improved, and more space is explored, however, with small amounts of inertial weight, the space under study is limited and the search is performed in this limited space. Therefore, the algorithm typically starts moving with a large amount of inertial weight, which causes a large space search at the beginning of the run. Over time, the weight of inertia decreases, which concentrates the search for small space in the final steps. Algorithm2 shows the pseudo-code of the PSO algorithm. The PSO algorithm is suitable for a variety of continuous and discrete problems and provides substantial answers to various optimization problems.

figure b

4 Proposed Model

In this paper, we try to predict the closing price of the stock day by using the support vector machine with RBF kernel. We also use the PSO algorithm to optimize variables C and \(\delta \) to increase the model prediction accuracy. We used hybrid methods in this paper because hybrid learning techniques are an ideal way to achieve the highest accuracy (Abraham et al., 2009). Our proposed model is based on a study of data from the last 5 years (2016–2020) of Amazon (is an e-commerce company), Oracle (is a software super company) and IBM (is an international commercial machinery company). We use the root mean square error (RMSE) as a criterion for comparing the proposed model with the artificial neural network (ANN) and SVM method without optimizing the mentioned parameters (C,\(\delta ).\) The proposed model structure uses six features to predict the closing price of the stock and the output of the model is the same as the closing price of the stock the next day. Figure 1 shows the structure of the proposed model. In the following, we will explain the structure steps of the proposed model.

Fig. 1
figure 1

Structure of the proposed model

4.1 Data Collection and Processing

Collecting stock market data inside the country has many obstacles because many companies on the stock exchange, in order to compete and protect their privacy, refuse to disclose their data to the public. In order to avoid such problems as well as other problems such as the system of countries, emotions, economic conditions and inflation, in this paper, the stock market data of three famous companies Amazon, Oracle and IBM have been used. All datasets used are available on the Yahoo Finance website (Yahoo Finance website), which is a media asset that is part of the Yahoo network. This website includes financial reports, news, data and commentary on the stock prices of various companies, press releases and more. Yahoo Finance also has some online financial management tools that it offers to its users.

Data preprocessing plays a key role in the data processing process and its results. There are several steps and tools for data preprocessing (Alasadi & Bhaya, 2017). Fortunately, the data used did not contain any missing values, but in some cases included outliers. The stock price on some days has not followed its usual pattern and has increased or decreased significantly, and this has led to the inclusion of outliers in the data set used. In this paper, to solve the problem of outliers data, we replace their values with the average of the previous normal data. We get the average value for non-outliers data and replace it with outliers data. This way all the data is within a reasonable range.

Given that the values of the data properties used in stock price forecasting are significantly different from each other, data normalization plays an important role in our work. To achieve more accurate results, it is necessary that the intervals related to the different features be somehow identical or close to each other. There are many ways to normalize data (Yu et al., 2009). In fact, normalizing the data increases the accuracy of the forecast (Panigrahi & Behera, 2013). In this paper, we use the Min–Max normalization method. In this method, each set of data is mapped to arbitrary intervals, the minimum and maximum values of which are already known. In this method, any desired interval can be mapped to new intervals with just a simple conversion. Suppose attribute A is to be mapped fromhe data set between \(Min_{A}\) to \(Max_{A}\) to the new range \(new_{Min}\) to \(new_{Max}\). For this purpose, any initial value such as t in the initial interval will be converted to the new value \(t^{\prime }\). in the new interval according to the following equation (Yu et al., 2009):

$$ t^{\prime } = \frac{{\left( {new_{Max} - new_{Min} } \right)}}{{Max_{A} - Min_{A} }}\left( {t - Min_{A} } \right) + new_{Min} $$
(12)

4.2 Extract and Select Features

Many features can be used to predict stock closing price. In this paper, we use six features to predict stock closing prices. According to studies, these six characteristics play an essential role in predicting stock prices (Adebiyi et al., 2014; Göçken et al., 2016; Li et al., 2014; Patel et al., 2015). In the following, we will briefly explain these extracted features.

Date of exchanges: In the proposed model, we have the date of the exchanged shares in each time period and we use this feature to calculate the closing price of the shares in the next date and days. Open stock price: This feature represents the price for the first stock trading in a period of time (usually one day). The open stock price starts the first trading price of a day or a certain period.

High stock prices: This feature actually represents the price at which the stock reaches its highest level during the trading period. This is the point where there are more sellers than stock buyers because stock sellers always tend to sell their stocks at high prices so supply volumes and the number of suppliers in this price increase. Also, the high price of stocks indicates that this is the highest price that buyers have been willing to buy stocks during that period. In other words, the high stock price shows that shareholders have been willing to buy stocks up to this price during this period.

Low stock prices: The low stock price actually represents that the stock has reached its lowest price level during the trading period. In this price there are more buyers than stock sellers. Obviously, shareholders always tend to buy stocks at low prices and based on this incentive, at low prices, the amount and number of demand increases. Also, the low price indicates that this is the lowest price at which Stock sellers were willing to sell their stock during that time.

Volume Trades: This feature shows the number of sales and buy transactions that take place over a period of time. The trading volume confirms the stock trading trend. Heavier trading volume indicates the buyers' desire for a share or the selling pressure on a share, which is in line with the moving trend of stocks. If the price trend is an uptrend, the volume needs to be heavier on positive days to continue this uptrend, conversely, if the downtrend dominates the share, the downtrend will continue as long as the trading volume is heavier on negative days.

Closing price of shares: This feature represents the last price that the stock has been traded during the trading period. The closing price of stocks is the weighted average price of stocks during the trading period, taking into account the base volume. The base volume is the minimum number of shares of a company that must be traded during the day so that the price of that share can increase or decrease based on the range of the fluctuation range. To calculate the base volume, it is enough to mint the number 0.0004 (it varies in different countries) in the number of shares of the desired company to calculate the base volume of that company. The effect of the base volume on the closing price of the stock is that if the volume of transactions is more than the base volume, the calculated average is recorded as the closing price of the share in the trading board but if the volume of transactions is not more than the base volume, the final price will be adjusted as much as the difference between the two volumes. In general, the closing price of the stock is calculated according to the following formula:

$$ P_{2} = P_{1} + \left[ {\left( {VWAP - P_{1} } \right)\frac{N}{M}} \right] $$
(13)

where \(P_{1}\) is the closing price of the stock previous day, VWAP is the volume weighted average price of the current day, N is the trading volume of that share in the current trading session, M is the base volume and \(P_{2}\) is the closing price of the stock today. How to calculate VWAP is as follows:

$$ VWAP = \frac{{{\text{Price}} \times N}}{{\mathop \sum \nolimits_{i = 1}^{n} N}} $$
(14)

where the price is the price of each transaction and n is the number of transactions per share. As mentioned, if the volume of transactions is more than or equal to the base volume the same VWAP price will be the basis for calculating the closing price of stocks in the stock index:

$$ P_{2} = VWAP $$
(15)

Also, the closing price of stocks on each day is the basis of price fluctuations the next trading day. For example, if the closing price of today's stock is 1,000 $ per share, the allowable range of price fluctuations on tomorrow's trading day is 5% positive (1,050 $) and 5% negative (950 $) of that share. (This range varies from country to country). The closing price of stocks is often the most used in stock market analysis. In this paper, we use this feature as the target feature and try to predict the closing price of the stock using the proposed model.

4.3 Optimization and Training of SVM Algorithm and RBF Kernel by PSO Algorithm

To optimize parameter C in the SVM algorithm as well as parameter δ in the RBF kernel, no specific rules have been defined to date. Since the values selected for these two parameters have a great impact on the prediction accuracy of the model and also affect the error rate, in this paper, we try to obtain the best values for these two parameters by using the particle swarm optimization algorithm. In the third part, Algorithm1 showed the selection of C and δ parameters by PSO algorithm.

After optimizing the C and δ parameters by the PSO algorithm, we train our proposed model with two support vector machine models without optimizing the mentioned parameters and the artificial neural network. We used data from January 2016 to December 2019 Amazon, Oracle and IBM as training data.

4.4 Testing SVM-PSO Model with New Data

To test the proposed SVM-PSO model, we use the 2020 datasets of three companies, Amazon, Oracle and IBM, to determine the accuracy of the proposed model. We also calculate the accuracy of the two models SVM and ANN and compare it with the proposed model. The results obtained in the fifth section show that the accuracy of the proposed model in all cases is higher than the SVM and ANN models.

4.5 Computing RMSE for SVM-PSO, SVM, and ANN

After calculating the accuracy of the proposed model and comparing it with the other two models, we will then compare the three models using the root mean square error evaluation criterion. The results obtained in the fifth section show that the proposed SVM-PSO model has better performance compared to the other two models and has a lower RMSE error rate.

The RMSE compares the values predicted by the model with the actual measured values (Dennison et al., 2004). The following is how to calculate RMSE (Dennison & Roberts, 2003):

$$ RMSE = \sqrt {\frac{1}{N}\mathop \sum \limits_{i = 1}^{N} \left( {x_{i} - \widehat{{x_{i} }}} \right)^{2} } $$
(16)

where N is the number of data, \(x_{i}\) is the actual measured value of the data, and \(\widehat{{x_{i} }}\) the amount of data predicted by the learning model. In this paper \(x_{i}\) is the closing price of stocks in one day and \(\widehat{{x_{i} }}\) is the closing price of stocks on the same day as predicted by the model.

5 Results and Discussion

SVM-PSO, SVM and ANN algorithms were trained and tested with datasets form January 2016 to December 2020. All datasets used are available in (Yahoo Finance website). All data sets are divided into two parts: education from 2016 to 2019 (80% of data) and the test part, which is 2020 data (20% of data). Table 2 describes the parameters used by SVM, ANN and SVM-PSO.

Table 2 Parameters of algorithms used in this work

Figures 2, 3 and 4 shows the application of the proposed SVM-PSO model in comparison with SVM and ANN (multilayer perceptron) algorithms in different data sets. In Fig. 2, which are the results of Amazon, the results show that the proposed SVM-PSO algorithm has the highest accuracy with the optimizations performed, followed by the SVM algorithm without optimization and finally the ANN algorithm with the highest accuracy. Figure 3 shows the results from IBM. As can be seen, the proposed SVM-PSO algorithm has the highest accuracy, followed by the ANN algorithm, and finally the SVM algorithm has the highest accuracy. Figure 4 shows the results from Oracle. It can be stated that the predicted curve using the proposed SVM-PSO algorithm is closer to the real curve in terms of accuracy, and followed by ANN, while SVM is the worst one.

Fig. 2
figure 2

Results for Amazon company

Fig. 3
figure 3

Results for IBM company

Fig. 4
figure 4

Results for Oracle company

Figures 5, 6 and 7 shows a comparison between SVM-PSO, SVM and ANN algorithms with respect to root mean square error function (RMSE) for the three companies surveyed. As can be seen, the error of the proposed algorithm in all three companies is less compared to other algorithms. Table 3 also shows the performance of the RMSE function for the proposed algorithm. It can be seen that the proposed SVM-PSO algorithm has always performed better than the SVM,ANN and LSTM algorithms, especially in cases where there are fluctuations in time series performance. Also, Table 4 shows the accuracy of stock forecasting in the three companies for the proposed method and other methods. The proposed algorithm can predict the stock price of each company in accordance with the mentioned features and show the high accuracy. As shown in Tables 3 and 4, we consider LSTM algorithm to compare the proposed method with this method. The results show that the error of the proposed method in three companies is less compared to other algorithms and the accuracy of the proposed method is higher than other methods. The results of stock prices for Amazon, IBM and Oracle companies by considering LSTM algorithm is presented in Fig. 8. This algorithm is to directly use yesterday's closing price as the predicted value of tomorrow's closing price.

Fig. 5
figure 5

RMSE for Amazon company

Fig. 6
figure 6

RMSE for IBM company

Fig. 7
figure 7

RMSE for Oracle company

Table 3 Root Mean Square Error (RMSE) for proposed algorithm
Table 4 Stock forecast accuracy for the proposed algorithm
Fig. 8
figure 8

Stock prices in Amazon, IBM and Oracle companies

6 Conclusions

In this paper, we used a machine learning method called support vector machine (SVM) to predict stock closing price. We also optimized the parameter C in SVM and \(\delta\) in the RBF kernel using the particle swarm optimization (PSO) algorithm and were able to obtain the best parameters for our proposed model. The results show that the proposed SVM-PSO algorithm in all cases has more accuracy and less error than SVM ANN and LSTM algorithms.

We can also predict the closing price of each company's stock according to the characteristics of open stock price, high stock price, low stock price and trading volume. Rather, the shares of each company can be predicted according to the considered indicators, which are also among the global indicators.

Among the challenges and limitations in this subject can be mentioned the following:

  • Fragmented and non-standard data.

  • Long data training time.

  • Lack of comprehensive cooperation of stock exchange organizations to provide the required information.

6.1 Practical Implications

The findings of this study also provide important suggestions for stock market researchers and investors. Stock and capital markets are one of the key facts about the development and progress of the economy of societies. In order to have an active and dynamic stock market, it is necessary to create conditions for the general public to be able to have an active presence in this market.

Prerequisite for the growth and development of the stock market is appropriate and exciting advertising by government agencies. Also, the existence of reliable ways to predict stock prices that increase profitability and reduce investor losses is one of the important factors in encouraging investors to invest in stocks.

So far, many evolutionary algorithms have been used to optimize machine learning parameters and deep learning algorithms in stock price prediction forecasting (Hegazy et al., 2015), but none of them have been more than 90% accurate. Compared to previous work done in the field of stock price forecasting, the highest accuracy of which was 70%, our proposed algorithm was able to achieve above 90% accuracy in all cases by accurately optimizing the parameters. Achieving this level of accuracy can create a dramatic start and change in the market economy of communities and cause more people to enter the stock market due to the accurate forecast of this market.

6.2 Suggestions for Future Research and Further Scope of Research

As future works, we can use other evolutionary algorithms such as firefly algorithm (FA) to adjust C and \(\delta\) parameters and see what changes are achieved in the results obtained. In general, evolutionary algorithms play an important and effective role in parameter optimization (Vala et al., 2021).

We can also, as future works, increase the accuracy of stock price forecasting by considering the precise and periodic feelings of people at any given time and building an algorithm for it.