A Deep Learning Model for Predicting Stock Prices in Tanzania

: A


INTRODUCTION
A stock is a security that represents the ownership of part of a company.Stock units are called shares and are traded in a stock exchange [1].The desirable scenario with stocks is to buy when there is a good chance that a stock's price will rise and sell when there is a chance that it will decrease to gain profit from the difference between the selling and buying prices.In a nutshell, an investor aims to know when to buy and sell stocks to realize profit [2].Many studies have been conducted to predict stock prices [3], and machine learning has been proven an effective method for this [4].Ensemble techniques [5] and support vector machines [6] are some of the machine-learning techniques used in stock prediction.However, recent advances in computational power and machine learning have led to deep learning methods [7].Such methods can compute non-linear relationships among features and improve prediction accuracy [4].Deep neural networks were used to forecast stock prices showing that they can improve prediction performance.
In Tanzania, the only stock exchange is the Dar es Salaam Stock Exchange (DSE), which was incorporated in 1996 and began trading operations in 1998 [8].The low financial literacy of most citizens is one of the challenges that DSE faces [9].This results in low stock market uptake and infrequent activity [10].Nevertheless, machine learning and deep learning techniques can be used to confront this challenge.Such methods have been used to predict stock prices in various countries, such as Morocco [11], Nigeria [12], and the United Kingdom [13].This study was conducted due to the lack of similar efforts to predict stock prices in Tanzania.Furthermore, this study selected active stocks from DSE, and instead of developing a separate model for each company, it developed one dataset and a single (joint) model to predict next-day closing prices.Since each stock has a different number of outstanding shares, this study also examined whether it is significant to account for the number of outstanding shares of each stock when constructing the model.
Long Short-Term Memory (LSTM) and Gated Recurrent Unit (GRU) are deep learning methods designed to handle time-series data, such as stock data [14][15].Unlike traditional recurrent neural networks, LSTM and GRU can memorize previous information for a long time as they do not suffer from vanishing and exploding gradients [16][17], making them a good fit for stock market predictions since stock market trends tend to repeat over time [18].Numerous studies used LSTM, GRU, and other techniques to predict stock prices.LSTM was compared with Artificial Neural Network (ANN), Support Vector Regression (SVR), and Random Forest (RF) models to predict the closing prices of the iShares MSCI United Kingdom exchange-traded fund [13].The results showed that LSTM performed better than the other models, but this study used only the previous closing price as predictive input.In [19], the daily closing prices of selected listed companies on the Colombo Stock Exchange were predicted using GRU, LSTM, Simple Recurrent Neural Network (SRNN), and Feedforward, and the previous closing, high, and low prices as predictive input.The results showed that SRNN and LSTM produced less errors compared to Feedforward in most cases.This study used only historical stock prices as features.In [2], different machine learning methods were used to forecast the daily closing price of companies listed on the NASDAQ stock exchange, concatenating data from multiple companies to form a single dataset for weekly and monthly predictions.Concatenation was performed on a sector basis, where companies from one sector formed one dataset, and models were developed for each sector/dataset.Linear Regression, Support Vector Machines, Decision Trees, Ada Boost, Random Forest, Bagging, and K-Nearest Neighbors were used.The results showed that linear regression performed better than the other methods.However, the study did not employ a deep learning algorithm and did not consider outstanding shares.In [20], LSTM and SVR models were developed to predict the daily closing price of companies listed in the Dow Jones Index.The predictive inputs were the oil and gold prices and the previous closing price.The findings showed that the LSTM model outperformed the SVR.However, this study used only historical prices as features.In [21], 8 sectors of the Indian National Stock Exchange were chosen to develop the corresponding LSTM models and optimum risk portfolios for each sector.The actual returns yielded by the optimum risk portfolios were calculated and compared with the predicted by LSTM.The actual returns yielded by the optimum risk portfolios closely matched those predicted by LSTM.However, this study used only the previous closing price as predictive input.In [22], a deep learning approach was used in a highfrequency database containing billions of transactions of US equities, finding that prediction accuracy could neither be improved by building company-specific models nor by building sector-specific models.On the other hand, including price and order flow history over many past observations improved forecast accuracy.This indicated that the patterns learned by the model are universal and not company or sectorspecific.However, this study did not consider outstanding shares.In [23], a mobile application that included a stock price prediction model was developed for East African stock markets.The model was developed using LSTM and the predictive input was the previous closing price.However, the study used data from the NASDAQ stock exchange, which is a US stock market, and did not state whether the resulting model was tested on actual data from East African stock markets.In [24][25][26], several methods were incorporated to predict future stock prices and currency exchange rates.
As none of these studies attempted to study the importance of accounting for the number of outstanding shares of each stock when developing a joint model to predict the stock prices of multiple stocks, this study was conducted to address this gap.

