Skip to content

TrueHOOHA/shanxi-securities-tushare-skill / sxsc-tushare-analysis

sxsc-tushare-analysis

Verified

对用户提到的标的(沪深股票、指数、公募基金、期货)进行综合分析,输出带 ECharts 图表的 HTML 分析报告。 触发条件:用户说"分析""研究""评估""看看XX怎么样""XX基本面""XX技术面""XX估值""XX财务""深度研究""对比"等分析类请求,且标的属于 A 股/指数/公募基金/期货。 本 skill 自动调用 sxsc_tushare 取数并输出完整报告,接管数据获取与分析全流程。当本 skill 被触发时,数据获取类 skill 不应同时激活——分析已包含数据。

scripts/basic_metrics.py

View on GitHub
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
基础指标模块:收益率、均线、波动率、回撤、夏普、Sortino、信息比率、财务趋势、风险信号。

Agent 按需复制函数,填入实际参数即可。
"""

from typing import Optional

import numpy as np


def calc_returns(df_close, periods=(5, 20, 60, 120, 250)):
    """区间收益率。df_close: 日期升序的 close 序列。
    返回 dict:键为「最新收盘」和 f"近{p}日涨幅%",调用时用字符串键,
    不要用整数 p 直接访问(如 out["近20日涨幅%"],而非 out[20])。
    """
    if len(df_close) == 0:
        return {"最新收盘": "N/A"}
    latest = df_close.iloc[-1]
    if isinstance(latest, float) and np.isnan(latest):
        return {"最新收盘": "N/A"}
    out = {"最新收盘": round(float(latest), 2)}
    for p in periods:
        if len(df_close) > p:
            out[f"近{p}日涨幅%"] = round(float((latest / df_close.iloc[-1 - p] - 1) * 100), 2)
    return out


def calc_cagr(start_value, end_value, days):
    """复合年化增长率(CAGR)。
    days: 持有天数(交易日)。年化按 250 交易日计。
    """
    if start_value <= 0 or days <= 0:
        return None
    years = days / 250
    cagr = (end_value / start_value) ** (1 / years) - 1
    return round(cagr * 100, 2)


def calc_ma(df_close, windows=(5, 10, 20, 60)):
    """均线值(用于判断多头/空头排列)。

    窗口数据不足时该窗口返回 None:不能用短窗口均值冒充 MA60
    (30 个点算出的"MA60"与真实 60 日均线完全不同)。
    """
    out = {}
    for w in windows:
        if len(df_close) < w:
            out[f"MA{w}"] = None
        else:
            out[f"MA{w}"] = round(float(df_close.tail(w).mean()), 2)
    return out


def calc_volatility(df_close, window: Optional[int] = None):
    """年化波动率。

    ⚠️ 默认用**全样本**收益率,与 calc_max_drawdown / calc_sharpe 口径一致——
    旧实现默认只取近 20 日收益率,导致同一句结论里"年化波动"(近20日窗口)
    与"最大回撤/夏普"(全样本)混用窗口(实测同序列 20 日波动 37.35% vs
    全样本 31.54%)。需要短期波动率时显式传 window。
    """
    ret = df_close.pct_change().dropna()
    if len(ret) < 2:
        return None
    sample = ret if window is None else ret.tail(window)
    if len(sample) == 0:
        return None
    return round(float(sample.std()) * np.sqrt(250) * 100, 2)


def calc_max_drawdown(df_nav):
    """最大回撤(%)。df_nav: 日期升序的净值序列。"""
    cummax = df_nav.cummax()
    dd = (df_nav / cummax - 1).min()
    return round(dd * 100, 2)


def calc_sharpe(df_nav, risk_free=0.02, periods=250):
    """夏普比率(年化)。"""
    ret = df_nav.pct_change().dropna()
    if len(ret) < 2 or ret.std() == 0:
        return None
    annual_ret = ret.mean() * periods
    annual_std = ret.std() * np.sqrt(periods)
    return round((annual_ret - risk_free) / annual_std, 2)


def calc_sortino(df_nav, risk_free=0.02, periods=250):
    """Sortino 比率(分母用全样本下行偏差,MAR 与分子无风险利率同口径)。
    下行偏差 = sqrt(mean(min(r - MAR, 0)^2)),MAR = risk_free / periods(与分子的
    无风险利率一致);旧实现用"负收益子集的样本标准差(围绕负收益均值)",
    既非标准下行偏差,又与分子 rf、MAR=0 的口径不一致,会系统性高估 Sortino。
    """
    ret = df_nav.pct_change().dropna()
    if len(ret) < 2:
        return None
    annual_ret = ret.mean() * periods
    mar = risk_free / periods  # 与分子 rf 同口径;rf=0 时即 MAR=0
    downside_dev = np.sqrt(np.mean(np.minimum(ret - mar, 0) ** 2))  # 全样本,未达标收益计 0
    if downside_dev == 0:
        return None
    return round((annual_ret - risk_free) / (downside_dev * np.sqrt(periods)), 2)


def calc_information_ratio(stock_returns, benchmark_returns, periods=250):
    """信息比率 = 超额收益年化 / 跟踪误差年化。"""
    excess = stock_returns - benchmark_returns
    if len(excess) < 2 or excess.std() == 0:
        return None
    annual_excess = excess.mean() * periods
    tracking_error = excess.std() * np.sqrt(periods)
    return round(annual_excess / tracking_error, 2)

reverse-engineer-anythingmorluto/reaReverse engineer native, managed, Electron/JavaScript, packaged, firmware, and browser targets with REA, and build agent environments (AgentEnv bundles) of web applications from a live instance or a capture, and of Linux desktop apps with a task from a request. Use shipped-artifact or requested runtime evidence to explain features, compare versions, decompile code, or guide a reconstruction. Skip REA for ordinary source-repository architecture analysis.Stars: 472kUpdated Claude CodeCodexVerifiedInstall
diagnosing-superpowersobra/superpowersUse when a superpowers session went wrong and your human partner wants to know why — repeated work, ignored plans, stumbles, poor results, a skill that didn't fire, "it took too long", "why is it so expensive", "what is it doing" — or wants to build a bug report for the superpowers maintainers, for the current session or a past one identified by id or path, on any harness.Stars: 322kUpdated Claude CodeCodexVerifiedInstall
dispatching-parallel-agentsobra/superpowersUse when facing 2+ independent tasks that can be worked on without shared state or sequential dependenciesStars: 322kUpdated Claude CodeCodexVerifiedInstall
executing-plansobra/superpowersUse when executing an implementation plan in the current session as the implementer yourself — your human partner chose inline execution, or no subagent tool is availableStars: 322kUpdated Claude CodeCodexVerifiedInstall