在国际股指期货量化开发中,日经 225是高频交易品种。本文基于脉动数据行情平台接口,用 Python 实现日经 225 实时行情的两种接入方式:WebSocket 实时推送、HTTP 轮询拉取,并提供可直接运行的代码。


一、支持品种与日经 225 实时数据

平台支持日经 225、台指期、恒指、富时 A50、德指、小标普、小道指等国际股指期货实时数据。

实时行情示例(2026-04-17)

表格

产品名称 代码 最新价 涨幅 涨跌 卖价 买价 交易量 更新时间
日经 225 ZY_NKD 59065 -0.46 -275 59075 59060 2035 2026-04-17 10:16:25
恒指 2604 HX_HSI 26151 -0.76 -200 26151 26149 44087 2026-04-17 10:16:23
富时 A50 HX_CN 15439 -0.32 -50 15439 15438 148272 2026-04-17 10:16:25
德指 ZY_DAX 24325 -0.02 -5 24326 24319 174 2026-04-17 10:16:25

二、接入前置条件

  1. 服务器 IP 需在平台授权
  2. 建议请求头添加 Accept-Encoding:gzip
  3. 支持 WebSocket 推送 / HTTP 拉取两种模式

三、Python 实现 WebSocket 实时订阅(推荐)

WebSocket 低延迟、主动推送,适合量化与实时看板。

安装依赖

bash

pip install websockets asyncio

Python 完整代码

python

import asyncio
import websockets
import json
import time

# 心跳:每10秒发送一次
def send_ping():
    return json.dumps({"ping": int(time.time())})

# 订阅日经225
def subscribe():
    return json.dumps({"Key": "ZY_NKD,TW_TXF,HX_HSI,ZY_DAX"})

async def connect_market():
    uri = "ws://39.107.99.235/ws"
    while True:
        try:
            async with websockets.connect(uri, ping_interval=None) as websocket:
                print("WebSocket 连接成功")
                # 发送订阅
                await websocket.send(subscribe())
                last_ping_time = time.time()

                while True:
                    # 定时发心跳
                    if time.time() - last_ping_time > 10:
                        await websocket.send(send_ping())
                        last_ping_time = time.time()
                    
                    # 接收数据
                    data = await websocket.recv()
                    if "pong" not in data:
                        print("实时行情:", json.loads(data))

        except Exception as e:
            print("断开重连中...", e)
            await asyncio.sleep(3)

if __name__ == "__main__":
    asyncio.run(connect_market())

核心返回关键字段

  • StockCode:品种代码
  • Price:最新价
  • High/Low:高低价
  • Open/Close:开盘/收盘价
  • BP1/BV1:买一价 / 量
  • SP1/SV1:卖一价 / 量
  • Diff/DiffRate:涨跌额 / 涨跌幅
  • Time:更新时间

四、Python 实现 HTTP API 拉取(简单稳定)

适合低频查询、定时刷新场景。

1. 获取日经 225 实时行情

python

import requests

url = "http://39.107.99.235:1008/getQuote.php"
params = {"code": "ZY_NKD"}
headers = {"Accept-Encoding": "gzip"}

resp = requests.get(url, params=params, headers=headers)
print(resp.json())

2. 获取日经 225 K 线数据

python

url = "http://39.107.99.235:1008/redis.php"
params = {
    "code": "ZY_NKD",
    "time": "1m",  # 1m/5m/15m/30m/1h/1d/1M
    "rows": 40
}
resp = requests.get(url, params=params, headers=headers)
print(resp.json())

3. 查询产品分类与代码列表

python

# 分类
resp = requests.get("http://39.107.99.235:1008/getCategory.php")

# 国际股指期货列表
resp = requests.get("http://39.107.99.235:1008/getSymbolList.php?category=7&page=1&pageSize=20")

五、开发注意事项(Python 专属)

  1. WebSocket 必须做断线重连,网络波动会断开
  2. 部分品种盘口深度可能为空,代码中做好 try-except 与判空

六、文章说明

本文为脉动数据行情平台 Python 接口开发技术分享,仅用于学习与项目开发参考

Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