Metadata-Version: 2.4
Name: fundapi
Version: 0.1.0
Summary: 基金数据 API 官方 Python SDK —— 净值 / 档案 / 季报持仓 / 基金经理 / 排行 / 公告 / 组合穿透（19 个端点，免费端点无需 Key）
Author: fundapi
License-Expression: MIT
Project-URL: Homepage, https://fundapi.cn
Project-URL: Documentation, https://fundapi.cn/docs/
Project-URL: Endpoint list, https://fundapi.cn/docs/
Project-URL: Changelog, https://fundapi.cn/changelog/
Project-URL: Repository, https://github.com/fundapi-cn/fundapi-python
Project-URL: Issues, https://github.com/fundapi-cn/fundapi-python/issues
Keywords: fund,fund-api,mutual-fund,finance,mcp,financial-data-api
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Office/Business :: Financial :: Investment
Classifier: Operating System :: OS Independent
Requires-Python: >=3.9
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: requests>=2.28
Provides-Extra: dev
Requires-Dist: pytest>=7; extra == "dev"
Requires-Dist: build; extra == "dev"
Requires-Dist: twine; extra == "dev"
Dynamic: license-file

<div align="center">

<a href="https://fundapi.cn"><img src="https://fundapi.cn/icon-round.png" width="88" height="88" alt="fundapi"></a>

# fundapi — 基金数据 API 官方 Python SDK

**净值 / 档案 / 季报持仓 / 基金经理 / 排行 / 公告 / 分红 / 交易规则 / 评级 / 组合穿透** —— **19 个端点**，**免费端点无需 Key**。

![Python](https://img.shields.io/badge/python-3.9%2B-3776AB?logo=python&logoColor=white)
[![PyPI](https://img.shields.io/pypi/v/fundapi?label=PyPI)](https://pypi.org/project/fundapi/)
![License](https://img.shields.io/badge/license-MIT-green)

**中文** · [English](README.en.md)

[官网](https://fundapi.cn) · [文档](https://fundapi.cn/docs/) · [MCP 接入](https://fundapi.cn/mcp) · [更新日志](https://fundapi.cn/changelog/)

</div>

```bash
pip install fundapi
```

## 这是什么

**基金数据 API 的官方 Python SDK** —— 19 个端点对应 19 个方法：鉴权、重试（含 429 自动退避）、错误映射、基金代码规范化、返回解析都由 SDK 处理，一行方法拿到结构化数据。

```python
from fundapi import FundAPI
api = FundAPI(key="cf-你的Key")      # 或用环境变量 FUND_KEY
print(api.fund_profile("110022"))    # 基金档案
```

**3 个端点无需 Key**（装上就能调）：`fund_search` · `fund_profile` · `fund_screener_options`。其余需要 Key（[获取](https://fundapi.cn/pricing)）。

## 30 秒上手

```python
from fundapi import FundAPI

api = FundAPI()                              # 免费端点，不用 Key

# 按名字搜基金（不知道代码时先搜）
for f in api.fund_search("易方达消费行业", limit=3)["list"]:
    print(f["code"], f["name"])

# 基金档案
print(api.fund_profile("110022"))
```

付费端点：

```python
api = FundAPI(key="cf-你的Key")
print(api.fund_nav("110022", size=5))        # 历史净值
print(api.fund_rating("110022"))             # 评级
```

## 方法一览（19 个，与 MCP 工具同名）

| 方法 | 端点 | 档位 |
|---|---|---|
| `fund_search(q, limit=20)` | 搜索基金 | 免费 |
| `fund_profile(code)` | 基金档案 | 免费 |
| `fund_screener_options()` | 筛选项字典 | 免费 |
| `fund_nav(code, page=1, size=20, start="", end="")` | 历史净值 | 专业版 |
| `fund_holdings(code, year="", quarter="")` | 季报持仓 | 专业版 |
| `fund_managers(code)` | 现任经理 | 专业版 |
| `fund_rank(ft="all", sort="1y", order="desc", page=1, size=50)` | 全市场排行 | 专业版 |
| `fund_notices(code, page=1, size=20)` | 基金公告 | 专业版 |
| `fund_estimate(code)` | 当日估值 | 专业版 |
| `fund_dividends(code)` | 历史分红 | 专业版 |
| `fund_trade_rule(code)` | 交易规则 | 专业版 |
| `fund_rating(code, page=1, size=20)` | 评级 | 专业版 |
| `fund_overlap(codes)` | 多基金持仓重叠 | 机构版 |
| `fund_portfolio_xray(codes, weights)` | 组合穿透到个股 | 机构版 |
| `fund_holding_changes(code, periods=4, top_n=10)` | 持仓变动 | 机构版 |
| `fund_manager_history(code)` | 基金经理历任 | 机构版 |
| `fund_portfolio_diagnose(codes, weights)` | 组合诊断 | 机构版 |
| `fund_screener(...)` | 条件筛选全市场 | 机构版 |
| `fund_compare(codes)` | 多基金对比 | 机构版 |

## 返回结构

默认返回端点的 `data` 字段（结构因端点而异，见[文档](https://fundapi.cn/docs/)）。要完整信封（含 `ok` / `tier` / `data` 等）：

```python
api = FundAPI(raw=True)
```

## 错误处理

```python
from fundapi import AuthError, RateLimitError, EmptyResultError, APIError

try:
    api.fund_nav("110022")
except AuthError:
    ...          # Key 无效 / 档位不足（去 https://fundapi.cn/pricing 升级）
except RateLimitError:
    ...          # 超频，稍后再试
except EmptyResultError:
    ...          # 当前无数据（不扣次数）
except APIError:
    ...          # 其他错误，消息里带服务端原话
```

## 环境

- Python 3.9+
- 依赖：`requests`

## License

MIT
