Skip to content

Instantly share code, notes, and snippets.

@saxenaiway
Created March 16, 2021 09:31
Show Gist options
  • Save saxenaiway/135ff46e9c2cc7cc8b27671b576f90ec to your computer and use it in GitHub Desktop.
Save saxenaiway/135ff46e9c2cc7cc8b27671b576f90ec to your computer and use it in GitHub Desktop.
Created on Skills Network Labs
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center>\n",
" <img src=\"https://s3-api.us-geo.objectstorage.softlayer.net/cf-courses-data/CognitiveClass/Logos/organization_logo/organization_logo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n",
"</center>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h1>Extracting Stock Data Using a Python Library</h1>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A company's stock share is a piece of the company more precisely:\n",
"\n",
"<p><b>A stock (also known as equity) is a security that represents the ownership of a fraction of a corporation. This\n",
"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]</p></b>\n",
"\n",
"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. \n",
"\n",
"<p>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 <coode>yfinance</code> library, it allows us to extract data for stocks returning data in a pandas dataframe. You will use the lab to extract.</p>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Table of Contents</h2>\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <ul>\n",
" <li>Using yfinance to Extract Stock Info</li>\n",
" <li>Using yfinance to Extract Historical Share Price Data</li>\n",
" <li>Using yfinance to Extract Historical Dividends Data</li>\n",
" <li>Exercise</li>\n",
" </ul>\n",
"<p>\n",
" Estimated Time Needed: <strong>30 min</strong></p>\n",
"</div>\n",
"\n",
"<hr>\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"!pip install yfinance\n",
"#!pip install pandas"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import yfinance as yf\n",
"import pandas as pd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Using the yfinance Library to Extract Stock Data\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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`.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"apple = yf.Ticker(\"AAPL\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.M12345678&cvo_campaign=000026UJ&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.M12345678&cvo_campaign=000026UJ).\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Stock Info\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Using the attribute <code>info</code> we can extract information about the stock as a Python dictionary.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"apple_info=apple.info\n",
"apple_info"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can get the <code>'country'</code> using the key country\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"apple_info['country']"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Extracting Share Price\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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 <code>history()</code> 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.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"apple_share_price_data = apple.history(period=\"max\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"apple_share_price_data.head()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"apple_share_price_data.reset_index(inplace=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can plot the `Open` price against the `Date`:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"apple_share_price_data.plot(x=\"Date\", y=\"Open\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Extracting Dividends\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"apple.dividends"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can plot the dividends overtime:\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"apple.dividends.plot()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Exercise\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now using the `Ticker` module create an object for AMD (Advanced Micro Devices) with the ticker symbol is `AMD` called; name the object <code>amd</code>.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"amd = yf.Ticker(\"AMD\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Question 1</b> Use the key <code>'country'</code> to find the country the stock belongs to, remember it as it will be a quiz question.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"United States"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Question 2</b> Use the key <code>'sector'</code> to find the sector the stock belongs to, remember it as it will be a quiz question.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Technology"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<b>Question 3</b> Find the max of the <code>Volume</code> column of AMD using the `history` function, set the <code>period</code> to max.\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"727200"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>About the Authors:</h2> \n",
"\n",
"<a href=\"https://www.linkedin.com/in/joseph-s-50398b136/\">Joseph Santarcangelo</a> 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.\n",
"\n",
"Azim Hirjani\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Change Log\n",
"\n",
"| Date (YYYY-MM-DD) | Version | Changed By | Change Description |\n",
"| ----------------- | ------- | ------------- | ------------------------- |\n",
"| 2020-11-10 | 1.1 | Malika Singla | Deleted the Optional part |\n",
"| 2020-08-27 | 1.0 | Malika Singla | Added lab to GitLab |\n",
"\n",
"<hr>\n",
"\n",
"## <h3 align=\"center\"> © IBM Corporation 2020. All rights reserved. <h3/>\n",
"\n",
"<p>\n"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python",
"language": "python",
"name": "conda-env-python-py"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.12"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment