Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +161 -171
streamlit_app.py
CHANGED
|
@@ -3,13 +3,30 @@ import yfinance as yf
|
|
| 3 |
import FinanceDataReader as fdr
|
| 4 |
import pandas as pd
|
| 5 |
import requests
|
|
|
|
| 6 |
from huggingface_hub import InferenceClient
|
| 7 |
|
| 8 |
-
# 1. ํ์ด์ง ์ค์
|
| 9 |
-
st.set_page_config(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
|
| 11 |
-
|
| 12 |
-
st.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 13 |
|
| 14 |
# 2. ํ ํฐ ํ์ธ
|
| 15 |
if "HF_TOKEN" in st.secrets:
|
|
@@ -18,101 +35,42 @@ else:
|
|
| 18 |
st.error("๐จ ์ค์ ์ค๋ฅ: Secrets์ 'HF_TOKEN'์ด ํ์ํฉ๋๋ค.")
|
| 19 |
st.stop()
|
| 20 |
|
| 21 |
-
# ---
|
| 22 |
-
# ์ฌ์ฉ์๊ฐ ์ด๋ฆ๋ง ๋๋ฉด ๋ฐ๋ก ์ฐ๊ฒฐํด์ฃผ๋ ํซ๋ผ์ธ์
๋๋ค.
|
| 23 |
GLOBAL_TICKER_MAP = {
|
| 24 |
-
|
| 25 |
-
"
|
| 26 |
-
"
|
| 27 |
-
"
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"
|
| 31 |
-
"TENCENT": "0700.HK", "ํ
์ผํธ": "0700.HK",
|
| 32 |
-
"BYD": "1211.HK", "๋น์ผ๋": "1211.HK",
|
| 33 |
-
"XIAOMI": "1810.HK", "์ค์ค๋ฏธ": "1810.HK",
|
| 34 |
-
"MEITUAN": "3690.HK", "๋ฉ์ดํฌ์": "3690.HK",
|
| 35 |
-
"MAOUTAI": "600519.SS", "๋ง์คํ์ด": "600519.SS", "๊ท์ฃผ๋ชจํ์ฃผ": "600519.SS",
|
| 36 |
-
|
| 37 |
-
# ๐ฎ๐ณ ์ธ๋ (India)
|
| 38 |
-
"RELIANCE": "RELIANCE.NS", "๋ฆด๋ผ์ด์ธ์ค": "RELIANCE.NS",
|
| 39 |
-
"TCS": "TCS.NS", "ํํ": "TCS.NS",
|
| 40 |
-
"INFOSYS": "INFY.NS", "์ธํฌ์์ค": "INFY.NS",
|
| 41 |
-
"HDFC": "HDFCBANK.NS",
|
| 42 |
-
|
| 43 |
-
# ๐ธ๐ฌ ์ฑ๊ฐํฌ๋ฅด (Singapore)
|
| 44 |
-
"DBS": "D05.SI", "๋๋น์์ค": "D05.SI",
|
| 45 |
-
"SINGTEL": "Z74.SI", "์ฑํ
": "Z74.SI",
|
| 46 |
-
"SHOPEE": "SE", "์ผํผ": "SE", # (SEA Ltd๋ ๋ฏธ๊ตญ ์์ฅ)
|
| 47 |
-
|
| 48 |
-
# ๐ฏ๐ต ์ผ๋ณธ (Japan)
|
| 49 |
-
"TOYOTA": "7203.T", "๋์ํ": "7203.T",
|
| 50 |
-
"SONY": "6758.T", "์๋": "6758.T",
|
| 51 |
-
"SOFTBANK": "9984.T", "์ํํธ๋ฑ
ํฌ": "9984.T",
|
| 52 |
-
"NINTENDO": "7974.T", "๋ํ
๋": "7974.T",
|
| 53 |
-
|
| 54 |
-
# ๐บ๐ธ ๋ฏธ๊ตญ
|
| 55 |
-
"TESLA": "TSLA", "ํ
์ฌ๋ผ": "TSLA", "APPLE": "AAPL", "NVDA": "NVDA",
|
| 56 |
-
|
| 57 |
-
# ๐ฐ๐ท ํ๊ตญ
|
| 58 |
-
"SAMSUNG": "005930.KS", "์ผ์ฑ์ ์": "005930.KS",
|
| 59 |
-
"HYNIX": "000660.KS", "SKํ์ด๋์ค": "000660.KS"
|
| 60 |
}
|
| 61 |
|
| 62 |
-
# --- [ํต์ฌ 2] ๊ตญ๊ฐ๋ณ ์์ฅ ๋ํ ์ํ (๋์๋ณด๋์ฉ) ---
|
| 63 |
MARKET_SAMPLES = {
|
| 64 |
-
"
|
| 65 |
-
"
|
| 66 |
-
"๐ฏ๐ต ์ผ๋ณธ (Japan)": ["7203.T", "6758.T", "9984.T", "8035.T", "6861.T"
|
| 67 |
-
"๐จ๐ณ ์ค๊ตญ/ํ์ฝฉ
|
| 68 |
-
"๐น๐ผ ๋๋ง (Taiwan)": ["2330.TW", "2454.TW", "2317.TW", "2308.TW"
|
| 69 |
-
"๐ฎ๐ณ ์ธ๋ (India)": ["RELIANCE.NS", "TCS.NS", "HDFCBANK.NS", "INFY.NS"
|
| 70 |
-
"
|
| 71 |
-
"๐ฎ๐ฉ ์ธ๋๋ค์์ (Indonesia)": ["BBCA.JK", "BBRI.JK", "TLKM.JK", "BMRI.JK", "ASII.JK", "UNVR.JK"],
|
| 72 |
-
"๐ฒ๐พ ๋ง๋ ์ด์์ (Malaysia)": ["1155.KL", "1023.KL", "1295.KL", "5347.KL", "5183.KL", "6033.KL"],
|
| 73 |
-
"๐น๐ญ ํ๊ตญ (Thailand)": ["PTT.BK", "AOT.BK", "CPALL.BK", "ADVANC.BK", "SCC.BK", "KBANK.BK"],
|
| 74 |
-
"๐ต๐ญ ํ๋ฆฌํ (Philippines)": ["SM.PS", "SMPH.PS", "BDO.PS", "ALI.PS", "BPI.PS", "ICT.PS"],
|
| 75 |
-
"๐ป๐ณ ๋ฒ ํธ๋จ (Vietnam)": ["VIC", "VHM", "VCB", "VNM", "GAS", "HPG", "BID"]
|
| 76 |
}
|
| 77 |
|
| 78 |
-
# ---
|
| 79 |
def get_ticker_from_ai(name):
|
| 80 |
try:
|
| 81 |
-
prompt = f""
|
| 82 |
-
Find the Yahoo Finance ticker for '{name}' in Asian markets.
|
| 83 |
-
|
| 84 |
-
[Rules for Suffixes]
|
| 85 |
-
- Japan: .T
|
| 86 |
-
- Hong Kong: .HK
|
| 87 |
-
- Taiwan: .TW
|
| 88 |
-
- India: .NS (NSE)
|
| 89 |
-
- Singapore: .SI
|
| 90 |
-
- Indonesia: .JK
|
| 91 |
-
- Malaysia: .KL
|
| 92 |
-
- Thailand: .BK
|
| 93 |
-
- Philippines: .PS
|
| 94 |
-
- Korea: .KS or .KQ
|
| 95 |
-
|
| 96 |
-
Return ONLY the ticker string (e.g., 2330.TW). No text.
|
| 97 |
-
"""
|
| 98 |
messages = [{"role": "user", "content": prompt}]
|
| 99 |
response = client.chat.completions.create(
|
| 100 |
model="Qwen/Qwen2.5-72B-Instruct", messages=messages, max_tokens=20
|
| 101 |
)
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
return ticker
|
| 105 |
-
except:
|
| 106 |
-
return None
|
| 107 |
|
| 108 |
-
# --- ํตํฉ ๊ฒ์ ํจ์ ---
|
| 109 |
def search_ticker(query):
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
return GLOBAL_TICKER_MAP[query_upper]
|
| 113 |
-
return get_ticker_from_ai(query)
|
| 114 |
|
| 115 |
-
# --- ๋ค์ด๋ฒ ๊ธ์ต ํฌ๋กค๋ง (ํ๊ตญ ์ ์ฉ) ---
|
| 116 |
def get_naver_stock_info(code):
|
| 117 |
try:
|
| 118 |
code_num = code.split('.')[0]
|
|
@@ -122,145 +80,177 @@ def get_naver_stock_info(code):
|
|
| 122 |
dfs = pd.read_html(res.text, encoding='euc-kr')
|
| 123 |
info = {}
|
| 124 |
for df in dfs:
|
| 125 |
-
|
| 126 |
-
if "PER" in str_df:
|
| 127 |
for i in range(len(df)):
|
| 128 |
for j in range(len(df.columns)):
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
try: info['trailingPE'] = float(df.iloc[i, j+1])
|
| 132 |
except: pass
|
| 133 |
-
if "PBR" in
|
| 134 |
-
try: info['priceToBook'] = float(df.iloc[i,
|
| 135 |
except: pass
|
| 136 |
return info
|
| 137 |
except: return {}
|
| 138 |
|
| 139 |
-
# --- ์คํธ๋ฆผ ํํฐ ---
|
| 140 |
def parse_stream(stream):
|
| 141 |
for chunk in stream:
|
| 142 |
if chunk.choices and len(chunk.choices) > 0:
|
| 143 |
delta = chunk.choices[0].delta
|
| 144 |
if hasattr(delta, 'content') and delta.content:
|
| 145 |
-
|
| 146 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 147 |
|
| 148 |
# ==============================================================================
|
| 149 |
-
#
|
| 150 |
# ==============================================================================
|
| 151 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 152 |
|
| 153 |
-
#
|
| 154 |
-
|
| 155 |
-
|
|
|
|
|
|
|
| 156 |
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 161 |
ticker = search_ticker(user_input)
|
| 162 |
|
| 163 |
if ticker:
|
| 164 |
-
st.write(f"โ
ํฐ์ปค ํ์ธ: **{ticker}**")
|
| 165 |
try:
|
|
|
|
| 166 |
stock = yf.Ticker(ticker)
|
| 167 |
hist = stock.history(period="3mo")
|
| 168 |
|
| 169 |
if not hist.empty:
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
change_1mo = ((curr_close - start_close) / start_close) * 100
|
| 176 |
|
|
|
|
| 177 |
info = stock.info
|
| 178 |
if ".KS" in ticker or ".KQ" in ticker:
|
| 179 |
info.update(get_naver_stock_info(ticker))
|
| 180 |
|
| 181 |
per = info.get('trailingPE', 'N/A')
|
| 182 |
pbr = info.get('priceToBook', 'N/A')
|
| 183 |
-
currency = info.get('currency', '
|
| 184 |
|
| 185 |
-
|
| 186 |
-
[ROLE]
|
| 187 |
-
Asian Stock Market Expert.
|
| 188 |
|
| 189 |
-
[
|
| 190 |
-
|
| 191 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 192 |
|
| 193 |
-
[
|
| 194 |
-
|
| 195 |
-
- 1D Change: {change_1d:.2f}%
|
| 196 |
-
- 1M Change: {change_1mo:.2f}%
|
| 197 |
-
- PER/PBR: {per} / {pbr}
|
| 198 |
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
|
|
|
|
|
|
|
|
|
| 204 |
"""
|
| 205 |
|
| 206 |
-
status.update(label="โ
๋ถ์ ์๋ฃ!", state="complete", expanded=False)
|
| 207 |
-
|
| 208 |
-
st.divider()
|
| 209 |
-
c1, c2, c3 = st.columns(3)
|
| 210 |
-
c1.metric(f"ํ์ฌ๊ฐ ({currency})", f"{curr_close:,.2f}", f"{change_1d:.2f}%")
|
| 211 |
-
c2.metric("1๋ฌ ์์ต๋ฅ ", f"{change_1mo:.2f}%")
|
| 212 |
-
c3.metric("PER / PBR", f"{per} / {pbr}")
|
| 213 |
-
|
| 214 |
-
st.line_chart(hist['Close'])
|
| 215 |
-
|
| 216 |
-
st.subheader(f"๐ AI Insight: {user_input}")
|
| 217 |
messages = [{"role": "user", "content": prompt}]
|
| 218 |
stream = client.chat.completions.create(
|
| 219 |
model="Qwen/Qwen2.5-72B-Instruct", messages=messages, stream=True
|
| 220 |
)
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
else:
|
| 223 |
-
status.update(label="โ
|
| 224 |
-
st.error("๋ฐ์ดํฐ
|
| 225 |
except Exception as e:
|
| 226 |
-
status.update(label="โ
|
| 227 |
-
st.error(f"
|
| 228 |
else:
|
| 229 |
-
status.update(label="โ
|
| 230 |
-
st.error("
|
| 231 |
|
| 232 |
-
#
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
|
|
|
| 236 |
|
| 237 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 238 |
|
| 239 |
-
if
|
| 240 |
-
|
| 241 |
-
with st.spinner(f"
|
| 242 |
try:
|
| 243 |
-
data = yf.download(
|
| 244 |
if not data.empty:
|
| 245 |
-
|
| 246 |
prev = data.iloc[-2]
|
| 247 |
-
pct = ((
|
| 248 |
|
| 249 |
-
|
| 250 |
-
|
| 251 |
|
| 252 |
-
#
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
|
| 257 |
-
with
|
| 258 |
-
st.
|
| 259 |
-
st.dataframe(
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
else:
|
| 264 |
-
st.warning("
|
| 265 |
except Exception as e:
|
| 266 |
-
st.error(f"
|
|
|
|
| 3 |
import FinanceDataReader as fdr
|
| 4 |
import pandas as pd
|
| 5 |
import requests
|
| 6 |
+
import plotly.graph_objects as go
|
| 7 |
from huggingface_hub import InferenceClient
|
| 8 |
|
| 9 |
+
# 1. ํ์ด์ง ์ค์ (Wide ๋ชจ๋ & ๋คํฌ ํ
๋ง ๊ฐ์ฑ)
|
| 10 |
+
st.set_page_config(
|
| 11 |
+
page_title="Pocket Quant Pro",
|
| 12 |
+
page_icon="๐",
|
| 13 |
+
layout="wide",
|
| 14 |
+
initial_sidebar_state="expanded"
|
| 15 |
+
)
|
| 16 |
|
| 17 |
+
# --- [์คํ์ผ๋ง] CSS๋ก ์ฝ๊ฐ์ ํ์ฅ (๋ฉํธ๋ฆญ ์นด๋ ์คํ์ผ) ---
|
| 18 |
+
st.markdown("""
|
| 19 |
+
<style>
|
| 20 |
+
[data-testid="stMetricValue"] {
|
| 21 |
+
font-size: 24px;
|
| 22 |
+
}
|
| 23 |
+
.st-emotion-cache-1r6slb0 {
|
| 24 |
+
border: 1px solid #333;
|
| 25 |
+
border-radius: 10px;
|
| 26 |
+
padding: 15px;
|
| 27 |
+
}
|
| 28 |
+
</style>
|
| 29 |
+
""", unsafe_allow_html=True)
|
| 30 |
|
| 31 |
# 2. ํ ํฐ ํ์ธ
|
| 32 |
if "HF_TOKEN" in st.secrets:
|
|
|
|
| 35 |
st.error("๐จ ์ค์ ์ค๋ฅ: Secrets์ 'HF_TOKEN'์ด ํ์ํฉ๋๋ค.")
|
| 36 |
st.stop()
|
| 37 |
|
| 38 |
+
# --- ๋ฐ์ดํฐ & ์กฑ๋ณด (์ด์ ๊ณผ ๋์ผ) ---
|
|
|
|
| 39 |
GLOBAL_TICKER_MAP = {
|
| 40 |
+
"TSMC": "2330.TW", "MEDIATEK": "2454.TW", "FOXCONN": "2317.TW",
|
| 41 |
+
"ALIBABA": "9988.HK", "TENCENT": "0700.HK", "BYD": "1211.HK",
|
| 42 |
+
"RELIANCE": "RELIANCE.NS", "TCS": "TCS.NS", "INFOSYS": "INFY.NS",
|
| 43 |
+
"DBS": "D05.SI", "SINGTEL": "Z74.SI", "SEA": "SE",
|
| 44 |
+
"TOYOTA": "7203.T", "SONY": "6758.T", "SOFTBANK": "9984.T", "NINTENDO": "7974.T",
|
| 45 |
+
"TESLA": "TSLA", "APPLE": "AAPL", "NVDA": "NVDA", "MSFT": "MSFT",
|
| 46 |
+
"SAMSUNG": "005930.KS", "HYNIX": "000660.KS", "NAVER": "035420.KS"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
}
|
| 48 |
|
|
|
|
| 49 |
MARKET_SAMPLES = {
|
| 50 |
+
"๐บ๐ธ ๋ฏธ๊ตญ (USA)": ["AAPL", "NVDA", "TSLA", "AMZN", "MSFT", "GOOGL", "AMD"],
|
| 51 |
+
"๐ฐ๐ท ํ๊ตญ (Korea)": ["005930.KS", "000660.KS", "035420.KS", "005380.KS", "051910.KS"],
|
| 52 |
+
"๐ฏ๐ต ์ผ๋ณธ (Japan)": ["7203.T", "6758.T", "9984.T", "8035.T", "6861.T"],
|
| 53 |
+
"๐จ๐ณ ์ค๊ตญ/ํ์ฝฉ": ["9988.HK", "0700.HK", "3690.HK", "1211.HK", "1810.HK"],
|
| 54 |
+
"๐น๐ผ ๋๋ง (Taiwan)": ["2330.TW", "2454.TW", "2317.TW", "2308.TW"],
|
| 55 |
+
"๐ฎ๐ณ ์ธ๋ (India)": ["RELIANCE.NS", "TCS.NS", "HDFCBANK.NS", "INFY.NS"],
|
| 56 |
+
"๐ป๐ณ ๋ฒ ํธ๋จ/์์ธ์": ["VIC", "VHM", "BBCA.JK", "D05.SI", "1155.KL"]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
}
|
| 58 |
|
| 59 |
+
# --- ํฌํผ ํจ์๋ค ---
|
| 60 |
def get_ticker_from_ai(name):
|
| 61 |
try:
|
| 62 |
+
prompt = f"Find Yahoo Finance ticker for '{name}'. Return ONLY ticker (e.g. 7203.T). No text."
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
messages = [{"role": "user", "content": prompt}]
|
| 64 |
response = client.chat.completions.create(
|
| 65 |
model="Qwen/Qwen2.5-72B-Instruct", messages=messages, max_tokens=20
|
| 66 |
)
|
| 67 |
+
return response.choices[0].message.content.strip().replace("`", "").upper().split()[0]
|
| 68 |
+
except: return None
|
|
|
|
|
|
|
|
|
|
| 69 |
|
|
|
|
| 70 |
def search_ticker(query):
|
| 71 |
+
q = query.upper().strip()
|
| 72 |
+
return GLOBAL_TICKER_MAP.get(q, get_ticker_from_ai(query))
|
|
|
|
|
|
|
| 73 |
|
|
|
|
| 74 |
def get_naver_stock_info(code):
|
| 75 |
try:
|
| 76 |
code_num = code.split('.')[0]
|
|
|
|
| 80 |
dfs = pd.read_html(res.text, encoding='euc-kr')
|
| 81 |
info = {}
|
| 82 |
for df in dfs:
|
| 83 |
+
if "PER" in str(df):
|
|
|
|
| 84 |
for i in range(len(df)):
|
| 85 |
for j in range(len(df.columns)):
|
| 86 |
+
if "PER" in str(df.iloc[i,j]) and "EPS" not in str(df.iloc[i,j]):
|
| 87 |
+
try: info['trailingPE'] = float(df.iloc[i,j+1])
|
|
|
|
| 88 |
except: pass
|
| 89 |
+
if "PBR" in str(df.iloc[i,j]):
|
| 90 |
+
try: info['priceToBook'] = float(df.iloc[i,j+1])
|
| 91 |
except: pass
|
| 92 |
return info
|
| 93 |
except: return {}
|
| 94 |
|
|
|
|
| 95 |
def parse_stream(stream):
|
| 96 |
for chunk in stream:
|
| 97 |
if chunk.choices and len(chunk.choices) > 0:
|
| 98 |
delta = chunk.choices[0].delta
|
| 99 |
if hasattr(delta, 'content') and delta.content:
|
| 100 |
+
yield delta.content.replace("```markdown", "").replace("```", "")
|
| 101 |
+
|
| 102 |
+
# --- [์ ๊ท ๊ธฐ๋ฅ] ํ๋กํ์
๋ ์บ๋ค ์ฐจํธ ๊ทธ๋ฆฌ๊ธฐ ---
|
| 103 |
+
def plot_candle_chart(hist, title):
|
| 104 |
+
fig = go.Figure(data=[go.Candlestick(
|
| 105 |
+
x=hist.index,
|
| 106 |
+
open=hist['Open'], high=hist['High'],
|
| 107 |
+
low=hist['Low'], close=hist['Close'],
|
| 108 |
+
name=title
|
| 109 |
+
)])
|
| 110 |
+
fig.update_layout(
|
| 111 |
+
title=dict(text=f"{title} Price Chart", x=0, font=dict(size=14)),
|
| 112 |
+
height=350, margin=dict(l=20, r=20, t=40, b=20),
|
| 113 |
+
xaxis_rangeslider_visible=False, # ํ๋จ ์ฌ๋ผ์ด๋ ์ ๊ฑฐ (๊น๋ํ๊ฒ)
|
| 114 |
+
template="plotly_dark", # ๋คํฌ ๋ชจ๋ ํ
๋ง
|
| 115 |
+
paper_bgcolor='rgba(0,0,0,0)', # ๋ฐฐ๊ฒฝ ํฌ๋ช
|
| 116 |
+
plot_bgcolor='rgba(0,0,0,0)'
|
| 117 |
+
)
|
| 118 |
+
return fig
|
| 119 |
|
| 120 |
# ==============================================================================
|
| 121 |
+
# ๐ฑ ์ฌ์ด๋๋ฐ (Navigation)
|
| 122 |
# ==============================================================================
|
| 123 |
+
with st.sidebar:
|
| 124 |
+
st.header("๐ Pocket Quant Pro")
|
| 125 |
+
st.write("AI Powered Global Investment")
|
| 126 |
+
st.divider()
|
| 127 |
+
|
| 128 |
+
# ๋ฉ๋ด ์ ํ
|
| 129 |
+
menu = st.radio("MENU", ["๐ ์ข
๋ชฉ ๋ถ์ (AI Analysis)", "๐ก ์์ฅ ์ค์บ๋ (Market Watch)"], index=0)
|
| 130 |
+
|
| 131 |
+
st.divider()
|
| 132 |
+
st.caption("Powered by Qwen-2.5-72B")
|
| 133 |
+
st.caption("Data: Yahoo Finance & Naver")
|
| 134 |
|
| 135 |
+
# ==============================================================================
|
| 136 |
+
# 1. ์ข
๋ชฉ ๋ถ์ ๋ชจ๋
|
| 137 |
+
# ==============================================================================
|
| 138 |
+
if "์ข
๋ชฉ ๋ถ์" in menu:
|
| 139 |
+
st.subheader("๐ AI Investment Analyst")
|
| 140 |
|
| 141 |
+
# ์
๋ ฅ์ฐฝ๊ณผ ๋ฒํผ์ ๋๋ํ ๋ฐฐ์น (๋ชจ๋ฐ์ผ ์นํ์ )
|
| 142 |
+
c1, c2 = st.columns([3, 1])
|
| 143 |
+
with c1:
|
| 144 |
+
user_input = st.text_input("Ticker or Name", "TSMC", label_visibility="collapsed", placeholder="์ข
๋ชฉ๋ช
์
๋ ฅ (์: ์ผ์ฑ์ ์, Tesla)")
|
| 145 |
+
with c2:
|
| 146 |
+
analyze_btn = st.button("Analysis", use_container_width=True)
|
| 147 |
+
|
| 148 |
+
if analyze_btn:
|
| 149 |
+
with st.status("๐ Processing Market Data...", expanded=True) as status:
|
| 150 |
ticker = search_ticker(user_input)
|
| 151 |
|
| 152 |
if ticker:
|
|
|
|
| 153 |
try:
|
| 154 |
+
# ๋ฐ์ดํฐ ์์ง
|
| 155 |
stock = yf.Ticker(ticker)
|
| 156 |
hist = stock.history(period="3mo")
|
| 157 |
|
| 158 |
if not hist.empty:
|
| 159 |
+
# ๊ธฐ๋ณธ ๋ฐ์ดํฐ ๊ณ์ฐ
|
| 160 |
+
curr = hist['Close'].iloc[-1]
|
| 161 |
+
prev = hist['Close'].iloc[-2]
|
| 162 |
+
change_pct = ((curr - prev) / prev) * 100
|
| 163 |
+
vol = hist['Volume'].iloc[-1]
|
|
|
|
| 164 |
|
| 165 |
+
# ์ ๋ณด ์์ง
|
| 166 |
info = stock.info
|
| 167 |
if ".KS" in ticker or ".KQ" in ticker:
|
| 168 |
info.update(get_naver_stock_info(ticker))
|
| 169 |
|
| 170 |
per = info.get('trailingPE', 'N/A')
|
| 171 |
pbr = info.get('priceToBook', 'N/A')
|
| 172 |
+
currency = info.get('currency', '')
|
| 173 |
|
| 174 |
+
status.update(label="โ
Data Ready! Rendering...", state="complete", expanded=False)
|
|
|
|
|
|
|
| 175 |
|
| 176 |
+
# --- [UI ๋์์ธ] ๋ฉํธ๋ฆญ ๋์๋ณด๋ ---
|
| 177 |
+
st.divider()
|
| 178 |
+
m1, m2, m3, m4 = st.columns(4)
|
| 179 |
+
m1.metric("Current Price", f"{curr:,.2f} {currency}", f"{change_pct:.2f}%")
|
| 180 |
+
m2.metric("PER", f"{per}")
|
| 181 |
+
m3.metric("PBR", f"{pbr}")
|
| 182 |
+
m4.metric("Volume", f"{vol:,.0f}")
|
| 183 |
|
| 184 |
+
# --- [UI ๋์์ธ] ์บ๋ค ์ฐจํธ (Plotly) ---
|
| 185 |
+
st.plotly_chart(plot_candle_chart(hist, ticker), use_container_width=True)
|
|
|
|
|
|
|
|
|
|
| 186 |
|
| 187 |
+
# --- AI ๋ฆฌํฌํธ ---
|
| 188 |
+
st.subheader(f"๐ง AI Insight: {user_input}")
|
| 189 |
+
prompt = f"""
|
| 190 |
+
[ROLE] Global Analyst.
|
| 191 |
+
[TASK] Analyze {user_input} ({ticker}).
|
| 192 |
+
[DATA] Price:{curr}, Chg:{change_pct:.2f}%, PER:{per}, PBR:{pbr}
|
| 193 |
+
[LANG] Reply in user's language.
|
| 194 |
+
[OUT] Markdown. No Code blocks. 1.Summary 2.Valuation 3.Strategy
|
| 195 |
"""
|
| 196 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 197 |
messages = [{"role": "user", "content": prompt}]
|
| 198 |
stream = client.chat.completions.create(
|
| 199 |
model="Qwen/Qwen2.5-72B-Instruct", messages=messages, stream=True
|
| 200 |
)
|
| 201 |
+
|
| 202 |
+
# ๋ฆฌํฌํธ ๋ฐ์ค ๋์์ธ
|
| 203 |
+
with st.container(border=True):
|
| 204 |
+
st.write_stream(parse_stream(stream))
|
| 205 |
+
|
| 206 |
else:
|
| 207 |
+
status.update(label="โ No Data", state="error")
|
| 208 |
+
st.error("๋ฐ์ดํฐ๋ฅผ ๋ถ๋ฌ์ฌ ์ ์์ต๋๋ค.")
|
| 209 |
except Exception as e:
|
| 210 |
+
status.update(label="โ Error", state="error")
|
| 211 |
+
st.error(f"Error: {e}")
|
| 212 |
else:
|
| 213 |
+
status.update(label="โ Not Found", state="error")
|
| 214 |
+
st.error("์ข
๋ชฉ์ ์ฐพ์ ์ ์์ต๋๋ค.")
|
| 215 |
|
| 216 |
+
# ==============================================================================
|
| 217 |
+
# 2. ์์ฅ ์ค์บ๋ ๋ชจ๋
|
| 218 |
+
# ==============================================================================
|
| 219 |
+
elif "์์ฅ ์ค์บ๋" in menu:
|
| 220 |
+
st.subheader("๐ก Global Market Watch")
|
| 221 |
|
| 222 |
+
# ์ปจํธ๋กค ํจ๋
|
| 223 |
+
col_sel, col_btn = st.columns([3, 1])
|
| 224 |
+
with col_sel:
|
| 225 |
+
target_market = st.selectbox("Select Market", list(MARKET_SAMPLES.keys()), label_visibility="collapsed")
|
| 226 |
+
with col_btn:
|
| 227 |
+
scan_trigger = st.button("Scan Market", use_container_width=True)
|
| 228 |
|
| 229 |
+
if scan_trigger:
|
| 230 |
+
tickers = MARKET_SAMPLES[target_market]
|
| 231 |
+
with st.spinner(f"Scanning {target_market}..."):
|
| 232 |
try:
|
| 233 |
+
data = yf.download(tickers, period="5d", progress=False)['Close']
|
| 234 |
if not data.empty:
|
| 235 |
+
last = data.iloc[-1]
|
| 236 |
prev = data.iloc[-2]
|
| 237 |
+
pct = ((last - prev)/prev)*100
|
| 238 |
|
| 239 |
+
df = pd.DataFrame({'Price': last, 'Change(%)': pct})
|
| 240 |
+
df = df.dropna().sort_values('Change(%)', ascending=False)
|
| 241 |
|
| 242 |
+
# ์๊ฐํ (์ปฌ๋ผ ๋ถํ )
|
| 243 |
+
st.markdown("##### ๐ฅ Market Heatmap")
|
| 244 |
+
st.bar_chart(df['Change(%)'], color=["#00FF00" if x > 0 else "#FF0000" for x in df['Change(%)']])
|
| 245 |
+
|
| 246 |
+
c1, c2 = st.columns(2)
|
| 247 |
+
with c1:
|
| 248 |
+
st.info("๐ Top Gainers")
|
| 249 |
+
st.dataframe(df.head(3).style.format({"Price": "{:,.2f}", "Change(%)": "{:,.2f}%"}), use_container_width=True)
|
| 250 |
+
with c2:
|
| 251 |
+
st.error("๐ Top Losers")
|
| 252 |
+
st.dataframe(df.tail(3).sort_values('Change(%)').style.format({"Price": "{:,.2f}", "Change(%)": "{:,.2f}%"}), use_container_width=True)
|
| 253 |
else:
|
| 254 |
+
st.warning("No data received.")
|
| 255 |
except Exception as e:
|
| 256 |
+
st.error(f"Scan failed: {e}")
|