Hi ,
I have the following questions after watching mean-reversion strategy lectures :
- I see code for calculating theta and half-life in section 4 of mean reversion strategies lecture.
Get historical data for the instruments
x = pd.read_csv('GDX2.csv',index_col=0)
y = pd.read_csv('GLD2.csv',index_col=0)
Hedge Ratio
model = sm.OLS(y.iloc[:90], x.iloc[:90])
model = model.fit()
Spread GLD - hedge ratio * GDX
spread = -model.params[0]*x['Close'] + y['Close']
spread = spread.iloc[:90]
Spread and differenence between spread
spread_x = np.mean(spread) - spread
spread_y = spread.shift(-1) - spread
spread_df = pd.DataFrame({'x':spread_x,'y':spread_y})
spread_df = spread_df.dropna()
Theta as regression beta between spread and difference between spread
model_s = sm.OLS(spread_df['y'], spread_df['x'])
model_s = model_s.fit()
theta= model_s.params[0]
Type your code below
hl=math.log(2)/theta
print(hl)
Where here mentioned about fitting the spread to OU process ?
model_s = sm.OLS(spread_df['y'], spread_df['x'])
model_s = model_s.fit()
these lines ? or this part (about fitting the spread) is absense in the following code ?
I know that the task of fitting the spread to OU process can be made via Vector Auto Regression.
You create the function , for instance - def VAR(data, p):
and then call this function
OU = VAR(e, 1)
OU.fit
Who knows whether this way of solving is more accurate ?
Thank's in advance.