Commodity Future Money Flows Trading Strategies Based on HMM

This paper aims to establish a quantitative trading strategy of commodity futures based on market money flows. Firstly, we use Accumulation/Distribution index to respectively construct the CMF index which represents the ratio of total capital flows to total volume, and the CHO index which represents the exponential moving average of the cumulative capital flows. In view of the different flows of money between buyers and sellers, the establishment of the transaction net volume index VTL is used to describe respectively the flow of money between buyers and sellers. On this basis, the HMM model is introduced, and the above three kinds of indexes are combined to choose the time, at which we execute the stop-loss operation and risk control. Finally, all performance index values of the strategy are as follows: the rate of initial capital return is 193.77%, the annual rate of return is 99.86%, the maximum retracement rate is 15.73%, the Sharpe rate is 2.05 and the price earnings ratio is 4.01.

Money flows includes the flow direction and volume of money , which is a standard technical index, reflecting the excess demand or supply on one stock of current market.Viewing the active buying of a stock as a capital inflow and the active selling as a capital outflow, if the difference is positive, an excess demand will be formed and if it is negative, an excess supply will be formed.
The value of money flow is that it reflects not only the excess supply and demand of the current stock, but also the excess supply and demand of future stocks.That is, in theory, the money flow index is not only correlated with the rate of return on the current stocks, but also the rate of return on future stocks.
In the futures markets, because of the existence of shorting mechanism, we cannot directly use the money flow formula of the stock market to depict the real money flow rules of futures contract.For example, the price for a futures contract is down, probably resulting from money inflows or outflows.This paper takes into account the factors such as the amount of holding and the turnover and the fluctuation of the price, to depict the flow direction of money of the futures products.
Many foreign researchers have done great research on money fiow.Clark (1974) proposed Mixture Distribution Hypothesis (MDH) and thought every day there was an unpredictability that went into the stock market and had an impact on stock trading.Andersen (1996) improved the MDH theory by studying the microstructure of the market and got the Modified Mixture Distribution Model (MMM).Kim (2000) took use of the money flow data tp find the imbalance of order had a negative predictive effect on stock returns.Chan and Lakonishok (1995) considered that money flows were positively correlated with historical yields.However, Bennett and Sias (2001) found that the flow of money was related to the income of same period and the flow of historical funds could predict future flows.Frazzini and Lamont (2008) used mutual fund flows as a measure of individual investors' sentiment toward different stocks, they found that the current high sentiment led to lower stock returns in the future.Cambell and Ramadorai (2009) thought that the agency day order stream has a negative effect on the future earnings of the stock, especially the negative effect of the commission sells orders.
The domestic exploration of money flow started late but also achieve certain results.Weihua Zhu (2009) proposed that through guaranteeing the completeness and sustainability of informed trading regulation and improving the corporate governance, improving the system of investor protection, building lawful insider trading market and disclosure system and so on we could improve the regulation efficiency of informed trading.Qiming Tang and Zhang Yun (2009) thought informed trading could stimulate the information asymmetry of company level, causing the trading volume fluctuations.
Gao Li and Weidong Fan (2002) through the cash flow estimation of the stock market in China, analysed the influence of the macro capital structure change caused by the source of funds, the capital structure and so on in the stock market on the market behavior of macroeconomic main body . Liuqi Lang (2008) did the research on aided computer analysis system of the securities funds flow from the perspective of computer engineering, computer.

Model Preparation
Hidden Markov Model is a statistical model used to describe a Markov process with implicit unknown parameters.Figure 1  For HMM, it has three important assumptions as follows:  assumption 1: the Markov hypothesis (The state forms a first order Markov chain)  assumption 2: immobility hypothesis(The state has nothing to do with the time)  assumption 3: output independence hypothesis(The output is only related to the current state) There is a probability on the relationship between hidden and observable states, assumed for P(O1 | H).If there are three kinds of observable state, then In this way, we can get confusion matrix.The content of the matrix is the probability that a hidden state is observed respectively to several different observable states.as its parameter.
In the operation process of this algorithm, the HMM cannot be judged directly, and for a given sequence of observable states O, we cannot accurately find out the optimal parameter  to make   | PO be the largest, so we use forward-backward algorithm (also known as Baum-Welch algorithm) to seek the optimal solution.Firstly we define two auxiliary variables, which are respectively the probability of state i at time t and the probability of state j at time t+1, namely The formula is equal to Make use of the preceding variable and the backward variable, the above formula can be represented as The second variable is defined as the posterior probability, which is the probability of state i at time t in a given observation state sequence and HMM, namely

 
Make use of the preceding variable and the backward variable, the above formula can be represented as The formula is the expectation of state i at any moment, namely the expectation of transfer from the state i to the observed state o expectation.In the same way, the formula is also the expectation of transfer from state i to state j, so we define relationship between the two variables for In the parameter learning process of forward-backward algorithm, it updates the HMM parameters constantly to make

