Grab Finance Price Data Using Python

Thomas
3 min readJan 1, 2021

--

If you are new to Python, interested in financial markets and wish to get started building your own tools/programs this will be for you.

We will be using Python and more specifically Pandas (data analysis package). Further information on Pandas can be found here if you are unfamiliar.

We will use ‘yfinance’ as the API to access daily closing prices for a handful of asset classes. What you wish to do with the data is completely up to you. You can plot it using Matplotlib (graph based package), sort it, analyse it, sync it with alerts, write it to Excel etc.

An alternative API that is also very useful is ‘AlphaVantage’.

The initial step is to install the necessary packages using pip in the CMD line. We will need pandas, yfinance and datetime (may already be installed).

pip install pandas
pip install yfinance

In the text editor, let’s now import the dependencies.

import pandas as pd
import yfinance as yf
from datetime import date, timedelta

Timedelta refers to differences in dates. In an example below we will use it to define specific time clamps.

First we’ll define an object that creates a pandas dataframe (neatly holds all the data in rows/columns). The download from ‘yf’, time periods and specific data we wish to extract will also be included in this.

It’s one big one-liner.

The example will be for Tesla (‘TSLA’). Because that’s the obvious example nowadays. Market-cap at the time of writing being $627bn. It might hit the big ‘T’ when you’re reading this.

When writing the ‘yf.download’ piece, you’ll see the auto-suggest include ‘tickers’, ‘start’, ‘end’, ‘actions’ etc.

TESLA = pd.DataFrame(yf.download('TSLA', start='2020-01-01', end='2020-12-30'))

print(TESLA)

The ticker (TSLA) needs to be exact to prevent any errors. You would use datetime/timedelta if you wish to create a program where you can execute it on any given day and the start/finish parameters remain dynamic (i.e. creating a dataframe that immediately provides you with data up to yesterday).

Below, we’ll create a similar dataframe but use objects to enable us to view the data for 2020.

Start = date.today() - timedelta(365)          # 1yr ago today
Start.strftime('%Y-%m-%d') # For yfinance format

End = date.today() + timedelta(2)
End.strftime('%Y-%m-%d')

TESLA = pd.DataFrame(yf.download('TSLA', start=Start, end=End))

print(TESLA)

Stf-time will format the datetime object into the correct string for use in yfinance (code — ‘%Y-%m-%d’). In both cases you should see a similar data-frame to below. Not all data will show when in the console.

Open High … Adj Close Volume
Date …
2020–01–02 84.900002 86.139999 … 86.052002 47660500
2020–01–03 88.099998 90.800003 … 88.601997 88892500
2020–01–06 88.094002 90.311996 … 90.307999 50665000
2020–01–07 92.279999 94.325996 … 93.811996 89410500
2020–01–08 94.739998 99.697998 … 98.428001 155721500
… … … … … …
2020–12–23 632.200012 651.500000 … 645.979980 33173000
2020–12–24 642.989990 666.090027 … 661.770020 22865600
2020–12–28 674.510010 681.400024 … 663.690002 32278600
2020–12–29 661.000000 669.900024 … 665.989990 22910800
2020–12–30 672.000000 696.599976 … 694.780029 42646800
[252 rows x 6 columns]

Yes you read that correctly. It was below $90 in January 2020.

This standard dataframe contains open, high, close and volume data etc. Let’s just use the ‘Adj Close’. Just add this to the end of the code.

TESLA = pd.DataFrame(yf.download('TSLA', start=Start, end=End)['Adj Close'])print(TESLA)--------------------------------------------------------------------Date                  
2020-01-02 86.052002
2020-01-03 88.601997
2020-01-06 90.307999
2020-01-07 93.811996
2020-01-08 98.428001
... ...
2020-12-23 645.979980
2020-12-24 661.770020
2020-12-28 663.690002
2020-12-29 665.989990
2020-12-30 694.780029
[252 rows x 1 columns]

86 → 694. Wow.

In terms of the initial step of accessing price data, that’s it. If we wish to duplicate the process for a variety of tickers and want to streamline the code to avoid excessive replication we can create a simple function.

def Closing_Price(ticker):
Asset = pd.DataFrame(yf.download(ticker, start=Start,
end=End)['Adj Close'])
return Asset

TESLA = Closing_Price('TSLA')
AMAZON = Closing_Price('AMZN')
JPMORGAN = Closing_Price('JPM')
# Then simply print/manipulate the given object

As you can see there is not much code to contend with to start playing with financial market data solely within a text editor.

To write this data to excel and plot graphs using Matplotlib stay tuned for more articles.

--

--

Thomas
Thomas

No responses yet