Chapter 3 – Accessing Price Data of Financial Assets

share on:

How to access historical price data of stocks, bonds, etc.? One method is to download the data from Yahoo! Finance manually, another method is to use Python package called yfinance to download more efficiently.

Content

  1. Download Price Data into CSV text File From Yahoo! Finance
  2. Quizzes and Answers
    1. Quiz 1
    2. Quiz 2
    3. Quiz 3

Access Price Data From Yahoo! Finance

Download Price Data into CSV text File From Yahoo! Finance


Quizzes

Quiz 1: Download the price data for Bitcoin (US Dollar) between January 1st, 2015 and October 5th, 2022. If you purchased one Bitcoin on January 1st, 2015, how much is your Bitcoin worth on October 5th, 2022? Find the answer using features of pandas DataFrame.

>>> BTC = yfinance.download(["BTC-USD"], start="2015-01-01")["Adj Close"]
[*********************100%***********************]  1 of 1 completed
>>> BTC.head()
Date
2015-01-01    314.248993
2015-01-02    315.032013
2015-01-03    281.082001
2015-01-04    264.195007
2015-01-05    274.473999
Name: Adj Close, dtype: float64
>>> BTC.tail()
Date
2022-10-01    19312.095703
2022-10-02    19044.107422
2022-10-03    19623.580078
2022-10-04    20336.843750
2022-10-05    20160.716797
Name: Adj Close, dtype: float64
>>> 
>>> BTC["2015-01-01"]
314.2489929199219
>>> 
>>> BTC["2022-10-05"]
20160.716796875

Quiz 2: If you spent $100 on January 1st, 2015 to buy some Bitcoin, how much fraction of a Bitcoin would you be able to buy? How much money was that $100 investment worth on October 5th, 2022?

>>> 100 / BTC["2015-01-01"]
0.31821899911540014

>>> BTC["2022-10-05"] * 0.318219
6415.523138384765

Quiz 3: Use the instructions above to download the price data of Apple stock in to a file saved on your computer, and then use Python package pandas to read that file.

>>> import pandas as pd
>>> 
>>> d1 =pd.read_csv("C:\\Users\\User Accout Name\\Downloads\\AAPL.csv", header=0, index_col=0, parse_dates=["Date"])
>>> d1.head()
                  Open        High  ...   Adj Close    Volume
Date                                ...                      
2021-10-06  139.470001  142.149994  ...  141.208725  83221100
2021-10-07  143.059998  144.220001  ...  142.491531  61732700
2021-10-08  144.029999  144.179993  ...  142.103699  58773200

Leave a Response