-
Notifications
You must be signed in to change notification settings - Fork 0
/
Finance Analysis.py
298 lines (122 loc) · 4.15 KB
/
Finance 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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
#!/usr/bin/env python
# coding: utf-8
# In[1]:
# Import Libraries
import numpy as np
import pandas as pd
from pandas_datareader import data, wb
from pandas.testing import assert_frame_equal
import matplotlib.pyplot as plt
import seaborn as sns
import datetime
import plotly
import cufflinks as cf
cf.go_offline()
get_ipython().run_line_magic('matplotlib', 'inline')
# In[2]:
# Set Duration
start = datetime.datetime(2010, 1, 1)
end = datetime.datetime(2020, 1, 1)
# In[3]:
# Import Data using DataReader and set Tickers
# Note: The Tickers are customised here by replacing the Original Tickers to Frequently Used Ticker Names.
BOI = data.DataReader("BIRG.IR",'yahoo',start,end)
SBI = data.DataReader("SBIN.NS",'yahoo',start,end)
AIB = data.DataReader("AIBG.L",'yahoo',start,end)
ICICI = data.DataReader("ICICIBANK.NS",'yahoo',start,end)
# In[4]:
BOI.head()
# In[5]:
SBI.head()
# In[6]:
AIB.head()
# In[7]:
ICICI.head()
# In[8]:
# Display Tickers
tickers = ['BOI','SBI','AIB','ICICI']
# In[9]:
# Concatenate Bank DataFrames together.
bank_stocks = pd.concat([BOI,SBI,AIB,ICICI],axis=1,keys=tickers)
# In[10]:
bank_stocks.head()
# In[11]:
# Set the Column name Levels
bank_stocks.columns.names = ['Bank Ticker','Stock Info']
# In[12]:
bank_stocks.head()
# # Exploratory Data Analysis (EDA)
# In[13]:
# Maximum Close Price
bank_stocks.xs(key='Close',axis=1,level='Stock Info').max()
# In[14]:
returns = pd.DataFrame()
# In[15]:
# Create returns Column for each Bank Stock Ticker
for tick in tickers:
returns[tick + 'Return'] = bank_stocks[tick]['Close'].pct_change()
# In[16]:
returns.head()
# In[17]:
# Pairplot using Seaborn for returns DataFrame
sns.set()
sns.pairplot(returns[1:])
# In[18]:
# Minimum (or Least) Single Day returns
returns.min()
# In[19]:
# Minimum (or Least) Single Day returns Date display for a single Bank
returns['BOIReturn'].idxmin()
# In[20]:
# Minimum (or Least) Single Day returns Date display for all Banks
returns.idxmin()
# In[21]:
# Maximum (or Most) Single Day returns Date display for all Banks
returns.idxmax()
# In[22]:
# Standard Deviation of the returns
returns.std()
# In[23]:
# Standard Deviation of the returns for a particular year
returns.loc['2018-01-01':'2018-12-31'].std()
# In[24]:
# Distplot using Seaborn for returns of a particular Bank in a particular Year
sns.distplot(returns.loc['2018-01-01':'2018-12-31']['SBIReturn'],color='blue',bins=50)
# In[25]:
# Distplot using Seaborn for returns of a particular Bank in a particular Year
sns.distplot(returns.loc['2018-01-01':'2018-12-31']['AIBReturn'],color='purple',bins=50)
# In[26]:
# Line Plot for Close Price of each bank for entire period
sns.set_style('whitegrid')
bank_stocks.xs(key='Close',axis=1,level='Stock Info').plot(figsize=(12,5))
# In[27]:
# iplot for Close Price of each bank for entire period
bank_stocks.xs(key='Close',axis=1,level='Stock Info').iplot()
# In[28]:
# 30-day Moving Average for Close Price of a particular Bank
plt.figure(figsize=(12,5))
SBI['Close'].loc['2019-01-01':'2020-01-01'].rolling(window=30).mean().plot(label='30 Day Moving Avg.')
SBI['Close'].loc['2019-01-01':'2020-01-01'].plot(label='SBI Close')
plt.legend()
# In[29]:
# Heatmap for Correlation between stocks Close Price
sns.heatmap(bank_stocks.xs(key='Close',axis=1,level='Stock Info').corr(),annot=True)
# In[30]:
# Clustermap to cluster the Correlations together
sns.clustermap(bank_stocks.xs(key='Close',axis=1,level='Stock Info').corr(),annot=True)
# In[31]:
# Close Correlation
close_corr = bank_stocks.xs(key='Close',axis=1,level='Stock Info').corr()
# In[32]:
# Close Correlation using iplot
close_corr.iplot(kind='heatmap',colorscale='rdylbu')
# In[33]:
# Candle Stick Plot using iplot for a particular Banks Stock in a particular Year
sbi17 = SBI[['Open','High','Low','Close']].loc['2017-01-01':'2018-01-01']
sbi17.iplot(kind='candle')
# In[34]:
# SMA plot for a particular Bank in a particular Year
BOI['Close'].loc['2017-01-01':'2018-01-01'].ta_plot(study='sma',periods=[9,18,27])
# In[35]:
ICICI['Close'].loc['2017-01-01':'2018-01-01'].ta_plot(study='sma')
# In[ ]: