Update src/app.py
Browse files- src/app.py +109 -44
src/app.py
CHANGED
|
@@ -785,13 +785,16 @@ FAV_FILE = "my_fav_v30.csv"
|
|
| 785 |
TRADE_FILE = "my_trades_v30.csv"
|
| 786 |
|
| 787 |
# μ 보μ μ§λ¨ ꡬ쑰μμ λ°λμ μμ΄μΌ ν νλ
|
| 788 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 789 |
|
| 790 |
def _is_new_structure(report: dict) -> bool:
|
| 791 |
-
"""ꡬ ꡬ쑰
|
| 792 |
if not isinstance(report, dict):
|
| 793 |
return False
|
| 794 |
-
# μ ꡬ쑰 νλ μ€ νλλΌλ μμΌλ©΄ μ κ΅¬μ‘°λ‘ νμ
|
| 795 |
return bool(_NEW_REPORT_FIELDS & report.keys())
|
| 796 |
|
| 797 |
def _hf_dl(fn):
|
|
@@ -1803,24 +1806,52 @@ def parse_target_prices(target_price_str):
|
|
| 1803 |
pass
|
| 1804 |
return sorted(set(result))
|
| 1805 |
|
| 1806 |
-
def get_price_alert(action, current_price, target_price_str, avg_price):
|
| 1807 |
-
if not target_price_str or target_price_str == "-": return None, None
|
| 1808 |
-
prices = parse_target_prices(target_price_str)
|
| 1809 |
-
if not prices: return None, None
|
| 1810 |
action = str(action)
|
| 1811 |
-
|
| 1812 |
-
|
| 1813 |
-
|
| 1814 |
-
|
| 1815 |
-
|
| 1816 |
-
|
| 1817 |
-
|
| 1818 |
-
|
| 1819 |
-
|
| 1820 |
-
|
| 1821 |
-
|
| 1822 |
-
|
| 1823 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1824 |
return None, None
|
| 1825 |
|
| 1826 |
def render_price_alert(level, message, ticker, name):
|
|
@@ -1939,9 +1970,16 @@ def _build_alert_groups(prices, rate):
|
|
| 1939 |
|
| 1940 |
rp = st.session_state.full_stock_reports.get(f"{t}_{ap}", {}).get("report", {})
|
| 1941 |
act = rp.get("action_type", "")
|
| 1942 |
-
|
| 1943 |
-
|
| 1944 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1945 |
|
| 1946 |
if abs(daily_pct) >= 5.0:
|
| 1947 |
groups["κΈλ³"].append(dict(
|
|
@@ -1954,12 +1992,17 @@ def _build_alert_groups(prices, rate):
|
|
| 1954 |
if not act:
|
| 1955 |
continue
|
| 1956 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1957 |
item_strat = dict(
|
| 1958 |
ticker=t, name=nm, change_pct=total_pct,
|
| 1959 |
current_price=int(ck), avg_price=int(ap),
|
| 1960 |
target_price=tgp, action=act
|
| 1961 |
)
|
| 1962 |
-
if "μμ " in act:
|
| 1963 |
groups["μ€ν±λ‘μ€"].append(item_strat)
|
| 1964 |
elif "μ΅μ " in act:
|
| 1965 |
groups["μ΅μ "].append(item_strat)
|
|
@@ -2406,17 +2449,32 @@ elif st.session_state.main_menu == "β ν & μ€μΊ":
|
|
| 2406 |
else: st.session_state.fav_df = pd.concat([st.session_state.fav_df, pd.DataFrame([{"ν°μ»€":tn,"μ’
λͺ©λͺ
":n}])], ignore_index=True)
|
| 2407 |
save_fav(); st.rerun()
|
| 2408 |
|
| 2409 |
-
bypass
|
| 2410 |
-
|
| 2411 |
-
|
| 2412 |
-
|
| 2413 |
-
|
| 2414 |
-
|
| 2415 |
-
|
| 2416 |
-
|
| 2417 |
-
|
| 2418 |
-
|
| 2419 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2420 |
|
| 2421 |
render_stock_header(n, tn, p, stars, "μ€μ₯κΈ° μ±μ₯μ£Ό μ€μΊ", has_rich)
|
| 2422 |
tp = get_analyst_target(tn)
|
|
@@ -2661,24 +2719,27 @@ elif st.session_state.main_menu == "β€ μμ° μμΈ μ§λ¨":
|
|
| 2661 |
prg.text("ν¬νΈν΄λ¦¬μ€ λμ μ€μΊ λ° μ¬λ¬΄ λ°μ΄ν° μμ§ μ€...")
|
| 2662 |
asset_list = []
|
| 2663 |
|
| 2664 |
-
# μΉν° μ§κ³ & λ² ν μ§κ³
|
| 2665 |
sector_summary = {}
|
| 2666 |
-
beta_info
|
|
|
|
| 2667 |
|
| 2668 |
def _analyze(row):
|
| 2669 |
t = row['ν°μ»€']; ap = row['νλ¨κ°']
|
| 2670 |
key = f"{t}_{ap}"
|
| 2671 |
-
st2 = st.session_state.full_stock_reports.get(key, {})
|
| 2672 |
-
needs2 = st2.get("date") != today_str or st2.get("avg_price") != ap
|
| 2673 |
ni, pi, ci = get_ticker_info(t)
|
| 2674 |
sec, beta = get_ticker_metadata(t)
|
| 2675 |
ck = int(pi)
|
| 2676 |
pct = ((ck/ap)-1)*100 if ap>0 else 0
|
| 2677 |
buy_reason = str(row.get('λ§€μκ·Όκ±°', '') or '').strip()
|
| 2678 |
|
| 2679 |
-
#
|
| 2680 |
-
|
| 2681 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2682 |
|
| 2683 |
if needs2:
|
| 2684 |
fc, _ = get_rich_financial_context(t)
|
|
@@ -2693,12 +2754,16 @@ elif st.session_state.main_menu == "β€ μμ° μμΈ μ§λ¨":
|
|
| 2693 |
bypass_cache=True
|
| 2694 |
)
|
| 2695 |
if rp and "action_type" in rp:
|
| 2696 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2697 |
act = rp.get("action_type","보μ ")
|
| 2698 |
return f"- {row['μ’
λͺ©λͺ
']}({t}) [μΉν°:{sec}/Beta:{beta:.2f}]: μμ΅λ₯ {pct:.1f}%, [μ λ΅:{act}]"
|
| 2699 |
return f"- {row['μ’
λͺ©λͺ
']}({t}): μμ΅λ₯ {pct:.1f}%"
|
| 2700 |
else:
|
| 2701 |
-
act =
|
| 2702 |
return f"- {row['μ’
λͺ©λͺ
']}({t}) [μΉν°:{sec}/Beta:{beta:.2f}]: μμ΅λ₯ {pct:.1f}%, [μ λ΅:{act}]"
|
| 2703 |
|
| 2704 |
ctx = get_script_run_ctx()
|
|
|
|
| 785 |
TRADE_FILE = "my_trades_v30.csv"
|
| 786 |
|
| 787 |
# μ 보μ μ§λ¨ ꡬ쑰μμ λ°λμ μμ΄μΌ ν νλ
|
| 788 |
+
# 보μ μ§λ¨ μ ꡬ쑰 νλ
|
| 789 |
+
_OWNED_NEW_FIELDS = {"thesis_validity", "action_analysis", "additional_buy_analysis", "target_remaining"}
|
| 790 |
+
# μ κ· μ€μΊ μ ꡬ쑰 νλ
|
| 791 |
+
_SCAN_NEW_FIELDS = {"growth_moat_quantitative", "growth_moat_qualitative", "catalyst", "entry_zones"}
|
| 792 |
+
_NEW_REPORT_FIELDS = _OWNED_NEW_FIELDS | _SCAN_NEW_FIELDS
|
| 793 |
|
| 794 |
def _is_new_structure(report: dict) -> bool:
|
| 795 |
+
"""ꡬ ꡬ쑰μΈμ§ μ ꡬ쑰μΈμ§ νλ³ β 보μ μ§λ¨/μ€μΊ νλ λͺ¨λ ν¬ν¨"""
|
| 796 |
if not isinstance(report, dict):
|
| 797 |
return False
|
|
|
|
| 798 |
return bool(_NEW_REPORT_FIELDS & report.keys())
|
| 799 |
|
| 800 |
def _hf_dl(fn):
|
|
|
|
| 1806 |
pass
|
| 1807 |
return sorted(set(result))
|
| 1808 |
|
| 1809 |
+
def get_price_alert(action, current_price, target_price_str, avg_price, report=None):
|
|
|
|
|
|
|
|
|
|
| 1810 |
action = str(action)
|
| 1811 |
+
report = report or {}
|
| 1812 |
+
|
| 1813 |
+
# μ΅μ μλ: target_remaining.target_price κΈ°μ€
|
| 1814 |
+
if "μ΅μ " in action and target_price_str and target_price_str != "-":
|
| 1815 |
+
prices = parse_target_prices(target_price_str)
|
| 1816 |
+
if prices:
|
| 1817 |
+
tp = min(prices)
|
| 1818 |
+
ratio = current_price / tp if tp > 0 else 0
|
| 1819 |
+
if ratio >= 1.0:
|
| 1820 |
+
return 'tp_hit', f"μ΅μ λͺ©νκ° {tp:,}μ λλ¬ Β· μ§κΈ λ°λ‘ μ κ²νμΈμ"
|
| 1821 |
+
elif ratio >= 0.95:
|
| 1822 |
+
return 'tp', f"μ΅μ λͺ©νκ° {tp:,}μκΉμ§ {tp-current_price:,}μ λ¨μ ({ratio*100:.1f}%)"
|
| 1823 |
+
|
| 1824 |
+
# μμ μλ: ν
μ νΌμ νμ μ°μ , μμΌλ©΄ κ°κ²© κΈ°μ€
|
| 1825 |
+
if "μμ " in action:
|
| 1826 |
+
thesis = report.get("thesis_validity", {})
|
| 1827 |
+
verdict = thesis.get("verdict", "") if isinstance(thesis, dict) else ""
|
| 1828 |
+
if verdict == "νΌμ":
|
| 1829 |
+
return 'sl_broken', "λ§€μ ν
μ νΌμ νμ Β· μμ κ²ν νμ"
|
| 1830 |
+
if target_price_str and target_price_str != "-":
|
| 1831 |
+
prices = parse_target_prices(target_price_str)
|
| 1832 |
+
if prices:
|
| 1833 |
+
sl = min(prices)
|
| 1834 |
+
if sl > 0 and current_price <= sl:
|
| 1835 |
+
return 'sl_broken', f"μμ λμκ° {sl:,}μ μ΄ν Β· μ¦μ λμ νμ"
|
| 1836 |
+
elif sl > 0 and current_price <= sl * 1.05:
|
| 1837 |
+
return 'sl', f"μμ λμκ° {sl:,}μ κ·Όμ ({((current_price/sl)-1)*100:+.1f}%)"
|
| 1838 |
+
|
| 1839 |
+
# 보μ μ€ μμ€ + ν
μ μ κ²νμ
|
| 1840 |
+
if "보μ " in action and current_price < avg_price:
|
| 1841 |
+
thesis = report.get("thesis_validity", {})
|
| 1842 |
+
verdict = thesis.get("verdict", "") if isinstance(thesis, dict) else ""
|
| 1843 |
+
if verdict == "μ κ²νμ":
|
| 1844 |
+
loss_pct = (current_price / avg_price - 1) * 100
|
| 1845 |
+
return 'sl', f"ν
μ μ κ²νμ + μμ€ μ€ ({loss_pct:+.1f}%) Β· μ¬μ§λ¨ κΆμ₯"
|
| 1846 |
+
if target_price_str and target_price_str != "-":
|
| 1847 |
+
prices = parse_target_prices(target_price_str)
|
| 1848 |
+
if prices:
|
| 1849 |
+
sl = min(prices)
|
| 1850 |
+
if sl > 0 and current_price <= sl:
|
| 1851 |
+
return 'sl_broken', f"μμ λμκ° {sl:,}μ μ΄ν Β· μ¦μ λμ νμ"
|
| 1852 |
+
elif sl > 0 and current_price <= sl * 1.05:
|
| 1853 |
+
return 'sl', f"μμ λμκ° {sl:,}μ κ·Όμ ({((current_price/sl)-1)*100:+.1f}%)"
|
| 1854 |
+
|
| 1855 |
return None, None
|
| 1856 |
|
| 1857 |
def render_price_alert(level, message, ticker, name):
|
|
|
|
| 1970 |
|
| 1971 |
rp = st.session_state.full_stock_reports.get(f"{t}_{ap}", {}).get("report", {})
|
| 1972 |
act = rp.get("action_type", "")
|
| 1973 |
+
|
| 1974 |
+
# ββ λͺ©νκ° μΆμΆ: μ ꡬ쑰(target_remaining) μ°μ , μΆκ°λ§€μκ° fallback ββ
|
| 1975 |
+
target_rem = rp.get("target_remaining", {})
|
| 1976 |
+
add_buy = rp.get("additional_buy_analysis", {})
|
| 1977 |
+
if isinstance(target_rem, dict) and target_rem.get("target_price", "-") not in ("-", "", None):
|
| 1978 |
+
tgp = target_rem["target_price"] # μ ꡬ쑰 λͺ©νκ°
|
| 1979 |
+
elif isinstance(add_buy, dict) and add_buy.get("suggested_price", "-") not in ("-", "", None):
|
| 1980 |
+
tgp = add_buy["suggested_price"] # μΆκ°λ§€μ μ μ κ° fallback
|
| 1981 |
+
else:
|
| 1982 |
+
tgp = "-" # λΆμ μ λλ λ―ΈκΈ°λ‘
|
| 1983 |
|
| 1984 |
if abs(daily_pct) >= 5.0:
|
| 1985 |
groups["κΈλ³"].append(dict(
|
|
|
|
| 1992 |
if not act:
|
| 1993 |
continue
|
| 1994 |
|
| 1995 |
+
# ν
μ νΌμ μ¬λΆ νμΈ β actionμ΄ "보μ "μ¬λ μ€ν±λ‘μ€ κ·Έλ£ΉμΌλ‘
|
| 1996 |
+
thesis = rp.get("thesis_validity", {})
|
| 1997 |
+
verdict = thesis.get("verdict", "") if isinstance(thesis, dict) else ""
|
| 1998 |
+
thesis_broken = (verdict == "νΌμ")
|
| 1999 |
+
|
| 2000 |
item_strat = dict(
|
| 2001 |
ticker=t, name=nm, change_pct=total_pct,
|
| 2002 |
current_price=int(ck), avg_price=int(ap),
|
| 2003 |
target_price=tgp, action=act
|
| 2004 |
)
|
| 2005 |
+
if "μμ " in act or thesis_broken:
|
| 2006 |
groups["μ€ν±λ‘μ€"].append(item_strat)
|
| 2007 |
elif "μ΅μ " in act:
|
| 2008 |
groups["μ΅μ "].append(item_strat)
|
|
|
|
| 2449 |
else: st.session_state.fav_df = pd.concat([st.session_state.fav_df, pd.DataFrame([{"ν°μ»€":tn,"μ’
λͺ©λͺ
":n}])], ignore_index=True)
|
| 2450 |
save_fav(); st.rerun()
|
| 2451 |
|
| 2452 |
+
bypass = st.session_state.pop('force_home_refresh', False)
|
| 2453 |
+
scan_key = f"scan_{tn}"
|
| 2454 |
+
stored_scan = st.session_state.full_stock_reports.get(scan_key, {})
|
| 2455 |
+
# λΉμΌ μΊμ μ ν¨ μ¬λΆ: λ μ§ κ°κ³ κ°μ μλ‘κ³ μΉ¨ μλ λ
|
| 2456 |
+
needs_scan = bypass or stored_scan.get("date") != today_str
|
| 2457 |
+
|
| 2458 |
+
if needs_scan:
|
| 2459 |
+
with st.spinner(f"{n} μ€μ₯κΈ° μ±μ₯μ£Ό μ€μΊ μ€..."):
|
| 2460 |
+
fin_ctx, has_rich = get_rich_financial_context(tn)
|
| 2461 |
+
news_ctx = get_recent_news(tn)
|
| 2462 |
+
owned_list = st.session_state.df['ν°μ»€'].tolist() if not st.session_state.df.empty else []
|
| 2463 |
+
target_info = f"μ’
λͺ©: {n}({tn})\nνμ¬κ°: {p:,}μ\n[μ€μ μ¬λ¬΄ λ°μ΄ν°]\n{fin_ctx}"
|
| 2464 |
+
report = get_ai_json_response(
|
| 2465 |
+
get_scan_prompt(target_info, tid=tn, owned=owned_list, news=news_ctx),
|
| 2466 |
+
bypass_cache=True
|
| 2467 |
+
)
|
| 2468 |
+
if report:
|
| 2469 |
+
st.session_state.full_stock_reports[scan_key] = {
|
| 2470 |
+
"date": today_str, "report": report, "avg_price": 0
|
| 2471 |
+
}
|
| 2472 |
+
save_cache()
|
| 2473 |
+
else:
|
| 2474 |
+
report = stored_scan["report"]
|
| 2475 |
+
has_rich = True
|
| 2476 |
+
|
| 2477 |
+
stars = report.get("recommendation_stars","β
β
β
β
β") if report else "β
β
β
β
β"
|
| 2478 |
|
| 2479 |
render_stock_header(n, tn, p, stars, "μ€μ₯κΈ° μ±μ₯μ£Ό μ€μΊ", has_rich)
|
| 2480 |
tp = get_analyst_target(tn)
|
|
|
|
| 2719 |
prg.text("ν¬νΈν΄λ¦¬μ€ λμ μ€μΊ λ° μ¬λ¬΄ λ°μ΄ν° μμ§ μ€...")
|
| 2720 |
asset_list = []
|
| 2721 |
|
| 2722 |
+
# μΉν° μ§κ³ & λ² ν μ§κ³ β μ€λ λ μμ Lock μ¬μ©
|
| 2723 |
sector_summary = {}
|
| 2724 |
+
beta_info = {}
|
| 2725 |
+
_shared_lock = threading.Lock()
|
| 2726 |
|
| 2727 |
def _analyze(row):
|
| 2728 |
t = row['ν°μ»€']; ap = row['νλ¨κ°']
|
| 2729 |
key = f"{t}_{ap}"
|
|
|
|
|
|
|
| 2730 |
ni, pi, ci = get_ticker_info(t)
|
| 2731 |
sec, beta = get_ticker_metadata(t)
|
| 2732 |
ck = int(pi)
|
| 2733 |
pct = ((ck/ap)-1)*100 if ap>0 else 0
|
| 2734 |
buy_reason = str(row.get('λ§€μκ·Όκ±°', '') or '').strip()
|
| 2735 |
|
| 2736 |
+
# ββ 곡μ λμ
λ리 μ°κΈ°: LockμΌλ‘ λ³΄νΈ ββββββββββββββ
|
| 2737 |
+
with _shared_lock:
|
| 2738 |
+
sector_summary[sec] = sector_summary.get(sec, 0) + 1
|
| 2739 |
+
beta_info[t] = round(beta, 2)
|
| 2740 |
+
cached = st.session_state.full_stock_reports.get(key, {})
|
| 2741 |
+
|
| 2742 |
+
needs2 = cached.get("date") != today_str or cached.get("avg_price") != ap
|
| 2743 |
|
| 2744 |
if needs2:
|
| 2745 |
fc, _ = get_rich_financial_context(t)
|
|
|
|
| 2754 |
bypass_cache=True
|
| 2755 |
)
|
| 2756 |
if rp and "action_type" in rp:
|
| 2757 |
+
# ββ μΊμ μ°κΈ°λ Lock λ³΄νΈ ββββββββββββββββββββ
|
| 2758 |
+
with _shared_lock:
|
| 2759 |
+
st.session_state.full_stock_reports[key] = {
|
| 2760 |
+
"date": today_str, "report": rp, "avg_price": ap
|
| 2761 |
+
}
|
| 2762 |
act = rp.get("action_type","보μ ")
|
| 2763 |
return f"- {row['μ’
λͺ©λͺ
']}({t}) [μΉν°:{sec}/Beta:{beta:.2f}]: μμ΅λ₯ {pct:.1f}%, [μ λ΅:{act}]"
|
| 2764 |
return f"- {row['μ’
λͺ©λͺ
']}({t}): μμ΅λ₯ {pct:.1f}%"
|
| 2765 |
else:
|
| 2766 |
+
act = cached.get('report', {}).get('action_type','보μ ')
|
| 2767 |
return f"- {row['μ’
λͺ©λͺ
']}({t}) [μΉν°:{sec}/Beta:{beta:.2f}]: μμ΅λ₯ {pct:.1f}%, [μ λ΅:{act}]"
|
| 2768 |
|
| 2769 |
ctx = get_script_run_ctx()
|