forked from ardnirum/quantPy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quantitative_analysis_python.py
118 lines (64 loc) · 2.13 KB
/
quantitative_analysis_python.py
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# -*- coding: utf-8 -*-
"""
Created on Sat Sep 10 14:59:11 2016
@author: gopal_a
"""
import numpy as np
import statsmodels.api as sm
import statsmodels.tsa.stattools as ts
import numpy as np
import pandas as pd
import pandas.io.data as web
import matplotlib.pylab as plt
#regression analysis
data1 = web.DataReader('^NSEI', data_source='yahoo',
start='4/4/2008', end='4/4/2016')
data2 = web.DataReader('ACC.NS', data_source='yahoo',
start='4/4/2008', end='4/4/2016')
data1['key']=data1.index
data2['key']=data2.index
result = pd.merge(data1, data2, on='key')
x1=result['Close_x']
y1=result['Close_y']
ret_data1 = np.log(x1 /x1.shift(1))
ret_data2 = np.log(y1 /y1.shift(1))
ret_data1 = ret_data1[~np.isnan(ret_data1)]
ret_data2 = ret_data2[~np.isnan(ret_data2)]
model = pd.ols(y=ret_data2, x=ret_data1)
plt.plot(ret_data1, ret_data2)
ax = plt.axis() # grab axis values
x = np.linspace(ax[0], ax[1] + 0.01)
plt.plot(x, model.beta[1] + model.beta[0] * x, 'b', lw=2)
plt.grid(True)
plt.axis('tight')
plt.xlabel('Nifty Returns')
plt.ylabel('ACC Returns')
np.correlate(ret_data1,ret_data2)
pd.rolling_corr(ret_data1,ret_data2,window=252).plot(grid=True, style='b')
# Augmented Dicky Fuller Test
stock = web.DataReader('AMBUJACEM.NS', data_source='yahoo',
start='4/4/2008', end='4/4/2016')
x=stock['Close']
ret_stock = np.log(x /x.shift(1))
ret_stock = ret_stock[~np.isnan(ret_stock)]
lag=1
ts.adfuller(ret_stock,lag)
########################################
#Cointegration
data1 = web.DataReader('AMBUJACEM.NS', data_source='yahoo',
start='4/4/2008', end='4/4/2016')
data2 = web.DataReader('ACC.NS', data_source='yahoo',
start='4/4/2008', end='4/4/2016')
data1['key']=data1.index
data2['key']=data2.index
result = pd.merge(data1, data2, on='key')
x1=result['Close_x']
y1=result['Close_y']
coin_result = ts.coint(x1, y1)
score = coin_result[0]
pvalue = coin_result[1]
x1.plot()
y1.plot()
diff_series= x1 - y1
diff_series.plot()
#http://statsmodels.sourceforge.net/devel/generated/statsmodels.tsa.stattools.adfuller.html