Skip to main content

获取基金溢价率的脚本

Project description

功能

获取场内外LOF基金溢价信息,寻找套利机会。


程序流程图

┌─────────────────────────────────────────────────────────────────────────┐
│                              lof_premium()                              │
│                         (主入口函数,可配置参数)                          │
└─────────────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│                          get_fund_data()                                │
│                      获取并合并场内外基金数据                              │
├─────────────────────────────────────────────────────────────────────────┤
│  ┌─────────────────┐    ┌─────────────────┐    ┌─────────────────────┐ │
│  │ ak.fund_purchase│    │ ak.fund_lof_spot│    │   pd.merge()        │ │
│  │    _em()        │    │    _em()        │    │  (left_on='基金代码' │ │
│  │  场外基金数据    │───▶│  场内LOF行情    │───▶│ right_on='代码')    │ │
│  │  净值/申赎状态   │    │  最新价/涨跌幅   │    │                     │ │
│  └─────────────────┘    └─────────────────┘    └─────────────────────┘ │
└─────────────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│                      calculate_premium()                                │
│                        计算每只基金溢价率                                 │
│              公式: (最新价 - 净值) / 净值 × 100%                         │
└─────────────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│                       filter_premium()                                  │
│                    筛选溢价率超过阈值的基金                               │
│              默认: |溢价率%| >= 5%  (可配置rate参数)                      │
└─────────────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│                      add_fund_tn_col()                                  │
│                    为每只基金添加T+N确认日信息                            │
│                    (t_n=False时跳过此步骤)                               │
├─────────────────────────────────────────────────────────────────────────┤
│  ┌─────────────────┐    ┌─────────────────┐    ┌─────────────────────┐ │
│  │   checkFundCode │    │ fund_tn_rules() │    │   返回T+N信息字典    │ │
│  │   (遍历每行)    │───▶│  爬取天天基金网页 │───▶│ {"买入确认日":...}  │ │
│  │  检查场外代码   │    │  fundf10.east   │    │ {"卖出确认日":...}  │ │
│  │  处理空值异常   │    │  money.com      │    │ {"jjfl_url":...}    │ │
│  └─────────────────┘    └─────────────────┘    └─────────────────────┘ │
│                                                                         │
│  异常处理:                                                              │
│  - 未开通代销 → 返回"未代销"                                            │
│  - 网页结构变化 → 记录error日志                                         │
│  - 网络/解析错误 → 记录error日志,返回空字典                             │
└─────────────────────────────────────────────────────────────────────────┘
                                    │
                                    ▼
┌─────────────────────────────────────────────────────────────────────────┐
│                         返回结果DataFrame                                │
│                    按COL_ORDER顺序排列列名                                │
│              保存到 ./lof_premium/YYYYMMDD.csv (main函数)                │
└─────────────────────────────────────────────────────────────────────────┘

功能

使用示例

先安装模块

pip install lof-fund

使用lof_premium即可获得基金溢价率信息。

from lof import lof_premium

if __name__ == "__main__":
    df_lof = lof_premium()   # 默认返回溢价率±5%的基金,并携带基金t+n信息
    print(df_lof)
    

或者直接控制台运行

lofpm

关键信息

[x] 场内外基金的溢/折价

[x] 基金申赎T日信息

[x] 申赎状态

[x] 申赎额度

可供选择的字段:

['场外代码', '基金简称', '最新净值/万份收益', 
'最新净值/万份收益-报告时间', '申购状态', 
'赎回状态', '下一开放日','购买起点', , 溢价率%
'日累计限定金额','手续费', '场内代码', '名称',
'最新价', '涨跌额', '涨跌幅', '成交量','成交额',
'开盘价', '最高价', '最低价', '昨收', '换手率',
'流通市值', '总市值','买入确认日', '卖出确认日']

Data columns (total 27 columns):
 #   Column          Non-Null Count  Dtype  
