Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Access Chinese financial market data (stocks, futures, funds) using the Tushare Pro API.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
scripts/fund_data_demo.py
1#!/usr/bin/env python32# -*- coding: utf-8 -*-3"""4基金数据获取示例脚本5"""67import tushare as ts8import pandas as pd9import os1011# 读取环境变量中的token, 或者读取本地记录的token12token = os.getenv('TUSHARE_TOKEN') or ts.get_token()1314# 初始化pro接口15pro = ts.pro_api(token)161718def get_fund_list():19"""20获取基金列表21"""22try:23data = pro.fund_basic(market='E', status='L', fields='ts_code,fund_name,fund_type,found_date,issue_date,delist_date')24print("基金列表获取成功:")25print(data.head())26return data27except Exception as e:28print(f"获取基金列表失败:{e}")29return None303132def get_fund_nav(ts_code, start_date, end_date):33"""34获取基金净值数据35"""36try:37data = pro.fund_nav(ts_code=ts_code, start_date=start_date, end_date=end_date)38print(f"{ts_code}基金净值数据获取成功:")39print(data.head())40return data41except Exception as e:42print(f"获取基金净值数据失败:{e}")43return None444546def get_fund_manager():47"""48获取基金经理数据49"""50try:51data = pro.fund_manager(limit=10, fields='ts_code,fund_name,manager_name,begin_date,end_date')52print("基金经理数据获取成功:")53print(data.head())54return data55except Exception as e:56print(f"获取基金经理数据失败:{e}")57return None585960def main():61"""62主函数63"""64print("===== tushare 基金数据获取示例 =====")6566# 获取基金列表67fund_list = get_fund_list()6869if fund_list is not None:70# 获取第一只基金的代码71ts_code = fund_list['ts_code'].iloc[0]72print(f"\n使用基金代码:{ts_code}")7374# 获取基金净值数据(最近30天)75import datetime76end_date = datetime.datetime.now().strftime('%Y%m%d')77start_date = (datetime.datetime.now() - datetime.timedelta(days=30)).strftime('%Y%m%d')78print(f"\n获取基金净值数据:{start_date} 至 {end_date}")79get_fund_nav(ts_code, start_date, end_date)8081# 获取基金经理数据82print("\n获取基金经理数据:")83get_fund_manager()848586if __name__ == "__main__":87main()88