-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStock Analysis
453 lines (231 loc) · 10.4 KB
/
Stock Analysis
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
#!/usr/bin/env python
# coding: utf-8
# In[70]:
get_ipython().system('pip install yfinance')
get_ipython().system('pip install pandas')
get_ipython().system('pip install matplotlib')
# In[71]:
import yfinance as yf
import pandas as pd
import matplotlib_inline
# ## Using the yfinance Library to Extract Stock Data
# Using the `Ticker` module we can create an object that will allow us to access functions to extract data. To do this we need to provide the ticker symbol for the stock, here the company is Apple and the ticker symbol is `AAPL`.
# In[80]:
apple = yf.Ticker("AAPL")
# Now we can access functions and variables to extract the type of data we need. You can view them and what they represent here https://aroussi.com/post/python-yahoo-finance.
# In[83]:
get_ipython().system('wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/data/apple.json')
# ### Stock Info
# Using the attribute info we can extract information about the stock as a Python dictionary.
# In[44]:
import json
with open('apple.json') as json_file:
apple_info = json.load(json_file)
# Print the type of data variable
#print("Type:", type(apple_info))
apple_info
# We can get the <code>'country'</code> using the key country
# In[47]:
apple_info['country']
# ### Extracting Share Price
# A share is the single smallest part of a companys stock that you can buy. The prices of these shares fluctuate over time. Using the `history()` method we can get the share price of the stock over a certain period of time. Using the `period` parameter we can set how far back from the present to get data. The options for `period` are 1 day (1d), 5d, 1 month (1mo) , 3mo, 6mo, 1 year (1y), 2y, 5y, 10y, ytd, and max.
#
# In[56]:
apple_share_price_data = apple.history(period="max")
# The format that the data is returned in is a Pandas DataFrame. With the Date as the index the share Open, High, Low, Close, Volume, and Stock Splits are given for each day.
# In[61]:
apple_share_price_data.head()
# We can reset the index of the DataFrame with the reset_index function. We also set the inplace paramter to True so the change takes place to the DataFrame itself.
# In[64]:
apple_share_price_data.reset_index(inplace=True)
# We can plot the Open price against the Date:
# In[67]:
apple_share_price_data.plot(x="Date", y="Open")
# ### Extracting Dividends
# Dividends are the distribution of a companys profits to shareholders. In this case they are defined as an amount of money returned per share an investor owns. Using the variable dividends we can get a dataframe of the data. The period of the data is given by the period defined in the 'history` function.
# In[71]:
apple.dividends
# We can plot the dividends overtime:
# In[74]:
apple.dividends.plot()
# ### Exercise
# Now using the Ticker module create an object for AMD with the ticker symbol is AMD called; name the object amd.
# In[112]:
amd = yf.Ticker("AMD")
# In[114]:
get_ipython().system('wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/data/amd.json')
# In[118]:
import json
with open('amd.json') as json_file:
amd_info = json.load(json_file)
# Print the type of data variable
#print("Type:", type(apple_info))
amd_info
# In[120]:
amd_info['country']
# In[122]:
amd_info['sector']
# In[124]:
amd = yf.Ticker("AMD")
amd_data = amd.history(period="max")
# In[126]:
first_day_volume = amd_data.iloc[0]['Volume']
print(f"Volume traded on the first day: {first_day_volume}")
# In[8]:
get_ipython().system('pip install yfinance')
get_ipython().system('pip install pandas')
get_ipython().system('pip install matplotlib')
# In[14]:
import yfinance as yf
import pandas as pd
import matplotlib_inline
# In[16]:
nvda = yf.Ticker("NVDA")
# In[20]:
print (nvda)
# In[22]:
nvda.info
# In[24]:
nvda.history(period="max")
# In[36]:
nvda.info['country']
# In[38]:
nvda = yf.Ticker("NVDA")
nvda_data = nvda.history(period="max")
# In[40]:
first_day_volume = nvda_data.iloc[0]['Volume']
print(f"Volume traded on the first day: {first_day_volume}")
# In[44]:
nvda_data.reset_index(inplace=True)
# In[46]:
nvda_data.plot(x="Date", y="Open")
# In[48]:
nvda.dividends
# In[52]:
nvda.dividends.plot()
# ### Extracting Stock Data Using a Web Scraping
#
# Not all stock data is available via the API in this assignment; you will use web-scraping to obtain financial data.
# You will extract and share historical data from a web page using the BeautifulSoup library.
# In[3]:
get_ipython().system('pip install pandas')
get_ipython().system('pip install requests')
get_ipython().system('pip install bs4')
get_ipython().system('pip install html5lib')
get_ipython().system('pip install lxml')
get_ipython().system('pip install plotly')
# In[12]:
import pandas as pd
import requests
from bs4 import BeautifulSoup
# In Python, you can ignore warnings using the warnings module. You can use the filterwarnings function to filter or ignore specific warning messages or categories.
#
# In[15]:
import warnings
# Ignore all warnings
warnings.filterwarnings("ignore", category=FutureWarning)
# ## Extracting NETFLIX Stock Data
# We will extract Netflix stock data https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/netflix_data_webpage.html.
# ### Steps for extracting the data
# 1. Send an HTTP request to the web page using the requests library.
# 2. Parse the HTML content of the web page using BeautifulSoup.
# 3. Identify the HTML tags that contain the data you want to extract.
# 4. Use BeautifulSoup methods to extract the data from the HTML tags.
# 5. Print the extracted data
# ### Step 1: Send an HTTP request to the web page
# In[21]:
url = "https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/netflix_data_webpage.html"
# The requests.get() method takes a URL as its first argument, which specifies the location of the resource to be retrieved. In this case, the value of the url variable is passed as the argument to the requests.get() method, because you will store a web page URL in a url variable.
#
# You use the .text method for extracting the HTML content as a string in order to make it readable.
# In[24]:
data = requests.get(url).text
print(data)
# ### Step 2: Parse the HTML content
# ## Parsing the data using the BeautifulSoup library¶
# * Create a new BeautifulSoup object.
# <br>
# <br>
# <b> Note: </b> To create a BeautifulSoup object in Python, you need to pass two arguments to its constructor:
#
# 1. The HTML or XML content that you want to parse as a string.
# 2. The name of the parser that you want to use to parse the HTML or XML content. This argument is optional, and if you don't specify a parser, BeautifulSoup will use the default HTML parser included with the library. here in this lab we are using "html5lib" parser.
# In[31]:
soup = BeautifulSoup(data, 'html.parser')
# ### Step 3: Identify the HTML tags
# As stated above, the web page consists of a table so, we will scrape the content of the HTML web page and convert the table into a data frame.
# You will create an empty data frame using the pd.DataFrame() function with the following columns:
#
# "Date"
# "Open"
# "High"
# "Low"
# "Close"
# "Volume"
# In[36]:
netflix_data = pd.DataFrame(columns=["Date", "Open", "High", "Low", "Close", "Volume"])
# <hr>
# <hr>
# <center>
#
# ### Working on HTML table </center>
# <br>
#
# These are the following tags which are used while creating HTML tables.
#
# * <table>: This tag is a root tag used to define the start and end of the table. All the content of the table is enclosed within these tags.
#
#
# * <tr>: This tag is used to define a table row. Each row of the table is defined within this tag.
#
# * <td>: This tag is used to define a table cell. Each cell of the table is defined within this tag. You can specify the content of the cell between the opening and closing <td> tags.
#
# * <th>: This tag is used to define a header cell in the table. The header cell is used to describe the contents of a column or row. By default, the text inside a <th> tag is bold and centered.
#
# * <tbody>: This is the main content of the table, which is defined using the <tbody> tag. It contains one or more rows of <tr> elements.
#
# <hr>
# <hr>
#
# ### Step 4: Use a BeautifulSoup method for extracting data
# We will use <b>find()</b> and <b>find_all()</b> methods of the BeautifulSoup object to locate the table body and table row respectively in the HTML.
# * The <i>find() method </i> will return particular tag content.
# * The <i>find_all()</i> method returns a list of all matching tags in the HTML.
#
# In[44]:
# First we isolate the body of the table which contains all the information
# Then we loop through each row and find all the column values for each row
for row in soup.find("tbody").find_all('tr'):
col = row.find_all("td")
date = col[0].text
Open = col[1].text
high = col[2].text
low = col[3].text
close = col[4].text
adj_close = col[5].text
volume = col[6].text
# Finally we append the data of each row to the table
netflix_data = pd.concat([netflix_data,pd.DataFrame({"Date":[date], "Open":[Open], "High":[high], "Low":[low], "Close":[close], "Adj Close":[adj_close], "Volume":[volume]})], ignore_index=True)
# ### Step 5: Print the extracted data
# We can now print out the data frame using the head() or tail() function.
# In[48]:
netflix_data.head()
# # Extracting data using `pandas` library
# We can also use the pandas read_html function from the pandas library and use the URL for extracting data.
# <center>
#
# ## What is read_html in pandas library?
# `pd.read_html(url)` is a function provided by the pandas library in Python that is used to extract tables from HTML web pages. It takes in a URL as input and returns a list of all the tables found on the web page.
# </center>
#
# In[55]:
read_html_pandas_data = pd.read_html(url)
#
# Or you can convert the BeautifulSoup object to a string.
# In[58]:
read_html_pandas_data = pd.read_html(str(soup))
# Because there is only one table on the page, just take the first table in the returned list.
# In[63]:
netflix_dataframe = read_html_pandas_data[0]
netflix_dataframe.head()
# In[ ]: