Bokeh - Data Visualization Library

Bokeh - Data Visualization Library

Bokeh is a powerful and interactive data visualization library for Python. Its name, derived from the Japanese term 'bokeh', refers to the aesthetic quality of out-of-focus blur in photography. In the context of the library, it symbolizes the focus on interactive visual experiences. Unlike many other visualization libraries, which focus primarily on static graphics, Bokeh is designed to seamlessly render its visualizations in modern web browsers, making it perfect for creating interactive plots and dashboards. A defining feature of Bokeh is its ability to connect rich visualizations directly to data sources, updating plots in real-time without the need to refresh the entire webpage. Whether you're plotting millions of data points from a live stream or building an interactive dashboard for business analytics, Bokeh handles it with finesse. Bokeh's architecture allows users to create visualizations at various levels of abstraction. From simple one-liner plots to intricate, multi-layered dashboards with widgets, Bokeh is versatile enough to cater to both beginners and advanced users. With seamless integration in Jupyter notebooks and compatibility with popular data science libraries like Pandas, Bokeh has established itself as a go-to tool in the data visualization space. Whether you're a data scientist seeking to visualize complex datasets, a business analyst aiming to build dynamic dashboards, or a developer crafting a web application, Bokeh offers the tools and flexibility to bring your data to life. 

 

Candlestick chart

A candlestick chart is a financial chart used to describe price movements of an asset (like stocks) over time. Each "candlestick" typically displays one day's worth of data and represents the opening, closing, high, and low prices for that day. Candlestick charts are widely used in financial and stock market analysis because they provide a visual representation of price action, helping traders to make more informed decisions.

 

!pip install openbb

from openbb_terminal.sdk import openbb

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import ColumnDataSource

# Fetch data
data = openbb.stocks.load(symbol = 'AAPL')

start_date = '2023-09-01'
filtered_data = data[start_date:]

# Prepare data for Bokeh
source = ColumnDataSource(data={
    'date': filtered_data.index,
    'open': filtered_data['Open'],
    'close': filtered_data['Close'],
    'high': filtered_data['High'],
    'low': filtered_data['Low']
})

# Create the candlestick chart
p = figure(x_axis_type="datetime", width=1000, height=400, title="AAPL Candlestick Chart for September 1st 2023 onwards")
p.grid.grid_line_alpha = 0.3

# Define segments for high and low
p.segment('date', 'high', 'date', 'low', color="black", source=source)

# Define vbars for open and close prices
p.vbar('date', 12*60*60*1000, 'open', 'close', fill_color="green", line_color="black", source=source, legend_label="Price Rise")
p.vbar('date', 12*60*60*1000, 'close', 'open', fill_color="red", line_color="black", source=source, legend_label="Price Drop")

# Display
output_notebook()
show(p)
  

 

 

Explanation:

  • The code begins by installing the openbb package and importing necessary libraries.
  • Stock data for Apple (AAPL) is fetched using the openbb.stocks.load() function.
  • A starting date is specified as September 1st, 2023, and the data is filtered to include only records from this date onward.
  • The data is then prepared for plotting by creating a ColumnDataSource, which is a fundamental Bokeh data structure to efficiently interface with the data.
  • A candlestick chart is created using Bokeh's figure function. This chart type is common for visualizing stock market data, displaying the open, close, high, and low values for each day.
  • Segments representing the high and low values of stocks for each day are plotted.
  • Vertical bars (vbars) representing the open and close values are plotted. These bars are colored green when the close value is higher than the open value (indicating a price rise) and red otherwise (indicating a price drop).
  • Finally, the generated plot is displayed in the notebook using output_notebook() and show() functions.

In essence, the code visualizes the stock market performance of Apple (AAPL) from September 1st, 2023, using a candlestick chart.

When interpreting this chart, green candlesticks represent days where the stock price increased (closed higher than it opened), while red candlesticks represent days where the stock price decreased (closed lower than it opened). The wicks on the candlesticks represent the range between the high and low prices for the day. This candlestick chart represents the stock performance of Apple (AAPL).

  • Red bars denote days when the opening price was higher than the closing price, indicating a "Price Drop" for the day. Green bars represent days when the closing price was higher than the opening price, indicating a "Price Rise."
  • The chart begins with a significant price drop on September 1st, with a high starting near 190 and closing around 175.
  • Subsequent days in the first half of September show fluctuations in the stock price, with no clear upward or downward trend.
  • Starting from late September, there's a noticeable pattern of alternating rises and drops until around mid-October.
  • From mid-October onward, the chart shows a more pronounced downward trend, indicating a bearish phase for the stock towards the end of the month.

Overall, while AAPL had periods of gains in October 2023, it ended the month on a declining note, with more days showing price drops than rises.

Back to blog