Combined trading strategy of bitcoin and gold

. Market traders buy and sell volatile assets frequently, with a goal to maximize their total return. We have been asked to develop a model that uses only the past stream of daily prices to date to determine each day if the trader should buy, hold, or sell their assets in their portfolio. The assets that can be traded are Bitcoin and gold. We will start with $1000 on 9/11/2016 and try to maximize the total return until 9/10/2021. We will start from forecasting prices and developing trading strategies. In terms of price prediction, we use MSE and Trend_Acc as indicators, and use XGBoost, a representative strong learning algorithm in traditional machine learning, and LSTM, which is good at time series prediction in deep learning, to fit and forecast the data respectively. At first glance, the curve fitting MSE are satisfactory , but, a closer look reveals that the model either firmly remembers the data of the training set, resulting in a lack of generalization ability for unknown data (XGBoost), or tends to take the previous day's results as the predicted results, resulting in a significant lag in the prediction curve (LSTM), all of which are reflected in the models' poor performance in predicting whether prices will rise or fall in the future. In our view, since a large number and complexity of factors affecting prices, it is unrealistic to predict future prices accurately from past prices alone, unless we can get rid of the limitation of the problem, use additional data to assist the prediction, or use all the data as a training set for fitting, we cannot achieve good results in the prediction, but such behavior is inconsistent with our original intention. We established restricted trading model based on composite index judgment. The model not only applies the traditional economic Relative Strength Index and Stochastics Oscillator Index, but also introduces the K-Lipschitz limitation in deep learning into the model. The model dynamically adjusts each transaction strategy according to the changes of working capital and total assets, purchase cost, selling profit and other factors, and the total income of the model is $132433.1. In the horizontal comparison, the profit of our model is more than 39.6%-283.6% than that of the traditional moving average strategy and RSI-STC strategy, and 51.3% higher than that of the random walk model using Montmarlowe algorithm. In addition, we collected the transaction data of bitcoin and gold from 2012-01-01 to 2022-02-21, and applied the model to the historical data, and received good returs. For example, from 2012-1-1 to 2022-2-21, the return was $8418074.9. It is proved that the model has high generalization performance and strong stability. To test the sensitivity of the model to transaction costs, we also analyze the changes in trading strategies that should occur when fees rise. The analysis shows that the traders' single trading volume decreases first and then increases with the increase of the commission fee. The results of sensitivity analysis show that the return change is less than 3%, which proves the robustness of the model. Finally, we made a memo to summarize our work.


Problem Background
People always regard bitcoin as "a new kind of gold" or "digital gold" in the financial market. However, there are great differences between gold and bitcoin in transactions. Bitcoin trading is round the clock. Its price fluctuates 24 hours without restrictions on rise and fall, and there is no third-party supervision. The unit price of gold fluctuated between $1100 and $2000 during 2011-2021. Although the price rose most of the time, no long-term trend was found. During this period, the price of bitcoin rose from less than $100 to more than $50000, showing an obvious upward trend. For the market, gold is a safe haven asset, and bitcoin still has a long way to go in terms of history, price stability and availability. It is a random and profound problem how to maximize the return.

Restatement of the Problem
We need to design a model to trade in the gold and bitcoin markets with the $1000 currently held. Through this model, determining how to make the best decision in the gold and bitcoin trading markets. We need to prove that this strategy can enable traders to obtain the maximum return after five years of trading and determine the sensitivity of the strategy to transaction costs.
However, the algorithm is limited by the following limitations Only two data sets given can be used The decision of that day can only be based on the price up to that day Gold is only traded on days the market is open

Our work
This question is a composite matter of prediction and planning. It gives the trading prices of gold and bitcoin in recent five years, and requires us not to trade from the "perspective of God".
Considering the background information and restricted conditions identified in the problem statement, we need to solve the following problems: Establish LSTM model, trying to predict the future price trend and judge whether the market will rise or fall on the next day Determine the trading time point by integrating the transaction costs and transaction characteristics of bitcoin and gold, and conduct transaction risk analysis to finally obtain the maximum return value Prove that current strategy offers the maximum benefit, by adjusting parameters, measuring model errors and making horizontal comparison with other models Analyze the sensitivity of the strategy to transaction costs by adjusting the value of transaction costs. At the same time, the impact of transaction cost on strategy and results is obtained Summarize the results and write a memo Firstly, try to predict future prices based on past prices. According to the predicted price, combined with the transaction characteristics and transaction costs on gold and bitcoin, design the objective function to maximize the income. After that, test and analyze the accuracy of the model . The vertical  analysis can use MSE, MAE, RMSE, RMAE and other indicators to measure the model error, the  horizontal analysis can be compared with the trading strategies such as moving average strategy. In the end, in order to explore the sensitivity of transaction strategy to transaction cost, we can increase or decrease the value of transaction cost, rerun the model and observe the change of income. In summary, the whole modeling process can be shown as follows:

