Research on Market Transaction Based on Dynamic Programming Model

. Based on the principle of dynamic programming model, this paper studies the market transaction problem and establishes a transaction model to help decision-making. First, on the basis of the historical price data of gold and bitcoin, the data is processed by the wavelet transform method to ensure the accuracy of the model. Second, a time-series forecasting model and a backpropagation neural network are employed to predict the future prices of gold and bitcoin. And use a dynamic programming model to recursively determine the specific investment ratio of each asset in each transaction to achieve the best return. Finally, the effect of the model is verified by the evaluation index and transaction cost sensitivity test. The results show that transaction costs affect both strategy and outcomes. As transaction costs increase, both transaction frequency and total assets show a downward trend.


Introduction
In the financial market, rational traders always seek to maximize returns. Gold, as an internationally recognized currency, is a tangible financial asset. As a digital product, Bitcoin is a virtual asset [1]. They are all assets with high price volatility. Risk and return are positively correlated, so many investors are keen to invest in gold and bitcoin [2].
However, from the financial crisis events in the past ten years, we can see that in reality, investors' trading decisions are extremely easily influenced by their emotions and lack rationality. The development of quantitative investment has greatly overcome this defect. It makes investment decisions by building models, avoiding the influence of factors such as investor emotions [3].
For the convenience of research, it is assumed that the principal is $1000, with a five-year trading period beginning from Sept.11, 2016 to Sept. 10,2021. Only three forms of assets are included in the trading portfolio. They are cash, gold and bitcoin. For each transaction, the commission of gold equals 1% of the amount traded , while bitcoin is 2%.
Our task is to develop a model based on the historical price data to tell the trader how to manage the portfolio: (1) Create a model based only on historical price data that will provide the best daily trading strategy to maximize revenue.
(2) Our model and strategy will be applied to calculate the final value on September 10, 2021. Work roughly as shown in Figure 1.

Data Pre-processing
The market is constantly changing, and investors are flooded with information and sometimes get lost. Therefore, only by establishing a suitable trading strategy and following the trading principles can we gain long-term profits in the market. In this section, we will build a trading strategy based on the assumption of rational investors [4].
Before we start to analyze the problem, it is essential to ensure that the given data is accurate and reliable. There are some price data missing inevitably. We let the average value to replace the missing value since the data before and after can be obtained. Then, we use Wavelet transform to eliminate outliers. The process is shown in Figure 2.

Moving Average Model
To predict the price of gold and bitcoin based on available data, the moving average model is used. It is a method of calculating sequential averages in turn, based on time series information, in order to reflect long-term trends [4].
The calculation method of the daily moving average is to add the daily prices within N days, then divide by N to obtain the N-day average. By connecting the points of these average values in sequence, we can get the line called the N-day moving average. In this paper, the daily moving average is calculated by the simple moving average method [5].
Assumes the observation sequence as I 1 ,…, I t and let the number of moving average items N < T. The formula of simple moving average is.
Since the main trend of the forecast target fluctuates up and down at a certain level, a moving average forecast model can be constructed using the simple moving average method: The mean value of the most recent N period sequence values is used as the prediction result for future periods. The value of N represents the type of moving average. For example, when N=5, we get a 5-day moving average. Figure 3 shows the forecast results of gold and bitcoin respectively based on the moving average forecast model. The forecast value always stays at the past level and cannot lead to higher or lower fluctuations in the future. In the investment market, there is a large error in the forecast results using the moving average method as a result of the non-linearity and high noise of the data. Thus, we use BP neural network for further forecasting.

BP Neural Network Forecasting
The algorithm of BP neural network consists of two processes: forward propagation of the signal and back propagation of the error [6]. In forward propagation, the input signal is processed layer by layer from the input layer to the output layer through the hidden layer, where the state of each layer of neurons only affects the state of the neurons in the next layer. If the expected output cannot be obtained in the output layer, it will turn to back propagation, where the error signal is returned along the original path and the error signal is minimized by modifying the weights of each neuron. The basic structure of the neural network is given in Figure 4. The neurons in each layer are not connected to each other while neurons in the adjacent layers are connected to each other. The connection between two neurons is the neural line. Different weights can be assigned to all neural lines, which is the core of the training [7]. As shown above, we use a two-layer linear network with a ReLU activation function to introduce a non-linear transformation and a stochastic gradient descent (SGD) optimiser with a mean-squared loss function to optimise the network parameters to better fit the data. The relevant formula is as follows: Here, X is the data, Y is the predicted value, R is the true value, the goal is to minimize ( )

