forked from thedataincubator/flask-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ticker.py
136 lines (113 loc) · 4.24 KB
/
ticker.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Render the bokeh visualization of the stock time series.
Display a stock ticker visualization based on user input.
A Simple Stock Ticker Visualization Web App
Copyright (C) 2020 Matthew Chan
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
"""
import json
import pandas as pd
import requests
import sys
import bokeh.plotting as bkp
from bokeh.embed import json_item
from bokeh.models import HoverTool
from bokeh.models.tickers import DatetimeTicker
def ticker(symbol):
"""Build stock ticker plot, return it to outside module."""
KEY = 'Q7JC95BEFD22FQDA'
def get_data(symbol):
"""Get stock data given symbol from api, return json."""
url = (f"https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&"
f"symbol={symbol}&apikey={KEY}")
response = requests.get(url)
data = response.json()
return data
def get_df(data):
"""Return formatted dataframe from json data."""
df = pd.DataFrame(data['Time Series (Daily)']).T
df.index = pd.to_datetime(df.index)
df = df.sort_index().copy()
df.columns = df.columns.str.replace(r'\d+\.\s', '')
df.columns = map(str.title, df.columns)
df = df.drop('Volume', axis=1).astype(float).copy()
return df
def build_plot(symbol):
"""Render the boken html file."""
data = get_data(symbol)
df = get_df(data)
monthago = df.index[-20]
now = df.index[-1]
high = df.loc[monthago:now].max().max() + 5
low = df.loc[monthago:now].min().min() - 5
p = bkp.figure(title=f'{symbol} Stock Price',
y_axis_label='Dollars',
x_axis_type='datetime',
plot_width=800,
plot_height=400,
x_range=(monthago, now),
y_range=(low, high),
toolbar_location="below",
tools="pan,box_zoom,wheel_zoom,reset,save",
active_drag="pan",
)
p.circle(df.index,
df['Open'],
size=8,
color='olive',
alpha=0.6,
legend_label='Open',
name='Open',
)
p.circle(df.index,
df['Close'],
size=8,
color='orange',
alpha=0.6,
legend_label='Close',
name='Close',
)
p.line(df.index,
df['High'],
line_width=2,
color='steelblue',
legend_label='High',
name='High',
)
p.line(df.index,
df['Low'],
line_width=2,
color='firebrick',
legend_label='Low',
name='Low'
)
p.xaxis.ticker = DatetimeTicker(desired_num_ticks=20)
hover = HoverTool(tooltips=[('Time Series', '$name'),
('Date', '@x{%F}'),
('Price', '$@y{0.00 a}'),
],
formatters={'@x': 'datetime'})
p.add_tools(hover)
p.toolbar.active_inspect = hover
p.sizing_mode = 'stretch_width'
return p
p = build_plot(symbol)
return p
def main():
"""Return raw json text from plot with given ticker symbol argument.
Default ticker symbol is 'IBM'.
"""
if len(sys.argv) == 1:
symbol = 'IBM'
else:
symbol = sys.argv[1]
p = ticker(symbol)
print(json.dumps(json_item(p, f'{symbol}_stocks')))
if __name__ == '__main__':
main()