Stock price forecast of macro-economic factor using recurrent neural network

Received Jul 23, 2020 Revised Nov 11, 2020 Accepted Jan 25, 2021 The stock market is one of the investment choices that always have traction from time to time. Aside from being a means of corporate funding, investing in the stock market can benefit investors. Investing also has a higher risk because the pattern of stock prices is volatile, which is caused by internal and external factors. One external factor that affects stock prices is the macroeconomic, where these factors are events that occur in a country where one of the economic sectors affected is stock prices. Investors often feel confused about the right time in decisions making related to buying or selling stock. One way to look at how the prospect of stock prices is a stock price forecasting activity. For this study, we will be making use of the recurrent neural network (RNN) to forecast stock prices for the next periods. This research involves two variables: the closing stock price and the rupiah exchange rate against the dollar for the daily period. We achieve a MAPE value of 1.546% for RNN model without the variable foreign exchange rate and 1.558% for the RNN model that uses the foreign exchange rate against the dollar.


INTRODUCTION
The stock market is one of the investment choices that always have traction from time to time. One indicator of the high level of interest in investing in the stock market is on the Indonesia stock exchange (IDX). These interests can be represented in the Indonesian central securities depository data, namely 434,107 new investors in 2015, 894,116 in 2016, and 1,122,668 in 2017 [1]. This increase is due to law no. 11 of 2016 concerning tax amnesty and capital market education and socialization conducted by IDX [2]. Investors often feel confused about the right time analysis and company selection when investing in the stock market. The stock market is known to be volatile and unstable. A stock price can increase one day, but a decrease in the next period [3]. In the stock market, there are several companies whose stock has blue-chip status. Blue-chip stock can be interpreted as leading stocks because it can generate large profits and routinely share these profits with investors. One example of this company is XYZ. XYZ's stock can be an option for investors who want to invest in blue-chip stocks, especially those who want to minimize losses due to investing in a stock. That is because these stocks can produce returns that tend to be stable. XYZ can produce a stable return value because of the large number of subsidiaries shaded by them. If one of these subsidiaries Int J Artif Intell ISSN: 2252-8938 Stock price forecast of macro-economic factor using recurrent neural network (M. Reza Pahlawan) 75 suffers a loss, it does not significantly affect XYZ's stock as the parent company because there are still other subsidiaries that can cover it.
Investing in the stock market has a higher risk of saving at the bank even though the opportunity for higher profits. The reason is due to fluctuating stock prices which is caused by internal and external factors [4]. One example of external factors that affect stock prices is macro-economic. These factors are events that occur as a whole regarding the economy in one country that can affect stock prices. Stock market volatility depends not only on the nonlinear nature of data, but also on the community's sentiment, economic and political conditions [5]. This factor occurs outside the company, so that it is difficult to control it. One example of macro-economic factors is the exchange rate. Exchange rates can affect domestic investments such as stocks. This situation will cause a decrease in demand for shares so that stock prices decline [6]. It is also supported through previous research, stock prices and currency exchange rates have a positive correlation [7].
Appropriate decision making is needed when investing in shares where the decision can be in the form of selling shares when stock prices go up or buy when it is down. One way is to look at how the prospect of stock prices in the future where to do so we need a stock price forecasting activity. In order to produce meaningful predictions, the error rate generated by the forecasting model must be minimized. Several ways in which investors predict stock prices such as technical analysis, fundamental analysis, and mathematical models [8]. Past data information is needed to forecast stock prices. This information can be collected to get an idea of how the stock price movement trends. From this trend, predictions can be made on how the stock price going forward.
This research provides a solution to minimize risk in the form of a stock price forecasting model which can minimize the risk of investing in stocks. Therefore, it is necessary to choose the right forecasting method. Today, several methods can be used to predict a data set, one is the implementation of machine learning. Machine learning is classified as an artificial intelligence method and has been widely implemented in the tasks of classification, spam filtering, and forecasting [9]. One machine learning method that can be used is an artificial neural network (ANN), one of the most accurate and widely used forecasting methods [10]. ANN is a mathematical model inspired by biological neural networks [8]. The advantage of ANN is that it can provide an easy way to model non-linear pattern data [11]. One development of the ANN model, recurrent neural network (RNN), is also one of the models that can be used to predict a time-series data pattern. RNN has also been used in previous time series data forecasting research where the resulting performance can outperform support vector machine (SVM) regression, which is 1.687% compared to 1.86% [12]. In another research, the RNN that produced a MAPE value of 1.5617% had better performance than the backpropagation neural network (BPNN), which only produced a 2.9561% [13].
RNN is a subtype of neural networks that uses feedback connections [14]. The basic concept of RNN is sequential data processing [15]. In ANN, data are processed simultaneously to produce output which is different from RNN. The RNN processes data sequentially forward so that the output value depends on the value of the previous input and has a memory of past information. This study uses RNN because it has advantages in making models for time series data and non-linear predictive capabilities, faster convergence, and more accurate mapping capabilities [16]. RNN has good time-series processing ability because it can process multistep input of time-series and its hidden layer is connected [17]. RNN uses its connected hidden layer to its recurrent layer to obtain temporary information from the previous step as input data for the next step, making this method suitable for time-series data [18]. ANN does not have that layer, so all input data being processed simultaneously.
In previous stock forecasting research, stock price forecasting is done based on the influence of social media. The RNN model's performance, which has a MAPE value of 22.31%, can outperform ANN and support vector regression (SVR), each of which is able to produce MAPE values of 78.37% and 133.9% [19]. In other studies, RNN can outperform ANN where one of the MAPE values generated is 1.61% for RNN and 27.76% for ANN [16]. From the research that has been mentioned previously it proves that RNN has a better performance in stock price forecasting. This study's expected results are to obtain XYZ's stock price predictions based on macro-economic factors and provide new insight into the effect of these factors in forecasting stock prices because from the research that has been mentioned before no one mentioned about the RNN performance on stock price forecasting based macro-economic factor. Moreover, it can provide information for investors in making investment decisions, especially when the exchange rate is reasonable or not. Figure 1 is the workflow in this research. Data was collected on historical data of stock prices and the dollar exchange rate against the rupiah. Stock data collection is carried out on the Investing site, where the company stock used is XYZ. The data used is the daily period of working days from October 2014 to September 2019. Feature engineering is done by performing feature selection and data merging. Feature selection is the process of selecting features or variables that will be input variables in the RNN model formation. Data merging is based on the same date on both data.

