An optimal portfolio method based on real time prediction of gold and bitcoin prices

Aiming at the portfolio problem of gold and bitcoin with a given linear trading commission, this paper puts forward the stage implementation forecast and optimal portfolio model. In the aspect of data prediction, SMA is used to predict the initial data, LSTM is used to predict the price trend of long-term data, and daily updated real-time price data is predicted. Considering the risk aversion of investors, the heuristic algorithm is used to solve the daily trading strategy of maximizing utility from September 12th, 2016 to September 12th, 2021. The simulation analysis of the sliding window shows that the algorithm can realize reasonable prediction, which verifies the effectiveness of the algorithm.


Introduction
Since COVID-19, the global economy has been affected to varying degrees, which once again aroused the interest of investors. For a long time, gold investment has been regarded as an effective means to resist inflation and other economic uncertainties. People invest in gold to resist the losses caused by inflation (Shahbaz et al., 2014). In the past decade, bitcoin, as an emerging investment asset, has attracted more and more attention. A considerable number of people believe that this virtual currency can be invested as a new type of gold with higher returns, and regard it as a means to fight inflation (Baur & Hoang, 2021), but there are still some experts who have expressed their concern about the stability of the investment of bitcoin (Cheah & Fry, 2015). Therefore, in the existing economic environment, how to make reasonable investments to maximize income is very important. Now, a trader asks to develop the best daily trading strategy that only uses past daily prices to determine whether the trader should buy, hold or sell the assets in his portfolio. The trader's initial capital is $1000 and will use a five-year trading period from September 11, 2016, to September 10, 2021. The commission for each transaction (purchase or sale) costs α% of the amount traded. Assume α gold = 1% and α bitcoin = 2%. There is no cost to hold an asset. Bitcoins can be traded every day, but gold is only traded on days when the market is open. CONTACT Zhongqi Miao 20191768@cqu.edu.cn In the past few decades, many scholars have been attracted to how to make asset investments. An important step before deciding how to invest is to predict the price of future investment products. A large number of scholars believe that this process is to model the time series. Bentes (2015) used GARCH's three volatility models to test the return of gold and get that FIGARCH is the best model to capture the linear correlation of conditional variance of gold price. Risse (2019) used discrete wavelet transform to separate the time domain and frequency domain of prediction variables and combined it with a support vector machine to predict the dynamic price of gold.
In recent years, with the rise of deep learning algorithms, many scholars are more and more inclined to use deep learning for price prediction. Chen et al. (2019) combined the ARIMA algorithm with the BPNN algorithm to predict bitcoin price. Livieris et al. (2020) proposed to use the internal representation ability of CNN to extract time series data and the long-term dependence of LSTM to extract data and combine the two to predict gold prices.
After forecasting, how to analyze the data for investment strategy is another important issue. Quite a several scholars believe that when investing, we need to consider not only the return but also the risk. Cai et al. (2000) introduced the risk measure into the optimization problem, characterized the risk by the maximum absolute deviation, and gave the analytical solution of the model based on the minimization and maximization criteria. Konno and Wijayanayake (2001) considered the mean absolute deviation model with fixed transaction cost and minimum transaction volume, gave the bound of the value of the optimal objective function, and used the bound to design an effective algorithm for the model. Lobo et al. (2007) considered the portfolio problem with linear and fixed transaction costs and gave a heuristic method to find the approximate solution to the investment problem by using the global bound obtained by convex relaxation. Unlike Livieris's work, because the data is onedimensional and the initial data is small, we use SMA to expand the data of short time series and LSTM to extract the data of long time series and combine the two to predict gold and bitcoin prices. On the decision algorithm, we selected and improved the model of Lobo et al., and set the constraint conditions: the trading time (Saturday and Sunday) and the trading threshold of gold and bitcoin, thus avoiding the extreme trading situation and making the results more reliable.

