Conteúdo do Curso
Introduction to Finance with Python
Introduction to Finance with Python
Challenge: Uploading Financial Data
Getting data from the web
Now, we are going to show how to get financial data directly from the web.
As an example, we are going to work with stock price data from financial provider Yahoo FInance.
To do so, we need a special library, called yfinance
, which could be imported with a code import yfinance as yf
.
If we want to see complete financial information, for example, about Amazon, we can use the following code :
import yfinance as yf GetAmazonInformation = yf.Ticker("AMZN") # Printing whole information from a dictionary of items for key, value in GetAmazonInformation.info.items(): print(key, ":", value)
As we can see, we are using shortened form of a company's name, like 'AMZN'
here for Amazon.
The same situation with all other companies, like 'META'
for Meta, 'GOOGL'
for Google, 'MSFT'
for Microsoft, 'NVDA'
for Nvidia and so on.
Retrieving stock history
Now we are going to discuss, how can we get historical data with stock prices.
Note, that in further cases, necessary data will be retrieved as a DataFrame object, described into previous chapter.
In case we want to get history of stock prices for a whole period available, we need to use history
method:
import yfinance as yf GetAmazonInformation = yf.Ticker("AMZN") # Getting stock data for the whole available period df=GetAmazonInformation.history(period="max") print(df)
Meanwhile, if we want to get data for a specific period, like 3 month, we should respectively set value of parameter period
:
import yfinance as yf GetAmazonInformation = yf.Ticker("AMZN") # Getting stock data for last 3 month df=GetAmazonInformation.history(period="3mo") print(df)
Other valid values of period
parameter: '1d'
, '5d'
, '1mo'
, '3mo'
, '6mo'
, '1y'
, '2y'
, '5y'
, '10y'
and 'ytd'
(which stands for 'Year to Date', and practically means period from the beginning of the year to the last date available).
Lastly, if we want to get data for a period with specific starting date and ending date, we need to change parameters start
and end
respectively:
import datetime import yfinance as yf GetAmazonInformation = yf.Ticker("AMZN") # Getting stock data for the period from 11th July of 2023 to the 11th July of 2024 df=GetAmazonInformation.history(start = datetime.datetime(2023, 7, 11), end = datetime.datetime(2024, 7, 11)) print(df)
Swipe to show code editor
In this task you need to:
- Get financial data
GetNvidiaInformation
for Nvidia company; - Get DataFrame
df
with the history of it's stock prices for the period from the beginning of the current year, till the last available date.
Obrigado pelo seu feedback!
Challenge: Uploading Financial Data
Getting data from the web
Now, we are going to show how to get financial data directly from the web.
As an example, we are going to work with stock price data from financial provider Yahoo FInance.
To do so, we need a special library, called yfinance
, which could be imported with a code import yfinance as yf
.
If we want to see complete financial information, for example, about Amazon, we can use the following code :
import yfinance as yf GetAmazonInformation = yf.Ticker("AMZN") # Printing whole information from a dictionary of items for key, value in GetAmazonInformation.info.items(): print(key, ":", value)
As we can see, we are using shortened form of a company's name, like 'AMZN'
here for Amazon.
The same situation with all other companies, like 'META'
for Meta, 'GOOGL'
for Google, 'MSFT'
for Microsoft, 'NVDA'
for Nvidia and so on.
Retrieving stock history
Now we are going to discuss, how can we get historical data with stock prices.
Note, that in further cases, necessary data will be retrieved as a DataFrame object, described into previous chapter.
In case we want to get history of stock prices for a whole period available, we need to use history
method:
import yfinance as yf GetAmazonInformation = yf.Ticker("AMZN") # Getting stock data for the whole available period df=GetAmazonInformation.history(period="max") print(df)
Meanwhile, if we want to get data for a specific period, like 3 month, we should respectively set value of parameter period
:
import yfinance as yf GetAmazonInformation = yf.Ticker("AMZN") # Getting stock data for last 3 month df=GetAmazonInformation.history(period="3mo") print(df)
Other valid values of period
parameter: '1d'
, '5d'
, '1mo'
, '3mo'
, '6mo'
, '1y'
, '2y'
, '5y'
, '10y'
and 'ytd'
(which stands for 'Year to Date', and practically means period from the beginning of the year to the last date available).
Lastly, if we want to get data for a period with specific starting date and ending date, we need to change parameters start
and end
respectively:
import datetime import yfinance as yf GetAmazonInformation = yf.Ticker("AMZN") # Getting stock data for the period from 11th July of 2023 to the 11th July of 2024 df=GetAmazonInformation.history(start = datetime.datetime(2023, 7, 11), end = datetime.datetime(2024, 7, 11)) print(df)
Swipe to show code editor
In this task you need to:
- Get financial data
GetNvidiaInformation
for Nvidia company; - Get DataFrame
df
with the history of it's stock prices for the period from the beginning of the current year, till the last available date.
Obrigado pelo seu feedback!