**Trade with cointegration**

# Introduction

The price of the Financial Derivatives will depends on its underlying asset, so 2 financial derivatives with the same underlying asset should behave similarly, i.e. correlated. In this article, I will investigate the correlation of 2 GOLD ETFs: (GLD and IAU), and then develop the trading strategy using their correlation properties.

# GOLD ETF

To know more what is ETF and Gold ETF, please refer to https://www.investopedia.com/articles/investing/032116/what-relationship-between-gold-and-gold-etfs-gld-iau.asp

GLD and IAU are 2 popular Gold ETFs held by different Trusts. Both will track the price of Gold very closely. Therefore, we will expect their rate of return are following similar pattern.

# Distribution analysis

- Download the Daily Close price using the Alpaca API.
- Compute the percentage return with previous day
- Scatter plot (in Fig. 1)
- Subtract the daily return of GLD from IAU
- Filter away the outliners by quantiles
- Plot the histogram (in Fig. 2)

**Observation**: Very close to the normal distribution of mean zero.

Use distribution fit function “fit” to verify the data is under the Normal distribution. Below is the Julia code to plot the histogram and plot the Normal distribution fit on the data.

using Random, Distributions

using StatsBase

df = DataFrame(data)lower = quantile(df[:,:Diff], 0.01)

upper = quantile(df[:,:Diff], 0.99)

df = filter(:Diff => x -> x > lower, df)

df = filter(:Diff => x -> x < upper, df)

@show mean(df[!,:Diff])

@show std(df[!,:Diff])

histogram(df[!,:Diff], labels="Data Samples")

d=fit(Normal, df[!,:Diff])

n = length(df[:,:Diff])

plot!(rand(d, n), seriestype=:scatterhist, linestyle=:solid, size=(600,150),labels="Normal Distribution")

# Stationarity

Stationary time series means its statistical properties such as mean, variance, autocorrelation, etc. are all constant over time.

Use Augmented Dickey–Fuller (ADF) test to test whether the time series is stationary or not. The *p-value* of the test result is a common way to check.

If the p-value > 0.05, it is non-stationary. If the p-value ≤ 0.05, it is stationary.

- Price of GLD and IAU are not stationary. (GLD p-value = 0.8521, IAU p-value = 0.9077)
- Daily return of GLD and IAU are stationary. (GLD return p-value <1e-25, IAU return p-value <1e-25)
- The difference of the daily return of GLD and IAU are stationary. (p-value <1e-99)

Below is the Julia code to test the stationarity.

`using Plots`

function testStationary(df)

y = autocor(df,1:20)

display(bar(y))

y = pacf(df,1:20)

display(bar(y))

ADFTest(df,:none,1)

end

# Cointegration

In fact, based on the above properties, we can say GLD and IAU are cointegrated, and we can use Engle-Granger Test to test the cointegration properties. For more information on cointegration, please refer to its Wiki page. For how to implement the Engle-Granger Test, please refer to this practical guide.

# Trading strategy

The difference of the return of the 2 ETFs are under normal distribution with mean zero. It is also a stationary time series, so it will revert to its mean zero.

As the difference of the 2 ETF returns will revert to zero, if the difference is above one standard deviation, we will expect the difference will decrease to revert to zero, that means the return of the first ETF will drop and the second will rise. So our trading signal is to sell the first ETF and to buy the second ETF.

Similarly, if the difference is minus below one standard deviation, we will expect the difference will increase to revert to zero, that means the return of the first ETF will rise and the second will drop. So our trading signal is to buy the first and to sell the second.

# Backtesting

I use the Close price data from 2019–07–12 to 2021–06–17 for the backtesting.

- If I got the buy signal and my position is 0, I will buy 1 share and deduct the Close price from the account balance.
- If I got the buy signal and my position is 1, I will still hold the 1 share.
- If I got the sell signal and my position is 1, I will sell the 1 share, and add the Close price to the account balance.
- If I got the sell signal and my position is 0, nothing will be done.

Since the price of GLD is about 5 times of IAU, when I trade with IAU, I will use 5 unit share instead of 1 in the case of GLD.

Below are the final result.

- Performed 53 trades on GLD and 52 trades on IAU.
- 28 trades on GLD and 32 trades on IAU are gain. On average, 57% trade made profit.
- Gained $31.8 and $3.9 on GLD and IAU respectively

The balance over time is show here:

The max loss (drawback) of GLD balance is 0.7, and that of IAU is 3.6.

# Comparison with Bollinger Bands

For the sake of comparison, I do the same backtesting on the trading strategy using Bollinger Bands.

- Performed 13 trades on GLD and 13 trades on IAU.
- 6 trades on GLD and 8 trades on IAU are gain. On average, 54% trade made profit.
- Loss $8.7 on GLD and gain $4.3 on IAU

# Conclusion

- The trading strategy using cointegration can generate much higher profit.
- Capture much more trading signals.
- Higher profitable trading percentage.