-
Notifications
You must be signed in to change notification settings - Fork 0
/
20200910FDA.py
205 lines (104 loc) · 3.08 KB
/
20200910FDA.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
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import pandas_datareader.data as web
import matplotlib.pyplot as plt
#plot이 주피터 노트북에 바로 보이게 해줌
get_ipython().run_line_magic('matplotlib', 'inline')
# *** Retrieving Data from Yahoo Finance ***
# In[2]:
#Retrieving Data from Yahoo Finance
#microsoft data
#web으로 get하면 dataframe형태로 불러와지고 index type은 'Date'가 된다.
msft = web.get_data_yahoo('MSFT', start = '2020-01-01', end = '2020-09-01')
msft.head()
# In[3]:
msft.index
# In[4]:
#Plot
#가로 10, 세로 5 만큼의 사이즈 생성
plt.figure(figsize = (10,5))
#x축은 index, y축은 Close로 그래프 하나 그림
plt.plot(msft.index, msft.Close, label = 'Close')
#x축은 index, y축은 Adj Close로 그래프 하나 그림
plt.plot(msft.index, msft['Adj Close'], label = 'Adj Close')
#legend를 왼쪽위에 배치
plt.legend(loc = 'best')
#grid생성
plt.grid()
# In[5]:
#Stock Return
#'pct_change', 혹은 'Shift' 함수 사용.
msft_ret = msft['Adj Close'].pct_change()
msft_ret.head()
#na를 drop하려면
#msft_ret = msft_ret.dropna(axis = 0)
#'Shift' 함수를 이용하는 방법
#Shift 함수는 index는 내버려 두고 데이터만 이동시킨다!!
#shift(1)은 데이터를 하나 밑으로 밀어내는 것.
#msft_ret = msft['Adj Close'] / msft['Adj Close'].shift(1) - 1
# In[6]:
#Plot Stock Return
plt.figure(figsize= (10,5))
plt.plot(msft.index, msft_ret, label = 'Return')
plt.legend(loc = 'best')
plt.grid()
# In[7]:
msft_ret.describe()
# *** Retrieving Data from FRED ***
# In[8]:
#DataReader을 사용해보자.
data = web.DataReader(['SP500', 'WILL5000INDFC'], 'fred', start = '2016-01-02', end = '2020-09-01')
data.columns = ['S&P500', 'WilShire5000']
data.head()
# In[14]:
#Plot and compare two indices
data.plot()
plt.legend(loc = 'best')
plt.grid()
#plt.show()는 그냥 공간만 보여준다는거인듯. 그래프를 보여주려면 plot이 필요.
plt.show()
# In[16]:
#둘의 지수가 다르니 비교하기 위해서 둘 다 시작을 100으로 맞춰서 증가 추이를 보자.
((data / data.iloc[0]) * 100).plot(figsize = (10,5))
plt.legend(loc = 'best')
plt.grid()
plt.show()
# In[17]:
#Retriving Home price
cs = web.DataReader('CSUSHPINSA', 'fred', start = '1990-01-01', end = '2020-09-01')
cs.head()
# In[20]:
cs.plot()
plt.grid()
# *** Retrieving Data from Kenneth R. French Data Library ***
# In[22]:
from pandas_datareader.famafrench import get_available_datasets
# In[23]:
get_available_datasets()
# In[24]:
ind = web.DataReader('5_industry_Portfolios', 'famafrench', start = '2000-01-01', end = '2020-09-01')
type(ind)
# In[26]:
#description
print(ind['DESCR'])
# In[28]:
ind
# In[31]:
#Convert percentages to decimals
(ind[0] / 100).head()
# In[32]:
(ind[0] / 100).plot()
# *** Retrieving Data from Naver ***
# In[33]:
#삼성전자
df = web.DataReader('005930', 'naver', start = '2018-01-01', end = '2020-09-01')
df.head()
# In[34]:
df.Close.head()
# In[37]:
df.Close = df.Close.astype(float)
# In[38]:
df.Close.head()
# In[39]:
df.Close.plot()