I have plotted the candlestick using bokeh. Now i want to plot the volume candles in the same chart? How do I achieve that. I am reading the data from csv which has open,high,low,close and volume.
Asked
Active
Viewed 2,806 times
1 Answers
1
This is an extension of the example of the offical documentation for a candlestick.
If you don't have the data yet, run this snippet first.
import bokeh
bokeh.sampledata.download()
If the data is available, you can create the candlestick like before. With the gridplot function you join the first figure with seconde one.
from math import pi
import pandas as pd
from bokeh.plotting import figure, output_notebook, show
from bokeh.layouts import gridplot
from bokeh.sampledata.stocks import MSFT
output_notebook()
df = pd.DataFrame(MSFT)[:50]
df["date"] = pd.to_datetime(df["date"])
inc = df.close > df.open
dec = df.open > df.close
w = 12*60*60*1000 # half day in ms
TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
p1 = figure(x_axis_type="datetime", tools=TOOLS, plot_width=700, plot_height=300, title = "MSFT Candlestick with Volume")
p1.xaxis.visible = False
p1.xaxis.major_label_orientation = pi/4
p1.grid.grid_line_alpha=0.3
p1.segment(df.date, df.high, df.date, df.low, color="black")
p1.vbar(df.date[inc], w, df.open[inc], df.close[inc], fill_color="#D5E1DD", line_color="black")
p1.vbar(df.date[dec], w, df.open[dec], df.close[dec], fill_color="#F2583E", line_color="black")
p2 = figure(x_axis_type="datetime", tools="", toolbar_location=None, plot_width=700, plot_height=200, x_range=p1.x_range)
p2.xaxis.major_label_orientation = pi/4
p2.grid.grid_line_alpha=0.3
p2.vbar(df.date, w, df.volume, [0]*df.shape[0])
show(gridplot([[p1],[p2]]))
To change the look a bit the x-axis of figure 1 is invisible using p1.xaxis.visible = False
and there is no title defined for figure 2.
To link the two figures together, the x_range of figure 1 is passed to figure two unsing x_range=p1.x_range
.
The output is this:

mosc9575
- 5,618
- 2
- 9
- 32
-
How would you overlay the volume on top of the price chart, similar to the way TradingView does it? – Jase Jul 07 '21 at 09:55
-
Does this [post](https://stackoverflow.com/questions/66232643/plotting-candlesticks-and-volume-bars-in-bokeh-on-the-same-figure/66247198#66247198) answer your question? – mosc9575 Jul 07 '21 at 14:43