Adventures in Machine Learning

Tracking Securities: Creating Custom Candlestick Charts in Python

Creating a Candlestick Chart in Python: Tracking the Rise and Fall of Securities

Visualizing stock prices is a crucial aspect of securities analysis, and one such way is through candlestick charts. These charts offer a vital visual representation of stock price movements within a given period.

Python, an immensely popular programming language, is well-suited to creating candlestick charts. By following some straightforward steps, we can create customized charts that are specific to our needs.

In this article, we will take you through these steps and present additional resources to help you expand your Python data visualization skills.

Data Preparation

Before creating any visualization, we must collect and organize the necessary data. We will use a pandas DataFrame containing the prices of a particular stock over a given time period.

The DataFrame must contain columns of information, including the date, open price, close price, high, and low prices of the stock in question.

Candlestick Chart Creation

With our data prepared, we can now create the candlestick charts. We will use Matplotlib, one of Python’s most popular plotting libraries, to create our charts.

Matplotlib allows us to customize the charts in many ways, and it is a perfect starting point for those who want to delve into data visualization with Python programming. To create a candlestick chart, we need to initialize a subplot instance that will contain our candlestick elements.

The following code example shows how to create an empty subplot and generate a candlestick chart using Matplotlib:


import matplotlib.pyplot as plt

from mpl_finance import candlestick_ohlc

fig, ax = plt.subplots()


candlestick_ohlc(ax, data, width=0.4)


Once we have generated the matplotlib figure, we call candlestick_ohlc function and pass onto our ax subplot the necessary DataFrame columns to create our candlestick chart. We can customize several elements of the candlesticks, including their widths, up and down days, colors, and up and down prices.

Customizing the Candlestick Chart

Now that we have a basic understanding of creating the candlestick chart and element customization lets consider some subtle tweaks to make the chart visually appealing and understandable.

One such way is to add colors to our charts.

By making each day’s movement’s color-specific, green for bullish and red for bearish, we convey additional information that makes our candlestick chart more impactful. Additionally, adjusting widths and adding annotations makes the chart more accessible to a broader audience.

Let’s try to modify our previous example by adding colors and annotations:


candlestick_ohlc(ax, data, width=0.6, colorup=’g’, colordown=’r’)

# Formatting




plt.title(“Stock Prices”)


ax.annotate(‘Up day’, xy=(x, y), xytext=(x, y+30),

arrowprops=dict(facecolor=’g’, shrink=0.05))

ax.annotate(‘Down day’, xy=(x, y), xytext=(x, y-30),

arrowprops=dict(facecolor=’r’, shrink=0.05))


You would call on the `candlestick_ohlc` method and pass on the dataframe and define a width and colors for the charts. We also need to annotate and add extra context, which is done using the `annotate()` method of Matplotlib.

Arrow props allow the arrows to stand out and be noticeable.

Additional Resources

Learning to create custom candlestick charts is an essential skill for any serious securities analyst. Still, Python data visualization offers many other charts and methods to explore and present many more insights into your data beyond these basics.

You should explore tutorials that cover these common visualization techniques and begin to incorporate them into your preferred workflows.


In conclusion, Python is an incredibly expressive and capable data visualization environment, capable of creating sophisticated candlestick charts and other charts for securities analysis with relative ease. We hope this article clarifies the key steps for creating these charts, inspires you to take your data visualization to the next level, and helps you find the resources you need to continue your learning journey.

In this article, we learned how to create candlestick charts in Python, a crucial element of securities analysis. By using Matplotlib and a pandas DataFrame containing the stock prices, we can create customized charts that convey information about price movements.

We covered the necessary data preparation steps, creating the charts, and customizing their elements and colors to make them visually appealing. We also provided additional resources to help expand our knowledge of Python data visualization and charts to use beyond candlesticks.

Python is a powerful tool that, when combined with other data analysis techniques, will provide you with valuable insights and help you make decisions.

Popular Posts