Spaces:
Running
Running
Update app.py
Browse filespolyfit stock data for trend
app.py
CHANGED
|
@@ -22,6 +22,8 @@ import yfinance as yf
|
|
| 22 |
from datetime import datetime, timedelta
|
| 23 |
import pytz
|
| 24 |
import math
|
|
|
|
|
|
|
| 25 |
|
| 26 |
|
| 27 |
load_dotenv(override=True)
|
|
@@ -127,11 +129,11 @@ def get_stock_news(search_symbol):
|
|
| 127 |
rv += f'Link: [URL]({item["link"]})\n\n'
|
| 128 |
|
| 129 |
if have_symbol:
|
| 130 |
-
(plot_df, ymax) = stock_week_df(search_symbol)
|
| 131 |
else:
|
| 132 |
-
(plot_df, ymax) = (pd.DataFrame(), 0.0)
|
| 133 |
|
| 134 |
-
return (rv, plot_df, ymax)
|
| 135 |
|
| 136 |
def stock_history_df(num_weeks):
|
| 137 |
values = []
|
|
@@ -152,6 +154,42 @@ def stock_history_df(num_weeks):
|
|
| 152 |
}
|
| 153 |
return (pd.DataFrame(data), f'{int(xmax + 10000)}')
|
| 154 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 155 |
def stock_week_df(symbol):
|
| 156 |
try:
|
| 157 |
dates = []
|
|
@@ -180,31 +218,17 @@ def stock_week_df(symbol):
|
|
| 180 |
ymax = v
|
| 181 |
for row in df.index:
|
| 182 |
dates.append(row.strftime('%Y-%m-%d'))
|
|
|
|
|
|
|
| 183 |
data = {
|
| 184 |
"date": dates,
|
| 185 |
-
"value" : values
|
|
|
|
| 186 |
}
|
| 187 |
-
|
|
|
|
| 188 |
except:
|
| 189 |
-
return (pd.DataFrame(), ymax)
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
for offset in range(num_weeks+1):
|
| 193 |
-
(value, date) = get_stock_report(False, offset)
|
| 194 |
-
date = date[5:]
|
| 195 |
-
values.append(value)
|
| 196 |
-
dates.append(date)
|
| 197 |
-
if float(value) > xmax:
|
| 198 |
-
xmax = float(value)
|
| 199 |
-
values.reverse()
|
| 200 |
-
dates.reverse()
|
| 201 |
-
data = {
|
| 202 |
-
"date": dates,
|
| 203 |
-
"value" : values
|
| 204 |
-
}
|
| 205 |
-
return (pd.DataFrame(data), f'{int(xmax + 10000)}')
|
| 206 |
-
|
| 207 |
-
|
| 208 |
|
| 209 |
def get_stock_report(verbose = True, offset = 0):
|
| 210 |
try:
|
|
@@ -549,6 +573,7 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 549 |
if not response:
|
| 550 |
response = ''
|
| 551 |
plot = gr.LinePlot(visible=False)
|
|
|
|
| 552 |
if user_window == unames[0] and pwd_window == pwdList[0]:
|
| 553 |
isBoss = True
|
| 554 |
if prompt == 'stats':
|
|
@@ -582,21 +607,22 @@ def chat(prompt, user_window, pwd_window, past, response, gptModel, uploaded_ima
|
|
| 582 |
elif args[1] == 'history':
|
| 583 |
(plot_df, ymax) = get_total_daily_closing_sequence(40) #stock_history_df(12)
|
| 584 |
# ymax = float(ymax)
|
| 585 |
-
return [past, md(response), None, gptModel, uploaded_image_file,
|
| 586 |
gr.LinePlot(plot_df, x="date", y="value", visible=True, x_label_angle=270,
|
| 587 |
y_lim=[500000, 700000], label="Portfolio Value History")]
|
| 588 |
elif num >= 3:
|
| 589 |
if args[1] == 'news':
|
| 590 |
symbol = ' '.join(args[2:])
|
| 591 |
-
(response, plot_df, ymax) = get_stock_news(symbol)
|
| 592 |
ymax *= 1.1
|
| 593 |
-
mdtxt = md(f'News for {symbol}:\n\n' + response)
|
| 594 |
if plot_df.empty:
|
| 595 |
return [past, mdtxt, None, gptModel, uploaded_image_file, plot]
|
| 596 |
else:
|
| 597 |
-
return [past, mdtxt, None, gptModel, uploaded_image_file,
|
| 598 |
gr.LinePlot(plot_df, x="date", y="value", visible=True, x_label_angle=270,
|
| 599 |
-
y_lim=[0, ymax],label=f"{symbol.upper()} Recent Prices"
|
|
|
|
| 600 |
# elif arg[1] == 'history':
|
| 601 |
# symbol = arg[2]
|
| 602 |
# response = 'ok' # get_
|
|
@@ -1062,6 +1088,7 @@ with gr.Blocks(theme=gr.themes.Soft()) as demo:
|
|
| 1062 |
with gr.Column():
|
| 1063 |
image_window = gr.Image(visible=False, label='Generated Image')
|
| 1064 |
with gr.Row():
|
|
|
|
| 1065 |
plot = gr.LinePlot(test_plot_df(), x="month", y="value", visible=False, label="Portfolio Value History")
|
| 1066 |
submit_button.click(chat,
|
| 1067 |
inputs=[prompt_window, user_window, password, history, output_window, model, uploaded_image_file],
|
|
|
|
| 22 |
from datetime import datetime, timedelta
|
| 23 |
import pytz
|
| 24 |
import math
|
| 25 |
+
import numpy as np
|
| 26 |
+
# import matplotlib.pyplot as plt
|
| 27 |
|
| 28 |
|
| 29 |
load_dotenv(override=True)
|
|
|
|
| 129 |
rv += f'Link: [URL]({item["link"]})\n\n'
|
| 130 |
|
| 131 |
if have_symbol:
|
| 132 |
+
(plot_df, ymax, pct_delta) = stock_week_df(search_symbol)
|
| 133 |
else:
|
| 134 |
+
(plot_df, ymax, pct_delta) = (pd.DataFrame(), 0.0, 0.0)
|
| 135 |
|
| 136 |
+
return (rv, plot_df, ymax, pct_delta)
|
| 137 |
|
| 138 |
def stock_history_df(num_weeks):
|
| 139 |
values = []
|
|
|
|
| 154 |
}
|
| 155 |
return (pd.DataFrame(data), f'{int(xmax + 10000)}')
|
| 156 |
|
| 157 |
+
# def make_mp_figure(dates, values, fit_values, ymax = 0.0):
|
| 158 |
+
# npdates = np.asarray(dates)
|
| 159 |
+
# npvals = np.asarray(values)
|
| 160 |
+
# npfits = np.asarray(fit_values)
|
| 161 |
+
# plt_format = '-'
|
| 162 |
+
# fig = plt.figure(layout="constrained", figsize=(6,2))
|
| 163 |
+
# ax = fig.add_subplot(111)
|
| 164 |
+
# tics = []
|
| 165 |
+
# labels = []
|
| 166 |
+
# i = len(dates) - 1
|
| 167 |
+
# while i >= 0:
|
| 168 |
+
# tics.append(i)
|
| 169 |
+
# labels.append(dates[i])
|
| 170 |
+
# i -= 5
|
| 171 |
+
# # tics = list(range(0,len(dates),5))
|
| 172 |
+
# # labels = dates[0::5]
|
| 173 |
+
# ax.set_xticks(tics, labels = labels)
|
| 174 |
+
# ax.plot(npdates, npvals, plt_format)
|
| 175 |
+
# ax.plot(npdates, npfits, plt_format)
|
| 176 |
+
# ax.set_ylim(0.0, ymax*1.05)
|
| 177 |
+
# # ax.set_xlim(dates[0], dates[-1:])
|
| 178 |
+
# return fig
|
| 179 |
+
|
| 180 |
+
def lms_fit_trend(dates, values):
|
| 181 |
+
# days = []
|
| 182 |
+
# fit_data = []
|
| 183 |
+
days = list(range(0, len(dates)))
|
| 184 |
+
fit = np.polyfit(days, values, 1)
|
| 185 |
+
delta = len(dates) * float(fit[0])
|
| 186 |
+
avg = float(fit[1]) + 0.5 * delta
|
| 187 |
+
pct_delta = 100 * delta / avg
|
| 188 |
+
# for day in days:
|
| 189 |
+
# fit_data.append(float(fit[0]) * day + float(fit[1]) )
|
| 190 |
+
# return fit_data
|
| 191 |
+
return pct_delta
|
| 192 |
+
|
| 193 |
def stock_week_df(symbol):
|
| 194 |
try:
|
| 195 |
dates = []
|
|
|
|
| 218 |
ymax = v
|
| 219 |
for row in df.index:
|
| 220 |
dates.append(row.strftime('%Y-%m-%d'))
|
| 221 |
+
# fit_data = lms_fit(dates, values)
|
| 222 |
+
pct_delta = lms_fit_trend(dates, values)
|
| 223 |
data = {
|
| 224 |
"date": dates,
|
| 225 |
+
"value" : values,
|
| 226 |
+
# "fit" : fit_data
|
| 227 |
}
|
| 228 |
+
# fig = make_mp_figure(dates, values, fit_data, ymax)
|
| 229 |
+
return (pd.DataFrame(data), ymax, pct_delta)
|
| 230 |
except:
|
| 231 |
+
return (pd.DataFrame(), ymax, 0.0)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 232 |
|
| 233 |
def get_stock_report(verbose = True, offset = 0):
|
| 234 |
try:
|
|
|
|
| 573 |
if not response:
|
| 574 |
response = ''
|
| 575 |
plot = gr.LinePlot(visible=False)
|
| 576 |
+
# plot = gr.Plot(visible=False)
|
| 577 |
if user_window == unames[0] and pwd_window == pwdList[0]:
|
| 578 |
isBoss = True
|
| 579 |
if prompt == 'stats':
|
|
|
|
| 607 |
elif args[1] == 'history':
|
| 608 |
(plot_df, ymax) = get_total_daily_closing_sequence(40) #stock_history_df(12)
|
| 609 |
# ymax = float(ymax)
|
| 610 |
+
return [past, md(response), None, gptModel, uploaded_image_file, # plot]
|
| 611 |
gr.LinePlot(plot_df, x="date", y="value", visible=True, x_label_angle=270,
|
| 612 |
y_lim=[500000, 700000], label="Portfolio Value History")]
|
| 613 |
elif num >= 3:
|
| 614 |
if args[1] == 'news':
|
| 615 |
symbol = ' '.join(args[2:])
|
| 616 |
+
(response, plot_df, ymax, pct_delta) = get_stock_news(symbol)
|
| 617 |
ymax *= 1.1
|
| 618 |
+
mdtxt = md(f'News for {symbol}:\nTrend = {int(pct_delta)}%\n\n' + response)
|
| 619 |
if plot_df.empty:
|
| 620 |
return [past, mdtxt, None, gptModel, uploaded_image_file, plot]
|
| 621 |
else:
|
| 622 |
+
return [past, mdtxt, None, gptModel, uploaded_image_file, #gr.Plot(plot_df, visible=True)]
|
| 623 |
gr.LinePlot(plot_df, x="date", y="value", visible=True, x_label_angle=270,
|
| 624 |
+
y_lim=[0, ymax],label=f"{symbol.upper()} Recent Prices",
|
| 625 |
+
color_map={''})]
|
| 626 |
# elif arg[1] == 'history':
|
| 627 |
# symbol = arg[2]
|
| 628 |
# response = 'ok' # get_
|
|
|
|
| 1088 |
with gr.Column():
|
| 1089 |
image_window = gr.Image(visible=False, label='Generated Image')
|
| 1090 |
with gr.Row():
|
| 1091 |
+
# plot = gr.Plot(visible=False)
|
| 1092 |
plot = gr.LinePlot(test_plot_df(), x="month", y="value", visible=False, label="Portfolio Value History")
|
| 1093 |
submit_button.click(chat,
|
| 1094 |
inputs=[prompt_window, user_window, password, history, output_window, model, uploaded_image_file],
|