-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathPlot_plotCorrelogram_Pair.c
43 lines (34 loc) · 1.14 KB
/
Plot_plotCorrelogram_Pair.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/* AUTOCORRELATION OF A MEAN REVERTING SPREAD
mean-reverting spread is a combination of two instruments into a portfolio ...
... such that the value of the portfolio is stationary =>
deviation from its mean value will be followed by
a subsequent reversion towards the mean
*/
#include <profile.c>
function run()
{
set(PLOTNOW);
StartDate = 2016;
EndDate = 2017;
BarPeriod = 1440;
LookBack = 201;
// get ETF data
string Name;
vars Closes[2];
int n = 0;
while(Name = loop("GLD", "GDX"))
{
if(is(INITRUN)) assetHistory(Name,FROM_AV);
asset(Name);
Closes[n] = series(priceClose());
n++;
}
// go long GLD and short GDX
// For every share of GLD we buy, we sell short 2.5 shares of GLD in an attempt to create a stationary spread.
var beta = 0.3;
vars spread = series((Closes[0])[0] - beta*(Closes[1])[0]);
plotCorrelogram(spread[0], 150);
// The value of the spread is anti-correlated with itself over longer periods, ...
// ... most strongly over a period of approximately 120 days.
// That is, the value of the spread 120 days ago tells us something about its value tomorrow
}