Assumptions and notations
In the actual scenario, the price forecast and investment portfolio of gold and bitcoin are very complicated, such as economic crisis, inflation, investor preference, etc., which are all factors that affect the forecast and decisionmaking.
To simplify the model, make the following assumptions: • No interest on cash. -cash can not be used by banks to earn interest; • There is no shorting. -consider the optimal investment made by investors according to the previous price data; • There is no prior information on the investment of bitcoin and gold.investors can not know which currency they invest in at the beginning is more profitable and risky; • Different investors have different choices about profit and risk. -only consider the optimal balance between risk and profit; • There is no sudden economic crisis in the market. -the previous price data can be trusted. The value currently assigned to item j r j profitability of item j on the decision day λ Investor's attitude value r Total rate of return on the investment day t Number of days elapsed since the starting point a Accuracy of prediction model n The size of the sliding window In this article, some important symbols are shown in Table 1 below, and other uncommon symbols will be introduced when they are used.

Model predictive algorithm
The model consists of two stages. In the early days, because of the small amount of known data, the average value of the data of the previous few days was used as the predicted value, that is, the simple moving average (SMA) model.
As the prediction results of SMA model will gradually converge with time, this paper selects 2n days as the time node (n is the size of the sliding window), at which time enough data has been accumulated to better describe a large amount of data. After 2n days, then the improved long-term and short-term memory (LSTM) based on the sliding window is used to explore the trend of data.

Simple moving average
For the simple moving average model, the average of historical data is used as the predicted value in the next period, and the formula is as follows.
where n is the sliding window size.

Long short-term memory based on the sliding window
According to historical data, the direct global forecast is unreasonable. On January 4th, 2017, for example, Bitcoin showed a counterintuitive decline, contrary to the steady increase before that day. It is difficult to get the correct result if the data from the first day to that day are directly used for analysis because the overall upward trend affects the judgment of the model. Furthermore, it is unreasonable to accurately predict the price rise and fall, but the future trend can be analyzed.
(1) Sliding Window When predicting bitcoin and gold prices, using too old data to predict the future may not be helpful or even harmful. To this end, the sliding window is applied to the model. When a new day's data is received, the oldest data will be discarded, and the sliding window will be used to update the model in real-time. This method is shown in Figure 1.
(2) Optimization of the loss function In the prediction task, the error between the predicted value and the true value is usually taken as the loss function of deep learning. However, to solve this problem, the model proposes to combine the Kendall rank correlation coefficient of predicted value and real value with mean squared error(MSE) as the loss function of the prediction task. The rank correlation coefficient is defined as follows: where d represents the difference of rank, n represents the amount of data. By depicting the hierarchical relationship between the real value and the predicted value, the correlation of the trend can be depicted. The total loss function (MSE + r s ) is as follows.
(3) Optimization of hyperparameters In the proposed model, in order to achieve better prediction effect, hyperparameter optimization is of significant importance. In this model, the size of sliding window and prediction days will affect the final prediction accuracy.
(4) Long Short Term Memory With the rising of deep learning, this method has been widely used in the field of economic forecasting. LSTM is a very effective prediction model, which establishes the relationship between data through input, output, and forgetting gate. In addition, this model can effectively deal with the time series relationship between variable-length input sequences of time series data. The structure diagram of the model is shown in Figure 2.

Model decision algorithm
For the decision model, according to the requirements of the title, starting from September 11th, 2016, only the generated transaction data will be used for decisionmaking. In this paper, the cold start of portfolio investment is established to simulate the investment decision of collecting data from 0 in the stock market.

Risk attitude
In actual decision-making trading, the trading strategy should consider the trader's attitude towards risk.
Activists often pursue the maximization of benefits under certain risks, while conservatives often pursue the minimization of risks under certain risks. Therefore, the attitude index should be introduced into the decision model to measure the user's attitude towards risk. First, calculate the yield of gold and bitcoin. Remember that the biggest rate of return in both historical data is β. The minimum rate of return is α, Remember that the average value in historical data is m, Get the triple (m, α, β).
The radical attitude function is: The conservative attitude function is: Then the attitude function model is: Among them, r is the total rate of return on the decisionmaking day. λ represents the attitude of investors, and its value range is [0, 1]. When the decision-making begins, according to the investor's attitude towards risk, choose its corresponding λ as a fixed value to influence the subsequent decision-making. Get the risk aversion index M e in daily decision-making, and then substitute it into the utility maximization portfolio model of the day to help complete the trading strategy of the day.

