Stock Trading System Based on Machine Learning and Kelly Criterion in Internet of Things

The evolution of the Internet of Things (IoT) has promoted the prevalence of the financial industry as a variety of stock prediction models have been able to accurately predict various IoT-based financial services. In practice, it is crucial to obtain relatively accurate stock trading signals. Considering various factors, finding profitable stock trading signals is very attractive to investors, but it is also not easy. In the past, researchers have been devoted to the study of trading signals. A genetic algorithm (GA) is often used to find the optimal solution. In this study, a long short-term (LSTM) memory neural network is used to study stock price fluctuations, and then, genetic algorithms are used to obtain appropriate trading signals. A genetic algorithm is a search algorithm that solves optimization. In this paper, the optimal threshold is found to determine the trading signal. In addition to trading signals, a suitable trading strategy is also crucial. In addition, this research uses the Kelly criterion for fund management; that is, the Kelly criterion is used to calculate the optimal investment score. Effective capital management can not only help investors increase their returns but also help investors reduce their losses.


Introduction
In recent years, the development of IoT is facilitating the flourishing of various industries, especially in the economic market. Investment, insurance, and forecasting are among the services related to the Internet of Things in the financial aspect. The financial market is very complicated, especially the various transactions in the stock market. If you want to get high returns through investment, the stock market is a very good choice. But often most investors are affected by emotions and are prone to make irrational judgments, leading to capital losses or low returns. Advances in financial theory and computer technology have made quantitative trading [1,2] possible. Quantitative trading can be realized automatically through technologies such as computers. Different from general trading methods, the behavior of quantitative trading is to use computer technology to mine relevant information from the historical data of the stock market to increase the profit of the transaction [3,4]. Therefore, to develop a quantitative trading strategy that can increase returns is what researchers hope. Whether a stock transaction is profitable has many factors, such as political factors and economic factors [5]. Therefore, stock trading will not only bring higher additional income to people but also bring certain risks. For investors in the stock market, the main purpose is to obtain higher returns, that is, to find a stock trading system that can avoid certain risks and obtain maximum profits.
Researchers mainly focus on two aspects when studying stock trading: stock trading strategy and fund management [6,7]. A good trading strategy is of great help to investors in the stock market. The Kelly criterion is usually used to determine the optimal investment ratio. In previous work, researchers and investors usually use Kelly's criterion to manage funds [8,9]. Wu and Chung [10] devised a strategy to reduce losses and increase profits by holding different options. In this strategy, the Kelly criterion is used to calculate the optimal investment ratio to determine the appropriate option portfolio.
Artificial intelligence technology is very popular in the financial field. In recent years, researchers have proposed many prediction methods for the stock market [11,12]. Many researchers have applied machine learning to financial transactions. Cohen et al. [13] used neural networks to study the unknown rules in price changes in the stock market. Chiang et al. [14] used artificial neural networks to study the fund's year-end net asset value. Experimental results show that the predictive performance of neural networks is better than traditional econometric techniques. Yu and Yan [15] proposed a framework for predicting stock prices based on deep neural networks (DNN). This framework combines phase-space reconstruction (PSR), and its prediction performance is better than some other prediction frameworks. Deep learning is also used in other areas [16,17]. The stock sequence array convolutional neural network (SSACNN) [18] is proposed on the basis of the convolutional neural network, which combines advanced indicators such as futures and options. The accuracy of this framework for stock price prediction is higher than that of several other frameworks. The price of the financial market is a time series, so some researchers use long short-term memory neural networks in financial transactions [19]. In an independent repeated gambling game with a positive pure income, the Kelly formula assists investors to maximize the growth rate of the principal. In this study, a stock trading system is designed to reduce trading risk by using the Kelly criterion for money management when trading.
The paper mainly includes the following contents. The second part talks about related work. The third part mainly talks about the methods used in this paper, including longand short-term memory neural networks based on leading indicators, genetic algorithms, trading strategies, and Kelly's criterion. The fourth part elaborates on the stock trading system in detail. The fifth section is the result of the experiment with the proposed stock trading system. The last section is a summary of the whole paper.

