Candlestick Chart with Python

Candlestick Chart is a powerful way to visualize the trends and changes in the stock market and other financial instruments. Most people use a Candlestick chart to visualize the trading patterns. This article will take you through how you can create an interactive Candlestick chart using Python and Plotly.

To visualize our data in the form of Candlesticks, we must be having data that comprises open price, high price, low price, and close price. It is mainly used in financial analysis, so I will use the stock market data of Google of this year starting from January till 8 August.

Interactive Candlestick Chart with Python

Japanese commodity Traders created this technique to build this type of chart, and initially, they were known as the Japanese Candlesticks. I will not go in that much detail because our end goal is to visualize this chart using Python.

I will scrape the stock market data of Google from yahoo finance using the pandas_datareader package. If you don’t want to scrape the data and want to use a CSV file, you can also download it from yahoo finance. Now let’s import the necessary packages we need for this task:

import plotly.graph_objects as go import pandas_datareader as web
Code language: Python (python)

Now let’s scrape the data using pandas_datareader:

df = web.DataReader('GOOG', data_source='yahoo', start='2020-01-01', end='2020-08-08') print(df.head())
Code language: Python (python)
High Low … Volume Adj Close
Date …
2019-12-31 1338.000000 1329.084961 … 961800 1337.020020
2020-01-02 1368.140015 1341.550049 … 1406600 1367.369995
2020-01-03 1372.500000 1345.543945 … 1186400 1360.660034
2020-01-06 1396.500000 1350.000000 … 1732300 1394.209961
2020-01-07 1402.989990 1390.380005 … 1502700 1393.339966

As the data is collected from yahoo finance, there is no possibility of missing figures and any need for data cleaning. So, as we are trying to build an interactive chart, I will use the Plotly package for this task, so I will now prepare the data so that it could be quickly baked into a Candlestick Chart:

fig = go.Figure(data=[go.Candlestick(x=df.index, open=df['Open'], high=df['High'], low=df['Low'], close=df['Close'], increasing_line_color='red', decreasing_line_color = 'blue' )])
Code language: Python (python)
Candlestick chart

Also, Read – Stock Price Prediction using Facebook Prophet Model.

So, this is the most straightforward way to visualize the stock data into the form of candlesticks. I have only used the data of 2020; you can try it using a larger dataset. I hope you liked this article, feel free to ask your valuable questions in the comments section below. You can also follow me on Medium to learn every topic of Machine Learning.

Also, Read – What is Big Data?

Follow Us:

Default image
Aman Kharwal

Coder with the ♥️ of a Writer || Data Scientist | Solopreneur | Founder

Articles: 998

Leave a Reply