baostock

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

baostock A股数据获取

BaoStock A-share Data Retrieval

任务目标

Task Objectives

  • 本 Skill 用于:通过 BaoStock 接口获取 A 股市场数据、财务指标和股票信息
  • 能力包含:历史 K 线、财务数据、股票信息、行业分类、指数成分股、交易日历
  • 触发条件:用户提及 baostock 或需要获取 A 股免费数据、历史行情、财务信息时
  • This Skill is used to: Obtain A-share market data, financial indicators and stock information via the BaoStock API
  • Capabilities include: Historical K-lines, financial data, stock information, industry classification, index constituent stocks, trading calendars
  • Trigger conditions: When users mention BaoStock or need to obtain free A-share data, historical quotes, or financial information

前置准备

Preparations

  • 依赖说明:安装 baostock 库(已包含在 dependency 中)
  • 凭证配置:BaoStock 无需注册或 API Key,直接调用即可使用
  • 可选配置:支持通过环境变量自定义服务器地址(用于代理场景)
    • BAOSTOCK_SERVER_IP
      :自定义服务器 IP(用于 nginx 代理等场景)
    • BAOSTOCK_SERVER_PORT
      :服务器端口,默认 10030
  • Dependency Description: Install the baostock library (included in dependencies)
  • Credential Configuration: BaoStock requires no registration or API Key; it can be used directly by calling the interface
  • Optional Configuration: Supports custom server address via environment variables (for proxy scenarios)
    • BAOSTOCK_SERVER_IP
      : Custom server IP (for scenarios like nginx proxy)
    • BAOSTOCK_SERVER_PORT
      : Server port, default 10030

操作步骤

Operation Steps

  • 标准流程:
    1. 确定数据需求 — 智能体理解用户意图(行情/K线/财务/股票信息等)
    2. 调用对应脚本 — 根据数据类型选择脚本
    3. 解析结果 — 智能体分析返回的 JSON 格式数据
  • 脚本调用映射:
    • K线/行情数据 →
      python scripts/market_data.py --type kline --code sh.600000 --start 2024-01-01 --end 2024-01-31
    • 财务数据 →
      python scripts/financial_data.py --type profit --code sh.600000 --year 2023 --quarter 4
    • 股票列表/信息 →
      python scripts/stock_info.py --type all_stocks --date 2024-01-02
    • 指数成分股 →
      python scripts/stock_info.py --type index_stocks --index hs300
    • 行业分类 →
      python scripts/stock_info.py --type industry
    • 交易日历 →
      python scripts/stock_info.py --type trade_dates --start 2024-01-01 --end 2024-12-31
  • 可选分支:
    • 当需要多只股票数据:使用逗号分隔多个代码,或使用
      --file
      指定代码列表文件
    • 当需要指定频率:使用
      --frequency
      参数(d/w/m/5/15/30/60)
    • 当需要调整复权类型:使用
      --adjust
      参数(1=后复权,2=前复权,3=不复权)
  • Standard Process:
    1. Confirm Data Requirements — The agent understands the user's intent (quotes/K-lines/finance/stock information, etc.)
    2. Call Corresponding Script — Select the script based on data type
    3. Parse Results — The agent analyzes the returned JSON format data
  • Script Call Mapping:
    • K-line/Quote Data →
      python scripts/market_data.py --type kline --code sh.600000 --start 2024-01-01 --end 2024-01-31
    • Financial Data →
      python scripts/financial_data.py --type profit --code sh.600000 --year 2023 --quarter 4
    • Stock List/Information →
      python scripts/stock_info.py --type all_stocks --date 2024-01-02
    • Index Constituent Stocks →
      python scripts/stock_info.py --type index_stocks --index hs300
    • Industry Classification →
      python scripts/stock_info.py --type industry
    • Trading Calendar →
      python scripts/stock_info.py --type trade_dates --start 2024-01-01 --end 2024-12-31
  • Optional Branches:
    • When multi-stock data is needed: Separate multiple codes with commas, or use
      --file
      to specify a code list file
    • When specifying frequency: Use the
      --frequency
      parameter (d/w/m/5/15/30/60)
    • When adjusting adjustment type: Use the
      --adjust
      parameter (1=post-adjustment, 2=pre-adjustment, 3=no adjustment)

使用示例

