Spaces:
Sleeping
Sleeping
Update streamlit_app.py
Browse files- streamlit_app.py +61 -36
streamlit_app.py
CHANGED
|
@@ -1,53 +1,78 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import yfinance as yf
|
|
|
|
| 3 |
|
| 4 |
-
# 1.
|
| 5 |
-
st.set_page_config(
|
| 6 |
-
page_title="Pocket Quant",
|
| 7 |
-
page_icon="๐",
|
| 8 |
-
layout="centered"
|
| 9 |
-
)
|
| 10 |
|
| 11 |
-
|
| 12 |
-
st.
|
| 13 |
-
st.caption("Zero Cost. Mobile Only. Docker Edition.")
|
| 14 |
|
| 15 |
-
#
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
|
| 18 |
-
#
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 20 |
|
| 21 |
-
|
|
|
|
|
|
|
| 22 |
try:
|
| 23 |
-
# yfinance๋ก ๋ฐ์ดํฐ ํธ์ถ
|
| 24 |
stock = yf.Ticker(ticker)
|
| 25 |
-
|
| 26 |
-
# ์ต๊ทผ 1๊ฐ์ ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
|
| 27 |
hist = stock.history(period="1mo")
|
| 28 |
|
| 29 |
if not hist.empty:
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
change_pct = ((current_price - prev_price) / prev_price) * 100
|
| 34 |
-
|
| 35 |
-
# ๋ฉํธ๋ฆญ ์นด๋ (์ฃผ๊ฐ ๋ณด์ฌ์ฃผ๊ธฐ)
|
| 36 |
-
st.metric(
|
| 37 |
-
label=f"{ticker} ํ์ฌ ์ฃผ๊ฐ",
|
| 38 |
-
value=f"{current_price:,.0f}", # ์ฒ๋จ์ ์ฝค๋ง
|
| 39 |
-
delta=f"{change_pct:.2f}%" # ๋ฑ๋ฝ๋ฅ (์๋ ์์)
|
| 40 |
-
)
|
| 41 |
|
| 42 |
-
|
| 43 |
-
st.subheader("๐ ์ต๊ทผ 1๊ฐ์ ์ถ์ด")
|
| 44 |
st.line_chart(hist['Close'])
|
| 45 |
-
|
| 46 |
-
# (๋ค์ ๋จ๊ณ๋ฅผ ์ํ ๋น๋์
)
|
| 47 |
-
st.info("๐ค **AI ์๋ฌธ:** ์ฐจํธ๋ ๋์๋ค์! ๋ค์ ๋จ๊ณ์์ ์ฌ๊ธฐ์ '๋งค์/๋งค๋' ์๊ฒฌ์ ๋ถ์ฌ๋ด
์๋ค.")
|
| 48 |
-
|
| 49 |
else:
|
| 50 |
-
st.error("๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค.
|
| 51 |
|
| 52 |
except Exception as e:
|
| 53 |
-
st.error(f"
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import yfinance as yf
|
| 3 |
+
import FinanceDataReader as fdr
|
| 4 |
|
| 5 |
+
# 1. ํ์ด์ง ์ค์
|
| 6 |
+
st.set_page_config(page_title="Pocket Quant", page_icon="๐", layout="centered")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
+
st.title("๐ค AI ์ฃผ์ ์๋ฌธ๊ฐ")
|
| 9 |
+
st.caption("์ด์ ํ๊ธ๋ก ๊ฒ์ํด๋ ๋ค ์์๋ฃ์ต๋๋ค!")
|
|
|
|
| 10 |
|
| 11 |
+
# 2. [ํต์ฌ] ํ๊ตญ ์ฃผ์ ๋ชฉ๋ก ๊ฐ์ ธ์ค๊ธฐ (์บ์ฑ: ํ ๋ฒ๋ง ๋ก๋ฉํด์ ์๋ ๋น ๋ฆ)
|
| 12 |
+
@st.cache_data
|
| 13 |
+
def get_stock_list():
|
| 14 |
+
# KRX ์ ์ฒด ๋ฆฌ์คํธ ๋ค์ด๋ก๋ (์ฝ 2~3์ด ์์)
|
| 15 |
+
df = fdr.StockListing('KRX')
|
| 16 |
+
return df
|
| 17 |
|
| 18 |
+
# ๋ฐ์ดํฐ ๋ก๋ฉ ์ค ํ์
|
| 19 |
+
with st.spinner('ํ๊ตญ ์ฃผ์ "์ ํ๋ฒํธ๋ถ"๋ฅผ ๋ถ๋ฌ์ค๋ ์ค...'):
|
| 20 |
+
df_krx = get_stock_list()
|
| 21 |
+
|
| 22 |
+
# 3. ๋๋ํ ๊ฒ์ ํจ์ (ํ๊ธ -> ํฐ์ปค ๋ณํ)
|
| 23 |
+
def search_ticker(input_text):
|
| 24 |
+
# 3-1. ์
๋ ฅ๊ฐ์ด ์์ด/์ซ์๋ฉด ๊ทธ๋ฅ ํฐ์ปค๋ก ๊ฐ์ฃผ (๋ฏธ๊ตญ ์ฃผ์ ๋ฑ)
|
| 25 |
+
if input_text.isascii():
|
| 26 |
+
return input_text.upper()
|
| 27 |
+
|
| 28 |
+
# 3-2. ํ๊ธ์ด๋ฉด KRX ๋ฆฌ์คํธ์์ ๊ฒ์
|
| 29 |
+
else:
|
| 30 |
+
# ์ ํํ ์ผ์นํ๋ ์ข
๋ชฉ ์ฐพ๊ธฐ
|
| 31 |
+
res = df_krx[df_krx['Name'] == input_text]
|
| 32 |
+
|
| 33 |
+
# ์์ผ๋ฉด ํฌํจํ๋ ์ข
๋ชฉ ์ฐพ๊ธฐ (์: "์ผ์ฑ" -> ์ผ์ฑ์ ์, ์ผ์ฑ์๋ช
...)
|
| 34 |
+
if res.empty:
|
| 35 |
+
return None
|
| 36 |
+
|
| 37 |
+
# ์ฐพ์์ผ๋ฉด ํฐ์ปค ์ฝ๋ ๋ณํ
|
| 38 |
+
code = res.iloc[0]['Code']
|
| 39 |
+
market = res.iloc[0]['Market']
|
| 40 |
+
|
| 41 |
+
# ์ผํ ํ์ด๋ธ์ค์ฉ ์ ๋ฏธ์ฌ ๋ถ์ด๊ธฐ (.KS ์ฝ์คํผ, .KQ ์ฝ์ค๋ฅ)
|
| 42 |
+
if market == 'KOSPI':
|
| 43 |
+
return f"{code}.KS"
|
| 44 |
+
elif market == 'KOSDAQ':
|
| 45 |
+
return f"{code}.KQ"
|
| 46 |
+
else:
|
| 47 |
+
return code # ๊ธฐํ
|
| 48 |
+
|
| 49 |
+
# 4. ์ฌ์ฉ์ ์
๋ ฅ
|
| 50 |
+
user_input = st.text_input("์ข
๋ชฉ๋ช
๋๋ ์ฝ๋๋ฅผ ์
๋ ฅํ์ธ์", "์ผ์ฑ์ ์")
|
| 51 |
+
|
| 52 |
+
# 5. ๋ถ์ ์์
|
| 53 |
+
if st.button("๐ ๋ถ์ ์คํ", use_container_width=True):
|
| 54 |
+
|
| 55 |
+
# ํ๊ธ ์ด๋ฆ์ ํฐ์ปค๋ก ๋ณํ!
|
| 56 |
+
ticker = search_ticker(user_input)
|
| 57 |
|
| 58 |
+
if ticker:
|
| 59 |
+
st.success(f"'{user_input}'์(๋ฅผ) ์ฐพ์์ต๋๋ค! ๐ ์ฝ๋: {ticker}")
|
| 60 |
+
|
| 61 |
try:
|
|
|
|
| 62 |
stock = yf.Ticker(ticker)
|
|
|
|
|
|
|
| 63 |
hist = stock.history(period="1mo")
|
| 64 |
|
| 65 |
if not hist.empty:
|
| 66 |
+
curr = hist['Close'].iloc[-1]
|
| 67 |
+
prev = hist['Close'].iloc[-2]
|
| 68 |
+
pct = ((curr - prev) / prev) * 100
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
|
| 70 |
+
st.metric(label="ํ์ฌ ์ฃผ๊ฐ", value=f"{curr:,.0f}์", delta=f"{pct:.2f}%")
|
|
|
|
| 71 |
st.line_chart(hist['Close'])
|
|
|
|
|
|
|
|
|
|
|
|
|
| 72 |
else:
|
| 73 |
+
st.error("๋ฐ์ดํฐ๊ฐ ์์ต๋๋ค. ์์ฅ ํ์ง๋์๊ฑฐ๋ ์ฝ๋๊ฐ ์๋ชป๋์์ต๋๋ค.")
|
| 74 |
|
| 75 |
except Exception as e:
|
| 76 |
+
st.error(f"์๋ฌ ๋ฐ์: {e}")
|
| 77 |
+
else:
|
| 78 |
+
st.error(f"โ '{user_input}'๋ผ๋ ์ข
๋ชฉ์ ํ๊ตญ ์์ฅ์์ ๋ชป ์ฐพ๊ฒ ์ด์. ์ ํํ ์ด๋ฆ์ ์
๋ ฅํด์ฃผ์ธ์.")
|