Report: Bitcoin Trading

2020-02-27 185浏览

  • 1.AC 297 FinalReport:Bitcoin Trading Daniel Rajchwald, Wenwan Yang, Wenshuai Ye May 13, 2015 1 Introduction Bitcoin is a virtual currency developed by an anonymous hacker under the name Satoshi Nakamoto in 2009. It is a completely decentralized peer-to-peer system governed by open sourced algorithms. Bitcoin accounts are anonymous. They are just public key addresses without any personally identifiable information attached. Unlike other currencies, bitcoin has a maximum circulation of just under 21 million coins which cause the value of each coin to increase over time. Every transaction is verified by the entire Bitcoin network which makes it nearly impossible to counterfeit a Bitcoin. The motivation of this project is to implement some trading strategies and find the arbitrage opportunities of bitcoin. Some trading algorithms will be introduced in the following part. They are either based on price trend signals or arbitrage opportunities. Our research shows that arbitragebased trading strategies outperform signal-based trading strategies. There are two ways to find the arbitrage opportunities. The first one is to buy a dual listed stock at a lower price in one market and simultaneously selling it at a higher price in another market. The second is the cross currency arbitrage which is an act of exploiting an arbitrage opportunity resulting from a pricing discrepancy among three different currencies in the foreign exchange currencies. We got the tick level data from Morgan Stanley and it is used to test performance of each algorithm. 2 Methodology Given the unique nature of the Bitcoin asset, the natural methodology would be to try a variety of approaches over different market conditions. Given that techniques are favorable to different trading algorithms, we implement each technique with its own algorithm. For example, pattern recognition trading is best implemented when the predictions are made with a predictive confidence interval and momentum trading can be optimal when the long and short positions are closed during the trading cycle rather than just at the end of the trading cycle. Despite the laundry list of parameters that needs to be tuned for each technique, we compare all algorithms under a common simple trading strategy where the positions are only closed at the end and a fixed number of assets is bought or sold at each trade. We hope to get a better understanding of the Bitcoin market environment given the relative performance of the algorithms over different time periods. 3 Trading Algorithms We tried various standard trading algorithms before building more sophisticated statistical models. These algorithms include momentum trading, pairs trading, backtesting, etc., which are simple and popular strategies that have been around for decades. By implementing all the strategies on one single dataset, we can compare the results and conclude which algorithms should be used in a given market. With these exploratory analyses done, our future work will focus on incorporating the bitcoin properties we have learned so far to build more sophisticated statistical models such as a Hidden Markov Model (HMM). HMM is a statistical Markov model in which the system being modeled is assumed to be a Markov process with unobserved (hidden) states. Our current thought 1
  • 2.Figure 1: OK Coin Trading Period 1 Figure 2: OK Coin Trading Period 2 is to model the intrinsic values of bitcoin as the unobserved state and the actual prices as the observed state. The trade signal relies on the current distribution of the intrinsic values. Depending on the performance, we might adjust our model when we implement it. 3.1 Momentum Trading This strategy looks to capture gains by riding ”hot” stocks and selling ”cold” ones. To participate in momentum investing, a trader will take a long position in an asset, which has shown an upward trending price, or short sell a security that has been in a downtrend. The basic idea is that once a trend is established, it is more likely to continue in that direction than to move against the trend. For example, one may consider taking a long position when the current price is greater than the moving average in a window, and a short position when the current price is less than the moving average in a window. 3.2 Pairs Trading Given two correlated assets, pairs trading is a technique that uses mean reversion to make trades. For the Bitcoin trading, the correlated assets are actually the Bitcoin prices on two different Bitcoin exchanges. In this project, we use Huobi and Okcoin. At a time t, given the 2 exchanges, 1 and 2, one can calculate Dt = F(ask1,t)–bid2,tandEt = G(bid1,t)–ask2,t (1) where F(p) = a*p + b, a and b are determined through regression on training times (similarly for G): 2
  • 3.Figure 3: OK Coin Trading Period 3 F(ask1,train1 ) = bid2,train1 , ..., F(ask1,trainm ) = bid2,trainm (2) Then one calculates the current deviation of these quantities away from a movingaverage:zDt = [Dt–mean(Dt−n, . . . , Dt−1)]/sd(Dt−n, . . . , Dt−1) and zEt = [Et–mean(Et−n, . . . , Et−1)]/sd(Et−n, . . . , Et−1) Algorithm 1 Pairs TradingInitialization:Trade = 0 for t = 1 to N do if zDt < −Threshold then Buy BTC from exchange 1 Sell BTC from exchange 2 Trade = 1 end if if zDt > Threshold then Sell BTC from exchange 1 Buy BTC from exchange 2 Trade = 1 end if if zDt < 0.5 and zEt < 0.5 and Trade then Close positions Trade = 0 end if end for=0 If the z score is less than a threshold, then we expect the gap to increase at the next time step so we sell from the lower exchange and buy from the higher exchange. If the z score is more than a threshold, then we do the opposite, and close our positions in the third case. 3.3 Pattern Recognition (Pattern Matching) Setup This Strategy saves the current trend of bitcoin prices and looks for similar patterns in the historial trends by computing the similarity of two patterns. A pattern corresponding to a time point is defined as a list of length N that stores the percentage difference between the price of the time point and the price i time unit before that where i equals 1 to N. Similarity can be computed using 3
  • 4.normalized Euclidean distances or Pearson / Spearman Correlation Coefficient. The historical fu- ture prices of matched patterns are then collected to make predictions based on their distributions. We decided to use normalized Euclidean distance because it not only takes the linear relationship but also the scale into account. Given two patterns A = [a1, a2, ..., aN] and B = [b1, b2, ..., bN], we N compute the similarity by calculating 100 N ∑ (ai − bi)/bi. Because (ai − bi)/bi is less than 1, the sim- i=1 ilarity score here will not exceed 100. The next step is to set a similarity threshold for us to select qualified patterns. Figure 4 visualizes the matching technique by displaying matched patterns us- ing lines. The dots represent the historical future outcomes and will be used for predictions, which would be discussed in detail. Figure 4: Pattern Matching Prediction With the historical future outcomes, we can construct a confidence interval to help us decide whether we should buy / sell bitcoins. In addition, we can use the confidence interval to adjust the volumes we desire to buy / sell. 3.4 HMM Hidden Markov Model is a technique for inferring hidden states through observations when the structure of the model is known but the parameters are not. The parameters are estimated using a version of the Expectation Maximization (EM) algorithm known as the Bausch and Lomb algorithm. Formally, given • HiddenStates:X = x1, ..., xn - xi ∈ {S1, ..., Sk} , Sj = N(µj, σj2) •Observations:Y = y1, ..., yn •Parameters:Λ = π = P(x1), Aij = P(Si, Sj), µ, σ P(X, Y) = ΠP(x1)ΠP(xi xi−1)ΠP(yi xi) (3) After initializing parameters with k-means, one finds argmaxΛP(Y Λ) through EM then calculates the most likely sequence of hiddenstates:argmaxX P(X Y, Λ) using the Viterbi algorithm. Then the next hidden observation ispredicted:argmaxyP(y argmaxxn+1 P(xn, xn+1)) from which a trading strategy can be easily generated. If a rise in price is predicted, one buys, if a fall is predicted, one sells, and if no change is predicted, one closes position. Lastly, one closes position at the end of the trading period. Note in the results that there is not a strict relationship between accuracy, RMSE, and profit among the stocks and number of hidden states, K. Given the nature of equity time series, arbitrage opportunities are very hard to track so there is a lot of randomness despite optimizing 4
  • 5.the predictions. We test for 2, 4, and 8 hidden states and pick the one that yields the most profit in the result tables. Figure 5: HMM Training Figure 6: HMM Trading with 2 Hidden States In Figure 5, the HMM parameters are initialized in the training region before the red curve. The number of hidden states must be specified and in this case it is 2. We use the K-means initialized with ”K-means Plus” to initialize the parameters. The HMM parameters over the first 500 trades of the bid price are illustrated in Figure 6. The relative distance and confidence intervals between the true bid price and the estimated means of the hidden states shows which hidden state the bid price is most likely to belong to at the moment. 3.5 MCMC It has been proposed that stock pricing can be modeled by the geometric Brownian motion (GBM). One of the most important issues is to determine the distribution of stock prices, their return and other financial mean. Classical model of stock prices involves certain assumptions about the relevant financial data, which typically adopt the form of logarithmically normal distribution. The Markov chain Monte Carlo (MCMC) method is ideal to sample from empirical probability density of stock prices. This technique is flexible and calculates the probability at any given point. The purpose of this project is to model and predict stock prices using MCMC method. 5
  • 6.Problem Setup The stock price can be modeled by a logarithmic-normal dynamics, which can be writtenas:'>as: