-
Notifications
You must be signed in to change notification settings - Fork 1
/
analysis.py
218 lines (124 loc) · 4.66 KB
/
analysis.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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from nsepy import get_history
from datetime import datetime as dt
plt.style.use("seaborn")
start_date = dt(2000,1,1)
end_date = dt.now()
# df = get_history("Nifty",start_date,end_date,index=True)
# print(df.tail())
# df.to_csv("Nifty50.csv")
def eichermot():
df = pd.read_csv("./data/EICHERMOT.csv",parse_dates=True,index_col=0)
df = df.resample('W').mean()
# print(df.head())
fig = plt.figure(figsize=(12,6))
plt.plot(df.index,df['Close'])
plt.xlabel("Year")
plt.ylabel("Price")
plt.title("Eichermotors stock price over the years")
# plt.show()
#Calculate a 50 day moving average
df['50ma'] = df['Close'].rolling(window=50).mean()
print(df.tail())
fig = plt.figure(figsize=(12,6))
ax1 = plt.subplot2grid((6,1),(0,0),rowspan=5,colspan=1)
ax2 = plt.subplot2grid((6,1),(5,0),rowspan=1,colspan=1,sharex=ax1)
ax1.plot(df.index,df['Close'])
ax1.plot(df.index,df['50ma'])
ax2.plot(df.index,df['Volume'])
plt.show()
df = pd.read_csv("./Nifty50_combined.csv",parse_dates=True,index_col=0)
df = df["2010-01-01":"2019-12-31"]
print(df.head())
def get_insights():
print("Summary Statistics of 50 stocks:")
print(df.describe().T)
#Plot average price of stocks
summary = df.describe().T
fig = plt.figure(figsize=(8,12))
plt.barh(summary.index,summary['mean'])
plt.title("Average price of Stocks")
plt.ylabel("Stocks")
plt.xlabel("Average price")
# plt.show()
#calculate simple returns or percentage returns of the stocks
fig2 = plt.figure()
df.pct_change().mean().plot(figsize=(10,6),kind='bar')
plt.xlabel("Stocks")
plt.ylabel("Percentage Change")
plt.title("Simple returns")
# plt.show()
#calculate and plot the log returns
rets = np.log(df/df.shift(1))
rets[rets.columns[30:45]].cumsum().apply(np.exp).plot(figsize=(10,6))
plt.title("Log returns")
# plt.show()
# select some well performing stocks overtime
print(df.columns)
stocks = df[['SHREECEM','BAJAJFINSV','EICHERMOT','INDUSINDBK','HINDUNILVR']]
ax = stocks.plot(figsize=(10,8),subplots=True,title="Well performing stocks over the past years")
ax[2].set_ylabel("Stock Price")
# plt.show()
#Simple moving average stratergy
#Rolling statistics
tick = "SHREECEM"
EM = df[:][tick].reset_index()
EM.set_index('Date',inplace=True)
# print(EM.head())
window = 30
#calculate mean,median,max,std_dev for the selected stock data
EM['min'] = EM[tick].rolling(window=window).min()
EM['max'] = EM[tick].rolling(window=window).max()
EM['mean'] = EM[tick].rolling(window=window).mean()
EM['std'] = EM[tick].rolling(window=window).std()
EM['median'] = EM[tick].rolling(window=window).median()
ax = EM[['min','mean','max']].iloc[-750:-350].plot(figsize=(10,6), style=['g--','r--','g--'], lw=0.8)
EM[tick].iloc[-750:-350].plot(ax=ax, lw=2)
plt.xlabel("Date")
plt.ylabel("Price")
plt.title("30 days max min simple moving average")
# plt.show()
# moving average cross over
EM['SMA1'] = EM[tick].rolling(window=52).mean()
EM['SMA2'] = EM[tick].rolling(window=252).mean()
EM.dropna(inplace=True)
EM['positions'] = np.where(EM['SMA1']>EM['SMA2'],1,-1)
ax = EM[[tick,'SMA1','SMA2','positions']].plot(figsize=(10,6), secondary_y='positions')
ax.get_legend().set_bbox_to_anchor((0.25, 0.85))
plt.title('SMA cross over stratergy')
plt.show()
# Correlation
def get_correlation():
df_corr = df.corr()
print(df_corr.head())
corr_data = df_corr.values
fig = plt.figure()
ax1 = fig.add_subplot(111)
heatmap1 = ax1.pcolor(corr_data, cmap=plt.cm.RdYlGn)
fig.colorbar(heatmap1)
ax1.set_xticks(np.arange(corr_data.shape[1]) + 0.5, minor=False)
ax1.set_yticks(np.arange(corr_data.shape[0]) + 0.5, minor=False)
ax1.invert_yaxis()
ax1.xaxis.tick_top()
column_labels = df_corr.columns
row_labels = df_corr.index
ax1.set_xticklabels(column_labels)
ax1.set_yticklabels(row_labels)
plt.xticks(rotation=90)
heatmap1.set_clim(-1,1)
plt.tight_layout()
plt.title("Correlation plot")
plt.show()
def correlated_stocks():
data = df[["ICICIBANK","EICHERMOT"]]
data["2021-01-01":'2021-01-25'].plot(secondary_y="EICHERMOT",figsize=(10,6))
plt.title("ICICIvsEICHER")
plt.show()
#Function call to get the summary statistics of the data and plots
get_insights()
#Function call to plot the correlation among the 50 stocks
get_correlation()
#Function call to visualize example correlated stocks
correlated_stocks()