-
-
Notifications
You must be signed in to change notification settings - Fork 500
/
pn_model.py
40 lines (29 loc) · 1.17 KB
/
pn_model.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
import datetime as dt
import hvplot.pandas
import param
import panel as pn
class StockScreener(param.Parameterized):
df = param.DataFrame(precedence=-1)
index = param.ListSelector()
normalize = param.Boolean(default=True)
start = param.Date()
def __init__(self, df, **params):
start = dt.date(year=df.index[0].year, month=df.index[0].month, day=df.index[0].day)
end = dt.date(year=df.index[-1].year, month=df.index[-1].month, day=df.index[-1].day)
super(StockScreener, self).__init__(df=df, start=start, **params)
self.param.start.bounds = (start, end)
columns = list(self.df.columns)
self.param.index.objects = columns
self.index = columns[:5]
@param.depends('index', 'normalize', 'start')
def update_plot(self):
pos = self.df.index.get_loc(self.start, method='bfill')
dfp = self.df.iloc[pos:][self.index]
if self.normalize:
dfp = 100 * dfp / dfp.iloc[0]
return dfp.hvplot(group_label='Ticker')
def panel(self):
return pn.Row(
pn.panel(self.param, widgets={'start': pn.widgets.DateSlider}),
self.update_plot
)