Back to Community
Plot Candlestick Charts in Research

Since Quantopian doesn't support yet, I hacked out a candlestick chart function purely using pyplot. It makes a candlestick chart from the dataframe returned by get_pricing. I wrote this to use in my research notebooks and it has made me happier, and if you like looking at technical charts this is golden. Cheers!

Works with day or minute data
Optional volume bars
Customizable candle colors
Technical Indicators

Loading notebook preview...
14 responses

Update: Added support for indicators (overlays or supplemental subplots).


RSI = talib.RSI(last_hour['close_price'].as_matrix())  
             title='1 minute candles + Bollinger Bands + RSI',  
             overlays=[upper, middle, lower],  
Click to load notebook preview

This is really neat, and will be helpful for so many I'm sure. Thanks for sharing!

Awesome! I feel like you've given me back hours of my life. Now I can visualize some of the data as I build my backtests WITHOUT having to go to another platform.

I've been playing with this candlestick charting code of yours a little more, and I'm very impressed. It was easy to adopt into some studies of my own. I have a question though: have you tried formatting labels the x-axis? I'm working on some charts with 1-minute data, and currently it plots all of the timestamps along the x-axis. There is so much overlap that you cannot read the labels.

I tried formatting the timestamps and specifying major labels (ax1.set_major_locator & ax1.set_major_formatter), but I get an error when I try to import dates from matplotlib:

Importing dates from matplotlib raised an ImportError. No modules or attributes with a similar name were found.  

Evidently this is not one of the libraries that Quantopian supports. Have you played with this label formatting feature at all? Have you found an acceptable alternative that can clean up the chart a little bit?

Click to load notebook preview

Hi, great work!

Due to some reasons research won't let you import dates from matplotlib, but you can still use it by:

import matplotlib  

If fixed this issue in the attachted notebook as well as a missing .xaxis. Unfortunately one can't read any of the labels in a minute chart :D

Click to load notebook preview

Sebastian, thanks! import matplotlib and matplotlib.dates works great! I'd like to use this, however I could not figure out how to get locators to do what I want, so I sliced the index instead to get every other minute, every 5 minutes, etc... I may come back to this and try again. Also, for multiple-day pricing, do you know if we can get DayLocator to skip weekends?

Even with limited x labels, minute data is still unreadable, so to view a day or more of intraday history I recommend aggregating into 2,5, or 15-minute bars. Example included.

Here is my latest, including:

  • More readable dates and times by default
  • max_x_ticks parameter to limit the number of x axis major ticks/labels
  • Filled / hollow bars in addition to color filled
  • Respects time zone when formatting intraday data
  • Added example aggregating price data into 5-minute bars
Click to load notebook preview

Thanks for sharing, that is fantastic ! And, a great example of how to use TA-LIB, Thank You.

Thank you SO, SO much! Fantastic work and a massive time saver!

Thank you Daniel. I have added code here so that you may also view custom timeframes: those other than minute or daily. Supported are: minute, hour, day, week, month, year. And you can specify how many. On display is the '15m'. This is a work in progress, so please feel free to critique the code especially for bad behavior.


Click to load notebook preview

JJ, thanks! The time bar aggregation functions are great, this is very helpful. Selecting the right time-date range and scale is a recurring pain point. Once I get some free time I'd like to try to get another update out that integrates all of these contributions.


Glad you like it. You may take my examples and walk.

Click to load notebook preview

@Daniel Treiman, thanks for sharing, did you were able to plot the first section of the day? the indicator plotting is not showing at the beginning of the chart :(

@Erick Gomez It depends on if you have data from before that time. I got around that by giving talib more data then what I wanted to plot then truncating the data outside the plot.

upper, middle, lower = talib.BBANDS(last_hour['close_price'].as_matrix())  
plot_candles(last_hour, title = '1 minute candles w/ Bollinger Bands', overlays = [upper, middle, lower])  


upper, middle, lower = talib.BBANDS(ym_minute_pricing['close_price'].as_matrix())  
plot_candles(last_hour, title = '1 minute candles w/ Bollinger Bands', overlays = [upper[-60:], middle[-60:], lower[-60:]])  
Click to load notebook preview