-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathiex.py
82 lines (62 loc) · 2.52 KB
/
iex.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import io
import os
import requests
import pandas as pd
from bokeh.models import ColumnDataSource, HoverTool, ResizeTool, SaveTool, CustomJS
from bokeh.models.widgets import Paragraph, Panel, Tabs, TextInput, Button, DataTable, TableColumn, DateFormatter
from bokeh.models.widgets import TextInput, Button
from bokeh.plotting import figure, curdoc
from bokeh.layouts import row, widgetbox
TICKER = "SPY"
base = "https://api.iextrading.com/1.0/"
data = ColumnDataSource(dict(time=[], display_time=[], price=[]))
def get_last_price(symbol):
payload = {
"format": "csv",
"symbols": symbol
}
endpoint = "tops/last"
if symbol == "TEST":
prices_df = get_test_data()
return prices_df
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
data.data = dict(time=[], display_time=[], price=[])
return
def update_price():
new_price = get_last_price(symbol=TICKER)
data.stream(dict(time=new_price["time"],
display_time=new_price["display_time"],
price=new_price["price"]), 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, 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")
update.on_click(update_ticker)
download = Button(label="Download", button_type="success")
download.callback = CustomJS(args=dict(source=data),
code=open(os.path.join(os.path.dirname(__file__), "export.js")).read())
inputs = widgetbox([ticker_textbox, update, download], 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)