Stocks Example#

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


URL: https://docs.bokeh.org/en/2.4.1/docs/gallery/stocks.html

Most examples work across multiple plotting backends, this example is also available for:

import numpy as np
import pandas as pd
import holoviews as hv
from holoviews import opts
hv.extension('bokeh')

Defining the data#

from holoviews.operation.timeseries import rolling
from bokeh.sampledata.stocks import AAPL, GOOG, IBM, MSFT

color_cycle = hv.Cycle(values=['#A6CEE3', '#B2DF8A','#33A02C', '#FB9A99'])

def get_curve(data, label=''):
    df = pd.DataFrame(data)
    df['date'] = df.date.astype('datetime64[ns]')
    return hv.Curve(df, ('date', 'Date'), ('adj_close', 'Price'), label=label)

hv.Dimension.type_formatters[np.datetime64] = '%Y'

aapl = get_curve(AAPL, label='AAPL')
goog = get_curve(GOOG, label='GOOG')
ibm  = get_curve(IBM, label='IBM')
msft = get_curve(MSFT, label='MSFT')

avg_curve = rolling(aapl, rolling_window=30).relabel('Average')
avg_scatter = hv.Scatter((np.array(AAPL['date'], dtype=np.datetime64), np.array(AAPL['adj_close'])), 
                         ('date', 'Date'), ('adj_close', 'Price'), label='close')

Plot#

((aapl * goog * ibm * msft) + (avg_scatter * avg_curve)).opts(
    opts.Curve(color=color_cycle),
    opts.Curve('Curve.Average', color='navy'),
    opts.Scatter(alpha=0.2, size=4, color='darkgrey'),
    opts.Overlay(width=400, height=400, legend_position='top_left'))
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).