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

如何统计API生成的动态orderId?求Python代码修改方案

问题解决:统计API生成的真实OrderId并按指定格式输出

原代码问题分析

原代码存在两个核心问题:

  1. 错误使用for orderId in range(1, 4)生成模拟的1-3号OrderId,而非调用API获取真实ID
  2. occurrences的初始化和计数逻辑完全无效,用字符串自身计数无法统计真实API调用次数

修改后的代码

假设你需要调用3次API生成真实OrderId(可根据需求调整循环次数),修改后的代码如下:

order_params = {
    "variety": "NORMAL",
    "tradingsymbol": TRADING_SYMBOL,
    "symboltoken": TOKEN,
    "transactiontype": transaction_type,
    "exchange": EXCHANGE.split("_")[0].upper(),
    "ordertype": "MARKET",
    "producttype": "INTRADAY",
    "duration": "DAY",
    "price": price,
    "squareoff": "0",
    "stoploss": "0",
    "quantity": QUANTITY
}

# 初始化累计计数
total_count = 0
# 循环调用API,次数可根据需求调整
for _ in range(3):
    # 调用API获取真实OrderId
    real_order_id = obj.placeOrder(order_params)
    total_count += 1
    # 按指定格式输出
    print(f"Number of occurrences of order id: {total_count}")
    print(f"The order id is: {real_order_id} [{total_count}]")
    print()  # 空行分隔记录,匹配期望格式

代码说明

  1. 移除生成模拟ID的逻辑,改为循环调用obj.placeOrder(order_params)获取真实OrderId
  2. 用total_count变量准确统计API调用次数(即真实OrderId的数量)
  3. 使用f-string格式化输出,同时展示累计计数和每条OrderId对应的序号
  4. 增加空行分隔每条记录,与期望输出格式完全匹配

输出效果

执行后会输出类似:

Number of occurrences of order id: 1
The order id is:230809000922748 [1]      

Number of occurrences of order id:2
The order id is: 230809000922753[2]       

Number of occurrences of order id:3
The order id is: 230809000922755 [3]

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 23:17:39