Accumulation/Distribution,(A/D) Index
Accumulation/Distribution (A/D) index is an index based on trading volume, which measures the cumulative traffic flow or money flow of flowing into and out of the product.In the classical A/D formula Close respectively indicates the highest, lowest and closing price of the i K-line, and Volume indicates the corresponding trading volume.When the difference between the closing price and the lowest price is greater than the difference between the highest price and the closing price, the closing price is greater than half the price of the highest and the lowest, namely 2

High Low Close
It is now seen as money flowing into the variety; When the difference between the closing price and the lowest price is less than the difference between the highest price and the closing price, the closing price is less than half the price of the highest and the lowest, namely 2

High Low Close
It is now seen as money flowing out of the variety.
The amount of money inflows or outflows in the price range can be expressed as

Close Low High Close High Low
Its range from -1 to 1.The absolute value of the value is closer to 1, indicating the greater the amount of money inflows or outflows.The classic A/D formula is the amount of capital inflow/outflow in the weighted volume of the transaction, reflecting the overall flow direction of capital.
To further improve the classical A/D formula, add the A/D value of the previous period to the A/D value of the current period, and get the classical increment formula

Close Low High Close A D Volume A D High Low
Add the existing / i AD , we can get the total capital flows of n periods

Close Open A D Volume A D High Low
In the formula, i Open is the opening price of the i K-line

CMF Index
Establish money flow index according to the classic A/D formula The CMF index is the ratio of the sum of capital flows in recent periods to total trading volume in recent periods

  
Take 30 days for a period, 0.1  as the upper and lower threshold, and apply to all products.When the CMF value is greater than the threshold we should be going long, but when the CMF value is less than the threshold we should be going short.

CHO Index
Establish money flow index according to another representation of the A/D formula Add the 1 VAR from initial period to current period, we can get accumulative capital 1 sumVAR .Make exponential moving average to accumulate capital respectively on 12 days and 20 days as a period to form the short-term EMA and long-term EMA.When a short-term EMA upward cross a long-term EMA, it forms a gold fork and we should be going long.On the contrary, when it forms a death fork we should be going short.

Transaction Net Volume Index VTL
The game of buyers and sellers results in the futures market to increase or decrease.The figure 3 simulates the tendency of the futures prices change every minute in 10 minutes.We can see the prices go down in period AB and CD, FG, GH, IG, suggesting that the current market is the seller's market and the selling volume is greater than the buying volume, so the market is in a state of money outflow; contrarily, the prices go up in period BC, DE, EF, indicating that the buyers are engaged and the buying volume is greater than the selling volume, so the market is in a state of money inflow.

ABS Close Open
The index V reflects the turnover of the unit money flow, the greater the V, the more active the market.In Figure 4, the buying volume is corresponding to the period BF, for and the selling volume is corresponding to the period AB+FG, for reflecting the seller's flow direction of money.
When the closing price is less than the opening price, the direction of money flow of the buyers and sellers shall be expressed separately for The difference between the money flow of the buyers and sellers is the net volume of the transaction, which is represented by netV .The VTL index differentiates the flow of money between buyers and sellers, making up for the shortcoming in the previous articles.
Add the netV from initial period to current period and get the cumulative transaction net volume of sequence sumnetV , which is used to build different types of indexes such as cloth Bollinger Band, SMA, Psychologicalline.
Through the comparison, ultimately choosing EMA when gold fork and death fork to choose the time plays the best effect.Make exponential moving average to sumnetV respectively on 12 days and 20 days as a period to form the short-term EMA and long-term EMA.When a short-term EMA upward cross a long-term EMA, it forms a gold fork and we should be going long.On the contrary, when it forms a death fork we should be going short.

Correlation Prediction
Information Coefficient(IC), represents the coefficient of cross section correlation between the factor value of selected varieties and the next yield of stock.Through the IC value, we can judge the predictive ability of the factor value on next yield of stock.Usually IC is considered to be more efficient when the IC value is greater than 0.2 or less than -0.2.Select two varieties of Cu and Ta randomly and calculate the IC value respectively between CMF index, the CHO index, the VTL index and the future yield of corresponding varieties.According to the figure 5,6,7,8,9,10, we can know Both traditional CMF, CHO index and VTL built in this paper, the future of commodity futures market assets yield has strong ability of explaining, therefore this paper by using quantitative trading strategy to each parameter values to construct in the capture of futures market funds flow rule can also obtain good returns.

