cognitiveclass.ai logo

Extracting and Visualizing Stock Data

Description

Extracting essential data from a dataset and displaying it is a necessary part of data science; therefore individuals can make correct decisions based on the data. In this assignment, you will extract some stock data, you will then display this data in a graph.

Table of Contents

  • Define a Function that Makes a Graph
  • Question 1: Use yfinance to Extract Stock Data
  • Question 2: Use Webscraping to Extract Tesla Revenue Data
  • Question 3: Use yfinance to Extract Stock Data
  • Question 4: Use Webscraping to Extract GME Revenue Data
  • Question 5: Plot Tesla Stock Graph
  • Question 6: Plot GameStop Stock Graph

Estimated Time Needed: 30 min


In [1]:
!pip install yfinance==0.1.67
#!pip install pandas==1.3.3
#!pip install requests==2.26.0
!mamba install bs4==4.10.0 -y
#!pip install plotly==5.3.1
Requirement already satisfied: yfinance==0.1.67 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (0.1.67)
Requirement already satisfied: lxml>=4.5.1 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from yfinance==0.1.67) (4.7.1)
Requirement already satisfied: pandas>=0.24 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from yfinance==0.1.67) (1.3.4)
Requirement already satisfied: multitasking>=0.0.7 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from yfinance==0.1.67) (0.0.10)
Requirement already satisfied: requests>=2.20 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from yfinance==0.1.67) (2.26.0)
Requirement already satisfied: numpy>=1.15 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from yfinance==0.1.67) (1.20.3)
Requirement already satisfied: python-dateutil>=2.7.3 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from pandas>=0.24->yfinance==0.1.67) (2.8.2)
Requirement already satisfied: pytz>=2017.3 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from pandas>=0.24->yfinance==0.1.67) (2021.3)
Requirement already satisfied: six>=1.5 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from python-dateutil>=2.7.3->pandas>=0.24->yfinance==0.1.67) (1.15.0)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from requests>=2.20->yfinance==0.1.67) (1.26.7)
Requirement already satisfied: certifi>=2017.4.17 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from requests>=2.20->yfinance==0.1.67) (2021.10.8)
Requirement already satisfied: idna<4,>=2.5 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from requests>=2.20->yfinance==0.1.67) (3.3)
Requirement already satisfied: charset-normalizer~=2.0.0 in /opt/conda/envs/Python-3.9/lib/python3.9/site-packages (from requests>=2.20->yfinance==0.1.67) (2.0.4)
/usr/bin/sh: mamba: command not found
In [2]:
import yfinance as yf
import pandas as pd
import requests
from bs4 import BeautifulSoup
import plotly.graph_objects as go
from plotly.subplots import make_subplots

Define Graphing Function¶

In this section, we define the function make_graph. You don't have to know how the function works, you should only care about the inputs. It takes a dataframe with stock data (dataframe must contain Date and Close columns), a dataframe with revenue data (dataframe must contain Date and Revenue columns), and the name of the stock.

In [3]:
def make_graph(stock_data, revenue_data, stock):
    fig = make_subplots(rows=2, cols=1, shared_xaxes=True, subplot_titles=("Historical Share Price", "Historical Revenue"), vertical_spacing = .3)
    stock_data_specific = stock_data[stock_data.Date <= '2021--06-14']
    revenue_data_specific = revenue_data[revenue_data.Date <= '2021-04-30']
    fig.add_trace(go.Scatter(x=pd.to_datetime(stock_data_specific.Date, infer_datetime_format=True), y=stock_data_specific.Close.astype("float"), name="Share Price"), row=1, col=1)
    fig.add_trace(go.Scatter(x=pd.to_datetime(revenue_data_specific.Date, infer_datetime_format=True), y=revenue_data_specific.Revenue.astype("float"), name="Revenue"), row=2, col=1)
    fig.update_xaxes(title_text="Date", row=1, col=1)
    fig.update_xaxes(title_text="Date", row=2, col=1)
    fig.update_yaxes(title_text="Price ($US)", row=1, col=1)
    fig.update_yaxes(title_text="Revenue ($US Millions)", row=2, col=1)
    fig.update_layout(showlegend=False,
    height=900,
    title=stock,
    xaxis_rangeslider_visible=True)
    fig.show()

