Spaces:
Running
Running
Update app.py
Browse filesadded stock alerts, changed stock history plot upper limit
app.py
CHANGED
@@ -262,42 +262,6 @@ def stock_history_df(num_weeks):
|
|
262 |
}
|
263 |
return (pd.DataFrame(data), f'{int(xmax + 10000)}')
|
264 |
|
265 |
-
# def make_mp_figure(dates, values, fit_values, ymax = 0.0):
|
266 |
-
# npdates = np.asarray(dates)
|
267 |
-
# npvals = np.asarray(values)
|
268 |
-
# npfits = np.asarray(fit_values)
|
269 |
-
# plt_format = '-'
|
270 |
-
# fig = plt.figure(layout="constrained", figsize=(6,2))
|
271 |
-
# ax = fig.add_subplot(111)
|
272 |
-
# tics = []
|
273 |
-
# labels = []
|
274 |
-
# i = len(dates) - 1
|
275 |
-
# while i >= 0:
|
276 |
-
# tics.append(i)
|
277 |
-
# labels.append(dates[i])
|
278 |
-
# i -= 5
|
279 |
-
# # tics = list(range(0,len(dates),5))
|
280 |
-
# # labels = dates[0::5]
|
281 |
-
# ax.set_xticks(tics, labels = labels)
|
282 |
-
# ax.plot(npdates, npvals, plt_format)
|
283 |
-
# ax.plot(npdates, npfits, plt_format)
|
284 |
-
# ax.set_ylim(0.0, ymax*1.05)
|
285 |
-
# # ax.set_xlim(dates[0], dates[-1:])
|
286 |
-
# return fig
|
287 |
-
|
288 |
-
# def lms_fit_trend(dates, values):
|
289 |
-
# # days = []
|
290 |
-
# # fit_data = []
|
291 |
-
# days = list(range(0, len(dates)))
|
292 |
-
# fit = np.polyfit(days, values, 1)
|
293 |
-
# delta = len(dates) * float(fit[0])
|
294 |
-
# avg = float(fit[1]) + 0.5 * delta
|
295 |
-
# pct_delta = 100 * delta / avg
|
296 |
-
# # for day in days:
|
297 |
-
# # fit_data.append(float(fit[0]) * day + float(fit[1]) )
|
298 |
-
# # return fit_data
|
299 |
-
# return pct_delta
|
300 |
-
|
301 |
def stock_deltas(values):
|
302 |
num = len(values)
|
303 |
month_end_avg = float(np.average(np.array(values[-3:])))
|
@@ -351,6 +315,62 @@ def stock_week_df(symbol):
|
|
351 |
except:
|
352 |
return (pd.DataFrame(), ymax, (0.0, 0.0, 0.0))
|
353 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
354 |
def get_stock_report(verbose = True, offset = 0):
|
355 |
try:
|
356 |
stock_data = {}
|
@@ -724,15 +744,19 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
724 |
response = stock_list()
|
725 |
return [past, md(response), None, gptModel, uploaded_image_file, plot]
|
726 |
elif num == 2:
|
727 |
-
|
728 |
-
|
729 |
return [past, md(response), None, gptModel, uploaded_image_file, plot]
|
730 |
-
|
731 |
-
|
732 |
-
|
733 |
-
|
734 |
-
|
735 |
-
|
|
|
|
|
|
|
|
|
736 |
elif num >= 3:
|
737 |
if args[1] == 'news':
|
738 |
symbol = ' '.join(args[2:])
|
|
|
262 |
}
|
263 |
return (pd.DataFrame(data), f'{int(xmax + 10000)}')
|
264 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
265 |
def stock_deltas(values):
|
266 |
num = len(values)
|
267 |
month_end_avg = float(np.average(np.array(values[-3:])))
|
|
|
315 |
except:
|
316 |
return (pd.DataFrame(), ymax, (0.0, 0.0, 0.0))
|
317 |
|
318 |
+
def stock_recent_delta(symbol):
|
319 |
+
try:
|
320 |
+
dates = []
|
321 |
+
values = []
|
322 |
+
ymax = 0
|
323 |
+
etime = etz_now()
|
324 |
+
if etime.hour >= 16:
|
325 |
+
etime = etime + timedelta(days=1)
|
326 |
+
week_ago = etime - timedelta(days=8)
|
327 |
+
end = etime.strftime('%Y-%m-%d')
|
328 |
+
start = week_ago.strftime('%Y-%m-%d')
|
329 |
+
df = yf.download(symbol.upper(),
|
330 |
+
start = start,
|
331 |
+
end = end,
|
332 |
+
progress = False,
|
333 |
+
)
|
334 |
+
vals2d = df.values.tolist()
|
335 |
+
valsTxt = []
|
336 |
+
numDays = len(vals2d)
|
337 |
+
for i in range(numDays):
|
338 |
+
valsTxt.append(vals2d[i][0])
|
339 |
+
for val in valsTxt:
|
340 |
+
v = round(float(val),2)
|
341 |
+
values.append(v)
|
342 |
+
if v > ymax:
|
343 |
+
ymax = v
|
344 |
+
for row in df.index:
|
345 |
+
dates.append(row.strftime('%Y-%m-%d'))
|
346 |
+
start_val = float(np.average(np.array(values[:2])))
|
347 |
+
end_val = float(values[len(values)-1])
|
348 |
+
return f'{(end_val/start_val - 1.0)*100:.1f}'
|
349 |
+
except:
|
350 |
+
return 'NA'
|
351 |
+
|
352 |
+
def get_alerts():
|
353 |
+
try:
|
354 |
+
rv = ''
|
355 |
+
# stock_data = {}
|
356 |
+
global stock_data_path
|
357 |
+
with open(stock_data_path, 'rt') as fp:
|
358 |
+
lines = fp.readlines()
|
359 |
+
for line in lines:
|
360 |
+
(name, symbol, shares) = line.rstrip().split(',')
|
361 |
+
name = name.strip()
|
362 |
+
symbol = symbol.strip()
|
363 |
+
delta_pct = stock_recent_delta(symbol)
|
364 |
+
if delta_pct == 'NA':
|
365 |
+
rv += f'\n{symbol} ({name}) NA'
|
366 |
+
else:
|
367 |
+
rv += f'\n{symbol} ({name}) {delta_pct}%'
|
368 |
+
if abs(float(delta_pct)) > 3:
|
369 |
+
rv += ' **\*\*\*** '
|
370 |
+
return 'Stock price changes over last week:\nChanges greater than +/-3% marked by **\*\*\***\n ' + rv + '\n'
|
371 |
+
except:
|
372 |
+
return "Error getting stock deltas\n"
|
373 |
+
|
374 |
def get_stock_report(verbose = True, offset = 0):
|
375 |
try:
|
376 |
stock_data = {}
|
|
|
744 |
response = stock_list()
|
745 |
return [past, md(response), None, gptModel, uploaded_image_file, plot]
|
746 |
elif num == 2:
|
747 |
+
if args[1] == 'alerts':
|
748 |
+
response = get_alerts()
|
749 |
return [past, md(response), None, gptModel, uploaded_image_file, plot]
|
750 |
+
else:
|
751 |
+
response = get_stock_report()
|
752 |
+
if args[1] == 'value':
|
753 |
+
return [past, md(response), None, gptModel, uploaded_image_file, plot]
|
754 |
+
elif args[1] == 'history':
|
755 |
+
(plot_df, ymax) = get_total_daily_closing_sequence(40) #stock_history_df(12)
|
756 |
+
# ymax = float(ymax)
|
757 |
+
return [past, md(response), None, gptModel, uploaded_image_file, # plot]
|
758 |
+
gr.LinePlot(plot_df, x="date", y="value", visible=True, x_label_angle=270,
|
759 |
+
y_lim=[400000, 800000], label="Portfolio Value History")]
|
760 |
elif num >= 3:
|
761 |
if args[1] == 'news':
|
762 |
symbol = ' '.join(args[2:])
|