Predict the Trend of Stocks’ Prices By AI

Hanzheng Wang
Bucknell AI & CogSci
6 min readMay 16, 2021
Figure 1. The tread of a stock shown in the figure
Firgure 1. A busy business day shown by the trend of a stock

Introduction

We create a market simulation environment that AI can buy and sell stock, starting with designing AI agents implemented with Q-Learning and Neural network to operate inside that environment. The matrices to measure the performance of the AI agent would be the amount of profit the agent can earn and the accuracy of the neural network of predicting the future price. The wanted final version would be a simulation of stock market where agents competes and communicates against each other.

Figure 2. Our stock market environment

Stock Price Prediction

The first AI model in our approach to stock price prediction is a LSTM-baseddeep recurrent neural network (DRNN) Architecture. The LSTM layer is able to handlenot only a single data point each time, but also a sequence of data. The input of eachnode in LSTM layer includes both the current input data to the node as well as theoutput from previous node (See Fig. 3). In this way, the LSTM layer is able toconnect prior information to the present task.

Figure 3. Structure of LSTM layer

Before making decisions, such structures help the neural network to include all theinformation. Given our task to predict prices of stock market, our assumption is thatthere are some hidden connections between the current price and the historical prices.

To retain such a historical connection and given the characteristics of LSTM layer, weuse this structure to predict the future price. The stacked LSTM architecture we useconsists of multiple layer of LSTMs (See Fig. 4). After each layer of LSTM, we add adropout layer. Such layers prevent neural network from over-fitting by randomlydropping out some nodes from neural network at each step. Thus, during each trainingprocess, only the remaining node in each layer is trained.

Figure 4. Structure of LSTM layer

To train this neural network, we use real stock price information from Yahoo!Finance API. This data set contains historical information about the performance of a stock including volume and indicator variables of the daily closing cost, high cost, and low cost (See Fig. 5). We will normalize the data using robust scaler. Since any outliersin data may affect the accuracy of the prediction, we try to reduce such effects. The robust scaler has features using statistics that are robust to outliers. It removes the median and scales the data according to the interquartile range (IQR). The IQR is therange between the 1st quartile (25th quantile) and the 3rd quartile (75th quantile).

Figure 5. historical Price of Apple Inc

Results

The huber loss function is used in this paper. Huber function is less sensitive to outliersthan mean square error. The reason we use this loss function is due to the fluctuations in stock price. Asshown in Fig. 6, the red line indicates the actual stock price, which has many tinyfluctuations from day to day. Huber loss function helps neural network to ignore tinyfluctuations and to pay more attention on general trend of prices.

We also find that a DRNN has a better performance compare to shallow LSTMstructure. By comparing Fig.6 and Fig.7, we find that with DRNN structure, theloss levels off with value around 0.25, while with shallow LSTM structure, the loss levelsoff with value above 0.5.

Figure 6. Losses of Prediction vs.Epochs (MSE + LSTM)
Figure 7. Losses of Prediction vs.Epochs (MSE + DRNN)
Figure 8. Losses of Prediction vs.Epochs (Huber + DRNN)

General Model

Generally, the combination of two of our models can be concluded as following(See Fig. 4) our agent obtain recent prices from stock environment. Then agent usesstock price prediction system to do a price prediction on the prices of future several days. Next, the predicted result is treated as input into stock trading system, whereagent operate selling and buying actions accordingly

Figure 9. General Design of AI Agent

Utopian and Dystopian

Here is Utopia which might brought by AI in capitalism area. Because AI has better calculation, data collection and comprehension, it can makemore precise prediction than humans generally. If we use AI in our stock market agent,prediction of stocks made by our stock market agent will more precise. So that suchdangers like stock market crashes and crisis will decrease dramatically. The stockmarket will be more stable under the help from AI agent. As we continue to makeprogress in AI technique, when better AI stock market agent appears in the future, wecould imagine that we will have an eternally peaceful stock market and economy.

Figure 10. AI makes our life more perfect and much closer to “Utopia”

Here is Dystopia which might brought by AI in capitalism area. AI might cause harm to humans without reason. The logic and rationale behind AI decisions may remain opaque to the person concerned and even the person responsible for the decisions. For example, the AI stock market agent might report thefault prediction intentionally so that it will cause chaos in the public brought people nightmare when they buy the stocks which the agent recommend. If stock market agents make AI-based decision, they must be able to let public know how their decisionsbe made and explain how they come to this conclusion. The decisions must be traceableand explainable to be contestable.

Ethical Concerns

As we get a breakthrough on our project technically, we begin to consider theinfluence of AI to humanity:

First is unemployment issue. Compared with humans, AI has better calculation and dataanalysis, so finally it will have more precise prediction of the trend of the stock market.If widely used, AI will dominate the labor force in this realm by winning thecompetition with humans. It will take away numerous jobs from humans who are engaging in the same area with AI.

Second is social instability risk. Although AI’s prediction has a higher correctness rate than humans, it is not one hundred percent correct. Even if it has the visible possibility to make mistakes, more people still trust it because AI has better prediction than humans. As a result, the wrong price prediction may lead to terrible outcomes, so that causes chaos and fears in the stock market even in the whole public.

Figure 11. We need make AI brought more harmony to humans

Third is inequality wealth distribution. Wealth created by machine might beunevenly distributed. Use of AI can help individuals or companies succeed in the stockmarket according to the correct prediction by it. But also, use of AI makes people losetheir jobs who engage in similar jobs. So the gap of wealth difference is enlarged by useof AI.

Last one is higher labor requirement.An additional concern about the inequality of AIsystems is that it puts individuals who lack the capital and education to create, train,and maintain AI systems at a disadvantage in terms of efficiency and time to those who do. In this way, AI within the stock market serves to perpetuate existing disparities in the ability of the wealthy and the non-wealthy to maintain their long term economic health

References

  1. Hochreiter, S., & Schmidhuber, J. (1997). Long short-term memory.Neural computation,9(8), 1735–1780.

2. Huber, P. J. (1964). Robust Estimation of a Location Parameter.The Annals ofMathematical Statistics,35(1),73101.

3. Lee, J. W. (2001). Stock price prediction using reinforcement learning.1, 690–695 vol.1. https://doi.org/10.1109/ISIE.2001.931880

4. Tsaih, R., Hsu, Y., & Lai, C. C. (1998). Forecasting s&p 500 stock index futures with ahybrid ai system.Decision Support Systems,23(2), 161–174. https://doi.org/https://doi.org/10.1016/S0167-9236(98)00028-1

5. Watkins, C. J. C. H., & Dayan, P. (1992). Q-learning.Machine Learning,8(3–4),279–292. https://doi.org/10.1007/BF00992698

--

--