Related Work
In recent years, machine learning technology has continued to advance, and researchers have applied machine learning in various fields. In the financial field, machine learning highlights its powerful learning capabilities. Lu et al. [20] proposed a framework based on the convolutional neural network (CNN) and Bidirectional Long Short-Term Memory (BiLSTM) to predict stock prices within 1,000 trading days. Compared with the other seven methods, the proposed framework has better performance and is more suitable for investors to make correct decisions in the stock market. Nelson et al. [21] used a long short-term memory neural network to predict future stock price fluctuations based on technical indicators and historical prices. Compared with other machine learning methods, this framework has good performance in predicting future stock price fluctuations. Bao et al. [22] used LSTM to predict stock prices based on wavelet transform (WT) and stacked autoencoders (SAEs). Through a series of experiments, the prediction accuracy of the newly proposed framework is higher than that of other similar models. Budiharto [23] used data from the Indonesian Stock Exchange to predict the price of stocks based on R language and LSTM. Experimental results show that the framework has better prediction performance on shortterm data. Based on historical prices and leading indicators in the two stock markets of the United States and Taiwan, Wu et al. [18] proposed a framework composed of CNN and LSTM to predict stock prices. Experimental results show that the framework has better prediction performance.
In 1975, Holland proposed the genetic algorithm for the first time and carried out the practical application of the genetic algorithm [24]. The genetic algorithm is used to find the optimal solution in a limited time [25]. Fu et al. [26] used genetic algorithms to find optimal solutions in portfolio management, including parameter optimization and combination weights. Different algorithms are used for evaluation, and the experimental results reflect the better optimization ability of the genetic algorithm. Chen and Zhou [27] combined LSTM and GA to get a stock prediction system. On the basis of the CSI 300 and China Construction Bank data sets, GA is used to obtain the most effective factors. The experimental results show that the proposed framework is superior to all benchmark frameworks. Chen and Shih [28] used GA to set the best parameters and forecast stock price fluctuations on the basis of technical indicators and Chinese news. Based on real data, a number of experiments have been carried out, and the experiments show that the method has relatively good performance. Therefore, a genetic algorithm is added to our research, and GA is used to determine the optimal threshold of trading signals.

Methodology
In this section, the methods used in this research will be discussed. First of all, the first section mainly talks about the framework used in this research, that is, long short-term memory based on leading indicators (LSTMLI), which uses LSTMLI to classify the rise and fall of stock prices. The second section mainly talks about how to use a genetic algorithm to find the best threshold to determine the trading signals in the stock market. The third section uses the Kelly criterion to manage funds.

Long Short-Term Memory Based on Leading Indicators.
In this study, a framework of long short-term memory based on leading indicators is used. The long short-term memory neural network is improved on the basis of the recurrent neural network, which solves many unsolvable problems of the recurrent neural network [29]. The recurrent neural network is a neural network with memory function, which adds a feedback mechanism of timing. This determines that the input of the recurrent neural network has not only the input at the current moment but also the input at the previous moment. For example, when watching a movie, if you want to know the next story, you need to make predictions based on the previous storyline and what is happening now. In this forecast, the issue of time series is involved. Based on such problems, recurrent neural networks are proposed. However, recurrent neural networks still face two problems: gradient explosion and gradient disappearance [30]. Subsequently, people continued to improve the cyclic neural network and obtained the long short-term memory neural network. The internal structure of LSTM is shown in Figure 1. Three gates are added to this network, namely, input gate, forget gate, and output gate, through which information is controlled.
In this paper, the inputs of LSTM are historical prices and leading indicators. Leading indicators can indicate turning points in the process of economic development and are one of the important indicators for predicting future economic development trends. Therefore, in this study, in addition to the historical stock price, the two leading indicators of futures and options are also used, that is, a total of three data sets. LSTM is used to classify the changes in stock prices. The changes in stock prices are divided into three categories: rising, falling, and unchanged. Therefore, the output of LSTM is also three types, namely, (1,0,0), (0,0,1), and (0,1,0). In other words, when the stock price change after preprocessing is greater than +0.01, the output result of LSTM should be (1,0,0); when the stock price change after preprocessing is less than -0.01, the output result of LSTM should be (0,0,1); when the stock price changes between +0.01 and -0.01, the output result of LSTM should be (0,1,0). According to these changes, a long short-term memory framework based on leading indicators is proposed, referred to as LSTMLI. The structure of LSTMLI is shown in Figure 2.
3.2. Genetic Algorithm. Buying and selling signals are a core issue of stock trading. Finding the right buying and selling signals can help investors obtain high returns. However, appropriate buying and selling signals are difficult to capture. Genetic algorithms are generally used to solve optimization problems. In this research, a genetic algorithm is used to find the threshold of buying signal and selling signal when trading stocks. Therefore, the chromosome is set up in two parts, as shown in Figure 3.