A. Data
Daily stock data was collected from the DSE for the period from January 2018 to April 2022.The data comprised of the features: opening price, low price, high price, previous closing price, volume, outstanding bids, outstanding offers, and price change.The "price change" feature was the difference between the closing and the opening price.In addition, the study examined the outstanding shares of each company and derived the features: "volume/outstanding shares", "outstanding bids/outstanding shares", and "outstanding offers/outstanding shares".

B. Experimental Setup 1) Experiment 1
This experiment did not take into account the number of outstanding shares of each stock.Therefore, the following features were employed: opening price, low price, high price, previous closing price, volume, outstanding bids, outstanding offers, and price change.

2) Experiment 2
This experiment considered the number of outstanding shares of each stock/company.As a result, the features "volume/outstanding shares", "outstanding bids/outstanding shares", and "outstanding offers/outstanding shares" were used instead of volume, outstanding bids, and outstanding offers, respectively.Therefore, the features used were: opening price, low price, high price, previous closing price, volume/outstanding shares, outstanding bids/outstanding shares, outstanding offers/outstanding shares, and price change.

C. Selection of Active Companies
Since DSE is a developing stock exchange, some companies have been inactive over time with zero or very low trading volumes [1].This study ignored such companies to focus on companies that attract investors' attention.To identify companies with active shares, the study took the average "volume/outstanding shares" of each company over the last year.According to the study's dataset, the last year was between April 2021 and April 2022.Companies whose average of "volume/outstanding shares" was greater or equal to 0.005% were selected [27].Table I shows companies whose average volume/outstanding shares was greater or equal to 0.005%.Data visualizations were applied to the closing price attribute of each company in Table I.TBL and TCC were eventually dropped since their closing prices were observed to be static for more than two years.According to stockbrokers, this was due to block trades that dominated their shares [28].Following the DSE regulations, block trades are reflected in volume statistics but are not used to update the closing price of the stock [29].For this reason, the "volume/outstanding shares" average of these companies appeared above the threshold but there were no corresponding price movements.This study did not use these companies to avoid distracting the model's learning.On the other hand, TPCC was appended even though its average volume/outstanding shares was 0.0034%, below the threshold of 0.005%, as its price was increasing since 2020 and therefore it was desirable for prediction.Figure 1 shows the closing price history of TPCC.Therefore, eight (8) companies were selected: CRDB, DSE, NICO, NMB, SWIS, TCCL, TPCC, and TOL.Closing price history of TPCC.

D. Model Construction
The LSTM and GRU deep learning methods were used to build the model.The model was developed in Google Colab using Keras with a TensorFlow backend.To ensure that all features are on a similar scale, the study utilized MinMaxScaler to scale all the features in a range between 0 and 1.In total, the dataset consisted of 8668 rows of data, where 80% were used for training and the remaining 20% for testing.The Adam optimizer was used together with the mean squared error as the loss function.The KerasTuner Bayesian optimization algorithm was used to tune the number of hidden layers, hidden units, and batch size.The regression evaluation metrics Root Mean Square Error (RMSE), Mean Absolute Error (MAE), and Mean Absolute Percentage Error (MAPE) were used to evaluate the performance of the models.