RESEARCH METHOD
The approach used to overcome the missing value is the last observation carried forward (LOCF) where if there is a missing value data, then the data filling is based on previous values [20]. After checking the missing value, data normalization is performed on the input variable. Normalizing data can speed up model training [21]. One approach that can be used is the min-max method which changes the value to between 0 and 1. Building the RNN model, the data is divided into training data and testing data, which have a proportion of 70:30 and these proportions are commonly used in several research. The training data is used as the RNN model's construction, while the testing data is used as the performance measurement of the RNN model. To avoid overfitting in the training model, this research also uses a validation set. The validation process will help reduce the chance of overfitting the test data [22]. So, the proportion of data used in this research is 52% training set, 18% validation set, and 30% testing set.
RNN is a method of learning sequential data patterns that are very powerful. The method is capable of receiving various information with a flexible and resilient to time distortion. That makes RNN good at sequential labeling where the input sequence is transcribed in a label stream [23]. In building forecasting models using the RNN method, two aspects are needed. First is defining input and output variables. At this stage, historical data of shares and exchange rates become input variables in the input layer, informing the RNN model as shown in Table 1. Meanwhile, stock prices for the next period become output variables in the output layer forecasting model. To determine how the influence of a macro-economic factor in forecasting stock prices, a comparison is made between two types of models. The first model only uses variable stock prices (univariate). The second uses variable stock prices along with a macro-economic factor (multivariate) as an input model.  Second is building a forecasting model using Elman-RNN architecture. The output at the hidden layer in Elman-RNN can provide feedback in the form of value to itself and other hidden neurons through the recurrent layer [24]. This feedback allows Elman-RNN to study past data. Each neuron in the Elman-RNN hidden layer has one recurrent layer through the link in the form of a load value as shown in Figure 1. Recursion at the hidden layer can act as a memory mechanism [25]. Chances of Elman-RNN overfitting are also smaller in the use of periods that are not too large [23].
The second uses variable stock prices along with a macro-economic factor (multivariate) as an input model. The amount of inputs in the input layer depends on the number of periods backward that is used to make predictions on future periods (timestep). The timestep amount used depends on the experimental results when doing research. Tuning parameters are performed where the value of each parameter is given, as shown in Table 1, which will be an experiment in finding the best model. Each parameter has its function for the forecasting model. The hidden layer node plays a role in converting value on the input layer to the output layer, as shown by the purple circle in Figure 2.
Dropout can prevent forecast results from being overfitting by randomly selecting nodes on a hidden layer to be dropped out with a given probability of each weight update cycle [26]. The number of epoch represents the number of iterations of the model trained. The activation plays a role in converting input value from the input layer to some ranged value, based on the type of activation. Tanh activation has its output value between -1 until 1.
The relu and softsign activation has the advantage over the tanh activation because it will not reach saturation point when it gets a large input value. The difference between those functions is relu has an output value between 0 until x positive value and softsign has an output value from -1 until 1. The optimizer is used to update the weight parameter in the RNN model on each iteration. Therefore, it can help minimize the loss Int J Artif Intell ISSN: 2252-8938 Stock price forecast of macro-economic factor using recurrent neural network (M. Reza Pahlawan) 77 function. The batch size defines the number of training samples to work through before updating the model parameters [27]. Each model parameters learned by using gradient descent learning algorithm. This algorithm used for training the Elman-RNN model [28].
Experiments were carried out based on the parameter scenario in Table 2 where the best model for each timestep was taken to help analyze the selection of the best model. The model analysis is performed to determine the level of forecasting model performance that has been generated in the test data. The next step is to conduct a final analysis to choose the best model. The best model selection will be determined through the smallest mean absolute percentage error (MAPE) value in each model based on the forecast result. MAPE is easy to understand because it represents error rate by percentage and often used in finance because gains and losses are often measured in relative values [29]. If the best model has been chosen, then the comparison is made by testing the stock data of other companies with different characteristics and making comparisons with the results of other methods.  Comparisons are made with the feed-forward neural network (FFNN) which is one of ANN method and the basis for developing the Elman-RNN and other RNN architectural methods such as long short-term memory (LSTM) and gated recurrent units (GRU). In the LSTM structure, there is an additional forget gate and output gate, while GRU has an additional controller gate where the structure allows the determination of the value that can be passed on [30].

