Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Simple Real-Time Stock Streaming with Bokeh
import io
import requests
import pandas as pd
from bokeh.models import ColumnDataSource, HoverTool, ResizeTool, SaveTool
from bokeh.models.widgets import TextInput, Button
from bokeh.plotting import figure, curdoc
from bokeh.layouts import row, widgetbox
base = ""
data = ColumnDataSource(dict(time=[], display_time=[], price=[]))
def get_last_price(symbol):
payload = {
"format": "csv",
"symbols": symbol
endpoint = "tops/last"
raw = requests.get(base + endpoint, params=payload)
raw = io.BytesIO(raw.content)
prices_df = pd.read_csv(raw, sep=",")
prices_df["time"] = pd.to_datetime(prices_df["time"], unit="ms")
prices_df["display_time"] = prices_df["time"].dt.strftime("%m-%d-%Y %H:%M:%S.%f")
return prices_df
def update_ticker():
global TICKER
TICKER = ticker_textbox.value
price_plot.title.text = "IEX Real-Time Price: " + ticker_textbox.value = dict(time=[], display_time=[], price=[])
def update_price():
new_price = get_last_price(symbol=TICKER)["time"],
price=new_price["price"]), 10000)
hover = HoverTool(tooltips=[
("Time", "@display_time"),
("IEX Real-Time Price", "@price")
price_plot = figure(plot_width=800,
tools=[hover, ResizeTool(), SaveTool()],
title="Real-Time Price Plot")
price_plot.line(source=data, x='time', y='price')
price_plot.xaxis.axis_label = "Time"
price_plot.yaxis.axis_label = "IEX Real-Time Price"
price_plot.title.text = "IEX Real Time Price: " + TICKER
ticker_textbox = TextInput(placeholder="Ticker")
update = Button(label="Update")
inputs = widgetbox([ticker_textbox, update], width=200)
curdoc().add_root(row(inputs, price_plot, width=1600))
curdoc().title = "Real-Time Price Plot from IEX"
curdoc().add_periodic_callback(update_price, 1000)
Copy link

ThomasBourgeois commented Nov 30, 2017

Love your code, I would like to adapt it for one of my project.
However, apparently resizetool does not exist any more... How would you replace it ?
Thanks !!! Thomas

Copy link

ceremony commented Feb 12, 2020

Good job my man!

Elegant, terse code that gets to the point and the job done.

Copy link

ppgiii commented May 5, 2020


I updated the code to current version and ability to extract and display the data. @zduey

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment