From ce9978e2068b170f148a12ae5d238afe48275f31 Mon Sep 17 00:00:00 2001 From: Shile Wen Date: Tue, 24 Nov 2020 16:51:31 -0800 Subject: [PATCH 1/2] adds 366 Crypto Mean Reversion --- .../01 Abstract.html | 3 + .../02 Introduction.html | 10 ++++ .../03 Method.html | 56 +++++++++++++++++++ .../04 Algorithm.html | 6 ++ .../05 Results.html | 8 +++ .../06 References.html | 5 ++ 6 files changed, 88 insertions(+) create mode 100644 04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/01 Abstract.html create mode 100644 04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/02 Introduction.html create mode 100644 04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/03 Method.html create mode 100644 04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/04 Algorithm.html create mode 100644 04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/05 Results.html create mode 100644 04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/06 References.html diff --git a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/01 Abstract.html b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/01 Abstract.html new file mode 100644 index 0000000..d3e9107 --- /dev/null +++ b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/01 Abstract.html @@ -0,0 +1,3 @@ +

+ In this strategy, we apply OLS and stationarity tests to construct a cointegrated portfolio of Cryptocurrencies. +

diff --git a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/02 Introduction.html b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/02 Introduction.html new file mode 100644 index 0000000..6fe7274 --- /dev/null +++ b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/02 Introduction.html @@ -0,0 +1,10 @@ +

+ Several methods have been developed to forecast time-series, from ARIMA to Neural Networks. In this strategy, we + combine a Support Vector Machine (SVM) and Wavelets in an attempt to forecast EURJPY. Although SVMs are generally + used for classification problems, such as classifying proteins, they can also be applied in regression problems, valued + for their ability to handle non-linear data. Furthermore, Wavelets are often applied in Signal Processing applications. Wavelets allow us + to decompose a time-series into multiple components, where each individual component can be denoised using thresholding, and this + leads to a cleaner time-series after the components are recombined. To use these two models in conjunction, we first + decompose the EURJPY data into components using Wavelet decomposition, then we apply the SVM to forecast one time-step + ahead of each of the components. After we recombine the components, we get the aggregate forecast of our SVM-Wavelet model. +

diff --git a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/03 Method.html b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/03 Method.html new file mode 100644 index 0000000..5cd8e93 --- /dev/null +++ b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/03 Method.html @@ -0,0 +1,56 @@ +

+ Given EURJPY data, our first step is to decompose our data into multiple resolutions. + We work with wavelets using the pywt package. For denoising, Daubechies and + Symlets are good choices for Wavelets, and we use Symlets 10 in our strategy. We create a Symlets 10 Wavelet + using the following:  +

+ +
+
+w = pywt.Wavelet('sym10')
+
+
+

To determine the length of the data we’d need for a certain number of levels after decomposition, we can solve for:

+\[log_{2}(\frac{len(data)}{wavelength-1})=levels\] +

+ Given the length of a Symlet 10 wavelet is 20, if we want three levels, we + solve for len(data) to get len(data) = 152, which means data would need to have at least + 152 values. Since we will denoise our components using thresholding, + we specify threshold = 0.5 to indicate the strength of the thresholding. + This threshold value can be any number between 0 and 1. +

+

To decompose our data, we use: 

+ +
+
+coeffs = pywt.wavedec(data, w)
+
+
+ +

For each component, we threshold/denoise the component (except for the first component, the approximation coefficients), + roll the values of the component one spot to the left, + and replace the last value of the component with a value forecasted from an SVM. This process looks like the following in code:

+ +
+
+for i in range(len(coeffs)):
+    if i > 0:
+        # we don't want to threshold the approximation coefficients
+        coeffs[i] = pywt.threshold(coeffs[i], threshold*max(coeffs[i]))
+    forecasted = __svm_forecast(coeffs[i])
+    coeffs[i] = np.roll(coeffs[i], -1)
+    coeffs[i][-1] = forecasted
+
+
+ +

The __svm_forecast method fits partitioned data to an SVM model then predicts one value into the + future, and can be found under SVMWavelet.py file under the Algorithm section

+

Once we forecast one value into the future, we can aggregate the forecasts by recombining the components into a simple time-series. We do this with:

+
+
+datarec = pywt.waverec(coeffs, w)
+
+
+

Since we want the aggregate forecast one time-step into the future, we return the last element of this time-series, or datarec[-1].

+ +

Our trading rules are simple: feed in the past 152 points of daily closing prices of EURJPY into our SVM Wavelet forecasting method, and divide that number by the current close of EURJPY to get the forecasted percent change. Then, we emit an Insight based on the direction of the percent change with the weight of the Insight as the absolute value of the percent change. We use the InsightWeightPortfolioConstructionModel so that the weight of the Insight determines the portfolio allocation percentage, which means larger forecasted moves will have a larger allocation.

diff --git a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/04 Algorithm.html b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/04 Algorithm.html new file mode 100644 index 0000000..935af6f --- /dev/null +++ b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/04 Algorithm.html @@ -0,0 +1,6 @@ +
+
+
+ +
+
diff --git a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/05 Results.html b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/05 Results.html new file mode 100644 index 0000000..96be5b5 --- /dev/null +++ b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/05 Results.html @@ -0,0 +1,8 @@ +

