Download data from Yahoo Finance and make OHLC chart — Python

Matjaž Hozjan
2 min readSep 27, 2021

--

Searching Upwork for job offer is my new hobby as it looks like. In case that you are suffering with lack of ideas what to do, build, make just go there and make some research and after a few minutes I can guarantee you will have more than one idea for realise.

Photo by Carlos Muza on Unsplash

I notice ad asking someone to make periodic downloads from yahoo finance and later send to Excel. Offering some crazy money for that task and it wasn’t only one related to Yahoo Finance. I didn’t apply for any of those works instead I took that as an opportunity to make this article and training my Python skills.

End Result is that after a few lines of code I made an OHLC chart with OIL data all downloaded from Yahoo for FREE.

YFinance is powerful package with a lot of options for backtesting, researching and making custom indicator or even trading strategy based on it.

For displaying chart at this time am using plotly with built in candlestick As for download data cames PERFECTly named. After installing and importing YFinance just one line of code is what we need to get history data in OHLC format from Yahoo. As seen at below and my chosen symbol is Crude OIL.

import yfinance as yf


# Get symbol OHLC data
data = yf.download("CL=F")
yfinance, python, import, OHLC

Next is time for generate OHLC bar chart. For some reason reset_index() is needed at our import from yahoo. Than is just straight forward Chart Figure generation.

import plotly.graph_objects as chart

# For some reason Index reset is a must.
data = data.reset_index()

fig = chart.Figure(data=[chart.Candlestick(x=data['Date'],
open=data['Open'],
high=data['High'],
low=data['Low'],
close=data['Close'])])
fig.show()
Plotly, Python, OHLC, yahoo Finance, YFinance

Conclusion

YFinance is an absolutely powerful library that am going to use it more often. And before making any crazy payments for some API’s that are too costly, consider using these and once you master it and more data is what you need or more up to date one’s go next level.

--

--

Matjaž Hozjan
Matjaž Hozjan

Written by Matjaž Hozjan

FX, stock trader and full time DataScientist at SportRadar AG

No responses yet