Back to Community
Plot Candlestick Charts in Research

Since Quantopian doesn't support matplotlib.finance 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!

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

Loading notebook preview...
Notebook previews are currently unavailable.
12 responses

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

ex.

RSI = talib.RSI(last_hour['close_price'].as_matrix())  
plot_candles(last_hour,  
             title='1 minute candles + Bollinger Bands + RSI',  
             overlays=[upper, middle, lower],  
             technicals=[RSI],  
             technicals_titles=['RSI'])  
Loading notebook preview...
Notebook previews are currently unavailable.

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:

InputRejected:  
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?

Loading notebook preview...
Notebook previews are currently unavailable.

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  
matplotlib.dates  

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

Loading notebook preview...
Notebook previews are currently unavailable.

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
Loading notebook preview...
Notebook previews are currently unavailable.

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.

-JJ

Loading notebook preview...
Notebook previews are currently unavailable.

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. I **** python, I wish this were all JavaScript. Pandas is so confusing. You may take my examples and walk.

Loading notebook preview...
Notebook previews are currently unavailable.