feat: 支持自定义API
This commit is contained in:
@@ -1,343 +1,344 @@
|
|||||||
import pandas as pd
|
import pandas as pd
|
||||||
import numpy as np
|
import numpy as np
|
||||||
from datetime import datetime, timedelta
|
from datetime import datetime, timedelta
|
||||||
import os
|
import os
|
||||||
import requests
|
import requests
|
||||||
from typing import Dict, List, Optional, Tuple
|
from typing import Dict, List, Optional, Tuple
|
||||||
from dotenv import load_dotenv
|
from dotenv import load_dotenv
|
||||||
|
|
||||||
class StockAnalyzer:
|
class StockAnalyzer:
|
||||||
def __init__(self, initial_cash=1000000):
|
def __init__(self, initial_cash=1000000, custom_api_url=None, custom_api_key=None, custom_api_model=None):
|
||||||
|
|
||||||
# 加载环境变量
|
# 加载环境变量
|
||||||
load_dotenv()
|
load_dotenv()
|
||||||
|
|
||||||
# 设置 Gemini API
|
# 设置 API 配置,优先使用自定义配置,否则使用环境变量
|
||||||
self.API_URL = os.getenv('API_URL')
|
self.API_URL = custom_api_url or os.getenv('API_URL')
|
||||||
self.API_KEY = os.getenv('API_KEY')
|
self.API_KEY = custom_api_key or os.getenv('API_KEY')
|
||||||
|
self.API_MODEL = custom_api_model or os.getenv('API_MODEL', 'gpt-3.5-turbo')
|
||||||
# 配置参数
|
|
||||||
self.params = {
|
# 配置参数
|
||||||
'ma_periods': {'short': 5, 'medium': 20, 'long': 60},
|
self.params = {
|
||||||
'rsi_period': 14,
|
'ma_periods': {'short': 5, 'medium': 20, 'long': 60},
|
||||||
'bollinger_period': 20,
|
'rsi_period': 14,
|
||||||
'bollinger_std': 2,
|
'bollinger_period': 20,
|
||||||
'volume_ma_period': 20,
|
'bollinger_std': 2,
|
||||||
'atr_period': 14
|
'volume_ma_period': 20,
|
||||||
}
|
'atr_period': 14
|
||||||
|
}
|
||||||
|
|
||||||
def get_stock_data(self, stock_code, market_type='A', start_date=None, end_date=None, ):
|
|
||||||
"""获取股票数据"""
|
def get_stock_data(self, stock_code, market_type='A', start_date=None, end_date=None, ):
|
||||||
import akshare as ak
|
"""获取股票数据"""
|
||||||
|
import akshare as ak
|
||||||
if start_date is None:
|
|
||||||
start_date = (datetime.now() - timedelta(days=365)).strftime('%Y%m%d')
|
if start_date is None:
|
||||||
if end_date is None:
|
start_date = (datetime.now() - timedelta(days=365)).strftime('%Y%m%d')
|
||||||
end_date = datetime.now().strftime('%Y%m%d')
|
if end_date is None:
|
||||||
|
end_date = datetime.now().strftime('%Y%m%d')
|
||||||
try:
|
|
||||||
# 根据市场类型获取数据
|
try:
|
||||||
if market_type == 'A':
|
# 根据市场类型获取数据
|
||||||
df = ak.stock_zh_a_hist(
|
if market_type == 'A':
|
||||||
symbol=stock_code,
|
df = ak.stock_zh_a_hist(
|
||||||
start_date=start_date,
|
symbol=stock_code,
|
||||||
end_date=end_date,
|
start_date=start_date,
|
||||||
adjust="qfq"
|
end_date=end_date,
|
||||||
)
|
adjust="qfq"
|
||||||
# A股数据列名映射
|
)
|
||||||
elif market_type == 'HK':
|
# A股数据列名映射
|
||||||
df = ak.stock_hk_daily(
|
elif market_type == 'HK':
|
||||||
symbol=stock_code,
|
df = ak.stock_hk_daily(
|
||||||
adjust="qfq"
|
symbol=stock_code,
|
||||||
)
|
adjust="qfq"
|
||||||
elif market_type == 'US':
|
)
|
||||||
df = ak.stock_us_hist(
|
elif market_type == 'US':
|
||||||
symbol=stock_code,
|
df = ak.stock_us_hist(
|
||||||
start_date=start_date,
|
symbol=stock_code,
|
||||||
end_date=end_date,
|
start_date=start_date,
|
||||||
adjust="qfq"
|
end_date=end_date,
|
||||||
)
|
adjust="qfq"
|
||||||
# elif market_type == 'CRYPTO':
|
)
|
||||||
# df = ak.crypto_js_spot(
|
# elif market_type == 'CRYPTO':
|
||||||
# symbol=stock_code
|
# df = ak.crypto_js_spot(
|
||||||
# )
|
# symbol=stock_code
|
||||||
else:
|
# )
|
||||||
raise ValueError(f"不支持的市场类型: {market_type}")
|
else:
|
||||||
|
raise ValueError(f"不支持的市场类型: {market_type}")
|
||||||
# 重命名列名以匹配分析需求
|
|
||||||
df = df.rename(columns={
|
# 重命名列名以匹配分析需求
|
||||||
"日期": "date",
|
df = df.rename(columns={
|
||||||
"开盘": "open",
|
"日期": "date",
|
||||||
"收盘": "close",
|
"开盘": "open",
|
||||||
"最高": "high",
|
"收盘": "close",
|
||||||
"最低": "low",
|
"最高": "high",
|
||||||
"成交量": "volume"
|
"最低": "low",
|
||||||
})
|
"成交量": "volume"
|
||||||
|
})
|
||||||
# 确保日期格式正确
|
|
||||||
df['date'] = pd.to_datetime(df['date'])
|
# 确保日期格式正确
|
||||||
|
df['date'] = pd.to_datetime(df['date'])
|
||||||
# 数据类型转换
|
|
||||||
numeric_columns = ['open', 'close', 'high', 'low', 'volume']
|
# 数据类型转换
|
||||||
df[numeric_columns] = df[numeric_columns].apply(pd.to_numeric, errors='coerce')
|
numeric_columns = ['open', 'close', 'high', 'low', 'volume']
|
||||||
|
df[numeric_columns] = df[numeric_columns].apply(pd.to_numeric, errors='coerce')
|
||||||
# 删除空值
|
|
||||||
df = df.dropna()
|
# 删除空值
|
||||||
|
df = df.dropna()
|
||||||
return df.sort_values('date')
|
|
||||||
|
return df.sort_values('date')
|
||||||
except Exception as e:
|
|
||||||
raise Exception(f"获取股票数据失败: {str(e)}")
|
except Exception as e:
|
||||||
|
raise Exception(f"获取股票数据失败: {str(e)}")
|
||||||
def calculate_ema(self, series, period):
|
|
||||||
"""计算指数移动平均线"""
|
def calculate_ema(self, series, period):
|
||||||
return series.ewm(span=period, adjust=False).mean()
|
"""计算指数移动平均线"""
|
||||||
|
return series.ewm(span=period, adjust=False).mean()
|
||||||
def calculate_rsi(self, series, period):
|
|
||||||
"""计算RSI指标"""
|
def calculate_rsi(self, series, period):
|
||||||
delta = series.diff()
|
"""计算RSI指标"""
|
||||||
gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
|
delta = series.diff()
|
||||||
loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
|
gain = (delta.where(delta > 0, 0)).rolling(window=period).mean()
|
||||||
rs = gain / loss
|
loss = (-delta.where(delta < 0, 0)).rolling(window=period).mean()
|
||||||
return 100 - (100 / (1 + rs))
|
rs = gain / loss
|
||||||
|
return 100 - (100 / (1 + rs))
|
||||||
def calculate_macd(self, series):
|
|
||||||
"""计算MACD指标"""
|
def calculate_macd(self, series):
|
||||||
exp1 = series.ewm(span=12, adjust=False).mean()
|
"""计算MACD指标"""
|
||||||
exp2 = series.ewm(span=26, adjust=False).mean()
|
exp1 = series.ewm(span=12, adjust=False).mean()
|
||||||
macd = exp1 - exp2
|
exp2 = series.ewm(span=26, adjust=False).mean()
|
||||||
signal = macd.ewm(span=9, adjust=False).mean()
|
macd = exp1 - exp2
|
||||||
hist = macd - signal
|
signal = macd.ewm(span=9, adjust=False).mean()
|
||||||
return macd, signal, hist
|
hist = macd - signal
|
||||||
|
return macd, signal, hist
|
||||||
def calculate_bollinger_bands(self, series, period, std_dev):
|
|
||||||
"""计算布林带"""
|
def calculate_bollinger_bands(self, series, period, std_dev):
|
||||||
middle = series.rolling(window=period).mean()
|
"""计算布林带"""
|
||||||
std = series.rolling(window=period).std()
|
middle = series.rolling(window=period).mean()
|
||||||
upper = middle + (std * std_dev)
|
std = series.rolling(window=period).std()
|
||||||
lower = middle - (std * std_dev)
|
upper = middle + (std * std_dev)
|
||||||
return upper, middle, lower
|
lower = middle - (std * std_dev)
|
||||||
|
return upper, middle, lower
|
||||||
def calculate_atr(self, df, period):
|
|
||||||
"""计算ATR指标"""
|
def calculate_atr(self, df, period):
|
||||||
high = df['high']
|
"""计算ATR指标"""
|
||||||
low = df['low']
|
high = df['high']
|
||||||
close = df['close'].shift(1)
|
low = df['low']
|
||||||
|
close = df['close'].shift(1)
|
||||||
tr1 = high - low
|
|
||||||
tr2 = abs(high - close)
|
tr1 = high - low
|
||||||
tr3 = abs(low - close)
|
tr2 = abs(high - close)
|
||||||
|
tr3 = abs(low - close)
|
||||||
tr = pd.concat([tr1, tr2, tr3], axis=1).max(axis=1)
|
|
||||||
return tr.rolling(window=period).mean()
|
tr = pd.concat([tr1, tr2, tr3], axis=1).max(axis=1)
|
||||||
|
return tr.rolling(window=period).mean()
|
||||||
def calculate_indicators(self, df):
|
|
||||||
"""计算技术指标"""
|
def calculate_indicators(self, df):
|
||||||
try:
|
"""计算技术指标"""
|
||||||
# 计算移动平均线
|
try:
|
||||||
df['MA5'] = self.calculate_ema(df['close'], self.params['ma_periods']['short'])
|
# 计算移动平均线
|
||||||
df['MA20'] = self.calculate_ema(df['close'], self.params['ma_periods']['medium'])
|
df['MA5'] = self.calculate_ema(df['close'], self.params['ma_periods']['short'])
|
||||||
df['MA60'] = self.calculate_ema(df['close'], self.params['ma_periods']['long'])
|
df['MA20'] = self.calculate_ema(df['close'], self.params['ma_periods']['medium'])
|
||||||
|
df['MA60'] = self.calculate_ema(df['close'], self.params['ma_periods']['long'])
|
||||||
# 计算RSI
|
|
||||||
df['RSI'] = self.calculate_rsi(df['close'], self.params['rsi_period'])
|
# 计算RSI
|
||||||
|
df['RSI'] = self.calculate_rsi(df['close'], self.params['rsi_period'])
|
||||||
# 计算MACD
|
|
||||||
df['MACD'], df['Signal'], df['MACD_hist'] = self.calculate_macd(df['close'])
|
# 计算MACD
|
||||||
|
df['MACD'], df['Signal'], df['MACD_hist'] = self.calculate_macd(df['close'])
|
||||||
# 计算布林带
|
|
||||||
df['BB_upper'], df['BB_middle'], df['BB_lower'] = self.calculate_bollinger_bands(
|
# 计算布林带
|
||||||
df['close'],
|
df['BB_upper'], df['BB_middle'], df['BB_lower'] = self.calculate_bollinger_bands(
|
||||||
self.params['bollinger_period'],
|
df['close'],
|
||||||
self.params['bollinger_std']
|
self.params['bollinger_period'],
|
||||||
)
|
self.params['bollinger_std']
|
||||||
|
)
|
||||||
# 成交量分析
|
|
||||||
df['Volume_MA'] = df['volume'].rolling(window=self.params['volume_ma_period']).mean()
|
# 成交量分析
|
||||||
df['Volume_Ratio'] = df['volume'] / df['Volume_MA']
|
df['Volume_MA'] = df['volume'].rolling(window=self.params['volume_ma_period']).mean()
|
||||||
|
df['Volume_Ratio'] = df['volume'] / df['Volume_MA']
|
||||||
# 计算ATR和波动率
|
|
||||||
df['ATR'] = self.calculate_atr(df, self.params['atr_period'])
|
# 计算ATR和波动率
|
||||||
df['Volatility'] = df['ATR'] / df['close'] * 100
|
df['ATR'] = self.calculate_atr(df, self.params['atr_period'])
|
||||||
|
df['Volatility'] = df['ATR'] / df['close'] * 100
|
||||||
# 动量指标
|
|
||||||
df['ROC'] = df['close'].pct_change(periods=10) * 100
|
# 动量指标
|
||||||
|
df['ROC'] = df['close'].pct_change(periods=10) * 100
|
||||||
return df
|
|
||||||
|
return df
|
||||||
except Exception as e:
|
|
||||||
print(f"计算技术指标时出错: {str(e)}")
|
except Exception as e:
|
||||||
raise
|
print(f"计算技术指标时出错: {str(e)}")
|
||||||
|
raise
|
||||||
def calculate_score(self, df):
|
|
||||||
"""计算股票评分"""
|
def calculate_score(self, df):
|
||||||
try:
|
"""计算股票评分"""
|
||||||
score = 0
|
try:
|
||||||
latest = df.iloc[-1]
|
score = 0
|
||||||
|
latest = df.iloc[-1]
|
||||||
# 趋势得分 (30分)
|
|
||||||
if latest['MA5'] > latest['MA20']:
|
# 趋势得分 (30分)
|
||||||
score += 15
|
if latest['MA5'] > latest['MA20']:
|
||||||
if latest['MA20'] > latest['MA60']:
|
score += 15
|
||||||
score += 15
|
if latest['MA20'] > latest['MA60']:
|
||||||
|
score += 15
|
||||||
# RSI得分 (20分)
|
|
||||||
if 30 <= latest['RSI'] <= 70:
|
# RSI得分 (20分)
|
||||||
score += 20
|
if 30 <= latest['RSI'] <= 70:
|
||||||
elif latest['RSI'] < 30: # 超卖
|
score += 20
|
||||||
score += 15
|
elif latest['RSI'] < 30: # 超卖
|
||||||
|
score += 15
|
||||||
# MACD得分 (20分)
|
|
||||||
if latest['MACD'] > latest['Signal']:
|
# MACD得分 (20分)
|
||||||
score += 20
|
if latest['MACD'] > latest['Signal']:
|
||||||
|
score += 20
|
||||||
# 成交量得分 (30分)
|
|
||||||
if latest['Volume_Ratio'] > 1.5:
|
# 成交量得分 (30分)
|
||||||
score += 30
|
if latest['Volume_Ratio'] > 1.5:
|
||||||
elif latest['Volume_Ratio'] > 1:
|
score += 30
|
||||||
score += 15
|
elif latest['Volume_Ratio'] > 1:
|
||||||
|
score += 15
|
||||||
return score
|
|
||||||
|
return score
|
||||||
except Exception as e:
|
|
||||||
print(f"计算评分时出错: {str(e)}")
|
except Exception as e:
|
||||||
raise
|
print(f"计算评分时出错: {str(e)}")
|
||||||
|
raise
|
||||||
def get_ai_analysis(self, df, stock_code):
|
|
||||||
"""使用 OpenAI 进行 AI 分析"""
|
def get_ai_analysis(self, df, stock_code):
|
||||||
try:
|
"""使用 OpenAI 进行 AI 分析"""
|
||||||
recent_data = df.tail(14).to_dict('records')
|
try:
|
||||||
|
recent_data = df.tail(14).to_dict('records')
|
||||||
technical_summary = {
|
|
||||||
'trend': 'upward' if df.iloc[-1]['MA5'] > df.iloc[-1]['MA20'] else 'downward',
|
technical_summary = {
|
||||||
'volatility': f"{df.iloc[-1]['Volatility']:.2f}%",
|
'trend': 'upward' if df.iloc[-1]['MA5'] > df.iloc[-1]['MA20'] else 'downward',
|
||||||
'volume_trend': 'increasing' if df.iloc[-1]['Volume_Ratio'] > 1 else 'decreasing',
|
'volatility': f"{df.iloc[-1]['Volatility']:.2f}%",
|
||||||
'rsi_level': df.iloc[-1]['RSI']
|
'volume_trend': 'increasing' if df.iloc[-1]['Volume_Ratio'] > 1 else 'decreasing',
|
||||||
}
|
'rsi_level': df.iloc[-1]['RSI']
|
||||||
|
}
|
||||||
prompt = f"""
|
|
||||||
分析股票 {stock_code}:
|
prompt = f"""
|
||||||
|
分析股票 {stock_code}:
|
||||||
技术指标概要:
|
|
||||||
{technical_summary}
|
技术指标概要:
|
||||||
|
{technical_summary}
|
||||||
近14日交易数据:
|
|
||||||
{recent_data}
|
近14日交易数据:
|
||||||
|
{recent_data}
|
||||||
请提供:
|
|
||||||
1. 趋势分析(包含支撑位和压力位)
|
请提供:
|
||||||
2. 成交量分析及其含义
|
1. 趋势分析(包含支撑位和压力位)
|
||||||
3. 风险评估(包含波动率分析)
|
2. 成交量分析及其含义
|
||||||
4. 短期和中期目标价位
|
3. 风险评估(包含波动率分析)
|
||||||
5. 关键技术位分析
|
4. 短期和中期目标价位
|
||||||
6. 具体交易建议(包含止损位)
|
5. 关键技术位分析
|
||||||
|
6. 具体交易建议(包含止损位)
|
||||||
请基于技术指标和市场动态进行分析,给出具体数据支持。
|
|
||||||
"""
|
请基于技术指标和市场动态进行分析,给出具体数据支持。
|
||||||
|
"""
|
||||||
# OpenAI API 调用
|
|
||||||
api_urls = [
|
# OpenAI API 调用
|
||||||
f"{self.API_URL}/chat/completions",
|
api_urls = [
|
||||||
f"{self.API_URL}/v1/chat/completions"
|
f"{self.API_URL}/chat/completions",
|
||||||
]
|
f"{self.API_URL}/v1/chat/completions"
|
||||||
|
]
|
||||||
last_error = None
|
|
||||||
for api_url in api_urls:
|
last_error = None
|
||||||
try:
|
for api_url in api_urls:
|
||||||
response = requests.post(
|
try:
|
||||||
api_url,
|
response = requests.post(
|
||||||
headers={
|
api_url,
|
||||||
"Authorization": f"Bearer {self.API_KEY}",
|
headers={
|
||||||
"Content-Type": "application/json"
|
"Authorization": f"Bearer {self.API_KEY}",
|
||||||
},
|
"Content-Type": "application/json"
|
||||||
json={
|
},
|
||||||
"model": os.getenv('API_MODEL', 'gpt-3.5-turbo'),
|
json={
|
||||||
"messages": [{"role": "user", "content": prompt}]
|
"model": self.API_MODEL,
|
||||||
},
|
"messages": [{"role": "user", "content": prompt}]
|
||||||
timeout=30
|
},
|
||||||
)
|
timeout=30
|
||||||
|
)
|
||||||
if response.status_code == 200:
|
|
||||||
return response.json()['choices'][0]['message']['content']
|
if response.status_code == 200:
|
||||||
else:
|
return response.json()['choices'][0]['message']['content']
|
||||||
last_error = f"API 错误: {response.status_code} - {response.text}"
|
else:
|
||||||
continue
|
last_error = f"API 错误: {response.status_code} - {response.text}"
|
||||||
|
continue
|
||||||
except Exception as e:
|
|
||||||
last_error = str(e)
|
except Exception as e:
|
||||||
continue
|
last_error = str(e)
|
||||||
|
continue
|
||||||
print(f"AI 分析暂时无法使用: {last_error}")
|
|
||||||
return f"AI 分析暂时无法使用: {last_error}"
|
print(f"AI 分析暂时无法使用: {last_error}")
|
||||||
|
return f"AI 分析暂时无法使用: {last_error}"
|
||||||
except Exception as e:
|
|
||||||
print(f"AI 分析发生错误: {str(e)}")
|
except Exception as e:
|
||||||
return f"AI 分析过程中发生错误: {str(e)}"
|
print(f"AI 分析发生错误: {str(e)}")
|
||||||
|
return f"AI 分析过程中发生错误: {str(e)}"
|
||||||
def get_recommendation(self, score):
|
|
||||||
"""根据得分给出建议"""
|
def get_recommendation(self, score):
|
||||||
if score >= 80:
|
"""根据得分给出建议"""
|
||||||
return '强烈推荐买入'
|
if score >= 80:
|
||||||
elif score >= 60:
|
return '强烈推荐买入'
|
||||||
return '建议买入'
|
elif score >= 60:
|
||||||
elif score >= 40:
|
return '建议买入'
|
||||||
return '观望'
|
elif score >= 40:
|
||||||
elif score >= 20:
|
return '观望'
|
||||||
return '建议卖出'
|
elif score >= 20:
|
||||||
else:
|
return '建议卖出'
|
||||||
return '强烈建议卖出'
|
else:
|
||||||
|
return '强烈建议卖出'
|
||||||
def analyze_stock(self, stock_code, market_type='A'):
|
|
||||||
"""分析单个股票"""
|
def analyze_stock(self, stock_code, market_type='A'):
|
||||||
try:
|
"""分析单个股票"""
|
||||||
# 获取股票数据
|
try:
|
||||||
df = self.get_stock_data(stock_code, market_type)
|
# 获取股票数据
|
||||||
|
df = self.get_stock_data(stock_code, market_type)
|
||||||
# 计算技术指标
|
|
||||||
df = self.calculate_indicators(df)
|
# 计算技术指标
|
||||||
|
df = self.calculate_indicators(df)
|
||||||
# 评分系统
|
|
||||||
score = self.calculate_score(df)
|
# 评分系统
|
||||||
|
score = self.calculate_score(df)
|
||||||
# 获取最新数据
|
|
||||||
latest = df.iloc[-1]
|
# 获取最新数据
|
||||||
prev = df.iloc[-2]
|
latest = df.iloc[-1]
|
||||||
|
prev = df.iloc[-2]
|
||||||
# 生成报告(保持原有格式)
|
|
||||||
report = {
|
# 生成报告(保持原有格式)
|
||||||
'stock_code': stock_code,
|
report = {
|
||||||
'analysis_date': datetime.now().strftime('%Y-%m-%d'),
|
'stock_code': stock_code,
|
||||||
'score': score,
|
'analysis_date': datetime.now().strftime('%Y-%m-%d'),
|
||||||
'price': latest['close'],
|
'score': score,
|
||||||
'price_change': (latest['close'] - prev['close']) / prev['close'] * 100,
|
'price': latest['close'],
|
||||||
'ma_trend': 'UP' if latest['MA5'] > latest['MA20'] else 'DOWN',
|
'price_change': (latest['close'] - prev['close']) / prev['close'] * 100,
|
||||||
'rsi': latest['RSI'],
|
'ma_trend': 'UP' if latest['MA5'] > latest['MA20'] else 'DOWN',
|
||||||
'macd_signal': 'BUY' if latest['MACD'] > latest['Signal'] else 'SELL',
|
'rsi': latest['RSI'],
|
||||||
'volume_status': 'HIGH' if latest['Volume_Ratio'] > 1.5 else 'NORMAL',
|
'macd_signal': 'BUY' if latest['MACD'] > latest['Signal'] else 'SELL',
|
||||||
'recommendation': self.get_recommendation(score),
|
'volume_status': 'HIGH' if latest['Volume_Ratio'] > 1.5 else 'NORMAL',
|
||||||
'ai_analysis': self.get_ai_analysis(df, stock_code)
|
'recommendation': self.get_recommendation(score),
|
||||||
}
|
'ai_analysis': self.get_ai_analysis(df, stock_code)
|
||||||
|
}
|
||||||
return report
|
|
||||||
|
return report
|
||||||
except Exception as e:
|
|
||||||
print(f"分析股票时出错: {str(e)}")
|
except Exception as e:
|
||||||
raise
|
print(f"分析股票时出错: {str(e)}")
|
||||||
|
raise
|
||||||
def scan_market(self, stock_list, min_score=60, market_type='A'):
|
|
||||||
"""扫描市场,寻找符合条件的股票"""
|
def scan_market(self, stock_list, min_score=60, market_type='A'):
|
||||||
recommendations = []
|
"""扫描市场,寻找符合条件的股票"""
|
||||||
|
recommendations = []
|
||||||
for stock_code in stock_list:
|
|
||||||
try:
|
for stock_code in stock_list:
|
||||||
report = self.analyze_stock(stock_code, market_type)
|
try:
|
||||||
if report['score'] >= min_score:
|
report = self.analyze_stock(stock_code, market_type)
|
||||||
recommendations.append(report)
|
if report['score'] >= min_score:
|
||||||
except Exception as e:
|
recommendations.append(report)
|
||||||
print(f"分析股票 {stock_code} 时出错: {str(e)}")
|
except Exception as e:
|
||||||
continue
|
print(f"分析股票 {stock_code} 时出错: {str(e)}")
|
||||||
|
continue
|
||||||
# 按得分排序
|
|
||||||
recommendations.sort(key=lambda x: x['score'], reverse=True)
|
# 按得分排序
|
||||||
return recommendations
|
recommendations.sort(key=lambda x: x['score'], reverse=True)
|
||||||
|
return recommendations
|
||||||
|
|||||||
1028
templates/index.html
1028
templates/index.html
File diff suppressed because it is too large
Load Diff
@@ -3,6 +3,8 @@ from stock_analyzer import StockAnalyzer
|
|||||||
from us_stock_service import USStockService
|
from us_stock_service import USStockService
|
||||||
import threading
|
import threading
|
||||||
import os
|
import os
|
||||||
|
import traceback
|
||||||
|
import requests
|
||||||
|
|
||||||
app = Flask(__name__)
|
app = Flask(__name__)
|
||||||
analyzer = StockAnalyzer()
|
analyzer = StockAnalyzer()
|
||||||
@@ -11,7 +13,14 @@ us_stock_service = USStockService()
|
|||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
announcement = os.getenv('ANNOUNCEMENT_TEXT') or None
|
announcement = os.getenv('ANNOUNCEMENT_TEXT') or None
|
||||||
return render_template('index.html', announcement=announcement)
|
# 获取默认API配置信息
|
||||||
|
default_api_url = os.getenv('API_URL', '')
|
||||||
|
default_api_model = os.getenv('API_MODEL', 'gpt-3.5-turbo')
|
||||||
|
# 不传递API_KEY到前端,出于安全考虑
|
||||||
|
return render_template('index.html',
|
||||||
|
announcement=announcement,
|
||||||
|
default_api_url=default_api_url,
|
||||||
|
default_api_model=default_api_model)
|
||||||
|
|
||||||
@app.route('/analyze', methods=['POST'])
|
@app.route('/analyze', methods=['POST'])
|
||||||
def analyze():
|
def analyze():
|
||||||
@@ -20,13 +29,26 @@ def analyze():
|
|||||||
stock_codes = data.get('stock_codes', [])
|
stock_codes = data.get('stock_codes', [])
|
||||||
market_type = data.get('market_type', 'A')
|
market_type = data.get('market_type', 'A')
|
||||||
|
|
||||||
|
# 获取自定义API配置
|
||||||
|
custom_api_url = data.get('api_url')
|
||||||
|
custom_api_key = data.get('api_key')
|
||||||
|
custom_api_model = data.get('api_model')
|
||||||
|
|
||||||
|
# 创建新的分析器实例,使用自定义配置
|
||||||
|
custom_analyzer = StockAnalyzer(
|
||||||
|
custom_api_url=custom_api_url,
|
||||||
|
custom_api_key=custom_api_key,
|
||||||
|
custom_api_model=custom_api_model
|
||||||
|
)
|
||||||
|
|
||||||
if not stock_codes:
|
if not stock_codes:
|
||||||
return jsonify({'error': '请输入代码'}), 400
|
return jsonify({'error': '请输入代码'}), 400
|
||||||
|
|
||||||
results = []
|
results = []
|
||||||
for stock_code in stock_codes:
|
for stock_code in stock_codes:
|
||||||
try:
|
try:
|
||||||
result = analyzer.analyze_stock(stock_code.strip(), market_type)
|
# 使用自定义配置的分析器
|
||||||
|
result = custom_analyzer.analyze_stock(stock_code.strip(), market_type)
|
||||||
results.append(result)
|
results.append(result)
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"分析股票 {stock_code} 失败: {str(e)}")
|
print(f"分析股票 {stock_code} 失败: {str(e)}")
|
||||||
@@ -55,8 +77,59 @@ def search_us_stocks():
|
|||||||
print(f"搜索美股代码时出错: {str(e)}")
|
print(f"搜索美股代码时出错: {str(e)}")
|
||||||
return jsonify({'error': str(e)}), 500
|
return jsonify({'error': str(e)}), 500
|
||||||
|
|
||||||
|
@app.route('/test_api_connection', methods=['POST'])
|
||||||
|
def test_api_connection():
|
||||||
|
"""测试API连接"""
|
||||||
|
try:
|
||||||
|
data = request.json
|
||||||
|
api_url = data.get('api_url')
|
||||||
|
api_key = data.get('api_key')
|
||||||
|
api_model = data.get('api_model')
|
||||||
|
|
||||||
|
if not api_url:
|
||||||
|
return jsonify({'error': '请提供API URL'}), 400
|
||||||
|
|
||||||
|
if not api_key:
|
||||||
|
return jsonify({'error': '请提供API Key'}), 400
|
||||||
|
|
||||||
|
# 构建API URL
|
||||||
|
test_url = api_url
|
||||||
|
if not (api_url.endswith('/chat/completions') or api_url.endswith('/v1/chat/completions')):
|
||||||
|
if api_url.endswith('/v1'):
|
||||||
|
test_url = f"{api_url}/chat/completions"
|
||||||
|
elif api_url.endswith('/'):
|
||||||
|
test_url = f"{api_url}chat/completions"
|
||||||
|
else:
|
||||||
|
test_url = f"{api_url}/v1/chat/completions"
|
||||||
|
|
||||||
|
# 发送测试请求
|
||||||
|
response = requests.post(
|
||||||
|
test_url,
|
||||||
|
headers={
|
||||||
|
"Authorization": f"Bearer {api_key}",
|
||||||
|
"Content-Type": "application/json"
|
||||||
|
},
|
||||||
|
json={
|
||||||
|
"model": api_model or "gpt-3.5-turbo",
|
||||||
|
"messages": [
|
||||||
|
{"role": "user", "content": "Hello, this is a test message. Please respond with 'API connection successful'."}
|
||||||
|
],
|
||||||
|
"max_tokens": 20
|
||||||
|
},
|
||||||
|
timeout=10
|
||||||
|
)
|
||||||
|
|
||||||
|
# 检查响应
|
||||||
|
if response.status_code == 200:
|
||||||
|
return jsonify({'success': True, 'message': '连接成功'})
|
||||||
|
else:
|
||||||
|
error_message = response.json().get('error', {}).get('message', '未知错误')
|
||||||
|
return jsonify({'success': False, 'message': f'连接失败: {error_message}', 'status_code': response.status_code}), 400
|
||||||
|
|
||||||
|
except requests.exceptions.RequestException as e:
|
||||||
|
return jsonify({'success': False, 'message': f'请求错误: {str(e)}'}), 400
|
||||||
|
except Exception as e:
|
||||||
|
return jsonify({'success': False, 'message': f'测试连接时出错: {str(e)}'}), 500
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app.run(host='0.0.0.0', port=8888, debug=True)
|
app.run(host='0.0.0.0', port=8888, debug=True)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user