The performance of the algorithm was decent. Over the past five years, the algorithm achieved a Sharpe Ratio of 0.252, + while buying and holding SPY over the same period would have achieved a Sharpe Ratio of 0.713. Some ideas for improvement include:

+ +

If a user comes across any interesting results with modifications of this algorithm, we’d love to hear about it in the Community Forum.

\ No newline at end of file diff --git a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/06 References.html b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/06 References.html new file mode 100644 index 0000000..1cc8866 --- /dev/null +++ b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/06 References.html @@ -0,0 +1,5 @@ +
    +
  1. + M. S. Raimundo and J. Okamoto, "SVR-wavelet adaptive model for forecasting financial time series," 2018 International Conference on Information and Computer Technologies (ICICT), DeKalb, IL, 2018, pp. 111-114, doi: 10.1109/INFOCT.2018.8356851. Online Copy. +
  2. +
\ No newline at end of file From 6b2bcf3a01655646496d47a022e699c22c45ef9b Mon Sep 17 00:00:00 2001 From: Shile Wen Date: Tue, 24 Nov 2020 16:53:39 -0800 Subject: [PATCH 2/2] adds 345 Crypto Mean Reversion --- .../01 Abstract.html | 2 +- .../02 Introduction.html | 11 +--- .../03 Method.html | 59 ++----------------- .../04 Algorithm.html | 2 +- .../05 Results.html | 9 +-- .../06 References.html | 2 +- 6 files changed, 10 insertions(+), 75 deletions(-) diff --git a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/01 Abstract.html b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/01 Abstract.html index d3e9107..4d23365 100644 --- a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/01 Abstract.html +++ b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/01 Abstract.html @@ -1,3 +1,3 @@

- In this strategy, we apply OLS and stationarity tests to construct a cointegrated portfolio of Cryptocurrencies. + In this strategy, we apply OLS and stationarity tests to construct a mean-reverting portfolio of Cryptocurrencies.

diff --git a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/02 Introduction.html b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/02 Introduction.html index 6fe7274..f4194b8 100644 --- a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/02 Introduction.html +++ b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/02 Introduction.html @@ -1,10 +1 @@ -

- Several methods have been developed to forecast time-series, from ARIMA to Neural Networks. In this strategy, we - combine a Support Vector Machine (SVM) and Wavelets in an attempt to forecast EURJPY. Although SVMs are generally - used for classification problems, such as classifying proteins, they can also be applied in regression problems, valued - for their ability to handle non-linear data. Furthermore, Wavelets are often applied in Signal Processing applications. Wavelets allow us - to decompose a time-series into multiple components, where each individual component can be denoised using thresholding, and this - leads to a cleaner time-series after the components are recombined. To use these two models in conjunction, we first - decompose the EURJPY data into components using Wavelet decomposition, then we apply the SVM to forecast one time-step - ahead of each of the components. After we recombine the components, we get the aggregate forecast of our SVM-Wavelet model. -

+

Mean Reversion Trading is the process of trading assets or relationships that deviate from the long-term mean in hopes that the prices revert to the long term mean. There are several methods and models to carry out mean reversion, including using Moving Average crossovers. Furthermore, Pairs Trading is simultaneously going long on one stock while shorting another, and it is a form of Mean Reversion as Pairs Trading is the process of going opposite of the direction of the spread in hopes that the spread will revert to the mean. In our Optimal Pairs Trading strategy, we model the spread as an Ornstein-Uhlenbeck process to find the optimal levels to buy and sell the pair. However, Pairs Trading is a form of Statistical Arbitrage, so like all other forms of Arbitrage, the opportunity is whittled away as more participants employ the strategy. Thus, in this strategy, we extend the concept of Pairs Trading to more than two stocks; we construct a basket of several securities that we long and short in an attempt to create a mean-reverting portfolio. Specifically, we use Bitcoin (BTC), Bitcoin Cash (BCH), Ethereum (ETH), and Litecoin (LTC), four cryptocurrencies, for our portfolio.

diff --git a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/03 Method.html b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/03 Method.html index 5cd8e93..7490ac6 100644 --- a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/03 Method.html +++ b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/03 Method.html @@ -1,56 +1,7 @@ -

- Given EURJPY data, our first step is to decompose our data into multiple resolutions. - We work with wavelets using the pywt package. For denoising, Daubechies and - Symlets are good choices for Wavelets, and we use Symlets 10 in our strategy. We create a Symlets 10 Wavelet - using the following:  -

+

We first gather 100 points of historical closing prices for BTC, BCH, ETH, and LTC. Using OLS, we regress BTC prices (we label BTC as the anchor in the code) against BCH, ETH, and LTC prices to create the following regression equation:

-
-
-w = pywt.Wavelet('sym10')
-
-
-

To determine the length of the data we’d need for a certain number of levels after decomposition, we can solve for:

-\[log_{2}(\frac{len(data)}{wavelength-1})=levels\] -