Utility maximization portfolio investment
For the decision-making model, according to the historical data that has happened, this paper establishes an improved utility maximization portfolio model to make decisions in the future. The utility function uses mean and variance to reflect the relationship between risk and expected return. Its general form is: Among them, U is the utility value, A is the index of investors' risk avoidance degree, R 0 is the expected return rate of portfolio, and σ 2 is the variance of investment return rate. Utility maximization portfolio model is developed on the basis of utility function, and the form of utility maximization model is as follows: R = (R 1 , R 2 , . . . R n ) represents the interest rate of n investment items. X = (x 1 , x 2 , . . . , x n ) representing the coefficient vector of the proportion of n portfolios. Q is (σ ij ) n×n . F is (1, 1, . . . , 1) . In the process of considering the portfolio, the utility maximization model does not takes into account the risk factors broadly. Moreover, it does not consider the transaction cost and the situation of market closing. Therefore, this paper improves the utility maximization model. Firstly, for risk factors, this paper uses semi-absolute deviation to improve the utility maximization model.
Compared with the variance in the original model, the semi-absolute deviation can avoid calculating the covariance between different asset returns and reduce the amount of calculation. This question needs cyclic iterative processing investment, involving cyclic operation, so using semi-absolute variance is more suitable for iterative decision-making. In addition, the semi-absolute deviation can better reflect investors' understanding of risk by taking the rate of return below as the risk measurement factor.
In view of the excellent properties of semi-absolute variance, this paper optimizes the difference term of the maximum utility model. In this question, the σ 2 is replaced as 1 T T t=1 | min{0, n j=1 (r jt − r j )x j }|. Thus, the corrected effect value is: In addition, there is no short selling in this question, so it is necessary to restrict the direct distribution values of gold, cash and bitcoin to be greater than 0, namely: At the same time, the total allocation value of the three should be less than the initial allocation value, and let G 0 , C 0 and B 0 be the total value of gold, cash and bitcoin in the previous state, respectively. There should be: Then, in this question, the transaction cost is a factor that can not be ignored. The transaction cost c i of the i risky asset is a V-shaped about the original portfolio x0 = (x0 1 , x0 2 , . . . , x0 n ) and new investment: In which, k i represents the first i (i = 1, . . . , n) kinds of risk assets unit transaction costs, and the total transaction cost refunded is n j=1 c j = n j=1 T t=1 k j |x jt − x 0 jt |. After deducting transaction costs, the portfolio and expected rate of return become: Among them, r j represents the rate of return of investment product j at the beginning of the investment. In addition, since gold can't be traded on rest days, it needs to be constrained in the constraint conditions. mod function is introduced to correct the date, and the first decision-making day is taken as the starting day t 0 .
The gold price on the rest day is regarded as the gold price from the nearest trading day, and the holding ratio remains unchanged. When the cycle reaches the 6th and 7th days, the value change of gold is 0, and the constraint equation is: That is when t cycles to the 6th and 7th days, the gold price is regarded as the gold price on Friday (neither rising nor falling), and the holding ratio remains unchanged. |x it − x jt | should be 0. Thus, the revised utility maximization portfolio investment model is obtained: Where the value of n is 3, and the agreed serial numbers are gold 1, cash 2, and bitcoin 3, then i in the constraint conditions is 1, and the combination of values of k is [0.1, 0, 0.2]. M e is the risk aversion index obtained in the first step of the model. At the same time, A is an indicator of investors' risk aversion. When the prediction accuracy of the prediction model reaches 100%, investors can completely avoid risks according to the price trend of investment products, so the risk at this time is zero. Based on this reasoning, this paper turns the risk aversion index A into the opposite value of the prediction accuracy of the prediction model in the previous stage. Let the accuracy be a, then A = 1−a.

Genetic algorithm
A heuristic algorithm is used to solve the multi-objective nonlinear programming problem. Owing to the contradiction between risk and benefit, it is impossible to require all objectives to reach the optimal solution, but only an effective solution can be obtained. Genetic algorithm (GA) is a computational model of the biological evolution process simulating the natural selection and genetic mechanism of Darwin's biological evolution theory. It is a method of searching for the optimal solution by simulating the natural evolution process. Its main operators include the selection operator, crossover operator, and mutation operator.