Assumptions and Justifications
To simplify the problem, we make the following basic assumptions, each of which is properly justified.
Assumption 1: Personal buying and selling behavior will not affect the market Justification: Some scholars believe that individual trading activities may have a butterfly effect on the stock market. When flow of the international, interest rates, psychological expectations and other factors are coupled under certain conditions, it will lead to the butterfly effect of the financial market. In order to ensure that the transaction prices of gold and bitcoin are independent of each other and the trading behavior of traders is independent, the impact of individual trading behavior on the financial market is not considered in this topic.
Assumption 2: To simplify the problem, we make the following basic assumptions, each of which is properly justified Justification: Moderate inflation and severe inflation will bring varying degrees of impact to the market. It can stimulates the stock market, but it can also inhibits the operation of the stock market. Inflation is mainly caused by an excessive increase in the money supply. The money supply is generally in direct proportion to the stock price. The increase of money supply means the rise of stock price. On the contrary, the reduction of money supply will reduce the stock price. In order to ensure the purchasing power of money, the inflation is not considered in this question.
Assumption 3: Traders are rational people Justification: We assume that the trader is a rational person. The trader is able to view the market rationally, waiting for the right trading opportunities and identifying traps. Traders will not take extreme actions in the course of trading.
Assumption 4: The minimum trading share is not considered in gold trading Justification: In the international financial market, gold trading takes "hands" as the unit, and each hand is 100 ounces. But $1000 can't even buy one hand gold. In order to simplify trading, we don't consider the trading unit of gold.
Assumption 5: The amount of each transaction of bitcoin is at least $5 Justification: The minimum unit for purchasing bitcoin is 0.00000001, and the minimum transaction amount allowed by different platforms is also different. In order to unify the measurement and avoid too frequent transactions, we assume that the minimum amount of bitcoin purchased each time is $5.
Assumption 6: bull transaction or bear transaction cannot be used in trading Justification: bull transaction or bear transaction can have a negative impact on the stock market. The price of gold or bitcoin could also be affected. To ensure fair trading, bull transaction nor bear transaction is used in this case.

Notations
The key mathematical notations used in this paper are listed in Table 1.

Model I: Daily price forecasting model
In recent years, more and more scholars use neural network model to predict stock price. Many scholars have achieved high accuracy and good prediction effect by continuously improving the model. Stock market time series is not any specific static function that can be mapped. Random walk is the best property to describe the time series motion of stock market. As a random process, the real random walk has no predictable model, so it is meaningless to try to model it. Fortunately, many parties continue to argue that the stock market is not a purely random process, which makes us understand that there may be some hidden pattern in the time series. In the face of these hidden patterns, LSTM depth network is the main candidate for prediction. LSTM is very powerful in solving the problem of sequence prediction, because they can store the previous information, and the previous stock price is very important for predicting the future trend of stock price.

Principle of LSTM
LSTM introduces three gates, namely input gate, forget gate and output gate. A memory cell with the same shape as the hidden state is used to record additional information. The formula for the three doors is as follows: Input gate: (1) Forget gate: is the number of hidden units, is the given time step, is small batch input, and is the hidden state of the last time step.
Next, we need to calculate candidate memory cells, The candidate memory cells of time step t are calculated as follows: (4) The calculation of the current time step memory cell h combines the information of the last time step memory cell and the current time step candidate memory cell, and controls the flow of information by forget gate and input gate: (5) With memory cells, we can also control the flow of information from memory cells to hidden states by output gate: The tanh function ensures that the value of the hidden state element is between -1 and 1. The following figure shows the whole calculation process of hidden state in LSTM:

Model Structure And Result
The first is the LSTM layer, which is used to extract timing information. The number of layers is set to 8, and the hidden_dim of the hidden layer is set to 128. Finally, there is the full connection layer, which converts the hidden layer's data into the 1-dimensional data required for output.
Take the data input of [3,20,1] as an example, its BATch_size is 3, time_step is 20 (that is, the past 20 days are used to predict the future one day), and Feature_DIM (the input feature only has the price dimension) is 1. The model finally outputs the forecast price for each batch. Details of the data and the number of parameters at each level of the model are as follows.
The training set and test set are divided to 7:3. Taking 20 days as the timestep (i.e. using the past 20 days to predict the next day), the MSE loss change of LSTM on the training set (the data has been normalized) is as follows:   Although the model seems to fit the trend of the gold price curve well, and the prediction of the trend of bitcoin before bitcoin surge seems to be accurate, the judgment after the surge is very poor. But if you observe carefully, you will find that the trend predicted by the model has a certain lag, that is, the model tends to simply take the price of the previous day as the prediction result of the day. Let's go a step further.

