Files
ai_stock/market_analyzer.py
2025-12-08 15:30:19 +08:00

75 lines
2.7 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# -*- coding: utf-8 -*-
"""
市场分析模块 (模拟云端分析/大模型分析)
"""
import random
class MarketAnalyzer:
def __init__(self):
print("🧠 初始化市场分析模型...")
# 这里可以加载模型或者连接云端API
def analyze(self, stock_data_list):
"""
分析股票数据并生成交易信号
Args:
stock_data_list: 股票数据列表
Returns:
list: 包含交易信号的字典列表
"""
signals = []
print(f"🧠 正在分析 {len(stock_data_list)} 只股票的数据...")
for stock in stock_data_list:
# 简单的策略示例:
# 如果涨幅超过 5%,产生买入信号 (模拟)
# 如果跌幅超过 5%,产生卖出信号 (模拟)
try:
# 兼容可接收数值小数0.0402 表示 4.02%)或字符串("4.02%")
raw_ratio = stock.get('eastmoney_change_ratio', 0.0)
ratio = 0.0
if isinstance(raw_ratio, (int, float)):
# 假定为小数形式
ratio = float(raw_ratio)
# 若误传为 4.02 这类百分数值,则做防御性归一化
if abs(ratio) > 1:
ratio = ratio / 100.0
elif isinstance(raw_ratio, str):
s = raw_ratio.strip().replace('%', '')
if s not in ('', '-'):
v = float(s)
# 从百分数值转小数
ratio = v / 100.0
symbol = stock.get('symbol')
name = stock.get('name')
price = stock.get('eastmoney_price')
# 阈值基于小数±5%
if ratio > 0.05:
signals.append({
'type': 'BUY',
'symbol': symbol,
'name': name,
'price': price,
'reason': f'涨幅显著 ({ratio:.2%}),模型建议买入',
'confidence': 0.85
})
elif ratio < -0.05:
signals.append({
'type': 'SELL',
'symbol': symbol,
'name': name,
'price': price,
'reason': f'跌幅显著 ({ratio:.2%}),模型建议抛售',
'confidence': 0.92
})
except Exception:
continue
return signals