Spaces:
Running
Running
Commit
·
0dbb99d
1
Parent(s):
23fd152
- .gitignore +3 -0
- README.md +40 -0
- app.py +224 -0
- requirements.txt +9 -0
.gitignore
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
__pycache__/
|
| 2 |
+
.env
|
| 3 |
+
myenv/
|
README.md
CHANGED
|
@@ -11,3 +11,43 @@ short_description: 透過即時新聞/財報/和股價 ,幫你評估股票值
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
---
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
# 股票基本面分析系統
|
| 20 |
+
|
| 21 |
+
這是一個使用 AI 技術的股票基本面分析系統,能夠提供:
|
| 22 |
+
- 股價走勢分析
|
| 23 |
+
- 技術指標分析
|
| 24 |
+
- 財務比率分析
|
| 25 |
+
- 新聞分析
|
| 26 |
+
- 產業分析
|
| 27 |
+
- 競爭對手分析
|
| 28 |
+
|
| 29 |
+
## 使用方式
|
| 30 |
+
1. 輸入股票代號(例如:2330.TW)
|
| 31 |
+
2. 選擇想要分析的資料期間(3個月、6個月或1年)
|
| 32 |
+
3. 等待系統生成分析報告
|
| 33 |
+
|
| 34 |
+
## 環境設置
|
| 35 |
+
系統需要設置以下環境變數:
|
| 36 |
+
- OPENAI_API_KEY:OpenAI API 金鑰
|
| 37 |
+
|
| 38 |
+
## 本地運行
|
| 39 |
+
```bash
|
| 40 |
+
# 建立虛擬環境
|
| 41 |
+
python -m venv venv
|
| 42 |
+
|
| 43 |
+
# 啟動虛擬環境
|
| 44 |
+
# Windows:
|
| 45 |
+
venv\Scripts\activate
|
| 46 |
+
# Linux/Mac:
|
| 47 |
+
source venv/bin/activate
|
| 48 |
+
|
| 49 |
+
# 安裝依賴
|
| 50 |
+
pip install -r requirements.txt
|
| 51 |
+
|
| 52 |
+
# 運行應用
|
| 53 |
+
python app.py
|
app.py
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import datetime as dt
|
| 3 |
+
import yfinance as yf
|
| 4 |
+
import traceback
|
| 5 |
+
import json
|
| 6 |
+
from typing import Union, Dict
|
| 7 |
+
|
| 8 |
+
import gradio as gr
|
| 9 |
+
from langchain_openai import ChatOpenAI
|
| 10 |
+
from langchain_core.messages import SystemMessage, HumanMessage, AIMessage
|
| 11 |
+
|
| 12 |
+
from ta.momentum import RSIIndicator, StochasticOscillator
|
| 13 |
+
from ta.trend import MACD
|
| 14 |
+
from ta.volume import volume_weighted_average_price
|
| 15 |
+
|
| 16 |
+
import dotenv
|
| 17 |
+
dotenv.load_dotenv()
|
| 18 |
+
|
| 19 |
+
FUNDAMENTAL_ANALYST_PROMPT = """
|
| 20 |
+
You are a fundamental analyst specializing in evaluating company (whose symbol is {company}) performance based on stock prices, technical indicators, financial metrics, recent news, industry trends, competitor positioning, and financial ratios. Your task is to provide a comprehensive summary.
|
| 21 |
+
|
| 22 |
+
You have access to the following tools:
|
| 23 |
+
1. **get_stock_prices**: Retrieves stock price data and technical indicators.
|
| 24 |
+
2. **get_financial_metrics**: Retrieves key financial metrics and financial ratios.
|
| 25 |
+
3. **get_financial_news**: Retrieves the latest financial news related to the stock.
|
| 26 |
+
4. **get_industry_data** *(if available)*: Retrieves industry trends and competitive positioning information.
|
| 27 |
+
|
| 28 |
+
---
|
| 29 |
+
|
| 30 |
+
### Your Task:
|
| 31 |
+
1. Use the provided stock symbol to query the tools.
|
| 32 |
+
2. Analyze the following areas in sequence:
|
| 33 |
+
- **Stock price movements and technical indicators**: Examine recent price trends, volatility, and signals from RSI, MACD, VWAP, and other indicators.
|
| 34 |
+
- **Financial health and key financial ratios**: Assess profitability, liquidity, solvency, and operational efficiency using metrics such as:
|
| 35 |
+
- Profitability Ratios: Gross Profit Margin, Net Profit Margin, Operating Profit Margin
|
| 36 |
+
- Liquidity Ratios: Current Ratio, Quick Ratio
|
| 37 |
+
- Solvency Ratios: Debt-to-Equity Ratio, Interest Coverage Ratio
|
| 38 |
+
- Efficiency Ratios: Inventory Turnover, Accounts Receivable Turnover
|
| 39 |
+
- Market Ratios: Price-to-Earnings Ratio (P/E), Price-to-Book Ratio (P/B)
|
| 40 |
+
- **Recent news and market sentiment**: Identify significant events or trends impacting the company's market perception.
|
| 41 |
+
- **Industry analysis**: Evaluate the industry’s growth trends, technological advancements, and regulatory environment. Identify how the industry is evolving and how it affects the target company.
|
| 42 |
+
- **Competitor analysis**: Compare the target company with key competitors in terms of market share, financial health, and growth potential.
|
| 43 |
+
|
| 44 |
+
3. Provide a concise and structured summary covering all sections, ensuring each area has actionable insights.
|
| 45 |
+
|
| 46 |
+
---
|
| 47 |
+
|
| 48 |
+
### Output Format : 以下請用繁體中文輸出
|
| 49 |
+
{
|
| 50 |
+
"stock": "",
|
| 51 |
+
"price_analysis": "<股票價格趨勢與技術指標分析>",
|
| 52 |
+
"technical_analysis": "<技術指標分析與見解>",
|
| 53 |
+
"financial_analysis": {
|
| 54 |
+
"profitability_ratios": "<獲利能力比率分析>",
|
| 55 |
+
"liquidity_ratios": "<流動性比率分析>",
|
| 56 |
+
"solvency_ratios": "<償債能力比率分析>",
|
| 57 |
+
"efficiency_ratios": "<營運效率比率分析>",
|
| 58 |
+
"market_ratios": "<市場表現比率分析>",
|
| 59 |
+
"summary": "<財務整體健康狀況與分析結論>"
|
| 60 |
+
},
|
| 61 |
+
"news_analysis": "<近期新聞摘要與其對股價的潛在影響>",
|
| 62 |
+
"industry_analysis": "<產業趨勢、成長動力與潛在風險>",
|
| 63 |
+
"competitor_analysis": "<主要競爭對手比較與市場地位分析>",
|
| 64 |
+
"final_summary": "<整體綜合結論與投資建議>",
|
| 65 |
+
"Asked Question Answer": "<根據上述分析的具體回答>"
|
| 66 |
+
}
|
| 67 |
+
"""
|
| 68 |
+
|
| 69 |
+
def period_to_start_end(period_str: str):
|
| 70 |
+
"""
|
| 71 |
+
根據使用者選擇的時間區間,計算起始與結束日期。
|
| 72 |
+
"""
|
| 73 |
+
now = dt.datetime.now()
|
| 74 |
+
if period_str == "3mo":
|
| 75 |
+
start = now - dt.timedelta(weeks=13)
|
| 76 |
+
elif period_str == "6mo":
|
| 77 |
+
start = now - dt.timedelta(weeks=26)
|
| 78 |
+
elif period_str == "1yr":
|
| 79 |
+
start = now - dt.timedelta(weeks=52)
|
| 80 |
+
else:
|
| 81 |
+
start = now - dt.timedelta(weeks=13)
|
| 82 |
+
return start, now
|
| 83 |
+
|
| 84 |
+
# --- 取得股票價格與技術指標 ---
|
| 85 |
+
def get_stock_prices(ticker: str, period: str = "3mo") -> Union[Dict, str]:
|
| 86 |
+
"""
|
| 87 |
+
使用 start 與 end 取得歷史股價資料(避免連線到 fc.yahoo.com),
|
| 88 |
+
並計算 RSI、Stochastic、MACD 與 VWAP 指標。
|
| 89 |
+
"""
|
| 90 |
+
try:
|
| 91 |
+
start_date, end_date = period_to_start_end(period)
|
| 92 |
+
data = yf.download(
|
| 93 |
+
ticker,
|
| 94 |
+
start=start_date,
|
| 95 |
+
end=end_date,
|
| 96 |
+
interval='1d'
|
| 97 |
+
)
|
| 98 |
+
df = data.copy()
|
| 99 |
+
# 若有 multi-index,調整欄位名稱
|
| 100 |
+
if df.columns.nlevels > 1:
|
| 101 |
+
df.columns = [col[0] for col in df.columns]
|
| 102 |
+
data.reset_index(inplace=True)
|
| 103 |
+
data['Date'] = data['Date'].astype(str)
|
| 104 |
+
|
| 105 |
+
indicators = {}
|
| 106 |
+
|
| 107 |
+
# RSI
|
| 108 |
+
rsi_series = RSIIndicator(df['Close'], window=14).rsi().iloc[-12:]
|
| 109 |
+
indicators["RSI"] = {date.strftime('%Y-%m-%d'): int(value) for date, value in rsi_series.dropna().to_dict().items()}
|
| 110 |
+
|
| 111 |
+
# Stochastic Oscillator
|
| 112 |
+
sto_series = StochasticOscillator(df['High'], df['Low'], df['Close'], window=14).stoch().iloc[-12:]
|
| 113 |
+
indicators["Stochastic_Oscillator"] = {date.strftime('%Y-%m-%d'): int(value) for date, value in sto_series.dropna().to_dict().items()}
|
| 114 |
+
|
| 115 |
+
# MACD 與訊號線
|
| 116 |
+
macd = MACD(df['Close'])
|
| 117 |
+
macd_series = macd.macd().iloc[-12:]
|
| 118 |
+
indicators["MACD"] = {date.strftime('%Y-%m-%d'): int(value) for date, value in macd_series.to_dict().items()}
|
| 119 |
+
macd_signal_series = macd.macd_signal().iloc[-12:]
|
| 120 |
+
indicators["MACD_Signal"] = {date.strftime('%Y-%m-%d'): int(value) for date, value in macd_signal_series.to_dict().items()}
|
| 121 |
+
|
| 122 |
+
# VWAP
|
| 123 |
+
vwap_series = volume_weighted_average_price(
|
| 124 |
+
high=df['High'],
|
| 125 |
+
low=df['Low'],
|
| 126 |
+
close=df['Close'],
|
| 127 |
+
volume=df['Volume']
|
| 128 |
+
).iloc[-12:]
|
| 129 |
+
indicators["vwap"] = {date.strftime('%Y-%m-%d'): int(value) for date, value in vwap_series.to_dict().items()}
|
| 130 |
+
|
| 131 |
+
return {'stock_price': data.to_dict(orient='records'), 'indicators': indicators}
|
| 132 |
+
except Exception as e:
|
| 133 |
+
return f"Error fetching price data: {str(e)}"
|
| 134 |
+
|
| 135 |
+
# --- 取得財務新聞 ---
|
| 136 |
+
def get_financial_news(ticker: str) -> Union[Dict, str]:
|
| 137 |
+
try:
|
| 138 |
+
stock = yf.Ticker(ticker)
|
| 139 |
+
news = stock.news
|
| 140 |
+
if not news:
|
| 141 |
+
return {"news": "No recent news found."}
|
| 142 |
+
latest_news = [
|
| 143 |
+
{
|
| 144 |
+
"title": item.get('title'),
|
| 145 |
+
"publisher": item.get('publisher'),
|
| 146 |
+
"link": item.get('link'),
|
| 147 |
+
"published_date": item.get('providerPublishTime')
|
| 148 |
+
}
|
| 149 |
+
for item in news[:5]
|
| 150 |
+
]
|
| 151 |
+
return {"news": latest_news}
|
| 152 |
+
except Exception as e:
|
| 153 |
+
return f"Error fetching news: {str(e)}"
|
| 154 |
+
|
| 155 |
+
# --- 取得財務指標 ---
|
| 156 |
+
def get_financial_metrics(ticker: str) -> Union[Dict, str]:
|
| 157 |
+
try:
|
| 158 |
+
stock = yf.Ticker(ticker)
|
| 159 |
+
info = stock.info
|
| 160 |
+
return {
|
| 161 |
+
'pe_ratio': info.get('forwardPE'),
|
| 162 |
+
'price_to_book': info.get('priceToBook'),
|
| 163 |
+
'debt_to_equity': info.get('debtToEquity'),
|
| 164 |
+
'profit_margins': info.get('profitMargins')
|
| 165 |
+
}
|
| 166 |
+
except Exception as e:
|
| 167 |
+
return f"Error fetching ratios: {str(e)}"
|
| 168 |
+
|
| 169 |
+
# --- 綜合基本面分析 ---
|
| 170 |
+
def analyze_stock(api_key: str, ticker: str, period: str) -> str:
|
| 171 |
+
"""
|
| 172 |
+
根據輸入的 LLM API key、股票代號與時間區間,抓取各項資料後,
|
| 173 |
+
組合成分析 Prompt 呼叫 LLM,最後回傳基本面分析結果。
|
| 174 |
+
"""
|
| 175 |
+
try:
|
| 176 |
+
# 建立 LLM 實例
|
| 177 |
+
llm = ChatOpenAI(
|
| 178 |
+
model='gpt-4o',
|
| 179 |
+
openai_api_key=api_key,
|
| 180 |
+
temperature=0
|
| 181 |
+
)
|
| 182 |
+
# 取得資料
|
| 183 |
+
price_data = get_stock_prices(ticker, period)
|
| 184 |
+
metrics = get_financial_metrics(ticker)
|
| 185 |
+
news = get_financial_news(ticker)
|
| 186 |
+
|
| 187 |
+
# 準備 prompt
|
| 188 |
+
prompt = FUNDAMENTAL_ANALYST_PROMPT.replace("{company}", ticker)
|
| 189 |
+
user_question = "Should I buy this stock?"
|
| 190 |
+
analysis_prompt = f"""
|
| 191 |
+
根據以下 {ticker} 的資料,進行全面的基本面分析並回答使用者問題:"{user_question}"
|
| 192 |
+
|
| 193 |
+
股價與技術指標資料:
|
| 194 |
+
{json.dumps(price_data, ensure_ascii=False, indent=2)}
|
| 195 |
+
|
| 196 |
+
財務指標:
|
| 197 |
+
{json.dumps(metrics, ensure_ascii=False, indent=2)}
|
| 198 |
+
|
| 199 |
+
相關新聞:
|
| 200 |
+
{json.dumps(news, ensure_ascii=False, indent=2)}
|
| 201 |
+
|
| 202 |
+
{prompt}
|
| 203 |
+
"""
|
| 204 |
+
# 呼叫 LLM 生成最終分析報告
|
| 205 |
+
response = llm.invoke(analysis_prompt)
|
| 206 |
+
return response.content
|
| 207 |
+
except Exception as e:
|
| 208 |
+
return f"分析過程中發生錯誤: {str(e)}\n{traceback.format_exc()}"
|
| 209 |
+
|
| 210 |
+
# --- Gradio 介面 ---
|
| 211 |
+
iface = gr.Interface(
|
| 212 |
+
fn=analyze_stock,
|
| 213 |
+
inputs=[
|
| 214 |
+
gr.Textbox(label="LLM API Key", type="password", placeholder="請輸入 OpenAI API Key"),
|
| 215 |
+
gr.Textbox(label="股票代號", placeholder="例如:TSLA 或 2330.TW"),
|
| 216 |
+
gr.Dropdown(choices=["3mo", "6mo", "1yr"], label="時間區間", value="3mo")
|
| 217 |
+
],
|
| 218 |
+
outputs=gr.Textbox(label="基本面分析結果"),
|
| 219 |
+
title="股票基本面分析 App",
|
| 220 |
+
description="輸入您的 LLM API key、股票代號與時間區間,取得該股票的基本面分析報告。"
|
| 221 |
+
)
|
| 222 |
+
|
| 223 |
+
if __name__ == "__main__":
|
| 224 |
+
iface.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio
|
| 2 |
+
yfinance
|
| 3 |
+
ta
|
| 4 |
+
langchain
|
| 5 |
+
langchain-openai
|
| 6 |
+
openai
|
| 7 |
+
python-dotenv
|
| 8 |
+
pandas
|
| 9 |
+
numpy
|