Initial Population. Initialize the chromosome first.
Because the sum of the three nodes obtained by the LSTMLI framework is approximately 1, two floating-point numbers between 0 and 1 are randomly generated as the threshold of the buying and selling signal, as shown in Figure 4. Then, use the thresholds of the two buying and selling signals to determine a trading strategy. Next, introduce the trading strategy of this research in detail. Assume that the result obtained by LSTMLI is shown in Figure 5, where "1, 2, 3, …" on the left represents the day. The first row of Figure 2 is the result of the first day obtained from the prediction. The value of its first node is 0.85551. It can be seen that 0:85551 > 0:76848, so a buy signal appeared on the first day. A buying operation must be followed by a selling operation, and the trading strategy set in this study does not allow simultaneous buying and selling transactions on the same day. Therefore, sell signals should be found from the next day. To find a buy signal, you need to observe the value of the third node. The value of the third node on the second day is 0.79545, which is less than 0.82843, so there is no sell signal on the second day. The value of the third node on the third day is 0.83419, and it can be known that 0:83419 > 0:82843, so a sell signal appeared on the third day, according to this trading strategy to buy and sell stocks.

Fitness Evaluation.
The fitness function is shown in Formulas (1), (2), and (3). Use fitness function to evaluate chromosomes. The result of this fitness function is the final funds, and the more funds, the better the chromosomes. B Figure 1: Schematic diagram of the internal structure of LSTM.
Input:30days ⁎ the variables of each day Output three nodes

Wireless Communications and Mobile Computing
Close i represents the closing price at the time of the buy signal, and SClose i represents the closing price at the time of the sell signal: Final 3.2.3. Crossover. The process of crossing two chromosomes is shown in Figure 6. Among them, one is the paternal chromosome, and the other is the maternal chromosome. A new chromosome can be obtained by crossing and splicing the two chromosomes. The crossover rate is set to 0.8.

Mutation.
The process of mutation of the two chromosomes is shown in Figure 7. Randomly select a chromosome for mutation, and set the mutation rate to 0.2.

Iteration.
After reaching a certain number of iterations, the iteration stops.

Trading Strategy.
A trading strategy is the core of a stock trading system, which is related to whether the stock trading can obtain higher returns. The trading strategy includes two key points: the time to buy stocks and the time to sell stocks. In this research, a genetic algorithm is used to find the threshold of buying and selling signals to determine the time point of stock trading. The process of finding buying and selling signals has been introduced in detail in Section 3.2. Figure 8 is an example of looking for buying and selling signals. In Figure 8, there are multiple sets of buying and selling signals. When a buy signal appears, the stock is bought; when a sell signal appears, the stock is sold. Buy and sell signals appear in pairs. When a buy signal appears, the next signal that appears will only be a sell signal.

Kelly Criterion.
After formulating a trading strategy, you can trade stocks. However, how much money should be invested when buying each stock is still a question worth studying. The Kelly criterion is a powerful tool for optimizing asset investment scores. It enables researchers or investors to maximize returns under repeated games. For example, there is now a binary outcome game; that is, there are only two situations of winning and losing. Assuming that the gambler puts $1 as a bet and the winning rate is p, if the gambler wins, it will get all the principal plus b 1 times the principal, that is, 1 + b 1 * 1, which means that the net profit is b 1 . If the gambler loses, he will be deducted b 2 times the principal, that is, 1 − b 2 * 1. Assuming that the gambler's initial capital is A 0 , f times the total capital is invested as a bet in each game, where 0% ≤ f ≤ 100%, and the total capital to step t is A t . Therefore, if the gambler wins the tth game, the total funds for the tth game are A t = A t−1 + b 1 * f * A t−1 . If the gambler loses in the tth game, the total funds for the tth game are A t = A t−1 − b 2 * f * A t−1 . After the T round of the game, assuming that the gambler wins W times and loses L times, that is, W + L = T, the total funds for the tth time are as follows: divide both sides of Equation (4) by A 0 , then take the logarithm at the same time and divide by T to get

Wireless Communications and Mobile Computing
assuming that the game is played countless times, that is, T tends to infinity, and the winning rate is p, then Equation (6) can be obtained: derivatives on both sides of Equation (6) can get the optimal investment score f , as shown in Therefore, if you want to know the investment score, you first require the winning rate p, the reward b 1 , and the penalty b 2 . Take an example to illustrate the process of solving f . Assuming that 0.5 times of the total assets are invested in each transaction, according to the trading strategy, the profit and loss of each transaction can be obtained. For example, a total of ten transactions have been made, and the profit and loss of each transaction are +5, -2, -3, +7, -4, +4, +8, +6, -3, and +6. In order to find rewards and punishments, it is necessary to separate profit and loss. Then, the profit is +5, +7, +4, +8, +6, and +6; the loss is -2, -3, -4, and -3. Therefore, the winning rate is 6/10 = 0:6; the reward is ð5 + 7 + 4 + 8 + 6 + 6Þ/6 = 6, and the penalty is −ð2 + 3 + 4 + 3Þ/4 = −3. Substituting the data into the Kelly formula can get f .

