Spaces:
Sleeping
Sleeping
Smart-Trader-EA commited on
Commit ·
0596210
1
Parent(s): 5d5d623
Initial commit
Browse files- app.py +59 -0
- requirements.txt +6 -0
app.py
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import yfinance as yf
|
| 3 |
+
import pandas as pd
|
| 4 |
+
import plotly.graph_objects as go
|
| 5 |
+
from prophet import Prophet
|
| 6 |
+
|
| 7 |
+
def analyze_stock(ticker):
|
| 8 |
+
stock = yf.Ticker(ticker)
|
| 9 |
+
hist = stock.history(period="1y")
|
| 10 |
+
if hist.empty:
|
| 11 |
+
return "股票代码无效,请检查后重试(示例:AAPL, TSLA, 600036.SS)", None, None
|
| 12 |
+
|
| 13 |
+
fig = go.Figure(data=[go.Candlestick(x=hist.index,
|
| 14 |
+
open=hist['Open'],
|
| 15 |
+
high=hist['High'],
|
| 16 |
+
low=hist['Low'],
|
| 17 |
+
close=hist['Close'])])
|
| 18 |
+
fig.update_layout(title=f"{ticker} 股票K线图", xaxis_title="日期", yaxis_title="价格")
|
| 19 |
+
|
| 20 |
+
df = hist[['Close']].reset_index()
|
| 21 |
+
df.columns = ['ds', 'y']
|
| 22 |
+
model = Prophet(daily_seasonality=True)
|
| 23 |
+
model.fit(df)
|
| 24 |
+
future = model.make_future_dataframe(periods=7)
|
| 25 |
+
forecast = model.predict(future)
|
| 26 |
+
|
| 27 |
+
fig2 = go.Figure()
|
| 28 |
+
fig2.add_trace(go.Scatter(x=df['ds'], y=df['y'], mode='lines', name='历史价格'))
|
| 29 |
+
fig2.add_trace(go.Scatter(x=forecast['ds'], y=forecast['yhat'], mode='lines', name='预测价格'))
|
| 30 |
+
fig2.update_layout(title=f"{ticker} 7天价格预测", xaxis_title="日期", yaxis_title="价格")
|
| 31 |
+
|
| 32 |
+
hist['MA20'] = hist['Close'].rolling(20).mean()
|
| 33 |
+
current_price = hist['Close'].iloc[-1]
|
| 34 |
+
ma20 = hist['MA20'].iloc[-1]
|
| 35 |
+
signal = "📈 看涨" if current_price > ma20 else "📉 看跌"
|
| 36 |
+
|
| 37 |
+
result_text = f"当前价格: ${current_price:.2f}\n20日均线: ${ma20:.2f}\n信号: {signal}"
|
| 38 |
+
return result_text, fig, fig2
|
| 39 |
+
|
| 40 |
+
with gr.Blocks(title="股票AI分析") as demo:
|
| 41 |
+
gr.Markdown("# 📈 股票AI分析系统")
|
| 42 |
+
gr.Markdown("输入股票代码(美股直接输入,A股加.SS后缀,如`600036.SS`)")
|
| 43 |
+
|
| 44 |
+
ticker_input = gr.Textbox(label="股票代码", value="AAPL")
|
| 45 |
+
analyze_btn = gr.Button("分析", variant="primary")
|
| 46 |
+
|
| 47 |
+
signal_output = gr.Textbox(label="分析结果")
|
| 48 |
+
kchart = gr.Plot(label="K线图")
|
| 49 |
+
pred_chart = gr.Plot(label="价格预测")
|
| 50 |
+
|
| 51 |
+
gr.Markdown("### 示例代码:\n- 美股: `AAPL`, `TSLA`, `GOOGL`\n- A股: `600036.SS`, `000001.SZ`\n- 港股: `00700.HK`")
|
| 52 |
+
|
| 53 |
+
analyze_btn.click(
|
| 54 |
+
fn=analyze_stock,
|
| 55 |
+
inputs=ticker_input,
|
| 56 |
+
outputs=[signal_output, kchart, pred_chart]
|
| 57 |
+
)
|
| 58 |
+
|
| 59 |
+
demo.launch()
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==4.19.1
|
| 2 |
+
yfinance==0.2.28
|
| 3 |
+
pandas==2.1.4
|
| 4 |
+
numpy==1.26.3
|
| 5 |
+
plotly==5.19.0
|
| 6 |
+
prophet==1.1.5
|