API - Application Programming Interface
An API, or Application Programming Interface, is a set of rules and protocols that allows software applications to communicate with each other. APIs are similar to a postal service for data, where they define the correct address format, the type of mail that's allowed, and what to expect in return.
How APIs Benefit AI and Data Science Projects
To use an API, you make a request to a remote web server and retrieve the data you need. APIs offer several key benefits for AI and data science projects:
Real-time data access: APIs allow you to retrieve up-to-date data on demand, which is crucial for AI models and data science projects that require real-time data to make accurate predictions or decisions.
Large datasets: Training AI models often requires large amounts of data. APIs provide a way to access and integrate data from multiple sources without the need for local storage and management.
Pre-processed data: Some APIs offer pre-processed or enriched data, such as sentiment analysis or entity recognition, saving significant time and resources in AI projects.
When to Use APIs Instead of Static Datasets
APIs are particularly useful in the following scenarios:
Rapidly changing data: For data that changes quickly, like stock prices, using an API is more efficient than repeatedly downloading a static dataset.
Specific subsets of data: If you only need a small piece of a larger dataset, such as your own comments on Reddit, an API allows you to retrieve just the relevant data.
Complex computations: APIs like Spotify's can provide information like music genres, leveraging their extensive data and computational resources.
Yahoo Finance offers an excellent range of market data on stocks, bonds, currencies, and cryptocurrencies. It also provides news reports with various insights into different markets from around the world – all accessible through the yfinance python library.
The most popular way to access Yahoo Data, and the API is open-source and free to use. There are other free and paid APIs to access Yahoo’s data, but yfinance is the best place to start, and here’s why.
It’s simple to use
It returns data as Pandas DataFrames
One-minute bar granularity
pip install yfinance --upgrade --no-cache-dir
bg1stamford@cloudshell:~ (consummate-tine-433617-f0)$ pip install yfinance --upgrade --no-cache-dir
Defaulting to user installation because normal site-packages is not writeable
Collecting yfinance
Downloading yfinance-0.2.44-py2.py3-none-any.whl.metadata (13 kB)
Collecting pandas>=1.3.0 (from yfinance)
Downloading pandas-2.2.3-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (89 kB)
bg1stamford@cloudshell:~/yfinance (consummate-tine-433617-f0)$ python3
Python 3.12.3 (main, Sep 11 2024, 14:17:37) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import pandas as pd
>>> import yfinance as yf
Lists modules of tf
>>> dir(yf)
['Ticker', 'Tickers', '__all__', '__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', 'base', 'data', 'download', 'exceptions', 'multi', 'pdr_override', 'scrapers', 'set_tz_cache_location', 'shared', 'ticker', 'tickers', 'utils', 'version']
>>>
Use the apple ticker and create an object aapl
>>> aapl = yf.Ticker('aapl')
>>>
>> aapl
yfinance.Ticker object <AAPL>
>>>
Leverage one of yfinance methods to pull history of apple opening price, high, low, closing price, volume traded and dividends date
>>> data = aapl.history()
Display top lines of history
>>> data.head()
>>>
Date
2024-09-12 00:00:00-04:00 222.500000 223.550003 219.820007 222.770004 37498200 0.0 0.0
2024-09-13 00:00:00-04:00 223.580002 224.039993 221.910004 222.500000 36766600 0.0 0.0
2024-09-16 00:00:00-04:00 216.539993 217.220001 213.919998 216.320007 59357400 0.0 0.0
2024-09-17 00:00:00-04:00 215.750000 216.899994 214.500000 216.789993 45519300 0.0 0.0
2024-09-18 00:00:00-04:00 217.550003 222.710007 217.539993 220.690002 59894900 0.0 0.0
>>>
Leverage different commands available
>>> aapl.get_financials()
2023-09-30 2022-09-30 2021-09-30 2020-09-30
TaxEffectOfUnusualItems 0.0 0.0 0.0 0.0
TaxRateForCalcs 0.147 0.162 0.133 0.144
NormalizedEBITDA 129188000000.0 133138000000.0 123136000000.0 81020000000.0
NetIncomeFromContinuingOperationNetMinorityInte... 96995000000.0 99803000000.0 94680000000.0 57411000000.0
ReconciledDepreciation 11519000000.0 11104000000.0 11284000000.0 11056000000.0
ReconciledCostOfRevenue 214137000000.0 223546000000.0 212981000000.0 169559000000.0
EBITDA 129188000000.0 133138000000.0 123136000000.0 81020000000.0
EBIT 117669000000.0 122034000000.0 111852000000.0 69964000000.0
NetInterestIncome -183000000.0 -106000000.0 198000000.0 890000000.0
InterestExpense 3933000000.0 2931000000.0 2645000000.0 2873000000.0
InterestIncome 3750000000.0 2825000000.0 2843000000.0 3763000000.0
NormalizedIncome 96995000000.0 99803000000.0 94680000000.0 57411000000.0
NetIncomeFromContinuingAndDiscontinuedOperation 96995000000.0 99803000000.0 94680000000.0 57411000000.0
TotalExpenses 268984000000.0 274891000000.0 256868000000.0 208227000000.0
TotalOperatingIncomeAsReported 114301000000.0 119437000000.0 108949000000.0 66288000000.0
DilutedAverageShares 15812547000.0 16325819000.0 16864919000.0 17528214000.0
BasicAverageShares 15744231000.0 16215963000.0 16701272000.0 17352119000.0
DilutedEPS 6.13 6.11 5.61 3.28
BasicEPS 6.16 6.15 5.67 3.31
DilutedNIAvailtoComStockholders 96995000000.0 99803000000.0 94680000000.0 57411000000.0
NetIncomeCommonStockholders 96995000000.0 99803000000.0 94680000000.0 57411000000.0
NetIncome 96995000000.0 99803000000.0 94680000000.0 57411000000.0
NetIncomeIncludingNoncontrollingInterests 96995000000.0 99803000000.0 94680000000.0 57411000000.0
NetIncomeContinuousOperations 96995000000.0 99803000000.0 94680000000.0 57411000000.0
TaxProvision 16741000000.0 19300000000.0 14527000000.0 9680000000.0
PretaxIncome 113736000000.0 119103000000.0 109207000000.0 67091000000.0
OtherIncomeExpense -382000000.0 -228000000.0 60000000.0 -87000000.0
OtherNonOperatingIncomeExpenses -382000000.0 -228000000.0 60000000.0 -87000000.0
NetNonOperatingInterestIncomeExpense -183000000.0 -106000000.0 198000000.0 890000000.0
InterestExpenseNonOperating 3933000000.0 2931000000.0 2645000000.0 2873000000.0
InterestIncomeNonOperating 3750000000.0 2825000000.0 2843000000.0 3763000000.0
OperatingIncome 114301000000.0 119437000000.0 108949000000.0 66288000000.0
OperatingExpense 54847000000.0 51345000000.0 43887000000.0 38668000000.0
ResearchAndDevelopment 29915000000.0 26251000000.0 21914000000.0 18752000000.0
SellingGeneralAndAdministration 24932000000.0 25094000000.0 21973000000.0 19916000000.0
GrossProfit 169148000000.0 170782000000.0 152836000000.0 104956000000.0
CostOfRevenue 214137000000.0 223546000000.0 212981000000.0 169559000000.0
TotalRevenue 383285000000.0 394328000000.0 365817000000.0 274515000000.0
OperatingRevenue 383285000000.0 394328000000.0 365817000000.0 274515000000.0
>>>
Stock Data Collection:
Yahoo stock data retrieval code, add functions to store data in Pandas and a Matplotlib chart, upload this visualization and the accompanying CSV file to a Google Cloud Storage bucket. Add a cronjob to the code that makes it run every weekday at 6PM EST, when the market closes.
Building python programs
You can now build programs to retrieve data from Yahoo.
=======================================================
import yfinance as yf
# Define the ticker symbol
ticker_symbol = "AAPL"
# Create a Ticker object
ticker = yf.Ticker(ticker_symbol)
# Fetch historical market data for the last 30 days
historical_data = ticker.history(period="1mo") # data for the last month
# Display a summary of the fetched data
print(f"Summary of Historical Data for {ticker_symbol}:")
print(historical_data[['Open', 'High', 'Low', 'Close', 'Volume']])
=======================================================
g1stamford@cloudshell:~/yfinance (consummate-tine-433617-f0)$ python3 prices.py
Summary of Historical Data for AAPL:
Open High Low Close Volume
Date
2024-09-12 00:00:00-04:00 222.500000 223.550003 219.820007 222.770004 37498200
2024-09-13 00:00:00-04:00 223.580002 224.039993 221.910004 222.500000 36766600
2024-09-16 00:00:00-04:00 216.539993 217.220001 213.919998 216.320007 59357400
2024-09-17 00:00:00-04:00 215.750000 216.899994 214.500000 216.789993 45519300
2024-09-18 00:00:00-04:00 217.550003 222.710007 217.539993 220.690002 59894900
2024-09-19 00:00:00-04:00 224.990005 229.820007 224.630005 228.869995 66781300
2024-09-20 00:00:00-04:00 229.970001 233.089996 227.619995 228.199997 318679900
2024-09-23 00:00:00-04:00 227.339996 229.449997 225.809998 226.470001 54146000
2024-09-24 00:00:00-04:00 228.649994 229.350006 225.729996 227.369995 43556100
2024-09-25 00:00:00-04:00 224.929993 227.289993 224.020004 226.369995 42308700
2024-09-26 00:00:00-04:00 227.300003 228.500000 225.410004 227.520004 36636700
2024-09-27 00:00:00-04:00 228.460007 229.520004 227.300003 227.789993 34026000
2024-09-30 00:00:00-04:00 230.039993 233.000000 229.649994 233.000000 54541900
2024-10-01 00:00:00-04:00 229.520004 229.649994 223.740005 226.210007 63285000
2024-10-02 00:00:00-04:00 225.889999 227.369995 223.020004 226.779999 32880600
2024-10-03 00:00:00-04:00 225.139999 226.809998 223.320007 225.669998 34044200
2024-10-04 00:00:00-04:00 227.899994 228.000000 224.130005 226.800003 37245100
2024-10-07 00:00:00-04:00 224.500000 225.690002 221.330002 221.690002 39505400
2024-10-08 00:00:00-04:00 224.300003 225.979996 223.250000 225.770004 31855700
2024-10-09 00:00:00-04:00 225.229996 229.750000 224.830002 229.539993 33591100
2024-10-10 00:00:00-04:00 227.779999 229.500000 227.169998 229.039993 28183500
2024-10-11 00:00:00-04:00 229.300003 229.410004 227.339996 227.550003 31668000
bg1stamford@cloudshell:~/yfinance (consummate-tine-
No comments:
Post a Comment