- Given the length of a Symlet 10 wavelet is 20, if we want three levels, we - solve for len(data) to get len(data) = 152, which means data would need to have at least - 152 values. Since we will denoise our components using thresholding, - we specify threshold = 0.5 to indicate the strength of the thresholding. - This threshold value can be any number between 0 and 1. -

-

To decompose our data, we use: 

+\[P_{BTC} = \alpha + \beta_{BCH}P_{BCH} + \beta_{ETH}P_{ETH} + \beta_{LTC}P_{LTC}\] -
-
-coeffs = pywt.wavedec(data, w)
-
-
- -

For each component, we threshold/denoise the component (except for the first component, the approximation coefficients), - roll the values of the component one spot to the left, - and replace the last value of the component with a value forecasted from an SVM. This process looks like the following in code:

- -
-
-for i in range(len(coeffs)):
-    if i > 0:
-        # we don't want to threshold the approximation coefficients
-        coeffs[i] = pywt.threshold(coeffs[i], threshold*max(coeffs[i]))
-    forecasted = __svm_forecast(coeffs[i])
-    coeffs[i] = np.roll(coeffs[i], -1)
-    coeffs[i][-1] = forecasted
-
-
- -

The __svm_forecast method fits partitioned data to an SVM model then predicts one value into the - future, and can be found under SVMWavelet.py file under the Algorithm section

-

Once we forecast one value into the future, we can aggregate the forecasts by recombining the components into a simple time-series. We do this with:

-
-
-datarec = pywt.waverec(coeffs, w)
-
-
-

Since we want the aggregate forecast one time-step into the future, we return the last element of this time-series, or datarec[-1].

- -

Our trading rules are simple: feed in the past 152 points of daily closing prices of EURJPY into our SVM Wavelet forecasting method, and divide that number by the current close of EURJPY to get the forecasted percent change. Then, we emit an Insight based on the direction of the percent change with the weight of the Insight as the absolute value of the percent change. We use the InsightWeightPortfolioConstructionModel so that the weight of the Insight determines the portfolio allocation percentage, which means larger forecasted moves will have a larger allocation.

+

Using this relationship, if we long 1 BTC, we take a position of -β{BCH, ETH, LTC}. Let this scenario be called “buying the portfolio”. If we go short 1 BTC, we take a position of β{BCH, ETH, LTC}. Let this scenario be called “shorting the portfolio”.

+

We then take the residuals of the OLS Regression and test them at the .05 significance level for both the augmented Dickey-Fuller and Phillips-Perron test. These are two different tests for stationarity, which helps us determine if the portfolio has mean-reverting qualities. If either test fails, we stop the trading.

+

To generate the rules of trading, we first compute the portfolio values of holding 1 BTC and -β{BCH, ETH, LTC}, and from these portfolio values we compute the mean (μ) and the standard deviation (σ). Then, if the value of the current portfolio trips below and then above μ - 1.5σ, we buy the portfolio, and if the value of the current portfolio trips above and then below μ + 1.5σ, we short the portfolio. Please note that the paper buys and sells the portfolio on the initial trips of the levels, however, we noticed that by waiting until the portfolio trips the levels the second time, we significantly reduce the drawdown and the losses.

\ No newline at end of file diff --git a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/04 Algorithm.html b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/04 Algorithm.html index 935af6f..1fb8a96 100644 --- a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/04 Algorithm.html +++ b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/04 Algorithm.html @@ -1,6 +1,6 @@
- +
diff --git a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/05 Results.html b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/05 Results.html index 96be5b5..f7837b8 100644 --- a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/05 Results.html +++ b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/05 Results.html @@ -1,8 +1 @@ -

The performance of the algorithm was decent. Over the past five years, the algorithm achieved a Sharpe Ratio of 0.252, - while buying and holding SPY over the same period would have achieved a Sharpe Ratio of 0.713. Some ideas for improvement include:

- -

If a user comes across any interesting results with modifications of this algorithm, we’d love to hear about it in the Community Forum.

\ No newline at end of file +

The algorithm achieved a Sharpe Ratio of 0.688 over approximately three years, while simply holding an equal weighting of BTC, BCH, ETH, and LTC would have achieved a Sharpe 0.12 over the same period.

\ No newline at end of file diff --git a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/06 References.html b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/06 References.html index 1cc8866..9c74b37 100644 --- a/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/06 References.html +++ b/04 Strategy Library/1038 Constructing a Mean-Reverting Portfolio of Cryptocurrencies/06 References.html @@ -1,5 +1,5 @@
  1. - M. S. Raimundo and J. Okamoto, "SVR-wavelet adaptive model for forecasting financial time series," 2018 International Conference on Information and Computer Technologies (ICICT), DeKalb, IL, 2018, pp. 111-114, doi: 10.1109/INFOCT.2018.8356851. Online Copy. + Leung, T. and Nguyen, H. (2019), "Constructing cointegrated cryptocurrency portfolios for statistical arbitrage", Studies in Economics and Finance, Vol. 36 No. 3, pp. 581-599. Online Copy.
\ No newline at end of file