RESULTS AND DISCUSSION
The experiments were performed on an Intel Core i5 7200U, Nvidia GT930MX 2GB graphic card, and RAM with 8GB which is running on Windows 10 Version 1909. The software tool used is Google Colab and Python for the programming language.

Data collection
The data collection results are XYZ's stock price data and historical data on foreign exchange rates from October 2014 to September 2019 on working days. Table 3 represents foreign exchange rates data for the last five periods. 'Price' feature contains information about closing price on each day, 'Open' contains information about opening price on each day, 'High' and 'Low' contains information about highest and lower price on each day, 'Vol.' is the number of stock that changed on each day, and 'Change%' is the difference between the current price and previous day's price. The variable used in this research is 'Price' on the stock price data and the dollar exchange rate against the rupiah.
For the scenario of testing the model's performance against the stock data of other companies that have different characteristics, data is taken from the stock data of ABC and DEF in the same period. ABC's stock data has an increase in the last three years, while DEF's stock data has declined in the last three years Figure 3 shows that XYZ, ABC, and DEF stocks have significant differences in patterns. XYZ stocks tend to be stable at Rp 6,000 to 8,000, while ABC's stocks are stable in the price range of around hundreds while DEF's stock prices are very volatile and tend to decline in the last three years.

Model analysis
In the univariate model formation experiment, the best model produces MAPE of 1.546% as in Table 4 so that the best model is obtained with the parameters of the number of hidden layer nodes 30, the activation function 'softsign', the number of batch sizes 64, the type of optimizer 'rmsprop', and the dropout rate of 0.1. In the multivariate model formation experiment, the best model produces MAPE of 1.558% as in Table 5 so that the best model is obtained with the parameters of the number of hidden layer nodes 52, the activation function 'tanh', the number of batch sizes 32, the type of optimizer 'rmsprop', and the dropout rate of 0.1.
It can be seen that there is a tendency of several parameters in the Elman-RNN model from each timestep. First, 8 out of 10 univariate models and 7 out of 10 multivariate models have 'rmsprop' as the optimizer's parameter. Second, all of the best model from each model has 0.1 as the dropout rate. From that tendency of several parameters, it indicates that the parameter is suitable for this study case in this research.

Comparison of univariate-multivariate model
Performance comparisons were made from the univariate and multivariate models. Figure 4 shows that the best univariate model performance in timestep 10 with a MAPE value of 1.546% as shown in Figure 4(a) and 1.558% for the best multivariate model in timestep 9 as shown in Figure 4(b). Figure 5 compares actual and forecast data using the best univariate model as shown in Figure 5(a) and multivariate model as shown in Figure 5(b) from April 2018 to September 2019. The univariate model has better performance than the multivariate model, where the difference is 0.012%. Figure 4 show is the univariate model has a standard deviation of 0.303 and the multivariate model has a standard deviation of 0.384 which indicates that the multivariate model has more diverse data distribution.  These results indicate that the addition of a macro-economic factor in the forecast model does not have a significant impact, and instead, the results are lower model performance, although the stock price and the exchange rate have a positive correlation. Also, it can be an additional insight for investors if they want to consider the exchange rate factor in stock investing.

