在跨境量化、汇率监控、自动化交易系统开发中,外汇行情数据的实时性与稳定性至关重要。很多开发者对接接口时,常遇到请求限流、长连接断连、多币种批量获取困难、时区时间戳错乱等问题。本文基于 PulseData 脉动行情 API,结合官方接口规范,完整演示外汇品种的 REST 实时查询、WebSocket 实时推送 两套接入方案,附带可直接运行的 Python 代码、请求规则与踩坑总结。
一、前置说明
- 接入前提:需联系客服完成服务器 IP 授权,未授权无法调用接口;
- 核心接口地址(官方)
- 实时行情 REST 接口:
http://39.107.99.235:1008/getQuote.php - 实时推送 WebSocket 地址:
ws://39.107.99.235/ws - K 线接口:
http://39.107.99.235:1008/redis.php
- 实时行情 REST 接口:
- 通用规则:REST 请求 Header 务必携带
Accept-Encoding:gzip,提升响应速度、节省带宽; - 外汇常用代码示例:
gbpusd(英镑美元)、eurusd(欧元美元)、usdcny(美元在岸人民币)。
二、REST 接口 单 / 多币种实时行情调用(GET 请求)
1. 接口规则
- 请求方式:GET
- 参数:
code,支持单个代码 / 英文逗号分隔多代码
2. 示例请求 URL
单品种(欧元美元)
plaintext
http://39.107.99.235:1008/getQuote.php?code=fx_seurusd
多品种(欧元美元 + 英镑美元 + 美元离岸人民币)
plaintext
http://39.107.99.235:1008/getQuote.php?code=fx_seurusd,fx_sgbpusd,fx_susdcnh
3. Python 完整调用代码
python
运行
import requests
# 接口地址
url = "http://39.107.99.235:1008/getQuote.php"
# 请求头
headers = {
"Accept-Encoding": "gzip"
}
# 批量查询外汇品种
params = {
"code": "fx_seurusd,fx_sgbpusd,fx_susdcny"
}
# 发起请求
response = requests.get(url, headers=headers, params=params, timeout=10)
# 解析JSON数据
result = response.json()
# 遍历打印行情数据
if result.get("code") == 200:
for data in result["data"]["body"]:
print(f"品种代码:{data['StockCode']}")
print(f"最新价:{data['Price']},开盘价:{data['Open']}")
print(f"最高价:{data['High']},最低价:{data['Low']}")
print(f"涨跌额:{data['Diff']},涨跌幅:{data['DiffRate']}%")
print("-" * 50)
三、WebSocket 实时推送接入(带心跳 + 断线重连)
外汇市场 7×24 交易,长连接是高频监控首选。本接口要求客户端每 10 秒发送一次心跳包,同时必须实现自动重连机制。
1. 通信规则
- 心跳格式:客户端发送
{"ping": 10位时间戳},服务端返回{"pong": 对应时间戳}; - 订阅格式:连接成功后发送
{"Key": "品种代码1,品种代码2"}; - 单连接支持多品种订阅,无需重复建立连接。
2. Python WebSocket 完整代码
python
运行
import websocket
import json
import time
import threading
# WebSocket地址
ws_url = "ws://39.107.99.235/ws"
# 订阅外汇品种
subscribe_code = "fx_seurusd,fx_sgbpusd"
def send_heartbeat(ws):
"""10秒定时发送心跳"""
while True:
timestamp = int(time.time())
heartbeat = json.dumps({"ping": timestamp})
ws.send(heartbeat)
time.sleep(10)
def on_open(ws):
"""连接成功回调:发起订阅+启动心跳线程"""
print("WebSocket连接成功,开始订阅行情...")
sub_msg = json.dumps({"Key": subscribe_code})
ws.send(sub_msg)
# 开启心跳子线程
threading.Thread(target=send_heartbeat, args=(ws,), daemon=True).start()
def on_message(ws, message):
"""接收行情数据"""
data = json.loads(message)
if "body" in data:
body = data["body"]
print(f"【实时行情】{body['StockCode']} 最新价:{body['Price']}")
def on_error(ws, error):
print(f"连接异常:{error}")
def on_close(ws, close_code, close_msg):
print("连接断开,3秒后自动重连...")
time.sleep(3)
start_ws() # 自动重连
def start_ws():
"""启动WebSocket连接"""
ws_app = websocket.WebSocketApp(
ws_url,
on_open=on_open,
on_message=on_message,
on_error=on_error,
on_close=on_close
)
ws_app.run_forever()
if __name__ == "__main__":
start_ws()
四、外汇 K 线数据获取(补充)
接口地址:http://39.107.99.235:1008/redis.php参数说明:
code:外汇品种代码;time:周期 1m/5m/15m/30m/1h/1d/1M;rows:数据条数(1m 最多 600 条,其余周期最多 300 条)。示例 URL:
plaintext
http://39.107.99.235:1008/redis.php?code=fx_seurusd&time=1m&rows=100
五、外汇对接避坑总结
- 外汇周末、节假日无主动成交,接口保留最后一笔有效报价,策略需做非交易时段判断;
- WebSocket 必须严格 10 秒心跳,超时会被服务端断开;
- 包含离岸人民币 (
fx_susdcnh)、新兴小众货币,全品类代码可查阅产品清单。