Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How can Bokeh be used to generate candle stick plot in Python?
Bokeh is a Python package that helps in data visualization. It is an open source project. Bokeh renders its plots using HTML and JavaScript, making it useful for web-based dashboards.
Bokeh can be easily used in conjunction with NumPy, Pandas, and other Python packages. It can be used to produce interactive plots, dashboards, and candlestick charts for financial data visualization.
Installation
Install Bokeh using pip:
pip3 install bokeh
Or using Anaconda:
conda install bokeh
Creating a Candlestick Plot
A candlestick plot shows the open, high, low, and close prices of a stock. Green candles represent days when the closing price was higher than the opening price, while red candles represent declining days.
from math import pi
import pandas as pd
from bokeh.plotting import figure, output_file, show
from bokeh.sampledata.stocks import MSFT
# Load and prepare data
my_df = pd.DataFrame(MSFT)[:35]
my_df["date"] = pd.to_datetime(my_df["date"])
# Identify increasing and decreasing days
inc = my_df.close > my_df.open
dec = my_df.open > my_df.close
w = 12*60*60*1000 # Width of bars in milliseconds
# Define tools for interaction
TOOLS = "pan,wheel_zoom,box_zoom,reset,save"
# Create figure
p = figure(x_axis_type="datetime", tools=TOOLS, plot_width=1000,
title="Candlestick using MSFT data")
p.xaxis.major_label_orientation = pi/4
p.grid.grid_line_alpha = 0.3
# Add high-low lines
p.segment(my_df.date, my_df.high, my_df.date, my_df.low, color="black")
# Add bars for increasing days (green)
p.vbar(my_df.date[inc], w, my_df.open[inc], my_df.close[inc],
fill_color="#D5E1DD", line_color="black")
# Add bars for decreasing days (red)
p.vbar(my_df.date[dec], w, my_df.open[dec], my_df.close[dec],
fill_color="#F2583E", line_color="black")
# Output to HTML file
output_file("candlestick.html", title="candlestick plot")
# Display plot
show(p)
Note: To access Bokeh's sample datasets, run this command first:
bokeh sampledata download
Output
How It Works
The candlestick plot uses two main components:
- Segments: Vertical lines showing the high and low prices for each day
- Vertical bars (vbar): Rectangles showing the open and close prices, colored based on price direction
Key Parameters
| Parameter | Purpose | Example Value |
|---|---|---|
x_axis_type |
Sets axis type for dates | "datetime" |
w |
Width of candlestick bars | 12*60*60*1000 (12 hours in ms) |
fill_color |
Bar color | "#D5E1DD" (green), "#F2583E" (red) |
TOOLS |
Interactive tools | "pan,wheel_zoom,box_zoom,reset,save" |
Conclusion
Bokeh makes it easy to create interactive candlestick charts for financial data visualization. The combination of segments for high-low ranges and colored bars for open-close prices provides a comprehensive view of stock price movements.
