A candlestick chart, created using the Plotly library in Python, is a graphical representation of financial data. It displays price movements over a specific time period, typically used in stock market analysis. Candlestick charts are composed of individual 'candlesticks', each representing the opening, closing, high, and low prices for a given time interval. These candlesticks are displayed on a two-dimensional coordinate system, with one axis representing time and the other axis representing price.

Plotly has a Candlestick() function that provides the capability to create candlestick charts easily. In this post, we'll see how to add moving averages in candlestick charts with this library. You might first want to take a look at how to customize a candlestick chart (color, layout...) with plotly.

Libraries

First, we need to install the plotly:

pip install plotly

And since we'll load data from yahoo finance, we need the yfinance library:

pip install yfinance

We'll also need the pandas module to calculate the moving average.

import plotly.graph_objects as go
import yfinance as yf
import pandas as pd

Dataset

Candlestick charts are mainly used to represent financial data, especially stock prices.

In this post, we'll load Apple's share price data, directly from our Python code via the yfinance library. All we need to do is define the desired start and end data (yyyy-mm-dd format), and the ticker or symbol associated with this company (in this case "AAPL").

Our dataset must have the characteristics needed to produce our graph easily:

  • be a pandas dataframe
  • a date index
  • an Open column
  • a High column
  • a Low column
  • a Close column

The tickers can be found on easily on yahoo finance.

# Define the stock symbol and date range
stock_symbol = "^FCHI"  # Example: CAC40 (largest French companies)
start_date = "2020-01-01"
end_date = "2020-03-30"

# Load historical data
stock_data = yf.download(stock_symbol,
                         start=start_date, end=end_date)
[*********************100%%**********************] 1 of 1 completed

Once we have our dataset, we just need to calculate the moving averages. Thanks to the rolling() function from pandas, it's actually pretty easy:

# Moving average over a window of 3 and 8
stock_data['moving3'] = stock_data['Close'].rolling(3).mean()
stock_data['moving8'] = stock_data['Close'].rolling(8).mean()

Candlestick with one moving average

Once we've opened our dataset, we'll now create the graph. We have to specify which column to use for opening, closing etc.

To add the moving average, all we need to do is add an Scatter to the data argument, and specify the values on the x-axis (in this case, time) and those on the y-axis (the moving average calculated previously).

fig = go.Figure(data=[go.Candlestick(
                
                x = stock_data.index, # date values
                open = stock_data['Open'],
                high = stock_data['High'],
                low = stock_data['Low'],
                close = stock_data['Close']),
                      
                      # Add the moving average
                      go.Scatter(x=stock_data['moving3'].index,
                                 y=stock_data['moving3'])
                     ])

# Mask a default range slider
fig.update_layout(xaxis_rangeslider_visible=False)

# Set layout size
fig.update_layout(
    autosize=False,
    width=700,
    height=500,
    showlegend=False)

Now, let's save the graph in a HTML file and display it in this website using an iframe

# save this file as a standalong html file:
fig.write_html("../../static/interactiveCharts/candlestick-plotly-moving-average.html")
%%html
<iframe 
    src="../../interactiveCharts/candlestick-plotly-moving-average.html" 
    width="800" 
    height="600" 
    title="candlestick with moving average" 
    style="border:none">
</iframe>

Multiple moving averages

You can easily add as many moving averages as you want by adding arguments when creating the figure. In our case, we'll add the 8-day moving average.

fig = go.Figure(data=[go.Candlestick(
                
                x = stock_data.index, # date values
                open = stock_data['Open'],
                high = stock_data['High'],
                low = stock_data['Low'],
                close = stock_data['Close']),
                      
                      # Add the moving average
                      go.Scatter(x=stock_data['moving3'].index,
                                 y=stock_data['moving3']),
                      go.Scatter(x=stock_data['moving8'].index,
                                 y=stock_data['moving8'])
                     ])

# Mask a default range slider
fig.update_layout(xaxis_rangeslider_visible=False)

# Set layout size
fig.update_layout(
    autosize=False,
    width=700,
    height=500,
    showlegend=False)

Now, let's save the graph in a HTML file and display it in this website using an iframe

# save this file as a standalong html file:
fig.write_html("../../static/interactiveCharts/candlestick-plotly-moving-average-2.html")
%%html
<iframe 
    src="../../interactiveCharts/candlestick-plotly-moving-average-2.html" 
    width="800" 
    height="600" 
    title="candlestick with moving average 2" 
    style="border:none">
</iframe>

Custom style of the moving averages

To make our graph easier to read and more informative, we can change the colors of the bars and moving averages.

  • change color: add a line argument (dictionnary) with the properties wanted (color, width and shape)
  • add legend: add a name argument with the text we want
fig = go.Figure(data=[go.Candlestick(
                
                x = stock_data.index, # date values
                open = stock_data['Open'],
                high = stock_data['High'],
                low = stock_data['Low'],
                close = stock_data['Close'],
                
                # color or bars
                increasing_line_color = 'limegreen',
                decreasing_line_color = 'orangered',
                showlegend=False), 
                      
                      # Add the moving average
                      go.Scatter(x=stock_data['moving3'].index,
                                 y=stock_data['moving3'],
                                 line=dict(color='gray',
                                           width=1,
                                           shape='spline'), # smooth the line
                                 name='MA3'),
                      
                      go.Scatter(x=stock_data['moving8'].index,
                                 y=stock_data['moving8'],
                                 line=dict(color='black',
                                           width=1,
                                           shape='spline'), # smooth the line
                                 name='MA8')
                     ])

# Mask a default range slider
fig.update_layout(xaxis_rangeslider_visible=False)

# Set layout size
fig.update_layout(
    autosize=False,
    width=600,
    height=500,
    legend=dict(
        x=0.82,  # Adjust the legend's x position
        y=0.98,  # Adjust the legend's y position
        font=dict(size=12)  # Customize font size
    )
)

Now, let's save the graph in a HTML file and display it in this website using an iframe

# save this file as a standalong html file:
fig.write_html("../../static/interactiveCharts/candlestick-plotly-moving-average-3.html")
%%html
<iframe 
    src="../../interactiveCharts/candlestick-plotly-moving-average-3.html" 
    width="800" 
    height="600" 
    title="candlestick with moving average 3" 
    style="border:none">
</iframe>

Going further

This post explains how to create a candlestick chart with plotly.

For more examples of how to create or customize your candlestick plots, see the candlestick section. You may also be interested in how to custom style of a plotly candlestick chart.

Timeseries

Contact & Edit


👋 This document is a work by Yan Holtz. You can contribute on github, send me a feedback on twitter or subscribe to the newsletter to know when new examples are published! 🔥

This page is just a jupyter notebook, you can edit it here. Please help me making this website better 🙏!