Update src/app.py
Browse files- src/app.py +64 -12
src/app.py
CHANGED
|
@@ -590,6 +590,28 @@ def _safe_price(ticker):
|
|
| 590 |
def get_prices_bulk(tickers_tuple):
|
| 591 |
return {t: _safe_price(t) for t in tickers_tuple}
|
| 592 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 593 |
@st.cache_data(ttl=3600)
|
| 594 |
def get_exchange_rate():
|
| 595 |
try: return float(yf.Ticker("KRW=X").fast_info['last_price'])
|
|
@@ -679,14 +701,27 @@ def get_rich_financial_context(ticker):
|
|
| 679 |
except Exception as e:
|
| 680 |
return f" - ์ฌ๋ฌด ๋ฐ์ดํฐ ์กฐํ ์คํจ: {str(e)[:60]}", False
|
| 681 |
|
| 682 |
-
@st.cache_data(ttl=
|
| 683 |
def get_ai_alert(summary, key_hash, model_name):
|
| 684 |
try:
|
| 685 |
model = genai.GenerativeModel(model_name)
|
| 686 |
prompt = f"[๋์ฒ ํ ์์คํธ๋ฆฌํธ ํธ๋ ์ด๋]\nํฌํธํด๋ฆฌ์ค: {summary}\n1~2๋ฌธ์ฅ์ ๋ ์นด๋ก์ด ์๋ฆผ. ๋งํฌ๋ค์ด ๊ธฐํธ ๊ธ์ง."
|
| 687 |
-
|
| 688 |
-
|
| 689 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 690 |
|
| 691 |
PRICE_TTL = 300
|
| 692 |
|
|
@@ -1078,24 +1113,33 @@ def _build_alert_groups(prices, rate):
|
|
| 1078 |
if st.session_state.df.empty:
|
| 1079 |
return groups
|
| 1080 |
|
|
|
|
|
|
|
|
|
|
| 1081 |
for _, row in st.session_state.df.iterrows():
|
| 1082 |
-
t = row[
|
| 1083 |
-
ap = float(row[
|
| 1084 |
-
nm = row[
|
| 1085 |
ck = prices.get(t, 0) * rate
|
| 1086 |
if ck <= 0 or ap <= 0:
|
| 1087 |
continue
|
| 1088 |
|
| 1089 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1090 |
rp = st.session_state.full_stock_reports.get(f"{t}_{ap}", {}).get("report", {})
|
| 1091 |
act = rp.get("action_type", "")
|
| 1092 |
tgp = rp.get("target_price", "")
|
| 1093 |
|
| 1094 |
-
item = dict(ticker=t, name=nm,
|
|
|
|
| 1095 |
current_price=int(ck), avg_price=int(ap),
|
| 1096 |
target_price=tgp, action=act)
|
| 1097 |
|
| 1098 |
-
|
|
|
|
| 1099 |
groups["๊ธ๋ณ"].append(item)
|
| 1100 |
continue
|
| 1101 |
|
|
@@ -2005,11 +2049,19 @@ elif st.session_state.main_menu == "โ ์์คํ
์ค์ ":
|
|
| 2005 |
st.success("์ค์ ์ด ์ ์ฅ๋์์ต๋๋ค.")
|
| 2006 |
time.sleep(0.5); st.rerun()
|
| 2007 |
|
| 2008 |
-
# โโ ์จ๊น ๋ฆฌํ๋ ์ ๋ฒํผ (
|
| 2009 |
st.button("alert_trigger", key="hidden_alert_btn", help="alert_trigger_btn", on_click=lambda: None)
|
| 2010 |
st.markdown("""
|
| 2011 |
<style>
|
| 2012 |
-
div[data-testid="stButton"]:has(button[title="alert_trigger_btn"]) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2013 |
</style>
|
| 2014 |
""", unsafe_allow_html=True)
|
| 2015 |
|
|
|
|
| 590 |
def get_prices_bulk(tickers_tuple):
|
| 591 |
return {t: _safe_price(t) for t in tickers_tuple}
|
| 592 |
|
| 593 |
+
@st.cache_data(ttl=300, show_spinner=False)
|
| 594 |
+
def get_daily_changes_bulk(tickers_tuple):
|
| 595 |
+
"""์ผ๊ฐ ๋ฑ๋ฝ๋ฅ ๋ฐํ: {ticker: day_pct} โ ์ ์ผ ์ข
๊ฐ ๋๋น ๋น์ผ ๋ฑ๋ฝ๋ฅ """
|
| 596 |
+
result = {}
|
| 597 |
+
for t in tickers_tuple:
|
| 598 |
+
try:
|
| 599 |
+
info = yf.Ticker(t).fast_info
|
| 600 |
+
cur = float(getattr(info, "last_price", 0) or 0)
|
| 601 |
+
prev = float(getattr(info, "previous_close", 0) or 0)
|
| 602 |
+
if cur > 0 and prev > 0:
|
| 603 |
+
result[t] = (cur / prev - 1) * 100
|
| 604 |
+
continue
|
| 605 |
+
except: pass
|
| 606 |
+
try:
|
| 607 |
+
h = yf.Ticker(t).history(period="2d", interval="1d")
|
| 608 |
+
if len(h) >= 2:
|
| 609 |
+
result[t] = (float(h["Close"].iloc[-1]) / float(h["Close"].iloc[-2]) - 1) * 100
|
| 610 |
+
continue
|
| 611 |
+
except: pass
|
| 612 |
+
result[t] = 0.0
|
| 613 |
+
return result
|
| 614 |
+
|
| 615 |
@st.cache_data(ttl=3600)
|
| 616 |
def get_exchange_rate():
|
| 617 |
try: return float(yf.Ticker("KRW=X").fast_info['last_price'])
|
|
|
|
| 701 |
except Exception as e:
|
| 702 |
return f" - ์ฌ๋ฌด ๋ฐ์ดํฐ ์กฐํ ์คํจ: {str(e)[:60]}", False
|
| 703 |
|
| 704 |
+
@st.cache_data(ttl=1800, show_spinner=False)
|
| 705 |
def get_ai_alert(summary, key_hash, model_name):
|
| 706 |
try:
|
| 707 |
model = genai.GenerativeModel(model_name)
|
| 708 |
prompt = f"[๋์ฒ ํ ์์คํธ๋ฆฌํธ ํธ๋ ์ด๋]\nํฌํธํด๋ฆฌ์ค: {summary}\n1~2๋ฌธ์ฅ์ ๋ ์นด๋ก์ด ์๋ฆผ. ๋งํฌ๋ค์ด ๊ธฐํธ ๊ธ์ง."
|
| 709 |
+
result = model.generate_content(prompt, generation_config={"temperature":0.5}).text.strip()
|
| 710 |
+
if not result:
|
| 711 |
+
raise ValueError("empty response")
|
| 712 |
+
return result
|
| 713 |
+
except Exception as e:
|
| 714 |
+
err = str(e).lower()
|
| 715 |
+
if "quota" in err or "rate" in err or "429" in err:
|
| 716 |
+
return "โ ๏ธ API ํธ์ถ ํ๋ ์ด๊ณผ โ ์ ์ ํ ์๋ก๊ณ ์นจํ๋ฉด ๋ธ๋ฆฌํ์ด ํ์๋ฉ๋๋ค."
|
| 717 |
+
elif "api key" in err or "invalid" in err or "401" in err or "403" in err:
|
| 718 |
+
return "โ ๏ธ API ํค ์ค๋ฅ โ ์ค์ (โ)์์ Gemini API ํค๋ฅผ ํ์ธํด์ฃผ์ธ์."
|
| 719 |
+
elif "model" in err or "not found" in err or "404" in err:
|
| 720 |
+
return f"โ ๏ธ ๋ชจ๋ธ({model_name}) ์ค๋ฅ โ ์ค์ ์์ ๋ค๋ฅธ ๋ชจ๋ธ์ ์ ํํด๋ณด์ธ์."
|
| 721 |
+
elif "timeout" in err or "deadline" in err:
|
| 722 |
+
return "โ ๏ธ ์๋ต ์๊ฐ ์ด๊ณผ โ ๋คํธ์ํฌ ์ํ๋ฅผ ํ์ธํ๊ณ ์๋ก๊ณ ์นจํด์ฃผ์ธ์."
|
| 723 |
+
else:
|
| 724 |
+
return f"โ ๏ธ ๋ธ๋ฆฌํ ์์ฑ ์คํจ โ {str(e)[:80]}"
|
| 725 |
|
| 726 |
PRICE_TTL = 300
|
| 727 |
|
|
|
|
| 1113 |
if st.session_state.df.empty:
|
| 1114 |
return groups
|
| 1115 |
|
| 1116 |
+
tickers = tuple(sorted(set(st.session_state.df["ํฐ์ปค"].tolist())))
|
| 1117 |
+
daily_changes = get_daily_changes_bulk(tickers)
|
| 1118 |
+
|
| 1119 |
for _, row in st.session_state.df.iterrows():
|
| 1120 |
+
t = row["ํฐ์ปค"]
|
| 1121 |
+
ap = float(row["ํ๋จ๊ฐ"])
|
| 1122 |
+
nm = row["์ข
๋ชฉ๋ช
"]
|
| 1123 |
ck = prices.get(t, 0) * rate
|
| 1124 |
if ck <= 0 or ap <= 0:
|
| 1125 |
continue
|
| 1126 |
|
| 1127 |
+
# ํ์์ฉ: ํ๋จ๊ฐ ๋๋น ์์ต๋ฅ
|
| 1128 |
+
pct_from_avg = (ck / ap - 1) * 100
|
| 1129 |
+
# ๊ธ๋ณ ํ๋จ์ฉ: ๋น์ผ ๋ฑ๋ฝ๋ฅ
|
| 1130 |
+
day_pct = daily_changes.get(t, 0.0)
|
| 1131 |
+
|
| 1132 |
rp = st.session_state.full_stock_reports.get(f"{t}_{ap}", {}).get("report", {})
|
| 1133 |
act = rp.get("action_type", "")
|
| 1134 |
tgp = rp.get("target_price", "")
|
| 1135 |
|
| 1136 |
+
item = dict(ticker=t, name=nm,
|
| 1137 |
+
change_pct=day_pct, # ํ๋กํ
ํจ๋์ ํ์๋๋ ๋ณํ์จ = ์ผ๊ฐ
|
| 1138 |
current_price=int(ck), avg_price=int(ap),
|
| 1139 |
target_price=tgp, action=act)
|
| 1140 |
|
| 1141 |
+
# ๊ธ๋ณ ๊ธฐ์ค: ๋น์ผ ๋ฑ๋ฝ๋ฅ ยฑ5% ์ด์
|
| 1142 |
+
if abs(day_pct) >= 5.0:
|
| 1143 |
groups["๊ธ๋ณ"].append(item)
|
| 1144 |
continue
|
| 1145 |
|
|
|
|
| 2049 |
st.success("์ค์ ์ด ์ ์ฅ๋์์ต๋๋ค.")
|
| 2050 |
time.sleep(0.5); st.rerun()
|
| 2051 |
|
| 2052 |
+
# โโ ์จ๊น ๋ฆฌํ๋ ์ ๋ฒํผ (ํ๋กํ
ํจ๋ goto: ์ข
๋ชฉ ์ด๋ ์ ์ฉ, ํ๋ฉด์ ์ ๋ ํ์ ์ ๋จ) โโ
|
| 2053 |
st.button("alert_trigger", key="hidden_alert_btn", help="alert_trigger_btn", on_click=lambda: None)
|
| 2054 |
st.markdown("""
|
| 2055 |
<style>
|
| 2056 |
+
div[data-testid="stButton"]:has(button[title="alert_trigger_btn"]) {
|
| 2057 |
+
display: none !important;
|
| 2058 |
+
visibility: hidden !important;
|
| 2059 |
+
width: 0 !important;
|
| 2060 |
+
height: 0 !important;
|
| 2061 |
+
overflow: hidden !important;
|
| 2062 |
+
position: absolute !important;
|
| 2063 |
+
pointer-events: none !important;
|
| 2064 |
+
}
|
| 2065 |
</style>
|
| 2066 |
""", unsafe_allow_html=True)
|
| 2067 |
|