E. Model Deployment
The extreme programming model was adopted to develop web-based prototype to embed the model and make it accessible to stockbrokers and investment advisors.This method was chosen because it ensures rapid delivery at minimum cost while promoting stakeholder participation in the development process [30].Figure 2 shows the lifecycle of the extreme programming technique.The planning phase is where requirements are gathered based on user stories describing various use case scenarios.Figure 3 shows the use case diagram of the system.At this stage, the work was broken down into iterations (small releases) based on which functionalities needed to be delivered first.Table II shows the iterations.The design, coding, and acceptance testing phases were performed iteratively.That is, after completing the designs for the first iteration, the study continued to implement them before proceeding to design the next iteration.After completing the first iteration, acceptance testing was performed, and adjustments were made per users' reviews.Thereafter, designing for the next iteration resumed, and the cycle was repeated.The design phase involved designing the user interface and logic.Figure 4 shows the flow chart of the make predictions use case.The coding phase involved the actual implementation of the designs, unit testing, and continuous integration, while acceptance testing involved users' validation of the implemented functionalities.The release phase involved the delivery of a complete system to end users, where new requirements were noted for future directions.The backend was implemented using Python and the Django framework due to its rich collection of libraries for performing different tasks, consistent documentation, and the active developer community [31].This framework also helped to ensure the quick development of the system while with security standards.The django-plotly-dash library was used to provide interactive visualizations, and MySQL was employed as the database engine.The system's frontend was built using HTML, CSS, and the Bootstrap framework.Furthermore, a datatable plugin was used to render interactive tables.

A. Model Performance Results
Tables III and IV show the results of experiment 1 and experiment 2, respectively.The results show that accounting for the outstanding shares of each stock significantly improved the prediction accuracy.In particular, the RMSE of LSTM improved from 10.4734 to 4.7524 when outstanding shares were considered, which was an improvement of 54.62%.On the other hand, the RMSE of GRU improved from 12.4583 before accounting for outstanding shares to 8.7162 when outstanding shares considered, which was an improvement of 30.04%.The results confirm that, for a dataset consisting of multiple stocks, taking the ratio of outstanding bids, outstanding offers, and volume of each stock to its corresponding number of outstanding shares significantly helps the model to learn from the dataset more effectively.In general, the LSTM model performed better than the GRU.This could be because GRU keeps track of both long-and short-term dependencies through only one memory cell, called the hidden state, while LSTM utilizes two memory cells, one for long-term dependencies and the other for shortterm [16].Therefore, this could make LSTM more efficient at learning and memorizing patterns than GRU.However, these results contradict the results of [1] where GRU performed better than LSTM in predicting the daily closing prices of selected companies from the National Stock Exchange of India.One reason for this could be that the nature of the stock dataset used in this study was best captured by LSTM.

B. The developed prototype
The prototype developed consisted of eight web page interfaces that included the login page, the data entry page, the page to manage the entered data, the page to run the model, the view predictions page, the page to view market analytics, and the change password page.On the other hand, there was a page for the administrator to add or remove users.Figure 5 shows the view predictions page.

IV. CONCLUSION AND FUTURE WORK
This study developed a deep-learning model to predict the next-day closing prices of selected active companies in Tanzania.Two deep learning methods, namely LSTM and GRU, were used and the results showed that the LSTM model performed better than the GRU.Therefore, the LSTM model was deployed in a web-based prototype application for stockbrokers or investment advisors.The study laid a foundation for more research to predict stock prices in Tanzania, showing how active stocks can be chosen and how the model can be developed and deployed.
Specifically, this study examined whether it is significant to account for the number of outstanding shares of each stock when developing a joint model to predict the stock prices of multiple stocks.The results showed that accounting for the number of outstanding shares of each stock significantly improved prediction accuracy.Specifically, the RMSE of LSTM decreased by 54.62%, while that of GRU decreased by 30.04% as a result of accounting for outstanding shares.Therefore, these findings indicate that it is significant to account for the number of outstanding shares of each stock when developing a joint model to predict the stock prices of multiple stocks.These results are useful to researchers who may require to concatenate together data of several stocks to realize a sizeable dataset for stock price prediction.
Future studies could explore other prediction ranges, such as the next hour, week, or month.Moreover, in addition to a model to predict stock prices, a model can also be embedded to classify the risk tolerance level of an investor, to enable the system to provide an automated advisory tailored to his financial position.On the other hand, the use of new features, such as investors' sentiments, can be explored.Moreover, new deep learning architectures or combinations of hybrid models can also be investigated.

TABLE I .
COMPANIES WITH AVERAGE OF VOLUME/OUTSTANDING SHARES ≥ 0.005%