Trading judgment indicators
Under the premise of known historical price, how to make investment decision can be judged by technical analysis. There is a wide variety of technical indicators to choose, but it is too quite difficult to make a successful decision with too many indicators. Therefore, the selection of indicators should be selective that ignoring the less important factors. Specifically, we choose the following four indicators to judge whether traders should buy, hold or sell assets based on the behavioral characteristics of rational investors in pursuit of maximization of returns and risk aversion.
(1) Investment risk Volatility is the fluctuation degree of financial asset price and the measurement of the uncertainty of asset return rate, which is used to reflect the risk level of financial asset. There will be more severe fluctuation of financial asset price and stronger uncertainty of asset return rate with higher volatility; On the contrary, the volatility of financial asset prices and the certainty of asset return rate will smoother and the stronger [8]. The standard deviation is a measure of how far data values deviate from the arithmetic mean, so we use the standard deviation of gold and Bitcoin prices as volatility. As a rational investor, we should choose the assets with small standard deviation under the condition of the same income. The standard deviation formulated as: Here, X is the mean of the sample; X  is the standard deviation of the sample.
(2) Golden Cross Chart A golden cross is a cross in which the short-term line of a stock quotation indicator crosses upward over the long-term line, called a golden cross, and conversely, a cross in which the short-term line of a quotation indicator crosses downward over the long-term line, called a dead cross. When both the short-term and long-term lines are below the zero axis, it can be assumed that the market is dominated by the short side.
If the short-term line breaks through the long-term line from the bottom up, it is a buy signal, called a "golden cross"; if the short-term line falls through the long-term line from the top down, it is a sell signal, called a "dead cross", then the market should be considered to have started to fall and investors should take profits in time to close.
(a) K daily moving average of gold (b) K daily moving average of bitcoin Figure 6. The above charts show the gold crosses in gold and bitcoin using the 5-day SMA and the 15-day SMA, respectively.
(3) BIAS BIAS is a technical indicator extended by moving averages. BIAS reflects the extent to which an asset's price deviates from its average price over a given period. BIAS can be divided into two categories: if the price is above the moving average, it is called positive BIAS; if the price is below the moving average, it is negative BIAS. When the price is further away from the moving average, the absolute value of the deviation is greater. The more positive "positive deviation" means "overbought" and downward pressure on the price; the more negative "negative deviation" means "Oversold", the price will have upward momentum. Therefore, when the divergence between gold and bitcoin is high, you should buy or sell. Formulas of gold BIAS and bitcoin BIAS are as follows: (4) Expected increase The expected rise in gold and bitcoin is also a basis for judging buying and selling. If you need to buy or sell urgently, when you anticipate the next day's rise, you can simply make a plan to take advantage of the market. Formulas of expected increase are as follows: Here, 1 price Gold − and 1 price Btc − represents the previous day's prices of gold and bitcoin respectively.

Entropy weighting method
The above indicators are important factors that influence whether or not we make a trade and how much we determine it. Among them, indicators 1, 2 and 4 are directly quantifiable indicators. To determine the impact of each indicator on the trading decision, here we use the entropy weighting method to assign weights to these indicators [9].
Entropy is a measure of uncertainty. The greater the amount of information, the smaller the uncertainty and the smaller the entropy; the smaller the amount of information, the greater the uncertainty and the larger the entropy. According to the characteristics of entropy, we can calculate the entropy value to judge the randomness and disorder degree of an event, and we can also use the entropy value to judge the dispersion degree of a certain index, the greater the dispersion degree of the index, the greater the influence of the index on the comprehensive evaluation.

Dynamic programming model
Since we can make trading decisions only based on historical prices, that is to say, how we operate depends on data prior to today. Every stage's decision related to time. Every day the price of gold and Bitcoin is in a constantly changing, when is the day to do business and how much to deal will influence our final trading gains, we should make good decisions in a changing state. So we use dynamic programming methods on a daily basis to help us make optimal decisions.
The variables in our transaction are updated below: Amount of gold after each transaction: Amount of bitcoin after each transaction: Amount of cash after each transaction: The maximum value of the formula is the optimal strategy, but in the actual transaction we also need to consider the following situation.
Each time when we trade, we have the following constraints. The amount of transaction and commission must be less than or equal to the cash we currently hold. Shown by the following: The amount of gold and Bitcoins we are going to sold must be smaller than the amount of gold and Bitcoins we already have. Shown by the following:  Figure 7 shows the changes in holdings of gold and Bitcoin respectively under the optimal trading strategy, both initially starting from 0. You can see that the holdings will be stable for a period of time, which means no trading is done at this point, and when the right time comes, the holdings will change dramatically after trading and remain stable for a period of time.

