# 量化系统 **Repository Path**: balloonchat/my-quant ## Basic Information - **Project Name**: 量化系统 - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-11-23 - **Last Updated**: 2025-11-28 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 量化交易系统 基于Vue3 + Express构建的现代化量化交易平台,集成数据获取、策略开发、回测分析、实时监控等核心功能。 ## 🚀 快速开始 okx 55c59495-ae40-41d6-8756-f7816b11ef5b FF1571D8EDF49699D2BCD742B63DB765 Wuhao1316@ ### 环境要求 - Node.js >= 18.0.0 ### 代理配置 如果需要使用代理访问交易所API,请在环境变量中设置: ```bash # HTTP代理 http_proxy=http://your-proxy:port https_proxy=http://your-proxy:port # 或者在Windows上使用set命令 set http_proxy=http://your-proxy:port set https_proxy=http://your-proxy:port ``` ### 安装依赖 ```bash # 安装前端依赖 cd frontend npm install # 安装后端依赖 cd ../backend npm install ``` ### 启动服务 ```bash # 启动后端服务 cd backend npm run dev # 启动前端服务 cd frontend npm run dev ``` ### 访问应用 - 前端: http://localhost:3001 - 后端API: http://localhost:8000 - 健康检查: http://localhost:8000/health ## 📋 功能特性 ### 🔧 核心模块 - **数据管理**: 多数据源集成,实时行情数据获取 - **策略开发**: 在线策略编辑器,支持多种策略类型 - **回测引擎**: 历史数据回测,性能分析报告 - **实盘交易**: 连接券商API,自动交易执行 - **监控告警**: 实时监控,多渠道告警通知 ### 🎯 技术特色 - **现代化技术栈**: Vue3 + TypeScript + Express - **响应式设计**: 适配多种设备 - **实时数据推送**: WebSocket实时数据 - **高性能**: 异步处理,缓存优化 - **容器化部署**: Docker一键部署 ## 🖼️ 界面展示 ### 首页仪表板 ![首页](images/readme/首页.png) ### 策略管理 ![策略列表](images/readme/策略列表.png) ![策略详情](images/readme/策略详情.png) ### 交易功能 ![交易面板](images/readme/交易面板.png) ![订单管理](images/readme/订单管理.png) ![持仓管理](images/readme/持仓管理.png) ![账户管理](images/readme/账户管理.png) ### 数据与分析 ![市场数据](images/readme/市场数据.png) ![数据导入](images/readme/数据导入.png) ### 回测系统 ![回测设置](images/readme/回测设置.png) ![回测结果](images/readme/回测结果.png) ### 参数优化 ![参数优化](images/readme/参数优化.png) ### 系统管理 ![用户管理](images/readme/用户管理.png) ## 📁 项目结构 ``` quant-trading-system/ ├── doc/ # 文档目录 │ ├── 需求.md # 需求文档 │ └── 架构设计.md # 架构设计 ├── frontend/ # 前端应用 │ ├── src/ │ │ ├── components/ # 组件 │ │ ├── views/ # 页面 │ │ ├── stores/ # 状态管理 │ │ └── api/ # API接口 │ └── package.json ├── backend/ # 后端应用 │ ├── src/ │ │ ├── controllers/ # 控制器 │ │ ├── services/ # 业务逻辑 │ │ ├── models/ # 数据模型 │ │ └── routes/ # 路由 │ └── package.json ├── images/ # 图片资源 │ └── readme/ # README图片 ├── docker/ # Docker配置 ├── docker-compose.yml # 容器编排 └── package.json # 根目录配置 ``` ## 🔧 配置说明 ### 环境变量配置 项目已经提供了完整的环境变量模板文件,用户只需要复制并根据需要修改: #### 根目录配置 (.env) ```bash # 主要用于Docker部署和全局配置 POSTGRES_PASSWORD=quant123 # PostgreSQL密码 REDIS_PASSWORD=redis123 # Redis密码 JWT_SECRET=your-jwt-secret # JWT密钥(生产环境请修改) NODE_ENV=development # 运行环境 PORT=8000 # 后端端口 ``` #### 后端配置 (backend/.env) ```bash # 数据库连接 DATABASE_URL=postgresql://quant:quant123@localhost:5432/quant_trading # Redis连接 REDIS_URL=redis://localhost:6379 # JWT配置 JWT_SECRET=your-secret-key-change-in-production JWT_EXPIRES_IN=7d # 服务器配置 PORT=8000 NODE_ENV=development CORS_ORIGIN=http://localhost:3000 # Bybit API配置(测试网) BYBIT_API_BASE_URL=https://api-testnet.bybit.com BYBIT_API_KEY= BYBIT_API_SECRET= BYBIT_TESTNET=true ``` #### 前端配置 (frontend/.env) ```bash # API配置 VITE_API_URL=http://localhost:8000/api VITE_WS_URL=http://localhost:8000/socket.io # 生产环境配置示例 # VITE_API_URL=https://your-domain.com/api # VITE_WS_URL=https://your-domain.com/socket.io ``` ### 数据库配置 ```bash # 进入后端目录 cd backend # 生成Prisma客户端 npm run db:generate # 运行数据库迁移 npm run db:migrate # 查看数据库管理界面 npm run db:studio # 推送schema更改(开发时) npm run db:push ``` ### 服务依赖 确保以下服务在启动前已经运行: #### PostgreSQL ```bash # 使用Docker启动PostgreSQL docker run -d --name postgres \ -e POSTGRES_USER=quant \ -e POSTGRES_PASSWORD=quant123 \ -e POSTGRES_DB=quant_trading \ -p 5432:5432 \ postgres:14 # 或使用系统安装的PostgreSQL sudo systemctl start postgresql ``` #### Redis ```bash # 使用Docker启动Redis docker run -d --name redis \ -p 6379:6379 \ redis:7-alpine # 或使用系统安装的Redis sudo systemctl start redis ``` ## 📊 API文档 ### 认证接口 - `POST /api/auth/login` - 用户登录 - `POST /api/auth/register` - 用户注册 - `GET /api/auth/profile` - 获取用户信息 ### 策略管理 - `GET /api/strategies` - 获取策略列表 - `POST /api/strategies` - 创建策略 - `PUT /api/strategies/:id` - 更新策略 - `DELETE /api/strategies/:id` - 删除策略 ### 数据接口 - `GET /api/data/market/:symbol` - 获取实时行情 - `GET /api/data/history/:symbol` - 获取历史数据 - `GET /api/data/indicators/:symbol` - 获取技术指标 ### 回测接口 - `POST /api/backtest/run` - 运行回测 - `GET /api/backtest/results/:id` - 获取回测结果 ### 系统管理 - `GET /api/system/logs` - 获取系统日志 - `GET /api/users` - 用户管理 (管理员) - `GET /api/monitoring` - 系统监控 ## 🚀 部署指南 ### 生产环境部署 1. 配置环境变量 2. 构建应用: `npm run build` 3. 启动服务: `npm run docker:up` ### 监控配置 - Prometheus: http://localhost:9090 - Grafana: http://localhost:3001 (admin/admin123) ## 🤝 开发指南 ### 代码规范 - 使用TypeScript编写类型安全的代码 - 遵循ESLint和Prettier规范 - 编写单元测试和集成测试 ### 提交规范 - feat: 新功能 - fix: 修复bug - docs: 文档更新 - style: 代码格式化 - refactor: 代码重构 - test: 测试相关 - chore: 构建工具或辅助工具的变动 ## 🛠️ 开发命令 ### 根目录命令 ```bash npm run install:all # 安装所有依赖 npm run dev # 同时启动前端和后端 npm run build # 构建所有应用 npm run test # 运行测试 npm run docker:up # 启动所有服务 npm run docker:down # 停止所有服务 npm run docker:logs # 查看服务日志 ``` ### 后端命令 ```bash cd backend npm run dev # 启动后端开发服务器 npm run build # 构建后端应用 npm run test # 运行后端测试 npm run db:generate # 生成Prisma客户端 npm run db:migrate # 运行数据库迁移 npm run db:push # 推送schema更改 npm run db:studio # 打开数据库管理界面 npm run lint # ESLint检查和修复 npm run format # Prettier格式化 ``` ### 前端命令 ```bash cd frontend npm run dev # 启动前端开发服务器 npm run build # 构建前端应用 npm run preview # 预览生产构建 npm run test # 运行前端测试 npm run lint # ESLint检查和修复 npm run format # Prettier格式化 ``` ## 📝 许可证 MIT License ## 🛠️ 故障排除 ### 常见问题 #### 1. 依赖安装失败 ```bash # 清除缓存重新安装 npm cache clean --force rm -rf node_modules package-lock.json npm run install:all ``` #### 2. 数据库连接失败 ```bash # 检查PostgreSQL服务状态 sudo systemctl status postgresql # 检查端口占用 netstat -tuln | grep 5432 # 测试数据库连接 cd backend && npm run test:connection ``` #### 3. Redis连接失败 ```bash # 检查Redis服务状态 sudo systemctl status redis # 测试Redis连接 redis-cli ping ``` #### 4. 端口占用问题 ```bash # 查看端口占用 netstat -tuln | grep :3000 netstat -tuln | grep :8000 # 修改端口(在.env文件中) PORT=8001 # 后端端口 CORS_ORIGIN=http://localhost:3001 # 前端端口 ``` #### 5. 权限问题 ```bash # 给予脚本执行权限 chmod +x deploy.sh chmod +x docker-compose.yml # 修复文件权限 sudo chown -R $USER:$USER /path/to/project ``` ### 日志查看 #### 后端日志 ```bash # 查看后端运行日志 npm run dev:backend # 或查看应用日志 tail -f logs/app.log ``` #### 前端日志 ```bash # 查看前端构建日志 npm run dev:frontend # 查看浏览器控制台 # 按 F12 打开开发者工具 ``` ### 性能优化 #### 开发环境优化 ```bash # 使用软链接加快构建 ln -s /path/to/node_modules ./node_modules # 增加内存限制 export NODE_OPTIONS="--max-old-space-size=4096" ``` #### 生产环境优化 ```bash # 构建优化版本 npm run build:optimized # 使用PM2管理进程 npm run pm2:start ``` ## 🙋‍♂️ 支持 ### 获取帮助 - 📧 **邮箱**: support@example.com - 🐛 **问题反馈**: [GitHub Issues](https://github.com/your-repo/issues) - 📖 **文档**: [Wiki](https://github.com/your-repo/wiki) - 💬 **讨论**: [GitHub Discussions](https://github.com/your-repo/discussions) ### 贡献指南 1. Fork 项目 2. 创建功能分支 (`git checkout -b feature/AmazingFeature`) 3. 提交更改 (`git commit -m 'Add some AmazingFeature'`) 4. 推送到分支 (`git push origin feature/AmazingFeature`) 5. 打开 Pull Request --- **系统特点**: 本系统采用微服务架构,支持高并发处理,具备完善的权限管理和数据安全保障。适合个人投资者和小型机构使用。 **快速开始**: 按照上面的步骤,从克隆项目到成功运行大约需要 5-10 分钟。 WS / 行情频道 获取产品的最新成交价、买一价、卖一价和24小时交易量等信息。在提前挂单阶段,best ask的价格有机会低于best bid。 最快100ms推送一次,没有触发事件时不推送,触发推送的事件有:成交、买一卖一发生变动。 URL Path /ws/v5/public 请求示例 import asyncio from okx.websocket.WsPublicAsync import WsPublicAsync def callbackFunc(message): print(message) async def main(): ws = WsPublicAsync(url="wss://wspap.okx.com:8443/ws/v5/public") await ws.start() args = [{ "channel": "tickers", "instId": "BTC-USDT" }] await ws.subscribe(args, callback=callbackFunc) await asyncio.sleep(10) await ws.unsubscribe(args, callback=callbackFunc) await asyncio.sleep(10) asyncio.run(main()) 请求参数 参数 类型 是否必须 描述 id String 否 消息的唯一标识。 用户提供,返回参数中会返回以便于找到相应的请求。 字母(区分大小写)与数字的组合,可以是纯字母、纯数字且长度必须要在1-32位之间。 op String 是 操作 subscribe unsubscribe args Array of objects 是 请求订阅的频道列表 > channel String 是 频道名 tickers > instId String 是 产品ID 成功返回示例 { "id": "1512", "event": "subscribe", "arg": { "channel": "tickers", "instId": "BTC-USDT" }, "connId": "a4d3ae55" } 失败返回示例 { "id": "1512", "event": "error", "code": "60012", "msg": "Invalid request: {\"op\": \"subscribe\", \"argss\":[{ \"channel\" : \"tickers\", \"instId\" : \"LTC-USD-200327\"}]}", "connId": "a4d3ae55" } 返回参数 参数 类型 是否必须 描述 id String 否 消息的唯一标识 event String 是 事件 subscribe unsubscribe error arg Object 否 订阅的频道 > channel String 是 频道名 > instId String 是 产品ID code String 否 错误码 msg String 否 错误消息 connId String 是 WebSocket连接ID 推送示例 { "arg": { "channel": "tickers", "instId": "BTC-USDT" }, "data": [{ "instType": "SPOT", "instId": "BTC-USDT", "last": "9999.99", "lastSz": "0.1", "askPx": "9999.99", "askSz": "11", "bidPx": "8888.88", "bidSz": "5", "open24h": "9000", "high24h": "10000", "low24h": "8888.88", "volCcy24h": "2222", "vol24h": "2222", "sodUtc0": "2222", "sodUtc8": "2222", "ts": "1597026383085" }] } 推送数据参数 参数名 类型 描述 arg Object 订阅成功的频道 > channel String 频道名 > instId String 产品ID data Array of objects 订阅的数据 > instType String 产品类型 > instId String 产品ID > last String 最新成交价 > lastSz String 最新成交的数量,0 代表没有成交量 > askPx String 卖一价 > askSz String 卖一价对应的量 > bidPx String 买一价 > bidSz String 买一价对应的数量 > open24h String 24小时开盘价 > high24h String 24小时最高价 > low24h String 24小时最低价 > volCcy24h String 24小时成交量,以币为单位 如果是衍生品合约,数值为交易货币的数量。 如果是币币/币币杠杆,数值为计价货币的数量。 > vol24h String 24小时成交量,以张为单位 如果是衍生品合约,数值为合约的张数。 如果是币币/币币杠杆,数值为交易货币的数量。 > sodUtc0 String UTC+0 时开盘价 > sodUtc8 String UTC+8 时开盘价 > ts String 数据产生时间,Unix时间戳的毫秒数格式,如 1597026383085 WS / K线频道 获取K线数据,推送频率最快是间隔1秒推送一次数据。 URL Path /ws/v5/business 请求示例 import asyncio from okx.websocket.WsPublicAsync import WsPublicAsync def callbackFunc(message): print(message) async def main(): ws = WsPublicAsync(url="wss://wspap.okx.com:8443/ws/v5/business") await ws.start() args = [ { "channel": "candle1D", "instId": "BTC-USDT" } ] await ws.subscribe(args, callback=callbackFunc) await asyncio.sleep(10) await ws.unsubscribe(args, callback=callbackFunc) await asyncio.sleep(10) asyncio.run(main()) 请求参数 参数 类型 是否必须 描述 id String 否 消息的唯一标识。 用户提供,返回参数中会返回以便于找到相应的请求。 字母(区分大小写)与数字的组合,可以是纯字母、纯数字且长度必须要在1-32位之间。 op String 是 操作 subscribe unsubscribe args Array of objects 是 请求订阅的频道列表 > channel String 是 频道名 candle3M candle1M candle1W candle1D candle2D candle3D candle5D candle12H candle6H candle4H candle2H candle1H candle30m candle15m candle5m candle3m candle1m candle1s candle3Mutc candle1Mutc candle1Wutc candle1Dutc candle2Dutc candle3Dutc candle5Dutc candle12Hutc candle6Hutc > instId String 是 产品ID 成功返回示例 { "id": "1512", "event": "subscribe", "arg": { "channel": "candle1D", "instId": "BTC-USDT" }, "connId": "a4d3ae55" } 失败返回示例 { "id": "1512", "event": "error", "code": "60012", "msg": "Invalid request: {\"op\": \"subscribe\", \"argss\":[{ \"channel\" : \"candle1D\", \"instId\" : \"BTC-USD-191227\"}]}", "connId": "a4d3ae55" } 返回参数 参数 类型 是否必须 描述 id String 否 消息的唯一标识 event String 是 事件 subscribe unsubscribe error arg Object 否 订阅的频道 > channel String 是 频道名 > instId String 是 产品ID code String 否 错误码 msg String 否 错误消息 connId String 是 WebSocket连接ID 推送示例 { "arg": { "channel": "candle1D", "instId": "BTC-USDT" }, "data": [ [ "1629993600000", "42500", "48199.9", "41006.1", "41006.1", "3587.41204591", "166741046.22583129", "166741046.22583129", "0" ] ] } 推送数据参数 参数名 类型 描述 arg Object 订阅成功的频道 > channel String 频道名 > instId String 产品ID data Array of Arrays 订阅的数据 > ts String 开始时间,Unix时间戳的毫秒数格式,如 1597026383085 > o String 开盘价格 > h String 最高价格 > l String 最低价格 > c String 收盘价格 > vol String 交易量,以张为单位 如果是衍生品合约,数值为合约的张数。 如果是币币/币币杠杆,数值为交易货币的数量。 > volCcy String 交易量,以币为单位 如果是衍生品合约,数值为交易货币的数量。 如果是币币/币币杠杆,数值为计价货币的数量。 > volCcyQuote String 交易量,以计价货币为单位 如 BTC-USDT和BTC-USDT-SWAP单位均是USDT。 BTC-USD-SWAP单位是USD。 > confirm String K线状态 0:K线未完结 1:K线已完结 WS / 交易频道 获取最近的成交数据,有成交数据就推送,每次推送可能聚合多条成交数据。 根据每个taker订单的不同成交价格,不同成交来源推送消息,并使用count字段表示聚合的订单匹配数量。 URL Path /ws/v5/public 请求示例 import asyncio from okx.websocket.WsPublicAsync import WsPublicAsync def callbackFunc(message): print(message) async def main(): ws = WsPublicAsync(url="wss://wspap.okx.com:8443/ws/v5/public") await ws.start() args = [ { "channel": "trades", "instId": "BTC-USDT" } ] await ws.subscribe(args, callback=callbackFunc) await asyncio.sleep(10) await ws.unsubscribe(args, callback=callbackFunc) await asyncio.sleep(10) asyncio.run(main()) 请求参数 参数 类型 是否必须 描述 id String 否 消息的唯一标识。 用户提供,返回参数中会返回以便于找到相应的请求。 字母(区分大小写)与数字的组合,可以是纯字母、纯数字且长度必须要在1-32位之间。 op String 是 操作 subscribe unsubscribe args Array of objects 是 请求订阅的频道列表 > channel String 是 频道名 trades > instId String 是 产品ID 成功返回示例 { "id": "1512", "event": "subscribe", "arg": { "channel": "trades", "instId": "BTC-USDT" }, "connId": "a4d3ae55" } 失败返回示例 { "id": "1512", "event": "error", "code": "60012", "msg": "Invalid request: {\"op\": \"subscribe\", \"argss\":[{ \"channel\" : \"trades\", \"instId\" : \"BTC-USD-191227\"}]}", "connId": "a4d3ae55" } 返回参数 参数 类型 是否必须 描述 id String 否 消息的唯一标识 event String 是 事件 subscribe unsubscribe error arg Object 否 订阅的频道 > channel String 是 频道名 > instId String 是 产品ID code String 否 错误码 msg String 否 错误消息 connId String 是 WebSocket连接ID 推送示例 { "arg": { "channel": "trades", "instId": "BTC-USDT" }, "data": [ { "instId": "BTC-USDT", "tradeId": "130639474", "px": "42219.9", "sz": "0.12060306", "side": "buy", "ts": "1630048897897", "count": "3", "source": "0", "seqId": 1234 } ] } 推送数据参数 参数名 类型 描述 arg Object 订阅成功的频道 > channel String 频道名 > instId String 产品ID data Array of objects 订阅的数据 > instId String 产品ID,如 BTC-USDT > tradeId String 聚合的多笔交易中最新一笔交易的成交ID > px String 成交价格 > sz String 成交数量 对于币币交易,成交数量的单位为交易货币 对于交割、永续以及期权,单位为张。 > side String 吃单方向 buy sell > ts String 成交时间,Unix时间戳的毫秒数格式,如 1597026383085 > count String 聚合的订单匹配数量 > source String 订单来源 0:普通订单 1:流动性增强计划订单 > seqId Integer 推送的序列号 聚合功能说明: 1. 系统将根据每个taker订单的不同成交价格,不同成交来源推送消息,并使用count字段表示聚合的订单匹配数量。 2. tradeId是聚合的多笔交易中最新一笔交易的 ID。 3. 当count = 1时,表示taker订单部分或完全成交时仅匹配了一个maker订单。 4. 当count > 1时,表示taker订单以相同价格匹配了多个maker订单。例如,如果tradeId = 123,且count = 3,表示该消息聚合了tradeId = 123, 122, 121的成交。maker侧有多笔价格相同的订单被成交。 5. 用户可以使用此数据与“全部交易”频道的数据进行对比。 6. 深度及聚合交易数据仍按顺序发布。 同时发生的不同交易推送数据的`seqId`可能相同。 WS / 全部交易频道 获取最近的成交数据,有成交数据就推送,每次推送仅包含一条成交数据。 URL Path /ws/v5/business 请求示例 import asyncio from okx.websocket.WsPublicAsync import WsPublicAsync def callbackFunc(message): print(message) async def main(): ws = WsPublicAsync(url="wss://wspap.okx.com:8443/ws/v5/business") await ws.start() args = [ { "channel": "trades-all", "instId": "BTC-USDT" } ] await ws.subscribe(args, callback=callbackFunc) await asyncio.sleep(10) await ws.unsubscribe(args, callback=callbackFunc) await asyncio.sleep(10) asyncio.run(main()) 请求参数 参数 类型 是否必须 描述 id String 否 消息的唯一标识。 用户提供,返回参数中会返回以便于找到相应的请求。 字母(区分大小写)与数字的组合,可以是纯字母、纯数字且长度必须要在1-32位之间。 op String 是 操作 subscribe unsubscribe args Array of objects 是 请求订阅的频道列表 > channel String 是 频道名 trades-all > instId String 是 产品ID 成功返回示例 { "id": "1512", "event": "subscribe", "arg": { "channel": "trades-all", "instId": "BTC-USDT" }, "connId": "a4d3ae55" } 失败返回示例 { "id": "1512", "event": "error", "code": "60012", "msg": "Invalid request: {\"op\": \"subscribe\", \"argss\":[{ \"channel\" : \"trades-all\", \"instId\" : \"BTC-USD-191227\"}]}", "connId": "a4d3ae55" } 返回参数 参数 类型 是否必须 描述 id String 否 消息的唯一标识 event String 是 事件 subscribe unsubscribe error arg Object 否 订阅的频道 > channel String 是 频道名 > instId String 是 产品ID code String 否 错误码 msg String 否 错误消息 connId String 是 WebSocket连接ID 推送示例 { "arg": { "channel": "trades-all", "instId": "BTC-USDT" }, "data": [ { "instId": "BTC-USDT", "tradeId": "130639474", "px": "42219.9", "sz": "0.12060306", "side": "buy", "source": "0", "ts": "1630048897897" } ] } 推送数据参数 参数名 类型 描述 arg Array of objects 订阅成功的频道 > channel String 频道名 > instId String 产品ID data Array of objects 订阅的数据 > instId String 产品ID,如 BTC-USDT > tradeId String 成交ID > px String 成交价格 > sz String 成交数量 对于币币交易,成交数量的单位为交易货币 对于交割、永续以及期权,单位为张。 > side String 成交方向 buy sell > source String 订单来源 0:普通订单 1:流动性增强计划订单 > ts String 成交时间,Unix时间戳的毫秒数格式,如 1597026383085 WS / 深度频道 获取深度数据。在提前挂单阶段,best ask的价格有机会低于best bid。books是400档频道,books5是5档频道, bbo-tbt是先1档后实时推送的频道,books-l2-tbt是先400档后实时推送的频道,books50-l2-tbt是先50档后实时推的频道; books 首次推400档快照数据,以后增量推送,每100毫秒推送一次变化的数据 books-elp 仅推送ELP订单,首次推400档快照数据,以后增量推送,每100毫秒推送一次变化的数据 books5 首次推5档快照数据,以后定量推送,每100毫秒当5档快照数据有变化推送一次5档数据 bbo-tbt 首次推1档快照数据,以后定量推送,每10毫秒当1档快照数据有变化推送一次1档数据 books-l2-tbt 首次推400档快照数据,以后增量推送,每10毫秒推送一次变化的数据 books50-l2-tbt 首次推50档快照数据,以后增量推送,每10毫秒推送一次变化的数据 单个连接、交易产品维度,深度频道的推送顺序固定为:bbo-tbt -> books-l2-tbt -> books50-l2-tbt -> books -> books5。 在相同连接下,用户将无法为相同交易产品同时订阅 books-l2-tbt 以及 books50-l2-tbt/books频道 更多细节,请参阅更新日志 2024-07-17 books-l2-tbt400档深度频道,只允许交易手续费等级VIP6及以上的API用户订阅。 books50-l2-tbt50档深度频道,只允许交易手续费等级VIP5及以上的API用户订阅. 身份认证参考登录功能 服务地址 /ws/v5/public 请求示例 import asyncio from okx.websocket.WsPublicAsync import WsPublicAsync def callbackFunc(message): print(message) async def main(): ws = WsPublicAsync(url="wss://wspap.okx.com:8443/ws/v5/public") await ws.start() args = [ { "channel": "books", "instId": "BTC-USDT" } ] await ws.subscribe(args, callback=callbackFunc) await asyncio.sleep(10) await ws.unsubscribe(args, callback=callbackFunc) await asyncio.sleep(10) asyncio.run(main()) 请求参数 参数 类型 是否必须 描述 id String 否 消息的唯一标识。 用户提供,返回参数中会返回以便于找到相应的请求。 字母(区分大小写)与数字的组合,可以是纯字母、纯数字且长度必须要在1-32位之间。 op String 是 操作 subscribe unsubscribe args Array of objects 是 请求订阅的频道列表 > channel String 是 频道名 books books5 bbo-tbt books-l2-tbt books50-l2-tbt > instId String 是 产品ID 返回示例 { "id": "1512", "event": "subscribe", "arg": { "channel": "books", "instId": "BTC-USDT" }, "connId": "a4d3ae55" } 失败示例 { "id": "1512", "event": "error", "code": "60012", "msg": "Invalid request: {\"op\": \"subscribe\", \"argss\":[{ \"channel\" : \"books\", \"instId\" : \"BTC-USD-191227\"}]}", "connId": "a4d3ae55" } 返回参数 参数 类型 是否必须 描述 id String 否 消息的唯一标识 event String 是 事件 subscribe unsubscribe error arg Object 否 订阅的频道 > channel String 是 频道名 > instId String 是 产品ID msg String 否 错误消息 code String 否 错误码 connId String 是 WebSocket连接ID 推送示例 :全量 { "arg": { "channel": "books", "instId": "BTC-USDT" }, "action": "snapshot", "data": [{ "asks": [ ["8476.98", "415", "0", "13"], ["8477", "7", "0", "2"], ["8477.34", "85", "0", "1"], ["8477.56", "1", "0", "1"], ["8505.84", "8", "0", "1"], ["8506.37", "85", "0", "1"], ["8506.49", "2", "0", "1"], ["8506.96", "100", "0", "2"] ], "bids": [ ["8476.97", "256", "0", "12"], ["8475.55", "101", "0", "1"], ["8475.54", "100", "0", "1"], ["8475.3", "1", "0", "1"], ["8447.32", "6", "0", "1"], ["8447.02", "246", "0", "1"], ["8446.83", "24", "0", "1"], ["8446", "95", "0", "3"] ], "ts": "1597026383085", "checksum": -855196043, "prevSeqId": -1, "seqId": 123456 }] } 推送示例:增量 { "arg": { "channel": "books", "instId": "BTC-USDT" }, "action": "update", "data": [{ "asks": [ ["8476.98", "415", "0", "13"], ["8477", "7", "0", "2"], ["8477.34", "85", "0", "1"], ["8477.56", "1", "0", "1"], ["8505.84", "8", "0", "1"], ["8506.37", "85", "0", "1"], ["8506.49", "2", "0", "1"], ["8506.96", "100", "0", "2"] ], "bids": [ ["8476.97", "256", "0", "12"], ["8475.55", "101", "0", "1"], ["8475.54", "100", "0", "1"], ["8475.3", "1", "0", "1"], ["8447.32", "6", "0", "1"], ["8447.02", "246", "0", "1"], ["8446.83", "24", "0", "1"], ["8446", "95", "0", "3"] ], "ts": "1597026383085", "checksum": -855196043, "prevSeqId": 123456, "seqId": 123457 }] } 推送数据参数 参数名 类型 描述 arg Object 订阅成功的频道 > channel String 频道名 > instId String 产品ID action String 推送数据动作,增量推送数据还是全量推送数据 snapshot:全量 update:增量 data Array of objects 订阅的数据 > asks Array of Arrays 卖方深度 > bids Array of Arrays 买方深度 > ts String 数据更新时间戳,Unix时间戳的毫秒数格式,如 1597026383085 > checksum Integer 检验和 (下方注解) > prevSeqId Integer 上一个推送的序列号。仅适用 books,books-l2-tbt,books50-l2-tbt > seqId Integer 推送的序列号 (下方注解)