Skip to main content

mairui — 麦蕊智数 Python SDK

完整接口字段说明见 API.md(含沪深数据中心 / 基金 / 量化因子)。方法对照见 API_METHOD_MAP.md

面向 https://api.mairuiapi.com,覆盖官网 API 文档(如 https://mairuiapi.com/hsdata)中对外公开的全部接口。

源码仓库:https://github.com/MaiRuiApi/mairui

安装

pip install mairui

PyPI:https://pypi.org/project/mairui/

30 秒上手

from mairui import Client

with Client(licence="YOUR-LICENCE-UUID") as api:
    # 股票列表
    stocks = api.stock_list()

    # 日线历史(可选 st/et/lt)
    bars = api.stock_history("000001.SZ", "d", "n", st="20240101", et="20241231")

    # 实时
    tick = api.stock_real_time("000001")

    # 财务
    bal = api.stock_balance("000001.SZ")

    # 批量并发(复用连接池)
    batch = api.map(
        api.stock_history,
        ["000001.SZ", "600519.SH", "000002.SZ"],
        period="d",
        dividend="n",
        lt=5,
        max_workers=8,
    )

只需绑定一次 licence,之后所有方法自动拼到 URL 末段。

设计要点(效率与工程)

措施 说明
requests.Session + 连接池 默认 pool_connections=32 / pool_maxsize=64,避免每次握手
Gzip 默认 Accept-Encoding: gzip, deflate
自动重试 429/502/503/504 指数退避重试(仅 GET)
超时 默认连接 5s / 读 60s,可构造时覆盖
api.map(...) 线程池批量拉多标的,适合回测灌数
路径编码 板块名等中文/特殊字符自动 quote
异常分层 MairuiAuthError / MairuiTimeoutError / MairuiHTTPError

接口 ↔ 方法对照(摘要)

完整对照见 API_METHOD_MAP.md。常用:

文档分类 示例方法
股票列表 stock_list / sectors_list / sector_detail
行情 / 指标 stock_history / stock_latest / stock_macd / stock_vip_history
实时 stock_real_time / stock_ssjy / stock_ssjy_more / stock_ssjy_all
财务 stock_balance / stock_income / stock_financial(table, code)
F10 company_profile / company_dividends / …
股池 limit_up_pool(date) / limit_down_pool / …
指数 index_history / index_macd / index_list
北交所 bj_history / bj_balance / …
基金 / 科创 fund_list / etf_list / star_real_time

查询参数与文档一致:

  • st / et:开始 / 结束时间(如 2024010120240101153000
  • lt:最新条数(从尾部截取)

不传则返回服务端完整可用数据(由证书权限与数据存量决定)。

构造参数

Client(
    licence="...",
    base_url="https://api.mairuiapi.com",  # 一般无需改
    timeout=(5, 60),
    max_retries=3,
    pool_connections=32,
    pool_maxsize=64,
)

异常处理

from mairui import Client, MairuiAuthError, MairuiHTTPError, MairuiTimeoutError

try:
    with Client(licence="...") as api:
        print(api.stock_history("000001.SZ", "d", "n", lt=3))
except MairuiAuthError as e:
    print("证书问题", e)
except MairuiTimeoutError as e:
    print("超时", e)
except MairuiHTTPError as e:
    print("HTTP", e.status_code, e.payload)

目录

mairui/
  __init__.py
  client.py      # 全部接口方法
  _http.py       # Session / 重试 / 编码
  exceptions.py
examples/
  quickstart.py
pyproject.toml
README.md
API_METHOD_MAP.md
LICENSE

说明

  • 接口清单与官网文档页一致,例如沪深 A 股:https://mairuiapi.com/hsdata(同站还有指数、基金等文档入口)。
  • SDK 默认请求主站 https://api.mairuiapi.com;证书可在官网获取。
  • 本包为同步 SDK;若需 asyncio,可在外层用 asyncio.to_thread 包一层,或后续再发 async 版。

Download files

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

Source Distribution

mairui-1.2.0.tar.gz (16.3 kB view details)

Uploaded Source

Built Distribution

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

mairui-1.2.0-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file mairui-1.2.0.tar.gz.

File metadata

  • Download URL: mairui-1.2.0.tar.gz
  • Upload date:
  • Size: 16.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.9

File hashes

Hashes for mairui-1.2.0.tar.gz
Algorithm Hash digest
SHA256 597c67bb0efe2ed1c1f4c6577ee01faaa3bda3851ae815367f0b3ad3de575472
MD5 22abf66bba2dd88d69b9329fc45ad4bc
BLAKE2b-256 e81691338821c9e0c76edf626391ec379317d07c358da34cb7db01aecfb4ea89

See more details on using hashes here.

File details

Details for the file mairui-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: mairui-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/7.0.0 CPython/3.12.9

File hashes

Hashes for mairui-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 27fd81ff304381e20531e203763672a4cb998d74e51e38c410cd3f40bdbf1ebe
MD5 973651345b3109e5ae98600b2ef0fa1f
BLAKE2b-256 9a71f8f563cdebc1795acfc14ee070dd8f2d43a2d0fe87ef92a142cf99b170a0

See more details on using hashes here.

Release history Release notifications | RSS feed

1.2.1

2 files

This release

1.2.0 This release

2 files

1.0.0

2 files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page