Model Solving and Analysis
After the model is established, it needs to be subjected to evaluate the performance of the trading strategy and sensitivity analysis. Firstly, we choose a series of widely accepted indicators to measure the trading strategy. It mainly includes six indicators: Net profit, Net profit ratio, Profit-loss ratio, Odds of winning, Maximum Profit & Loss, Maximum number of consecutive profit and loss. Then, in order to explore the impact of transaction costs on trading decisions, we selected gold commission percentages: 1% to 19% and bitcoin commission percentages: 1% to 19% and conducted cross-tests to observe the impact of different transaction costs on trading decision outcomes.

Index evaluation
Net profit and net profit margin are shown in Figure 8.
(a) Retained profits (b) Retained profits ratio Figure 8. Net profit and net profits ratio As can be seen from Figure 8, net profit P did not change significantly in the early part of the investment period, fluctuating around the value of zero. In the middle to late stages, net profit changes considerably, with ups and downs, but generally showing an upward trend. In the net profit ratio, ratio P oscillates around 1 at first and then starts to fluctuate slightly again after an apparent upward decline, with an overall increasing trend. Our strategy is judged to be profitable. is greater than 100%, but as the P/L ratio only represents the profitability ratio situation, it does not necessarily show a positive relationship with the return, so the total number of trades and the total amount of trades also need to be taken into account. The Odds of winning Or is greater than 50%, which means more wins and less losses; in the calculation of 1 M , the first value is larger, which is regarded as a chance event, and after removing the maximum value, the second calculation is still profitable; 2 M is 0.03, which indicates that our strategy has better profitability and stability; 3 M is also 0.03 which proves that our strategy has certain risk resistance; the maximum number of profits MCP is 3 and the maximum number of losses MCL is 1, which to some extent The maximum number of profits is 3 and the maximum number of losses is 1, reflecting to some extent the cyclical nature of market patterns and the beneficial nature of our strategy.
In summary, the evaluation indicators are all in line with expectations and the strategy delivers substantial expected returns for investors and controls risk. This demonstrates that our model provides the optimal trading strategy.

Sensitivity Analysis
The previous analysis was based on the condition that the commission on each gold transaction is 1% of the transaction value and the commission on bitcoin transactions is 2% of the transaction value. Increasing commissions will increase investors' transaction costs, and transaction costs are also an important factor influencing investment be-haviour. In order to explore the impact of transaction costs on trading decisions, we selected gold commission percentages: 1% to 19% and bitcoin commission percentages: 1% to 19% and conducted cross-tests to observe the impact of different transaction costs on trading decision outcomes.
The Figure 9 below shows a three-dimensional surface plot of our investment results after varying the transaction costs. The two horizontal axes are the commissions for bitcoin and gold respectively, and the vertical axes are the trading results under the corresponding commissions. As can be seen from the three-dimensional chart, lower commissions result in higher total assets, while higher commissions correspond to lower total assets. Total assets are clearly affected by changes in commissions, i.e. the investment strategy is more sensitive to transaction costs. It can be seen from the cross-test. Commissions affect trading frequency and total assets. Increasing commissions will result in a decrease in trading frequency and a decrease in total assets. When gold commissions are certain, total assets trend down as bitcoin commissions increase. When bitcoin commissions are certain, there is also a downward trend in the number of trades and total assets as gold commissions increase.

Conclusion
In general, time series data contain both linear and non-linear components, and the same is true for gold and bitcoin prices. We applied two forecasting models, simple moving average and BP neural network, respectively. By using moving average we discover the linear pattern of the data and by using BP neural network we introduce the non-linear characteristics of the data, greatly improving the accuracy and credibility of the forecasts. In evaluating the trading strategy, we selected multiple evaluation indicators to measure both return rate and risk control ability. By establishing a multiindicator evaluation system, our model is fully justified.