You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

运行DolphinDB白皮书parseDepth函数遇错误及数据接收问题求助

DolphinDB币安深度WebSocket数据解析函数问题修复

问题概述

运行DolphinDB加密货币白皮书中binanceDepthWS.dos文件的parseDepth函数时,先触发语法错误:Syntax error: Cannot recognize the token b,同时发现return语句存在笔误(typ应为type)。修正笔误后函数可单独运行,但调用getSubJobStat()时出现错误:Failed to call onMessage, with error: The input argument must be a vector, tuple, matrix, or table..,且wssDepthST无法接收数据。

原代码问题分析

原代码存在4处关键错误:

  • 未定义变量引用:matrix(b)和matrix(a)中的b、a未定义,实际应使用rawDataDict.b和rawDataDict.a作为矩阵构造的数据源
  • 变量名笔误:return语句中typ应为之前定义的type
  • 无分支返回值:当rawDataDict["e"] != "depthUpdate"时,函数无返回值,导致WebSocket回调处理失败
  • 矩阵构造逻辑错误:rawDataDict.b.double().matrix(b)不符合DolphinDB语法,正确写法应为直接将转换后的数组传入matrix()函数

修复后的完整代码

// Function to parse market depth data
def parseDepth(rawData){
    rawDataDict = parseExpr(rawData).eval().data
    // 仅处理depthUpdate类型数据,否则返回结构一致的空表避免回调报错
    if (rawDataDict["e"] != "depthUpdate"){
        return table(1:0, `type`eventTime`transactionTime`code`firstId`lastId`finalId`bidPrice`bidQty`askPrice`askQty`currentTime, 
            [STRING, TIMESTAMP, TIMESTAMP, STRING, INT, INT, INT, DOUBLE[], DOUBLE[], DOUBLE[], DOUBLE[], DATETIME])
    }
    // Process bid data
    bidArray = rawDataDict.b.double()
    bTransposed = matrix(bidArray).transpose()
    bidPrice = bTransposed[0]
    bidQty = bTransposed[1]
    
    // Process ask data
    askArray = rawDataDict.a.double()
    aTransposed = matrix(askArray).transpose()
    askPrice = aTransposed[0]
    askQty = aTransposed[1]
    
    // Extract other relevant fields
    type = rawDataDict["e"]
    eventTime = timestamp(rawDataDict["E"])
    transactionTime = timestamp(rawDataDict["T"])
    code = rawDataDict["s"]
    firstId = rawDataDict["U"]
    lastId = rawDataDict["u"]
    finalId = rawDataDict["pu"]
    currentTime = gmtime(now())
    
    // 确保所有列长度一致,将标量字段转为对应长度的向量
    rowCount = size(bidPrice)
    typeVec = take(type, rowCount)
    eventTimeVec = take(eventTime, rowCount)
    transactionTimeVec = take(transactionTime, rowCount)
    codeVec = take(code, rowCount)
    firstIdVec = take(firstId, rowCount)
    lastIdVec = take(lastId, rowCount)
    finalIdVec = take(finalId, rowCount)
    currentTimeVec = take(currentTime, rowCount)
    
    // Return a standard table
    return table(
        typeVec as type, 
        eventTimeVec as eventTime, 
        transactionTimeVec as transactionTime, 
        codeVec as code, 
        firstIdVec as firstId, 
        lastIdVec as lastId, 
        finalIdVec as finalId, 
        bidPrice, 
        bidQty, 
        askPrice, 
        askQty, 
        currentTimeVec as currentTime
    )
}

修复说明

  1. 修正矩阵构造:将错误的rawDataDict.b.double().matrix(b)改为matrix(rawDataDict.b.double()),明确指定矩阵数据源
  2. 修复笔误:将return中的typ改为type
  3. 补充无匹配分支返回:当数据类型非depthUpdate时,返回结构一致的空表,避免回调函数因无返回值报错
  4. 统一列长度:将标量字段转为与bidPrice/askPrice等列长度一致的向量,确保表结构合法,解决getSubJobStat()的参数类型错误

验证步骤

  1. 替换原parseDepth函数为修复后的版本
  2. 重新启动WebSocket订阅任务
  3. 调用getSubJobStat()检查任务状态,确认无报错
  4. 查询wssDepthST表,验证是否能正常接收解析后的深度数据

内容的提问来源于stack exchange,提问作者Drac Pet

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 06:15:09