Usage Examples

  • 示例1:获取股票日K线数据
    • 场景/输入:用户说"查看贵州茅台最近一个月的K线数据"
    • 预期产出:返回日期、开盘价、最高价、最低价、收盘价、成交量等 OHLCV 数据
    • 关键要点:调用
      python scripts/market_data.py --type kline --code sh.600519 --start 2024-03-01 --end 2024-03-31 --frequency d --adjust 2
  • 示例2:查询财务指标
    • 场景/输入:用户说"获取工商银行2023年的盈利能力指标"
    • 预期产出:返回 ROE、净利润率、毛利率等财务指标
    • 关键要点:调用
      python scripts/financial_data.py --type profit --code sh.601398 --year 2023 --quarter 4
  • 示例3:获取沪深300成分股
    • 场景/输入:用户说"查询沪深300指数的成分股列表"
    • 预期产出:返回沪深300指数的所有成分股代码和名称
    • 关键要点:调用
      python scripts/stock_info.py --type index_stocks --index hs300
  • 示例4:批量下载多只股票数据
    • 场景/输入:用户说"下载茅台、平安、招行的2024年日K线数据"
    • 预期产出:返回多只股票的 K 线数据并保存为 CSV
    • 关键要点:调用
      python scripts/market_data.py --type kline --code sh.600519,sz.000001,sh.600036 --start 2024-01-01 --end 2024-12-31 --output stocks.csv
  • Example 1: Obtain Daily K-line Data of a Stock
    • Scenario/Input: User says "Check the K-line data of Kweichow Moutai for the past month"
    • Expected Output: Returns OHLCV data such as date, opening price, highest price, lowest price, closing price, trading volume, etc.
    • Key Points: Call
      python scripts/market_data.py --type kline --code sh.600519 --start 2024-03-01 --end 2024-03-31 --frequency d --adjust 2
  • Example 2: Query Financial Indicators
    • Scenario/Input: User says "Obtain the profitability indicators of Industrial and Commercial Bank of China for 2023"
    • Expected Output: Returns financial indicators such as ROE, net profit margin, gross profit margin, etc.
    • Key Points: Call
      python scripts/financial_data.py --type profit --code sh.601398 --year 2023 --quarter 4
  • Example 3: Obtain CSI 300 Constituent Stocks
    • Scenario/Input: User says "Query the constituent stock list of the CSI 300 Index"
    • Expected Output: Returns all constituent stock codes and names of the CSI 300 Index
    • Key Points: Call
      python scripts/stock_info.py --type index_stocks --index hs300
  • Example 4: Batch Download Multi-stock Data
    • Scenario/Input: User says "Download the 2024 daily K-line data of Moutai, Ping An, and China Merchants Bank"
    • Expected Output: Returns K-line data of multiple stocks and saves as CSV
    • Key Points: Call
      python scripts/market_data.py --type kline --code sh.600519,sz.000001,sh.600036 --start 2024-01-01 --end 2024-12-31 --output stocks.csv

资源索引

Resource Index

脚本:

Scripts:

  • scripts/market_data.py(用途与参数:获取 K 线/行情数据,支持 --type/--code/--start/--end/--frequency/--adjust/--fields/--output)
  • scripts/financial_data.py(用途与参数:获取财务数据,支持 --type/--code/--year/--quarter/--output)
  • scripts/stock_info.py(用途与参数:获取股票列表/信息/行业/指数成分股,支持 --type/--date/--code/--index/--output)
  • scripts/auth.py(用途与参数:认证模块,支持自定义服务器配置)
  • scripts/market_data.py (Purpose & Parameters: Obtain K-line/quote data, supports --type/--code/--start/--end/--frequency/--adjust/--fields/--output)
  • scripts/financial_data.py (Purpose & Parameters: Obtain financial data, supports --type/--code/--year/--quarter/--output)
  • scripts/stock_info.py (Purpose & Parameters: Obtain stock list/information/industry/index constituent stocks, supports --type/--date/--code/--index/--output)
  • scripts/auth.py (Purpose & Parameters: Authentication module, supports custom server configuration)

参考文档

Reference Documents

  • 常用API文档(何时读取:需要使用 BaoStock Python API、查看详细 API 参数说明时)
  • 知识库文档目录(何时读取:需要查看更为全面详细 API 参数说明时)
  • Common API Documentation (When to read: When needing to use the BaoStock Python API, view detailed API parameter descriptions)
  • Knowledge Base Document Directory (When to read: When needing to view more comprehensive and detailed API parameter descriptions)

注意事项

Notes

  • BaoStock 无需注册或 API Key,直接调用
    bs.login()
    即可使用
  • BaoStock 股票代码格式:
    sh.600000
    (上海)、
    sz.000001
    (深圳)、
    bj.430047
    (北京)
  • 部分数据(如财务数据)可能有延迟,建议在交易日 9:30-15:00 之外获取
  • 脚本返回 JSON 格式数据,智能体负责解析并转换为用户友好的展示
  • 每个会话必须以
    bs.login()
    开始、
    bs.logout()
    结束
  • K 线数据默认返回字符串类型,数值计算前需用
    .astype(float)
    转换
  • 非线程安全,并行下载请使用多进程而非多线程
  • 财务数据按季度提供,报告期结束后约有 2 个月的延迟
  • BaoStock requires no registration or API Key; simply call
    bs.login()
    to use it
  • BaoStock stock code format:
    sh.600000
    (Shanghai),
    sz.000001
    (Shenzhen),
    bj.430047
    (Beijing)
  • Some data (such as financial data) may have delays; it is recommended to obtain it outside of trading hours (9:30-15:00)
  • Scripts return data in JSON format; the agent is responsible for parsing and converting it into user-friendly displays
  • Each session must start with
    bs.login()
    and end with
    bs.logout()
  • K-line data is returned as string type by default; use
    .astype(float)
    to convert before numerical calculations
  • Not thread-safe; use multiprocessing instead of multithreading for parallel downloads
  • Financial data is provided quarterly, with a delay of approximately 2 months after the end of the reporting period