Level2 行情接口接入代码示例
支持websocket开放协议推送,包含沪深主板、科创板、创业板股票/可转债,支持Level1、Level2级别推送。
详见level2行情接口API文档。行情在线测试 websocket行情接口
Level2行情接口和Level1行情接口共用websocket通道,输入订阅指令获取行情数据流。
支持与通达信、东方财富、同花顺、新浪等券商level2数据对比。
提供券商在线交易接口接入,详见交易接口API文档。交易在线测试 在线交易接口
level2行情接入示例
PYTHON
#!python3
# -*- coding:utf-8 -*-
import time
import websocket
import zlib
# 发送订阅
def on_open(ws):
ws.send("all=lv2_600519,lv1_000001")
# 接收推送
def on_message(ws, message, type, flag):
# 命令返回文本消息
if type == websocket.ABNF.OPCODE_TEXT:
print(time.strftime('%H:%M:%S', time.localtime(time.time())), "Text响应:", message)
print(time.strftime('%H:%M:%S', time.localtime(time.time())), "Text响应:", message)
# 行情推送压缩二进制消息,在此解压缩
if type == websocket.ABNF.OPCODE_BINARY:
rb = zlib.decompress(message, -zlib.MAX_WBITS)
print(time.strftime('%H:%M:%S', time.localtime(time.time())), "Binary响应:", rb.decode("utf-8"))
def on_error(ws, error):
print(error)
def on_close(ws, code, msg):
print(time.strftime('%H:%M:%S', time.localtime(time.time())), "连接已断开")
wsUrl = "ws://<服务器地址>?token=<jvQuant token>"
ws = websocket.WebSocketApp(wsUrl,
on_open=on_open,
on_data=on_message,
on_error=on_error,
on_close=on_close)
ws.run_forever()
Copy
GOLANG
package main
import (
"bytes"
"compress/flate"
"github.com/gorilla/websocket"
"log"
"time"
)
func main() {
//连接地址
wsUrl := "ws://<服务器地址>?token=<jvQuant token>"
conn, _, err := websocket.DefaultDialer.Dial(wsUrl, nil)
if err != nil {
log.Fatalln("连接错误:", err)
}
//接收协程
go func() {
receive(conn)
}()
//发送订阅
cmd := "all=lv2_600519,lv1_000001"
err = conn.WriteMessage(websocket.TextMessage, []byte(cmd))
if err != nil {
log.Fatalln("发送指令错误:", err)
}
log.Println("发送指令成功,等待接收")
for {
time.Sleep(time.Second)
}
}
func receive(conn *websocket.Conn) {
for {
//阻塞接收
messageType, rb, err := conn.ReadMessage()
if err != nil {
log.Fatalln("接收错误:", err)
return
}
//文本消息
if messageType == websocket.TextMessage {
log.Println("Text响应:", string(rb))
}
//二进制消息
if messageType == websocket.BinaryMessage {
unZipByte := DeCompress(rb)
log.Println("Binary推送:", string(unZipByte))
}
}
}
//解压方法
func DeCompress(b []byte) []byte {
var buffer bytes.Buffer
buffer.Write([]byte(b))
reader := flate.NewReader(&buffer)
var result bytes.Buffer
result.ReadFrom(reader)
reader.Close()
return result.Bytes()
}
Copy
PHP
<?php
//需安装swoole扩展
use Swoole\Coroutine\Http\Client;
use function Swoole\Coroutine\run;
run(function () {
//服务器地址
$host = '<服务器地址>';
//服务器端口
$port = <服务器端口>;
//连接
$conn = new Client($host, $port);
$conn->upgrade("/?token=<jvQuant token>");
//发送订阅
$conn->push("add=lv2_600519,lv1_000001");
//开启接收协程
go("receive", $conn);
});
function receive($client)
{
while (true) {
$data = $client->recv();
$time = date("H:i:s");
//解压
@$zipStr = gzinflate($data->data);
if ($zipStr) {
echo "{$time} Binary推送:{$zipStr}\n";
} else {
echo "{$time} Text响应:{$data->data}\n";
}
}
}