---  ------          --------------  -----
 0   场外代码            350 non-null    object
 1   基金简称            350 non-null    object
 2   最新净值/万份收益       350 non-null    float64
 3   最新净值/万份收益-报告时间  350 non-null    object
 4   申购状态            350 non-null    object
 5   赎回状态            350 non-null    object
 6   下一开放日           37 non-null     object
 7   购买起点            350 non-null    float64
 8   日累计限定金额         350 non-null    float64
 9   手续费             350 non-null    float64
 10  场内代码            351 non-null    object
 11  名称              351 non-null    object
 12  最新价             351 non-null    float64
 13  涨跌额             351 non-null    float64
 14  涨跌幅             351 non-null    float64
 15  成交量             346 non-null    float64
 16  成交额             346 non-null    float64
 17  开盘价             346 non-null    float64
 18  最高价             346 non-null    float64
 19  最低价             346 non-null    float64
 20  昨收              351 non-null    float64
 21  换手率             347 non-null    float64
 22  流通市值            351 non-null    int64
 23  总市值             351 non-null    int64
 24  溢价率%            350 non-null    float64
 25  买入确认日           350 non-null    object
 26  卖出确认日           350 non-null    object
dtypes: float64(15), int64(2), object(10)

数据来源

天天基金接口 https://fund.eastmoney.com/161116.html

基金档案(获取T+n信息):https://fundf10.eastmoney.com/jjfl_161129.html

基金公告:https://fundf10.eastmoney.com/jjgg_161116.html

查询指定基金信息分析

当前能力评估

❌ 不支持直接查询单只基金

当前 lof_premium() 函数的工作模式:

  1. 全量获取:调用 ak.fund_purchase_em() 获取所有场外基金数据
  2. 全量获取:调用 ak.fund_lof_spot_em() 获取所有场内LOF行情
  3. 全量计算:计算全部基金的溢价率
  4. 阈值筛选:仅返回 |溢价率%| >= rate 的基金(默认±5%)

如果要查询指定基金

方案1:调用后过滤(推荐,改动最小)

from lof import lof_premium

# 获取全部高溢价基金
df = lof_premium(rate=0)  # rate=0 返回全部,不筛选

# 过滤指定基金
target_code = "161116"  # 场外代码
df_target = df[df['场外代码'] == target_code]
print(df_target)

方案2:新增查询函数(需要修改lof.py)

def query_fund(fund_code: str, t_n: bool = True) -> pd.DataFrame:
    """查询单只基金信息"""
    df = lof_premium(rate=0, t_n=t_n)
    return df[df['场外代码'] == fund_code]

方案3:直接调用底层函数(跳过全量获取)

from lof import fund_tn_rules

# 直接获取单只基金的T+N信息
tn_info = fund_tn_rules("161116")
print(tn_info)
# {'买入确认日': 'T+1', '卖出确认日': 'T+1', 'jjfl_url': '...'}

性能瓶颈

  • T+N信息爬取慢:每只基金需单独请求 fundf10.eastmoney.com
  • 无法单点查询:akshare接口返回全量数据,没有单基金查询参数
  • IP限制风险:频繁爬取T+N信息可能被天天基金拉黑

v2.0 (ing)

考虑爬集思录的数据或优化原版。

集思录可能出现数据问题,2025/6/3出现过此问题。

v1 (now)

使用akshare和爬虫程序完成了基本需求,但是速度较慢

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

lof_fund-1.1.8-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

Details for the file lof_fund-1.1.8-py3-none-any.whl.

File metadata

  • Download URL: lof_fund-1.1.8-py3-none-any.whl
  • Upload date:
  • Size: 7.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.7

File hashes

Hashes for lof_fund-1.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 5d48baee428ce0c8018ab02d9cad2ce97d47b020866a4e277a9dc6b558e26605
MD5 dccba8e3a4105993ffbec068da7c31f0
BLAKE2b-256 b4c5609692f04e40e90792bf714d7a52c4a1be956abeae88cdb85d3cd1f9f6a5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page