Question 1: Use yfinance to Extract Stock Data¶

Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is Tesla and its ticker symbol is TSLA.

In [4]:
Tesla=yf.Ticker("TSLA")

Using the ticker object and the function history extract stock information and save it in a dataframe named tesla_data. Set the period parameter to max so we get information for the maximum amount of time.

In [5]:
tesla_data=Tesla.history(period='max')
tesla_data
Out[5]:
Open High Low Close Volume Dividends Stock Splits
Date
2010-06-29 3.800000 5.000000 3.508000 4.778000 93831500 0 0.0
2010-06-30 5.158000 6.084000 4.660000 4.766000 85935500 0 0.0
2010-07-01 5.000000 5.184000 4.054000 4.392000 41094000 0 0.0
2010-07-02 4.600000 4.620000 3.742000 3.840000 25699000 0 0.0
2010-07-06 4.000000 4.000000 3.166000 3.222000 34334500 0 0.0
... ... ... ... ... ... ... ...
2022-03-07 856.299988 866.140015 804.570007 804.580017 24164700 0 0.0
2022-03-08 795.530029 849.989990 782.169983 824.400024 26799700 0 0.0
2022-03-09 839.479980 860.559998 832.010010 858.969971 19728000 0 0.0
2022-03-10 851.450012 854.450012 810.359985 838.299988 19549500 0 0.0
2022-03-11 840.200012 843.799988 793.770020 795.349976 22272800 0 0.0

2947 rows × 7 columns

Reset the index using the reset_index(inplace=True) function on the tesla_data DataFrame and display the first five rows of the tesla_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 1 to the results below.

In [6]:
tesla_data.reset_index(inplace=True)
tesla_data.head()
Out[6]:
Date Open High Low Close Volume Dividends Stock Splits
0 2010-06-29 3.800 5.000 3.508 4.778 93831500 0 0.0
1 2010-06-30 5.158 6.084 4.660 4.766 85935500 0 0.0
2 2010-07-01 5.000 5.184 4.054 4.392 41094000 0 0.0
3 2010-07-02 4.600 4.620 3.742 3.840 25699000 0 0.0
4 2010-07-06 4.000 4.000 3.166 3.222 34334500 0 0.0

Question 2: Use Webscraping to Extract Tesla Revenue Data¶

Use the requests library to download the webpage https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue. Save the text of the response as a variable named html_data.

In [7]:
html_data=requests.get("https://www.macrotrends.net/stocks/charts/TSLA/tesla/revenue?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0220ENSkillsNetwork23455606-2021-01-01").text

Parse the html data using beautiful_soup.

In [9]:
soup=BeautifulSoup(html_data,'html.parser')

Using BeautifulSoup or the read_html function extract the table with Tesla Quarterly Revenue and store it into a dataframe named tesla_revenue. The dataframe should have columns Date and Revenue.

Click here if you need help locating the table

Below is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab

soup.find_all("tbody")[1]

If you want to use the read_html function the table is located at index 1


In [68]:
tesla_tables = soup.find_all('table')

for index,table in enumerate(tesla_tables):
    if ("Tesla Quarterly Revenue" in str(table)):
        tesla_table_index = index

tesla_revenue = pd.DataFrame(columns=["Date", "Revenue"])

for row in tesla_tables[tesla_table_index].tbody.find_all("tr"):
    col = row.find_all("td")
    if (col !=[]):
        date = col[0].text
        revenue = col[1].text.replace("$", "").replace(",", "")
        tesla_revenue = tesla_revenue.append({"Date" : date, "Revenue" : revenue}, ignore_index=True)

Execute the following line to remove the comma and dollar sign from the Revenue column.

In [69]:
tesla_revenue["Revenue"] = tesla_revenue['Revenue'].str.replace(',|\$',"",regex=True)

Execute the following lines to remove an null or empty strings in the Revenue column.

In [70]:
tesla_revenue.dropna(inplace=True)

tesla_revenue = tesla_revenue[tesla_revenue['Revenue'] != ""]

Display the last 5 row of the tesla_revenue dataframe using the tail function. Take a screenshot of the results.

In [73]:
tesla_revenue.tail()
Out[73]:
Date Revenue
45 2010-09-30 31
46 2010-06-30 28
47 2010-03-31 21
49 2009-09-30 46
50 2009-06-30 27