Forecast the rise and fall of tomorrow
We use the model to predicted the rise and fall of the next day, representing the accuracy as Trend_Acc, and the results are shown in the table below: Although the MSE loss has been fitted very low, the model cannot make a more accurate judgment on the rise and fall of the next day in the training set and verification set.

Traditional machine learning model --XGBoost
The performance of the neural network was unacceptable, and we tried traditional mahine learning model--xgboost. Taking prices of the past seven days as the feature, and the price of the day as the label, The performance of the model in the training set is as follows: Its prediction curve almost coincides with the real data curve, as shown in the figure. Xgboost perfectly fits the data of the training set on the training set.  Looking at the trend of the curve, although the prediction result of xgboost is acceptable, the judgment of rise and fall has obvious lag. The result of the rise and fall accuracy are shown in the table below. The accuracy rate is 100% in the training set, while in the test set, the accuracy rate is only about 44%. It seems that the model simply remembers the data of the training set. Such result is hard to accept, and it will not be better than blind speculation.

RSI: Relative Strength Index
The relative strength index RSI predicts the future direction of price changes based on the price changes in a specific period, and displays the strength of the market according to the price rise and fall range. When the short-term RSI is below 20, the cross of the long-term RSI from bottom to top can be a buy signal. When the short-term RSI is above 80, the cross of the long-term RSI from top to bottom can be a sell signal. RSI below 50 is a weak area and RSI above 50 is a strong area. Breaking through the 50 line from bottom to top is from weak to strong, and breaking through the 50 line from top to bottom is from strong to weak. It is generally believed that the accuracy of RSI above 50 is high. RSI is calculated as follows: A is the sum of closing gains in N days, B is the sum of closing decline in N days, N is the calculation cycle of RSI.

Stochastics oscillator
The random index Stochastics oscillator is suitable for the technical analysis of medium and shortterm stocks. The range of K and D is from 0 to 100. When D > 80, the market is overbought; When D < 20, the market is oversold. When K value > D value, the display trend is rising. And when k value < D value, the display trend is down. The buy signal appears when K line breaks through D line upward. On the contrary, k line falls below D line represents the sell signal. K-line and D-line intersect above 70 and below 30 means more reliable to buy or sell. If KD gold crossover occurs below 20, it is the best buying point; If KD death crossover occurs above 80, it is the best-selling point. When the KD index deviates from the stock price, it is not only a turning signal, but also the medium-term or short-term trend may have peaked or bottomed. This is usually an early warning sign of short-term trend when the rising or falling speed of K value and D value weakens and the inclination tends to be flat. The relevant calculation formula is as follows: is the closing price in N days, is the lowest price in N days, and is the highest price in N days?

Results
We used XGBoost, a representative strong learning algorithm in traditional machine learning, and LSTM, which is good at time series prediction in deep learning, to fit and predict the data respectively. Although the curve fitting results and MSE are satisfactory at first glance, however, a closer look reveals that the model either firmly remembers the data of the training set, resulting in a lack of generalization ability for unknown data (XGBoost), or tends to take the previous day's results as the predicted results, resulting in a significant lag in the prediction curve (LSTM), all of which are reflected in the models' poor performance in predicting whether prices will rise or fall in the future.
However, LSTM prediction results may act synergistically with the indexes in the stock market to judge whether the stock price is rising or falling. We will combine the two in the trading model and assign different priorities respectively to comprehensively judge the trend of price changes.

Model II: Model of restricted trading based on comprehensive index judgment
Considering transaction costs and price fluctuations, Bitcoin is a virtual currency whose price sharply fluctuates in the short term, making it suitable for short-term or ultra-short-term transactions. Gold the international currency, whose price is stable for a long time, is suitable for medium -and long-term transactions. In the formulation of daily trading strategy, we have introduced three indicators: namely KD (Stochastics oscillator), RSI (RelativeStrengthIndex) and K-Lipschitz. These three indicators can predict tomorrow's price direction within a certain range. Combined with these three indicators and the prediction of future prices by LSTM , we plan and limit the buying points and selling points respectively.
The specific algorithm is shownin the figure below:

Purchasing rules
If buying gold or bitcoin that day, consider the following indicators in turn: (1) the price trend is on k-lipschitz, which indicates the price is on an upward trend and at an Angle.
(2) K line breaks through D line upward: when K line crosses D line upward, the two form the golden fork shape of KDJ index. This pattern is a good trading signal as it indicates a sharp rise in price in the short term.
(3) Abnormal RSI: If the RSI exceeds 70 or is below 30, prices are likely to fluctuate. In this case, the purchase situation should be considered together. The probability of purchase depends on the probability distribution.
The ratio of total assets to liquidity affects the amount of transactions. In short, the larger the liquidity to total assets ratio, the more conservative the trading volume. The addition of a constant term has the effect of smoothing the curve.

