import pandas as pd import holoviews as hv from bokeh.sampledata import stocks from holoviews.operation.timeseries import rolling, rolling_outlier_std from holoviews.streams import Stream hv.notebook_extension('bokeh') def load_symbol(symbol, **kwargs): df = pd.DataFrame(getattr(stocks, symbol)) df['date'] = df.date.astype('datetime64[ns]') return hv.Curve(df, ('date', 'Date'), ('adj_close', 'Adjusted Close')) stock_symbols = ['AAPL', 'FB', 'GOOG', 'IBM', 'MSFT'] dmap = hv.DynamicMap(load_symbol, kdims='Symbol').redim.values(Symbol=stock_symbols) %%opts Curve [width=600] {+framewise} dmap smoothed = rolling(dmap, rolling_window=30) smoothed rolling_stream = Stream.define('rolling', rolling_window=5) stream = rolling_stream() %%opts Curve [width=600] {+framewise} def rolled_data(symbol, rolling_window, **kwargs): curve = load_symbol(symbol) return rolling(curve, rolling_window=rolling_window) rolled_dmap = hv.DynamicMap(rolled_data, kdims='Symbol', streams=[stream]).redim.values(Symbol=stock_symbols) rolled_dmap stream.event(rolling_window=50) %%opts Scatter (color='red' marker='triangle') stream = rolling_stream() smoothed = rolling(dmap, streams=[stream]) outliers = rolling_outlier_std(dmap, streams=[stream]) smoothed * outliers stream.event(rolling_window=50)