Ritvik Kharkar
How to Get Stock Data Using Python

Python one-liners for getting stock price data and company data

Photo by Sharon McCutcheon on Unsplash

I’ve been pretty interested in stock data lately; seeing if I can pick a carefully chosen selection of stocks capable of giving modest returns in the medium term. I’m hoping to write a whole article on the strategy I came up with, but first, I wanted to share how to even get stock price data in the first place so you can conduct similar analyses for yourself!

It turns out that using the very convenient Python library called yfinance, this task becomes really easy.

To motivate the problem, let’s say we’re interested in getting stock price data for Microsoft (stock ticker MSFT) for the last 10 years.

First, we’ll install the yfinance library:

  • Open up your Anaconda Prompt or other command prompt and type: pip install yfinance

Then, here is the basic code you need to collect historical stock data for Microsoft:

The key arguments here are:

  • period: the frequency at which to gather the data; common options would include ‘1d’ (daily), ‘1mo’ (monthly), ‘1y’ (yearly)
  • start: the date to start gathering the data. For example ‘2010–1–1’
  • end: the date to end gathering the data. For example ‘2020–1–25’

Your result should be a Pandas dataframe containing daily historical stock price data for Microsoft. Key fields include:

  • Open: the stock price at the beginning of that day/month/year
  • Close: the stock price at the end of that day/month/year
  • High: the highest price the stock achieved that day/month/year
  • Low: the lowest price the stock achieved that day/month/year
  • Volume: How many shares were traded that day/month/year

You can use the resulting dataframe to create charts like this:

Source link