A Stock Trading System Based on Trading Signals and Fund Management
This section mainly includes two parts: the input of the framework and the structure of the stock trading system.
4.1. The Input of the LSTMLI Framework. The data sets used in this experiment are historical prices and leading indicators. Leading indicators can indicate turning points in the process of economic development and are one of the important indicators for predicting future economic development trends. Therefore, in this study, in addition to the historical stock price, the two leading indicators of futures and options are also used, that is, a total of three data sets. In the experiment, we used data from China's Taiwan stock market and data from the US stock market. However, there are only historical prices and options in the data of the US stock market. Therefore, when researching US stocks, only these two data sets are used. When researching Taiwan stocks, three data sets are used. In the experiment, the 30-day data was integrated into a matrix as the input of the framework. For example, use historical data as input to the framework, as shown in Table 1. Among them, "open, high, …" are the attributes of the data, and "1, 2, …" means the day.

Stock Trading
System. This paper is mainly to study the issue of stock returns. First, use the framework LSTMLI to classify stock price fluctuations. The classification method is shown in label = class + 1, change > 0:01, class 0, others, class − 1, change<−0:01: Among them, change represents the fluctuation value of the stock price. Then, use GA to study the output value of LSTMLI to obtain the optimal threshold for buying and    Figure 9: The flow chart of the stock trading system. 5 Wireless Communications and Mobile Computing selling signals. Thresholds can help investors find trading signals to determine profitable trading strategies. As we all know, stock trading has a certain degree of risk. If you invest incorrectly, you may face bankruptcy. Therefore, the amount of investment needs to be managed. Use the Kelly criterion to determine the proportion of the investment amount and then trade according to the trading strategy to calculate the return on the investment. The flow chart of the stock trading system is shown in Figure 9. The main process regarding the genetic algorithm is shown in Subsection 3.2. The appropriate trading signals are crucial to make high profits in stock trading. Trading signals include buy signals and sell signals, which are employed to allow investors to identify the right time to trade. In addition, the Kelly criterion is applied to determine the proportion of money invested in stock to decrease the risk of trades. The thresholds and the Kelly criterion simultaneously constrain the trading system in

Experimental Results
This section mainly introduces the results of the experiment. In the experiment, 10 stocks were selected (including 5 stocks in the Taiwan market and 5 stocks in the US market). Taking stock historical price data, futures data, and option data as data sets, the proposed stock trading system is used to conduct experiments on 10 stocks. "No Kelly" means that the Kelly criterion is not used in the process of seeking profit, and it is set to use 0.2 times of the total funds for transactions each time. "Kelly" means that when trading stocks, the Kelly criterion is used to calculate the share of funds for each transaction. First, the experimental results of 5 stocks in the Taiwan market are shown in Figures 10-14. The abscissa represents the data set, where "HP" is historical price, "O" is options, "F" is futures, and "HP+O+F" is a data set that combines historical prices, futures, and options. The ordinate represents the ratio of profit to capital. It can be seen from the figure that no matter which data set is used, the ratio of profit to capital obtained by using the Kelly criterion for fund management in stock trading is much higher than that obtained without the Kelly criterion.
Then, experiment on the data set of the US stock market. The experimental results are shown in Figures 15-19. The abscissa represents the data set, where "HP" is historical price, "O" is options, and "HP+O" is a data set that combines historical prices and options. The ordinate represents the ratio of profit to capital. Similarly, when using Kelly criteria for fund management, the benefits obtained are higher. In general, the benefits of using a data set based on historical prices and leading indicators are higher than the benefits of using a single data set.
In terms of stock price volatility forecasting, leading indicators do facilitate accuracy. This experiment utilizes the Kelly criterion for money management of stock trading.

Wireless Communications and Mobile Computing
As shown in the above figures, the trading strategy incorporating the Kelly criterion is more profitable in stock trading.

Conclusion
Stock trading has always attracted people's attention because of its high return characteristics, but it also comes with great risks. The Kelly Code is a powerful tool for fund management. In this research, a stock trading system is proposed. First, a long-and short-term memory neural network based on leading indicators is used to classify stock price fluctuations. Among them, the stock price fluctuations are divided into three categories: +1, 0, and -1. Second, use genetic algorithms to find the optimal threshold, which is the threshold of trading signals (buy signals and sell signals). Finally, the Kelly criterion is used to determine the best investment score to control the risk of the transaction. A series of experiments show that the use of Kelly's criterion for fund management does reduce the risk of trading, and the return is higher. Although a simple stock trading system has been implemented in this work, there are several aspects requiring further improvement: first, the prediction accuracy of stock price fluctuation trends should be enhanced; second, it is expected that more accurate trading signals can be obtained by other intelligent algorithms.

Data Availability
No data were used to support this study.

Conflicts of Interest
The authors declare that they have no conflicts of interest.