Model robustness test
Testing the best model that has been formed is done by testing by forecasting the shares of other companies and future data. The measurement of model performance also uses MAPE. Figure 6 is a comparison chart of forecasting data produced by the model and actual data from October 2019 to May 2020 using best univariate as shown in Figure 6(a) and multivariate model as shown in Figure 6(b). The model cannot follow the pattern of data in the April to May 2020 period. Perhaps the reason is that the significant reduction where the model cannot follow the data pattern properly. The ongoing Covid-19 pandemic is one of the causes of this. The data used in the scenario of future data testing is from October 2019 to May 2020. The univariate model has a significant decrease in performance where the MAPE produced is 10.25% (Sept 2014-May 2020) from 1.546% in a previous period (Sept 2014-Oct 2019). Similar to the univariate model, the multivariate model's performance has decreased significantly, where the resulting MAPE is 11.43% from 1.558%.    Table 6 show is the best univariate model is used to test the performance of other companies. Figure 7 compares the actual data and forecast results of ABC as shown in Figure 7(a) and DEF as shown in Figure 7(b) stock prices using the best univariate model. The best univariate model performance when used to forecast ABC's stock price is 3.03%, while the model's performance on DEF's shares has a performance of 6.02%. Table 7 show is the best multivariate model performance testing is on other companies. Figure 8 compares the actual data and the forecasting of ABC as shown in Figure 8(a) and DEF as shown in Figure  8(b) stock prices using the best multivariate model. The best multivariate model performance used to forecast ABC's stock price is 6.382%, while the model's performance on DEF's stock has an accuracy of 4.466%.

Method comparison
In Table 8, the Elman-RNN method has a higher performance than FFNN. It can be seen from the MAPE produced by Elman-RNN is 1.546%, while FFNN is 1.835%. Even though the performance of the model produced by the Elman-RNN is lower than LSTM and GRU, where both methods can produce MAPE values of 1.345% and 1.352% consecutively, the runtime of Elman-RNN is significantly faster. The Elman-RNN requires 3.974 seconds, while the LSTM and GRU require 38.956 and 37.84 seconds.
In Table 9, the Elman-RNN method has a higher performance than FFNN. It can be seen from the MAPE produced by the Elman-RNN is 1.558%, while FFNN is 2.961%. The performance is lower than the LSTM and GRU, where both methods can produce MAPE values of 1.351% and 1.375%. However, in terms of speed, Elman-RNN is significantly better than the LSTM and GRU methods because it requires 4.020 seconds, while the two methods require 33.533 and 19.409 seconds consecutively. A comparison of the model performance between univariate and multivariate models as shown in Figure 9(a-b).    To sum up, Elman-RNN performance for both univariate and multivariate models is good and fast enough compare to the other methods because in terms of MAPE is not the worst and the runtime is not the longest. Similar to the univariate model, the multivariate model produced by the Elman-RNN is still lower than the LSTM and GRU where both methods can produce MAPE values of 1.351% and 1.375%. In terms of speed, Elman-RNN is better than the LSTM and GRU methods because it requires 4.020 seconds while the two methods require 33.533 and 19.409 seconds. However, Elman-RNN is still slower than FFNN which requires 3.193 seconds. It can be concluded that the addition of a macro-economic factor does not provide a better model performance.

CONCLUSION
From there result, there is an indication that the exchange rate does not have a significant impact, although the stock price and the exchange rate have a positive correlation. Furthermore, it becomes an additional insight for investors if they want to consider the exchange rate factor in stock investing, especially when the Rupiah is weakened against the US Dollar. For further research, one can consider other macroeconomic sectors such as oil prices, inflation, and gross domestic product (GDP) or the impact of public social sentiment and government political policies on stock prices. It is caused by many factors that can affect the stock price. The Elman-RNN outperforms FFNN in terms of model performance even though it is lower in the runtime model. Even though the performance of the univariate and multivariate model produced by the Elman-RNN is lower than LSTM and GRU, the runtime of Elman-RNN is significantly faster. Determining the value that can be entered or not on the gate structure in the LSTM and GRU methods plays a role in why the runtime model is longer.