Simulation verification
To verify the effectiveness of the algorithm for price forecasting and portfolio decision. First of all, based on the above prediction model, the average value of the historical data of the previous two days is calculated as the prediction value of the next day (for gold data, the rest days need to be deleted). In the first two weeks, the price forecast results of gold and bitcoin in the first two weeks are shown in Figure 3.
In the simulation verification, we set the parameters of genetic algorithm: The set of group size M is 20, The set of termination evolution algebra of genetic operation T is 100, and the crossover probability p c and mutation probability p m are 0.8 and 0.2 respectively. In this way, the best trading strategy within 3 days can be calculated and implemented according to this plan. Then take 3 days as a cycle to get the globally optimal result.
The pseudocode of genetic algorithm is as follows: Algorithm 1 Genetic algorithm for our system.

Influence of sliding window size on prediction results
After two weeks, based on LSTM model, the sliding window is used to extract the historical data, and the grid search algorithm is used to optimize the parameters of the sliding window.
Because the market price is time-sensitive and the setting parameters of the window can't be too large, the size parameters of the sliding window are selected as 4, 5, 6, and 7, respectively, to predict the price changes in the next three days. The prediction results are shown in the following Figures 4 and 5.
Combined with Table 2 of the Root Mean Square, it can be seen that the root mean square of the prediction results fluctuates with the increase of the sliding window, but it gradually decreases in general. At the same time, under the same sliding window, the mean square deviation of Bitcoin is far greater than that of gold, which is also in line with the reality that Bitcoin is riskier and more difficult to predict.
Finally, through machine simulation, the size of the parameter sliding window is set at 7 days, which means n equals 7, and the predicted number is 3 days. The price It can be found that the model has a good prediction effect on gold. This is because the smaller the change data of gold, the more stable the fluctuation, and the better the prediction model can capture the relationship of its internal data; For Bitcoin data, because of its 'mutation point', although the model can't be as accurate as gold, it is very investor-friendly with the original data in the trend.

The influence of setting a threshold on decision results
On the basis of the decision model, genetic algorithm function package of Matlab is used to solve the nonlinear programming problem. Considering that genetic algorithm is easy to converge, which leads to a certain   asset ratio approaching zero infinitely, this is not in line with investment common sense.
After many machine simulations, it is found that the investment results without a threshold fluctuate around $1000, and even negative returns will be realized in some cases. Select one of the cases without a threshold, as shown in Figure 8 below, and the assets finally stabilize at $1248, which doesn't realize a large return. Therefore, it is necessary to add a threshold to the decision model.
At each decision-making, the threshold value between 0 and 0.33 is randomly generated for each investment choice. The investment result considering the threshold value is shown in Figure 9 below.   As can be seen from Figure 9, when investors can take certain risks, the overall investment shows a positive return, that is $4138, which is much larger than $1000. Meanwhile, in Figure 10, when the threshold is randomly set, it reflects the curve of the proportion of bitcoin, gold, and cash with time. It can be seen that the proportion of bitcoin is the highest in the investment process, and the interest rate of gold is slightly higher than that of cash.
There is rarely a cliff-like decline in profit in the curve, which can prove that model will try its best to make investors avoid risks by taking them into consideration. Even if investors lose money, they will only lose a small amount, which fully embodies the idea of maximizing the utility of our model, and makes investors minimize possible risks when trying to maximize profits. It also reflects that the prediction model is accurate to a certain extent. If there is a big deviation in the forecast model, the profit margin of investors will inevitably be greatly reduced.

Conclusion
Through the analysis of the asset portfolio with a given linear transaction cost, and considering the risk preference of investors, this paper proposes a model control algorithm based on the combination of SMA and LSTM for real-time prediction, to realize a reasonable price prediction. Simultaneously, the investment portfolio with maximum utility is established, and the decision-making is simulated by genetic algorithm to achieve the balance between profit and risk.
In the future work, we can focus on the following two aspects: (1) The actual degree of risk aversion of investors is difficult to quantify, and the risk aversion of investors can be improved in the future by combining existing related research.
(2) The iteration of LSTM prediction model and nonlinear programming model involves a large amount of computation, which leads to the need for stronger computing power for the model to pursue higher accuracy and higher profit. Thus, an improved genetic algorithm can be used to optimize the calculation.

Disclosure statement
No potential conflict of interest was reported by the author(s).