0% found this document useful (0 votes)
9 views

Final Assignment Library Extract Stock Data From Webpage

Uploaded by

Linh Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Final Assignment Library Extract Stock Data From Webpage

Uploaded by

Linh Nguyen
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

Final_Assignment_Library 2021-03-06, 8:41 AM

Extracting Stock Data Using a Python Library


A company's stock share is a piece of the company more precisely:

A stock (also known as equity) is a security that represents the ownership of a fraction of
a corporation. This entitles the owner of the stock to a proportion of the corporation's assets and
profits equal to how much stock they own. Units of stock are called "shares." [1]
An investor can buy a stock and sell it later. If the stock price increases, the investor profits, If it
decreases,the investor with incur a loss. Determining the stock price is complex; it depends on the number
of outstanding shares, the size of the company's future profits, and much more. People trade stocks
throughout the day the stock ticker is a report of the price of a certain stock, updated continuously
throughout the trading session by the various stock market exchanges.

You are a data scientist working for a hedge fund; it's your job to determine any suspicious stock activity. In
this lab you will extract stock data using a Python library. We will use the yfinance library, it allows us to
extract data for stocks returning data in a pandas dataframe. You will use the lab to extract.

Table of Contents

Using yfinance to Extract Stock Info


Using yfinance to Extract Historical Share Price Data
Using yfinance to Extract Historical Dividends Data
Exercise

Estimated Time Needed: 30 min

about:srcdoc Page 1 of 11
Final_Assignment_Library 2021-03-06, 8:41 AM

In [1]:

!pip install yfinance


#!pip install pandas

Collecting yfinance
Downloading https://files.pythonhosted.org/packages/7a/e8/b9d7104d
3a4bf39924799067592d9e59119fcfc900a425a12e80a3123ec8/yfinance-0.1.55
.tar.gz
Requirement already satisfied: pandas>=0.24 in /home/jupyterlab/cond
a/envs/python/lib/python3.6/site-packages (from yfinance) (1.1.5)
Requirement already satisfied: numpy>=1.15 in /home/jupyterlab/conda
/envs/python/lib/python3.6/site-packages (from yfinance) (1.19.4)
Requirement already satisfied: requests>=2.20 in /home/jupyterlab/co
nda/envs/python/lib/python3.6/site-packages (from yfinance) (2.25.0)
Collecting multitasking>=0.0.7 (from yfinance)
Downloading https://files.pythonhosted.org/packages/69/e7/e9f1661c
28f7b87abfa08cb0e8f51dad2240a9f4f741f02ea839835e6d18/multitasking-0.
0.9.tar.gz
Collecting lxml>=4.5.1 (from yfinance)
Downloading https://files.pythonhosted.org/packages/bd/78/56a7c88a
57d0d14945472535d0df9fb4bbad7d34ede658ec7961635c790e/lxml-4.6.2-cp36
-cp36m-manylinux1_x86_64.whl (5.5MB)
|████████████████████████████████| 5.5MB 872kB/s eta 0:00:01
|█▋ | 276kB 4.9MB/s eta 0:00:02 |██
█████████████▌ | 2.7MB 4.9MB/s eta 0:00:01 |█████
███████████▋ | 2.9MB 4.9MB/s eta 0:00:01 |████████
█████████████▎ | 3.7MB 4.9MB/s eta 0:00:01
Requirement already satisfied: pytz>=2017.2 in /home/jupyterlab/cond
a/envs/python/lib/python3.6/site-packages (from pandas>=0.24->yfinan
ce) (2020.4)
Requirement already satisfied: python-dateutil>=2.7.3 in /home/jupyt
erlab/conda/envs/python/lib/python3.6/site-packages (from pandas>=0.
24->yfinance) (2.8.1)
Requirement already satisfied: chardet<4,>=3.0.2 in /home/jupyterlab
/conda/envs/python/lib/python3.6/site-packages (from requests>=2.20-
>yfinance) (3.0.4)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/jupyte
rlab/conda/envs/python/lib/python3.6/site-packages (from requests>=2
.20->yfinance) (1.25.11)
Requirement already satisfied: certifi>=2017.4.17 in /home/jupyterla
b/conda/envs/python/lib/python3.6/site-packages (from requests>=2.20
->yfinance) (2020.12.5)
Requirement already satisfied: idna<3,>=2.5 in /home/jupyterlab/cond
a/envs/python/lib/python3.6/site-packages (from requests>=2.20->yfin
ance) (2.10)
Requirement already satisfied: six>=1.5 in /home/jupyterlab/conda/en
vs/python/lib/python3.6/site-packages (from python-dateutil>=2.7.3->
pandas>=0.24->yfinance) (1.15.0)
Building wheels for collected packages: yfinance, multitasking