Selling rules
If we want to sell gold or bitcoin on this day, we should consider the following indicators.
(1) When the expected return exceeds the cost by a certain percentage, a sell operation is executed. The expected rate of return for this model is 20% (2) If the price of the day is slightly lower than the previous day but within an acceptable range, it is still considered to be in an uptrend. This is a good signal to buy at that point. The execution of a purchase is based on the above algorithm. The acceptable hedge float is 1.6%.
(3) Whether the D line breaks through the K line: If the K line falls below the D line, it means that the upward momentum of the stock price is still very weak and not enough to continue to pull up the stock price, at this time investors should sell stop loss.
(4) Decide whether to sell or not based on the range of abnormal RSI indexes defined above. This indicator determines whether to perform a sell operation, exactly opposite to determining whether to buy.
Pseudo code of process as follows: Table 5. Pseudo code of process

Result
The final result is shown in the figure below: Figure 9. investment bewteen the five years our strategy can harvest $132433 in five years, more than 130 times the initial capital.

Evidence of Best Ststrategy
In this section, we show that our model is the optimal strategy. To demonstrate this: (1) Compare horizontally: In this paper, we compare the results of our model to other models on the same data set.
(2) Compare vertically: In this paper, we add previous gold and bitcoin data to the input. For different time periods, this model ends up with better returns.

Compare horizontally
We compared the model with DoNothing, RIS, STC, RIS-KD mixed strategy, moving average strategy and random walk strategy, and the results show that the expected income of our model is significantly higher than other models. First, let's take a brief look at some strategies for participating in comparison.
The horizontal comparison is summarized as follows: DoNothing: It is often said that as long as you do not trade stocks, you have already earned more than half the people who trade stocks; Also, in lots of circumstance, someone forgets about an investment and finds a big return a few years later. This strategy of meeting changes with constancy is also worth considering. Using this strategy, we bought bitcoin in full cash on the first day of the simulation and held it until the last day before selling it。 Moving average strategy: Execute buy and sell operations at the intersection of different SMAs (5 days mean line,20 days mean line) Random walk: The buy and sell operation is completely random, that is, the system generates a random number from 0 to 1, if the number is greater than 0.5 then buy, less than 0.5 then sell The rest of the models have been mentioned in Part5 and will not be repeated here The above results show that our decision-making model can basically obtain more benefits in a longer time range, which proves the superiority of our decision-making model. (Of course, the continuous growth of the overall price of Bitcoin in these years also fits our robust and long-term operational decision model) In conclusion, from horizontal and vertical comparisons, as well as additional data and longer time dimensions, our decision model has the maximum benefits, and the model has good stability and low risk.

Sensitivity analysis
The sensitivity of trading strategy to transaction cost can be regarded as the influence of the change of transaction cost on the final return. Obviously, since our investment is mainly biased towards bitcoin, the transaction cost of gold does not have a significant impact. Therefore, the transaction cost of Bitcoin is the key to test the sensitivity of our model to transaction cost. By adjusting the value of bitcoin transaction cost α (in the range of 0.2‰ to 2‰, taking α values as 0.2‰ steps), we re-run the model to observe the change of earning.
The results are shown in the figure below: Different trading strategies are reflected in the model by the variation of the parameter values. In this paper, the single sale volume, as well as the target profit margin and K-Lipschitz values are adjusted according to different trading strategies. If the single sale rate of a round of trading increases, the K-Lipschitz value increases, and the target margin increases, then the adjustment of the a-value for this round requires a more aggressive trading strategy, and vice versa with a more conservative trading strategy.
The results show that as the a-value increases, the target profitability decreases and increases, and the other two variables remain constant. This indicates that when trading fees are increased, the trading strategy should be conservative and then aggressive.
The analysis shows that when the commission is low, the loss on each trade is small and the trader can increase his profit through multiple short-term trades; when the commission increases, the trader has to consider his loss on a single trade and becomes more conservative; When the commission continues to increase, traders need to maximize their profits through long-term gains and therefore sell more in a single trade.

Strengths
We use the prediction results of LSTM and stock market trading indicators for comprehensive consideration, making up for the lag of neural network in prediction to some extent.
By formulating multiple trading conditions, we avoid frequent transactions and improve the accuracy of trading points We avoid some trading risks by calculating probability The yield of bitcoin is significantly higher than that of gold. The yield of bitcoin and gold are considered in the model, reasonably allocate the funds investment

weakness
Single model can not predict more accurately whether the price of the second day is up or down. Stock market trading is a complex process, and the mathematical model cannot predict the emergence of special situate.