Strategies Selection and Retesting
For the entry strategy and signal unwinding strategy based on CMF index, take 30 days for a period, 0.1  as the upper and lower threshold, and apply to all products.When the CMF value is greater than the threshold we should be going long, but when the CMF value is less than the threshold we should be going short.
For the entry strategy and signal unwinding strategy based on CHO index, make exponential moving average to accumulate capital respectively on 12 days and 20 days as a period to form the short-term EMA and long-term EMA.When a short-term EMA upward cross a long-term EMA, it forms a gold fork and we should be going long.On the contrary, when it forms a death fork we should be going short.
For the entry strategy and signal unwinding strategy based on VTR index, make exponential moving average to sumnetV respectively on 12 days and 20 days as a period to form the short-term EMA and long-term EMA.When a short-term EMA upward cross a long-term EMA, it forms a gold fork and we should be going long.On the contrary, when it forms a death fork we should be going short.
Combine the above three indexes and introduce the HMM model.Use 1, 0, -1 respectively represents the direction of each index signal, according to the trading signals in the different K-lines of the CMF index, the CHO index and the VTL index , and form the observable sequence.This sequence is divided into parts needing training and parts needing not training, and when two or more than two indexes give the same opening signal, the minority is subordinate to the majority, so that we think that the sequence does not need training, namely the opening position is determined.The details are as follows: Otherwise, we should need to use HMM model to train, the details are as follows: Series 12 -1 0 0 Define the hidden state vector In the above vector, state 1 indicates that the closing price are up before and after the signal changes and the increase is more than two times of the handling charge; state -1 indicates a decline in the closing price before and after the signal changes and the decrease is more than two times of the handling charge; state 0 indicates that the absolute value of the difference between the closing price before and after the signal changes is less than twice of the fee.Use historical data to retest, obtain the state transition matrix and confusion matrix by training, use the Viterbi algorithm, and combined with the observable sequence further to get the opening direction of hidden condition.

Go up Keep flat
Go down 1 0 -1

Stop-loss Strategy
By introducing the ATR index in the stop-loss strategy, the mean real wave of the ATR is calculated as follows:  the range between the highest and lowest price of the current K-line  the range between the closing price of the a former K-line and the highest price of the current K-line  the range between the closing price of the a former K-line and the lowest price of the current K-line The maximum of the current K-line amplitude, the difference between current maximum and the closing price of a former K-line and the difference between the current lowest and the closing price of a former K-line is the real amplitude.Work out the average moving the real wave of N K-lines, and now set the parameter N as 10, the formula is , ATR MA TR N   Long stop-loss：record the opening price as Avgprice , when the price P below the opening price of 2*ATR , the stop-loss appears，namely 2* P Avgprice ATR   Short stop-loss：record the opening price as Avgprice , when the price P increases over the opening price of 2*ATR , the stop-loss appears，namely 2* P Avgprice ATR   Long profit-limit：record the opening price as Avgprice , when the highest price increases over the opening price of 4*ATR and the maximum withdrawal back reach 1.5* ATR , the stop-loss appears，namely 4* Highestprice Avgprice ATR  1.5* back ATR   Short profit-limit：record the opening price as Avgprice ，when the lowest price below the opening price of 4*ATR and the maximum withdrawal back reach 1.5* ATR , the stop-loss appears，namely 4* Lowestprice Avgprice ATR  1.5* back ATR 

Retesting Result
Selecting the 15min K-lines of various varieties, the above four strategies were retested on the Auto-Trader platform, and the results were as follows:  Obviously, The HMM strategy ranked first in all performance indicators, indicating that the strategy was robust while maintaining high returns.Therefore, we choose the entry strategy and signal unwinding strategy based on HMM model and the stop-loss strategy based on ATR index as the final strategy combination.

Risk Control
To effectively control the risk and minimize the withdrawal, the program limits the number of per transaction.
 Method one: get the current available money and the current dynamic equity of the account and calculate average respectively.Then take the smaller one, recorded for percash ，and take the bigger one of the opening price Avgprice and100* ATR , recorded for price ，defining the transaction number * percash amount price

 
In the formula,  is a parameter, which we can change to control the number of per transaction.
Method two: Count the total amount of historical loss money and the ratio of the maximum withdrawal loss amount to total losses, namely loss contribution rate  .Define the ratio of maximum withdrawal rate to the expected maximum withdrawal rate as Rback , set the initial deal hand number as n , and establish the dynamic risk control process, so that the transaction number can be expressed as the value that the initial deal hand number scales target multiple according to contribution rate:  By using the the separation index of buying volume and selling volume, we can better capture the money flow rule of the Chinese commodity futures market relative to the traditional money flow indexes.
 By using the HMM model to transfer trading signal to observed state and regard opening position as a hidden state, at the same time through the machine training to reduce the risk under a certain return and achieve effective control of risk, the retesting result is quite ideal.
 Parameter optimization is not required, and the over fitting phenomenon is avoided.Some research report will make network parameter search for some technical indicators, which makes the performance of the strategy in different market environment very inconsistent.But using the HMM model for parameter estimation of each variety, we can effectively avoid the problem of parameter optimization.
 Through optimizing the number of trades to control the risk, the maximum withdrawal rate was reduced effectively.

Figure 1 .
Figure 1.Hidden Markov Model State Transition Diagram in the classical increment formula with the difference between the closing price and the opening price, and get another representation of the A/D formula 1

Figure 5 .Figure 6 .
Figure 5.The IC value of the CMF index of the Cu variety

Figure 11 .
Figure 11.CMF index strategy equity curve


Considering the technical index design of the money volume, which can effectively capture the game situation of commodity futures real money, avoids the defects of traditional technical indexes frequently buying in.
, backward variable  and expectation  , update the HMM parameters based on the following three heavy estimate formulas, and iterate over it.Thus constantly revaluing the HMM parameters, and after many iterations we can get a maximum likelihood estimates of the HMM model, which is a local optimal solution.

Table 1 .
Strategic performance indicator