Plotting a candlestick chart with custom per candlestick individual coloring

4.6k views Asked by At

I would like to plot some candlestick data onto a chart. The problem i'm having is finding a library that allows me to specify the color of each candle on a per candle basis.

Alot of libraries allow me to set 2 colors, for the bullish/bearish candles.. but I want to actually specify the candle color per row.

eg:

Date,Open,High,Low,Close,Volume,Color

If anyone could point me in the direction of any library that allows me todo that. I would be most appreciative. I don't understand why it's so hard.

I have checked plot.ly, matplotlib, bokeh.. all with no luck (unless I am blind!)

I also posted here 9 days ago without any luck; https://community.plot.ly/t/individual-candlestick-colors/2959

1

There are 1 answers

3
bigreddot On BEST ANSWER

This is fairly trivial to accomplish with Bokeh. Bokeh's model for glyphs is very consistent: every visual property (including colors) can be vectorized. If you want all your vbar glyphs (that you would use to draw candlesticks with) to each have a different color, just pass a list or array of the different colors you want.

from math import pi

import pandas as pd

from bokeh.palettes import viridis
from bokeh.plotting import figure, show, output_file
from bokeh.sampledata.stocks import MSFT

df = pd.DataFrame(MSFT)[:50]
df["date"] = pd.to_datetime(df["date"])

# NOTE: list of colors one for each candlestick
df['colors'] = viridis(50) 

w = 12*60*60*1000 # half day in ms

TOOLS = "pan,wheel_zoom,box_zoom,reset,save"

p = figure(x_axis_type="datetime", tools=TOOLS, 
           plot_width=1000, title = "MSFT Candlestick")
p.xaxis.major_label_orientation = pi/4
p.grid.grid_line_alpha=0.3

p.segment(df.date, df.high, df.date, df.low, color="black")
p.vbar(df.date, w, df.open, df.close, fill_color=df.colors, line_color="black")

output_file("candlestick.html", title="candlestick.py example")

show(p)  # open a browser

enter image description here