Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Challenge: Uploading Financial Data | Python Basics
Introduction to Finance with Python
course content

Course Content

Introduction to Finance with Python

Introduction to Finance with Python

1. Python Basics
2. Options Trading
3. Time Series Forecasting

book
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 :

12345
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)
copy

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:

12345
import yfinance as yf GetAmazonInformation = yf.Ticker("AMZN") # Getting stock data for the whole available period df=GetAmazonInformation.history(period="max") print(df)
copy

Meanwhile, if we want to get data for a specific period, like 3 month, we should respectively set value of parameter period:

12345
import yfinance as yf GetAmazonInformation = yf.Ticker("AMZN") # Getting stock data for last 3 month df=GetAmazonInformation.history(period="3mo") print(df)
copy

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:

1234567
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)
copy
Task
test

Swipe to show code editor

In this task you need to:

  1. Get financial data GetNvidiaInformation for Nvidia company;
  2. 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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 6
toggle bottom row

book
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 :

12345
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)
copy

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:

12345
import yfinance as yf GetAmazonInformation = yf.Ticker("AMZN") # Getting stock data for the whole available period df=GetAmazonInformation.history(period="max") print(df)
copy

Meanwhile, if we want to get data for a specific period, like 3 month, we should respectively set value of parameter period:

12345
import yfinance as yf GetAmazonInformation = yf.Ticker("AMZN") # Getting stock data for last 3 month df=GetAmazonInformation.history(period="3mo") print(df)
copy

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:

1234567
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)
copy
Task
test

Swipe to show code editor

In this task you need to:

  1. Get financial data GetNvidiaInformation for Nvidia company;
  2. 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.

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 6
Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt