Analyzing Financial Data with Python: A Guide to Yfinance and Plotly Candlestick Charts

Saurabh Nakoti
3 min readMay 13, 2023

--

Introduction:

Welcome to this blog post, where we embark on an exciting journey into the realm of charting with Plotly in Python and the fascinating world of financial data analysis. As a Python developer with a strong interest in finance, I have discovered the immense value of leveraging Plotly’s powerful charting capabilities to analyze and visualize financial data.

Throughout this blog post, we will dive into various techniques and tools that enable us to chart and analyze financial data effectively. We will explore the foundations of Plotly’s charting capabilities and how to create stunning visualizations, with a specific focus on candlestick charts — an indispensable tool in the financial analysis toolkit.

To begin, we will utilize the following libraries:
1. Yfinance: Yfinance is a powerful Python library that enables us to access and analyze historical market data from Yahoo Finance. It simplifies the process of retrieving financial information, such as stock prices, volumes, dividends, and more.

2. Plotly: Plotly is a widely-used data visualization library in Python that provides interactive and visually appealing charting capabilities. Its flexibility allows us to create dynamic and interactive charts, making it ideal for presenting financial data.

As an example, let’s retrieve and visualize the one-month historical data of AAPL (Apple Inc.) using Yfinance and Plotly. This will allow us to gain insights into the price movement and trends of this popular stock.

To begin, we need to install two essential libraries: yfinance and plotly

import yfinance as yf
import plotly.graph_objects as go

# Retrieve AAPL historical data
symbol = "AAPL"
ticker = yf.Ticker(symbol)
data = ticker.history(period="1mo")

# Create candlestick chart
fig = go.Figure(data=[go.Candlestick(x=data.index,
open=data['Open'],
high=data['High'],
low=data['Low'],
close=data['Close'])])

# Customize the chart layout
fig.update_layout(title=f"{symbol} Candlestick Chart (1 Month)",
yaxis_title="Price",
xaxis_rangeslider_visible=False)

# Display the chart
fig.show()

This code snippet demonstrates how to retrieve the historical data of AAPL (Apple Inc.) stock and create a candlestick chart using the yfinance and plotly.graph_objects libraries in Python.

  1. We begin by importing the necessary libraries: yfinance as yf and plotly.graph_objects as go.
  2. We specify the stock symbol we are interested in, which in this case is “AAPL”.
  3. Using the yf.Ticker() function, we create a Ticker object for the specified symbol.
  4. We retrieve the historical data for the past month using the history() method of the Ticker object. The fetched data is stored in the data variable.
  5. Next, we create a Figure object from the plotly.graph_objects library, specifically a candlestick chart. We pass in the necessary data attributes, such as the index (date), open, high, low, and close prices.
  6. To customize the chart layout, we use the update_layout() method. Here, we set the chart's title to "AAPL Candlestick Chart (1 Month)" and label the y-axis as "Price". Additionally, we hide the range slider on the x-axis.
  7. Finally, we display the candlestick chart using the show() method of the Figure object.

After executing the code, we can see the resulting candlestick chart for AAPL stock.

I hope you found this tutorial helpful and gained a deeper understanding of how to utilize Python for financial data analysis. Thank you for your interest in reading my story on Medium!

Happy coding and happy analyzing!

--

--

Saurabh Nakoti

Experienced Python developer focused on creating applications. Proficient in Django, Flask, FastAPI, MongoDB, AWS, Linux, SQL.