Skip to content

Instantly share code, notes, and snippets.

@timkpaine
Last active December 9, 2022 19:46
Show Gist options
  • Save timkpaine/0f8b4157b09762e4a94960f3798e7a9b to your computer and use it in GitHub Desktop.
Save timkpaine/0f8b4157b09762e4a94960f3798e7a9b to your computer and use it in GitHub Desktop.
Simple Real-Time Stock Streaming with Bokeh
'''
To run:
python -m bokeh serve iex.py
'''
import io
import requests
import pandas as pd
import pyEX
from bokeh.models import ColumnDataSource, HoverTool
from bokeh.models.widgets import TextInput, Button
from bokeh.plotting import figure, curdoc
from bokeh.layouts import row, widgetbox
TICKER = ""
base = "https://api.iextrading.com/1.0/"
data = ColumnDataSource(dict(time=[], display_time=[], price=[]))
def get_last_price(symbol):
# endpoint = "tops/last"
return pd.DataFrame([pyEX.delayedQuote(symbol)])
def update_ticker():
global TICKER
TICKER = ticker_textbox.value
price_plot.title.text = "IEX Real-Time Price: " + ticker_textbox.value
data.data = dict(time=[], display_time=[], price=[])
return
def update_price():
new_price = get_last_price(symbol=TICKER)
data.stream(dict(time=new_price["delayedPriceTime"],
display_time=new_price["processedTime"],
price=new_price["delayedPrice"]), 10000)
return
hover = HoverTool(tooltips=[
("Time", "@display_time"),
("IEX Real-Time Price", "@price")
])
price_plot = figure(plot_width=800,
plot_height=400,
x_axis_type='datetime',
tools=[hover],
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")
update.on_click(update_ticker)
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)
@treatmesubj
Copy link

treatmesubj commented May 27, 2018

tried bokeh serve iex.py from cmd and got the error that I didn't have the "PIL" module. I tried a pip install and couldn't find it. Must be referring to Python Imaging Library. There's no version for python 3 yet :/ Update: Never mind I pip installed Pillow instead

@bkasaeer
Copy link

Hi Tim,

I have been trying to run your app using both the python shells or Spyder and windows command line, but none of them work. I wonder if you can give me an advice on how to proceed.

In Spyder shell, I run "bokeh serve --show DataStream1.py" and it shows this error:

"bokeh serve --show DataStream1.py File "", line 1 bokeh serve --show DataStream1.py ^ SyntaxError: invalid syntax"

When I run it on the cmd as "bokeh serve --show DataStream1.py", it shows this error:

'bokeh' is not recognized as an internal or external command, operable program or batch file.

When I run it through cmd as "python -m bokeh serve DataStream1.py" it shows a long error ending as follows:

"ImportError: DLL load failed: The specified module could not be found."

Best,
Babak

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