about:srcdoc Page 2 of 11
Final_Assignment_Library 2021-03-06, 8:41 AM

Building wheel for yfinance (setup.py) ... done


Stored in directory: /home/jupyterlab/.cache/pip/wheels/04/98/cc/2
702a4242d60bdc14f48b4557c427ded1fe92aedf257d4565c
Building wheel for multitasking (setup.py) ... done
Stored in directory: /home/jupyterlab/.cache/pip/wheels/37/fa/73/d
492849e319038eb4d986f5152e4b19ffb1bc0639da84d2677
Successfully built yfinance multitasking
Installing collected packages: multitasking, lxml, yfinance
Successfully installed lxml-4.6.2 multitasking-0.0.9 yfinance-0.1.55

In [3]:

import yfinance as yf
import pandas as pd

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 [4]:

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 (https://aroussi.com/post/python-
yahoo-finance?cm_mmc=Email_Newsletter-_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-
Courses-IBMDeveloperSkillsNetwork-PY0220EN-SkillsNetwork-
23455606&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12
_-Developer_Ed%2BTech-_-WW_WW-_-SkillsNetwork-Courses-IBMDeveloperSkillsNetwork-PY0220EN-
SkillsNetwork-
23455606&cm_mmca1=000026UJ&cm_mmca2=10006555&cm_mmca3=M12345678&cvosrc=email.Newsletter.M12

Stock Info

Using the attribute info we can extract information about the stock as a Python dictionary.

In [5]:

apple_info=apple.info
apple_info

about:srcdoc Page 3 of 11
Final_Assignment_Library 2021-03-06, 8:41 AM

Out[5]:

{'zip': '95014',
'sector': 'Technology',
'fullTimeEmployees': 147000,
'longBusinessSummary': 'Apple Inc. designs, manufactures, and marke
ts smartphones, personal computers, tablets, wearables, and accessor
ies worldwide. It also sells various related services. The company o
ffers iPhone, a line of smartphones; Mac, a line of personal compute
rs; iPad, a line of multi-purpose tablets; and wearables, home, and
accessories comprising AirPods, Apple TV, Apple Watch, Beats product
s, HomePod, iPod touch, and other Apple-branded and third-party acce
ssories. It also provides AppleCare support services; cloud services
store services; and operates various platforms, including the App St
ore, that allow customers to discover and download applications and
digital content, such as books, music, video, games, and podcasts. I
n addition, the company offers various services, such as Apple Arcad
e, a game subscription service; Apple Music, which offers users a cu
rated listening experience with on-demand radio stations; Apple News
+, a subscription news and magazine service; Apple TV+, which offers
exclusive original content; Apple Card, a co-branded credit card; an
d Apple Pay, a cashless payment service, as well as licenses its int
ellectual property. The company serves consumers, and small and mid-
sized businesses; and the education, enterprise, and government mark
ets. It sells and delivers third-party applications for its products
through the App Store. The company also sells its products through i
ts retail and online stores, and direct sales force; and third-party
cellular network carriers, wholesalers, retailers, and resellers. Ap
ple Inc. was founded in 1977 and is headquartered in Cupertino, Cali
fornia.',
'city': 'Cupertino',
'phone': '408-996-1010',
'state': 'CA',
'country': 'United States',
'companyOfficers': [],
'website': 'http://www.apple.com',
'maxAge': 1,
'address1': 'One Apple Park Way',
'industry': 'Consumer Electronics',
'previousClose': 120.13,
'regularMarketOpen': 120.98,
'twoHundredDayAverage': 123.10817,
'trailingAnnualDividendYield': 0.0067177224,
'payoutRatio': 0.2177,
'volume24Hr': None,
'regularMarketDayHigh': 121.935,
'navPrice': None,
'averageDailyVolume10Day': 135608637,
'totalAssets': None,
'regularMarketPreviousClose': 120.13,

about:srcdoc Page 4 of 11
Final_Assignment_Library 2021-03-06, 8:41 AM

'fiftyDayAverage': 131.63971,
'trailingAnnualDividendRate': 0.807,
'open': 120.98,
'toCurrency': None,
'averageVolume10days': 135608637,
'expireDate': None,
'yield': None,
'algorithm': None,
'dividendRate': 0.82,
'exDividendDate': 1612483200,
'beta': 1.251354,
'circulatingSupply': None,
'startDate': None,
'regularMarketDayLow': 117.57,
'priceHint': 2,
'currency': 'USD',
'trailingPE': 32.931923,
'regularMarketVolume': 153766501,
'lastMarket': None,
'maxSupply': None,
'openInterest': None,
'marketCap': 2038411034624,
'volumeAllCurrencies': None,
'strikePrice': None,
'averageVolume': 109567300,
'priceToSalesTrailing12Months': 6.9301887,
'dayLow': 117.57,
'ask': 121.39,
'ytdReturn': None,
'askSize': 1000,
'volume': 153766501,
'fiftyTwoWeekHigh': 145.09,
'forwardPE': 25.944445,
'fromCurrency': None,
'fiveYearAvgDividendYield': 1.42,
'fiftyTwoWeekLow': 53.1525,
'bid': 121.48,
'tradeable': False,
'dividendYield': 0.0068,
'bidSize': 1800,
'dayHigh': 121.935,
'exchange': 'NMS',
'shortName': 'Apple Inc.',
'longName': 'Apple Inc.',
'exchangeTimezoneName': 'America/New_York',
'exchangeTimezoneShortName': 'EST',
'isEsgPopulated': False,
'gmtOffSetMilliseconds': '-18000000',
'quoteType': 'EQUITY',
'symbol': 'AAPL',

about:srcdoc Page 5 of 11
Final_Assignment_Library 2021-03-06, 8:41 AM

'messageBoardId': 'finmb_24937',
'market': 'us_market',
'annualHoldingsTurnover': None,
'enterpriseToRevenue': 7.05,
'beta3Year': None,
'profitMargins': 0.21735,
'enterpriseToEbitda': 24.35,
'52WeekChange': 0.82469845,
'morningStarRiskRating': None,
'forwardEps': 4.68,
'revenueQuarterlyGrowth': None,
'sharesOutstanding': 16788100096,
'fundInceptionDate': None,
'annualReportExpenseRatio': None,
'bookValue': 3.936,
'sharesShort': 88329668,
'sharesPercentSharesOut': 0.0053,
'fundFamily': None,
'lastFiscalYearEnd': 1601078400,
'heldPercentInstitutions': 0.59661996,
'netIncomeToCommon': 63929999360,
'trailingEps': 3.687,
'lastDividendValue': 0.205,
'SandP52WeekChange': 0.39881885,
'priceToBook': 30.848576,
'heldPercentInsiders': 0.00065,
'nextFiscalYearEnd': 1664150400,
'mostRecentQuarter': 1608940800,
'shortRatio': 0.88,
'sharesShortPreviousMonthDate': 1610668800,
'floatShares': 16770804261,
'enterpriseValue': 2073627590656,
'threeYearAverageReturn': None,
'lastSplitDate': 1598832000,
'lastSplitFactor': '4:1',
'legalType': None,
'lastDividendDate': 1612483200,
'morningStarOverallRating': None,
'earningsQuarterlyGrowth': 0.293,
'dateShortInterest': 1613088000,
'pegRatio': 2.02,
'lastCapGain': None,
'shortPercentOfFloat': 0.0053,
'sharesShortPriorMonth': 99897228,
'impliedSharesOutstanding': None,
'category': None,
'fiveYearAverageReturn': None,
'regularMarketPrice': 120.98,
'logo_url': 'https://logo.clearbit.com/apple.com'}

about:srcdoc Page 6 of 11
Final_Assignment_Library 2021-03-06, 8:41 AM

We can get the 'country' using the key country

In [6]:

apple_info['country']

Out[6]:

'United States'

Extracting Share Price

A share is the single smallest part of a company's 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 [7]:

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 [8]:

apple_share_price_data.head()

Out[8]:

Open High Low Close Volume Dividends Stock Splits

Date

1980-12-12 0.100922 0.101361 0.100922 0.100922 469033600 0.0 0.0

1980-12-15 0.096096 0.096096 0.095657 0.095657 175884800 0.0 0.0

1980-12-16 0.089075 0.089075 0.088636 0.088636 105728000 0.0 0.0

1980-12-17 0.090830 0.091268 0.090830 0.090830 86441600 0.0 0.0

1980-12-18 0.093463 0.093902 0.093463 0.093463 73449600 0.0 0.0

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.

about:srcdoc Page 7 of 11
Final_Assignment_Library 2021-03-06, 8:41 AM

In [9]:

apple_share_price_data.reset_index(inplace=True)

We can plot the Open price against the Date :

In [10]:

apple_share_price_data.plot(x="Date", y="Open")

Out[10]:

<AxesSubplot:xlabel='Date'>

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.

about:srcdoc Page 8 of 11
Final_Assignment_Library 2021-03-06, 8:41 AM

In [11]:

apple.dividends

Out[11]:

Date
1987-05-11 0.000536
1987-08-10 0.000536
1987-11-17 0.000714
1988-02-12 0.000714
1988-05-16 0.000714
...
2020-02-07 0.192500
2020-05-08 0.205000
2020-08-07 0.205000
2020-11-06 0.205000
2021-02-05 0.205000
Name: Dividends, Length: 70, dtype: float64

We can plot the dividends overtime:

In [12]:

apple.dividends.plot()

Out[12]:

<AxesSubplot:xlabel='Date'>

Exercise

about:srcdoc Page 9 of 11
Final_Assignment_Library 2021-03-06, 8:41 AM

Now using the Ticker module create an object for AMD (Advanced Micro Devices) with the ticker symbol
is AMD called; name the object amd .

In [13]:

amd = yf.Ticker("AMD")

Question 1 Use the key 'country' to find the country the stock belongs to, remember it as it will be a
quiz question.

In [15]:

amd_info = amd.info
amd_info['country']

Out[15]:

'United States'

Question 2 Use the key 'sector' to find the sector the stock belongs to, remember it as it will be a quiz
question.

In [16]:

amd_info['sector']

Out[16]:

'Technology'

Question 3 Find the max of the Volume column of AMD using the history function, set the period
to max.

In [19]:

amd_share_price_data = amd.history(period="max")
volume = amd_share_price_data['Volume']
max_volume = volume.max()
max_volume

Out[19]:

325058400

about:srcdoc Page 10 of 11
Final_Assignment_Library 2021-03-06, 8:41 AM

About the Authors:


Joseph Santarcangelo (https://www.linkedin.com/in/joseph-s-50398b136/) 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.

about:srcdoc Page 11 of 11

You might also like