Member-only story

Leveraging Yahoo Finance And Python to Get Any Stock Data

A brief introduction to the yfinance API

Pol Marin
3 min readDec 30, 2022

--

5 stacks of 1, 2, 5, 10, and 20-cent coinss respectively.
Photo by Ibrahim Rifath on Unsplash

As someone interested in using Python to make money, it was about time that I started getting into the world of trading. I’m just embarking on it and nothing cool has already been done, but the first natural step is marked as done in my to-do list: get the data.

This post is about how anyone can use Python and yfinance (Yahoo Finance’s API) to download the data they need.

As always, the first thing we need to do is to install the library. To do so, we can count on our beloved friend pip:

pip install yfinance

We can now start playing with it. You have the full documentation here, but I’m going to provide a brief intro here.

To make it a real example, let’s suppose we want to download all the data from the Tesla stock (TSLA) between 2022–01–01 and 2022–12–01.

  1. Import the libraries: I’d also import pandas, but yfinance already can return DataFrame objects. Cool huh? I’ve added other imports that will be relevant later.
import yfinance as yf
from datetime import datetime

2. Retrieve the data: there is a download function that should work straightforwardly for our case, but it seems…

--

--

Pol Marin
Pol Marin

Written by Pol Marin

Data Scientist @FCBarcelona | Naturally inclined towards sports analytics and health/nutrition - Find me on LinkedIn: linkedin.com/in/polmarin/

Responses (1)