Question 3: Use yfinance to Extract Stock Data¶

Using the Ticker function enter the ticker symbol of the stock we want to extract data on to create a ticker object. The stock is GameStop and its ticker symbol is GME.

In [74]:
GameStop=yf.Ticker('GME')

Using the ticker object and the function history extract stock information and save it in a dataframe named gme_data. Set the period parameter to max so we get information for the maximum amount of time.

In [75]:
gme_data=GameStop.history('max')

Reset the index using the reset_index(inplace=True) function on the gme_data DataFrame and display the first five rows of the gme_data dataframe using the head function. Take a screenshot of the results and code from the beginning of Question 3 to the results below.

In [76]:
gme_data.reset_index(inplace=True)
gme_data.head()
Out[76]:
Date Open High Low Close Volume Dividends Stock Splits
0 2002-02-13 6.480514 6.773400 6.413184 6.766667 19054000 0.0 0.0
1 2002-02-14 6.850828 6.864294 6.682503 6.733001 2755400 0.0 0.0
2 2002-02-15 6.733001 6.749833 6.632006 6.699336 2097400 0.0 0.0
3 2002-02-19 6.665672 6.665672 6.312190 6.430017 1852600 0.0 0.0
4 2002-02-20 6.463682 6.648839 6.413184 6.648839 1723200 0.0 0.0

Question 4: Use Webscraping to Extract GME Revenue Data¶

Use the requests library to download the webpage <https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html%3E;. Save the text of the response as a variable named html_data.

In [77]:
html_data=requests.get("https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork/labs/project/stock.html").text

Parse the html data using beautiful_soup.

In [78]:
soup=BeautifulSoup(html_data,"html.parser")

Using BeautifulSoup or the read_html function extract the table with GameStop Quarterly Revenue and store it into a dataframe named gme_revenue. The dataframe should have columns Date and Revenue. Make sure the comma and dollar sign is removed from the Revenue column using a method similar to what you did in Question 2.

Click here if you need help locating the table

Below is the code to isolate the table, you will now need to loop through the rows and columns like in the previous lab

soup.find_all("tbody")[1]

If you want to use the read_html function the table is located at index 1


In [80]:
gme_tables = soup.find_all('table')

for index,table in enumerate(gme_tables):
    if ("GameStop Quarterly Revenue" in str(table)):
        gme_table_index = index

gme_revenue = pd.DataFrame(columns=["Date", "Revenue"])

for row in gme_tables[gme_table_index].tbody.find_all("tr"):
    col = row.find_all("td")
    if (col !=[]):
        date = col[0].text
        revenue = col[1].text.replace("$", "").replace(",", "")
        gme_revenue = gme_revenue.append({"Date" : date, "Revenue" : revenue}, ignore_index=True)

Display the last five rows of the gme_revenue dataframe using the tail function. Take a screenshot of the results.

In [83]:
gme_revenue.tail()
Out[83]:
Date Revenue
57 2006-01-31 1667
58 2005-10-31 534
59 2005-07-31 416
60 2005-04-30 475
61 2005-01-31 709

Question 5: Plot Tesla Stock Graph¶

Use the make_graph function to graph the Tesla Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(tesla_data, tesla_revenue, 'Tesla'). Note the graph will only show data upto June 2021.

In [ ]:
make_graph(tesla_data, tesla_revenue, 'Tesla')

Question 6: Plot GameStop Stock Graph¶

Use the make_graph function to graph the GameStop Stock Data, also provide a title for the graph. The structure to call the make_graph function is make_graph(gme_data, gme_revenue, 'GameStop'). Note the graph will only show data upto June 2021.

In [ ]:
 

About the Authors:

Joseph Santarcangelo has a PhD in Electrical Engineering, his research focused on using machine learning, signal processing, and computer vision to determine how videos impact human cognition. Joseph has been working for IBM since he completed his PhD.

Azim Hirjani

Change Log¶

Date (YYYY-MM-DD) Version Changed By Change Description
2020-11-10 1.1 Malika Singla Deleted the Optional part
2020-08-27 1.0 Malika Singla Added lab to GitLab

© IBM Corporation 2020. All rights reserved.