Pandas candlestick plot of BTC

Beginner's Guide To Using Pandas For Bitcoin Analysis: Part 3 - Candlesticks

By Michael_Brown | Python For Crypto | 4 Feb 2020

Hi folks,

In this next tutorial, I am excited to say we'll be going over how to make a candlestick plot of our price data!

We'll cover:

  • What is a candlestick chart
  • How to select a sub-section of our price data
  • How to create a candlestick plot of the selected data

What Is A Candlestick Chart

Originally developed in Japan, candlestick charting is a means to display the movement of stock prices visually. If you don't know what a candlestick chart looks like, here is an example:

Anatomy of a candlestick plot

A candlestick consists of the following components:

  • Opening price
  • Closing price
  • Highest price
  • Lowest price
  • Colour component (green or red)

If a candlestick is green, it means the closing price was higher than the opening price (stock went up in value). If it is coloured red, then the closing price was lower than the opening price (stock went down in value).

What is interesting is the patterns that the bodies (Open and Close) and the wicks (Highest and Lowest) prices make. Entire books have been written on candlestick patterns and analysis, and as I'm not an expert on them, I'll probably read one soon.

The main idea of plotting candlesticks is that specific patterns repeat or indicate stock movement, if you spot the trend early you can make a trade with greater confidence and accuracy, which should equate to more money in the long run.

With that brief explanation, let's look at the code to your own candlestick chart.


How To Select A Subsection Of Our Price Data

As usual, we'll read in our BTC data and create a pandas dataframe:

# Folder name of the directory where the data is stored
folder_name = 'Bitcoin_Historical_Data'

# Name of the csv file to load into a pandas dataframe
btc_daily = 'Coinbase_BTCUSD_d.csv'

# Join various path components
btc_daily_path = os.path.join(folder_name, btc_daily)

# Read in csv file data into pandas dataframe
btc_daily_df = pd.read_csv(btc_daily_path, skiprows=1)

# Convert the 'Date' column to datetime format
btc_daily_df['Date'] = pd.to_datetime(btc_daily_df['Date'])

# Set the 'Date' column as the index
btc_daily_df = btc_daily_df.set_index('Date')

Currently, we have all our price data in the dataframe, however, oftentimes we'll want to explore a subsection and to do that we can use a mask to filter the data.

Image of a mask: Photo by Adnan Khan on Unsplash

A mask is simply a set of logical constraints for each row that evaluates as true or false, and when the mask is applied to the dataframe, only those rows that are true are returned.

Don't worry if that doesn't make a great deal of sense now, seeing it done will explain everything. Basically, I'm going to ask whether the date in the index column is between a start date and end date, and only the rows for which this is true will be returned:

# Create start and end dates using pd.to_datetime
start_date = datetime(day=1, month=12, year=2019)
end_date = datetime(day=31, month=12, year=2019)

# Create a mask based upon the start and end date
mask = (btc_daily_df.index >= start_date) & (btc_daily_df.index <= end_date)

Looking at the mask:

boolean mask

we see it is simply a series of true or false statements, and when this is applied to our dataframe, a new dataframe can be created consisting of only the rows where a true value in the mask was present:

# Apply the mask to select the specific subset of our data
last_month = btc_daily_df[mask]

# Plot our last_month dataframe to see if contains only the data we want
last_month.plot(y=['Open', 'Close', 'High', 'Low'],
title="BTC Prices Between \n" + str(start_date) + "-" + str(end_date))

All BTC prices plotted from last month of 2019

How To Create A Candlestick Plot

Awesome, we now have all of the price data from the last month of 2019 in a dataframe, though looking at the plot above it's quite hard to discern any noticeable patterns right away with all the lines overlapping. To reduce this visual noise we can convert the line plot into a candlestick plot:

# We've also reset the index, which has moved the 'Date' column back into the dataframe, as we will need these for the charting function.
last_month = last_month.reset_index()

import plotly.graph_objects as go

fig = go.Figure(data=[go.Candlestick(x=last_month['Date'],

Candlestick plot of BTC prices for last month of 2019

And there we have it, a beautiful candlestick plot. All I have to do now is learn how to interpret a candlestick plot and I will be able to divine the future stock movements and become a millionaire. Okay, maybe it's not that easy, but at least I can make pretty plots until I read up on candlestick charting.

As always, I hope you've enjoyed this quick tutorial and can begin applying candlestick charting to your own data analysis.

How do you rate this article?



Physics graduate, with a passion for programming in Python.

Python For Crypto
Python For Crypto

I'm Michael, a Python developer and I'm documenting some useful libraries and techniques you can use for free to aid in your Crypto-analysis and Crypto-portfolio.

Send a $0.01 microtip in crypto to the author, and earn yourself as you read!

20% to author / 80% to me.
We pay the tips from our rewards pool.