121 lines
4.4 KiB
Python
121 lines
4.4 KiB
Python
"""
|
||
高级数据技能
|
||
封装Tushare Pro高级数据接口(需要5000+积分)
|
||
"""
|
||
from typing import Dict, Any
|
||
from app.skills.base import BaseSkill, SkillParameter
|
||
from app.services.tushare_advanced_service import tushare_advanced_service
|
||
from app.utils.logger import logger
|
||
|
||
|
||
class AdvancedDataSkill(BaseSkill):
|
||
"""高级数据技能"""
|
||
|
||
def __init__(self):
|
||
super().__init__()
|
||
self.name = "advanced_data"
|
||
self.description = "获取高级财务数据、估值数据、资金流向等(Tushare Pro 5000+积分)"
|
||
self.parameters = [
|
||
SkillParameter(
|
||
name="stock_code",
|
||
type="string",
|
||
description="股票代码",
|
||
required=True
|
||
),
|
||
SkillParameter(
|
||
name="data_type",
|
||
type="string",
|
||
description="数据类型:financial(财务)、valuation(估值)、money_flow(资金流向)、all(全部)",
|
||
required=False,
|
||
default="all"
|
||
)
|
||
]
|
||
|
||
async def execute(self, **kwargs) -> Dict[str, Any]:
|
||
"""
|
||
执行高级数据查询
|
||
|
||
支持的数据类型:
|
||
- financial: 财务数据(利润表、资产负债表、财务指标)
|
||
- valuation: 估值数据(PE、PB、PS、市值等)
|
||
- money_flow: 资金流向
|
||
- margin: 融资融券
|
||
- block_trade: 大宗交易
|
||
- all: 全部数据
|
||
"""
|
||
stock_code = kwargs.get('stock_code')
|
||
data_type = kwargs.get('data_type', 'all') # 默认获取所有数据
|
||
|
||
if not stock_code:
|
||
return {
|
||
"success": False,
|
||
"error": "缺少股票代码"
|
||
}
|
||
|
||
try:
|
||
result = {
|
||
"success": True,
|
||
"data": {}
|
||
}
|
||
|
||
# 财务数据(利润表、资产负债表、财务指标)
|
||
if data_type in ['financial', 'all']:
|
||
financial_data = {}
|
||
|
||
# 获取财务指标(最重要)
|
||
indicators = tushare_advanced_service.get_financial_indicators(stock_code)
|
||
if indicators:
|
||
financial_data['indicators'] = indicators
|
||
|
||
# 获取利润表(最近一期)
|
||
income = tushare_advanced_service.get_income_statement(stock_code)
|
||
if income and income.get('data'):
|
||
financial_data['income'] = income['data'][0] if income['data'] else None
|
||
|
||
# 获取资产负债表(最近一期)
|
||
balance = tushare_advanced_service.get_balance_sheet(stock_code)
|
||
if balance and balance.get('data'):
|
||
financial_data['balance'] = balance['data'][0] if balance['data'] else None
|
||
|
||
if financial_data:
|
||
result['data']['financial'] = financial_data
|
||
|
||
# 估值数据
|
||
if data_type in ['valuation', 'all']:
|
||
valuation = tushare_advanced_service.get_daily_basic(stock_code)
|
||
if valuation:
|
||
result['data']['valuation'] = valuation.get('data')
|
||
|
||
# 资金流向
|
||
if data_type in ['money_flow', 'all']:
|
||
money_flow = tushare_advanced_service.get_money_flow(stock_code)
|
||
if money_flow:
|
||
# 只取最近5天的数据
|
||
result['data']['money_flow'] = money_flow.get('data', [])[:5]
|
||
|
||
# 融资融券
|
||
if data_type in ['margin', 'all']:
|
||
margin = tushare_advanced_service.get_margin_detail(stock_code)
|
||
if margin:
|
||
# 只取最近5天的数据
|
||
result['data']['margin'] = margin.get('data', [])[:5]
|
||
|
||
# 大宗交易
|
||
if data_type in ['block_trade', 'all']:
|
||
block_trade = tushare_advanced_service.get_block_trade(stock_code)
|
||
if block_trade:
|
||
# 只取最近10条
|
||
result['data']['block_trade'] = block_trade[:10]
|
||
|
||
# 注意:重大公告功能已移除(需要特殊权限)
|
||
|
||
logger.info(f"获取高级数据成功: {stock_code}, 类型: {data_type}")
|
||
return result
|
||
|
||
except Exception as e:
|
||
logger.error(f"获取高级数据失败: {e}")
|
||
return {
|
||
"success": False,
|
||
"error": str(e)
|
||
}
|