Ohlc#

Download this notebook from GitHub (right-click to download).


import pandas as pd
import hvplot.pandas  # noqa

ohlc is a useful chart type to visualize stock movements

from bokeh.sampledata import stocks

df = pd.DataFrame(stocks.AAPL)
df['date'] = pd.to_datetime(df.date)

df.iloc[-50:].hvplot.ohlc(grid=True)

We can control the neg_color, pos_color, line_color and bar_width:

df.iloc[-50:].hvplot.ohlc(neg_color='indianred', pos_color='chartreuse', line_color='gray', bar_width=0.9, grid=True)

By default ohlc will assume the index OR the first datetime column should be mapped to the x-axis and the first four non-datetime columns correspond to the O (open), H (high), L (low) and C (close) components. The default call is therefore equivalent to:

df.iloc[-50:].hvplot.ohlc('date', ['open', 'low', 'high', 'close'], grid=True)

Using the HoloViews RangeToolLink we can make it easy to scroll through the data while still seeing an overview of the overall timeseries:

from holoviews.plotting.links import RangeToolLink

df_2012 = df[df.date > pd.to_datetime('2012')]

ohlc = df_2012.hvplot.ohlc(ylabel='Price ($)', grid=True, xaxis=None)
overview = df_2012.hvplot.ohlc(yaxis=None, height=150, fields={'date': 'Date'})
volume = df_2012.hvplot.step('date', 'volume', height=100, xaxis=None)

RangeToolLink(overview.get(0), ohlc.get(0))

layout = (volume + ohlc + overview).cols(1)

layout.opts(merge_tools=False)
This web page was generated from a Jupyter notebook and not all interactivity will work on this website. Right click to download and run locally for full Python-backed interactivity.

Download this notebook from GitHub (right-click to download).