如何统计API生成的动态orderId?求Python代码修改方案
问题解决:统计API生成的真实OrderId并按指定格式输出
原代码问题分析
原代码存在两个核心问题:
- 错误使用
for orderId in range(1, 4)生成模拟的1-3号OrderId,而非调用API获取真实ID 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() # 空行分隔记录,匹配期望格式
代码说明
- 移除生成模拟ID的逻辑,改为循环调用
obj.placeOrder(order_params)获取真实OrderId - 用
total_count变量准确统计API调用次数(即真实OrderId的数量) - 使用f-string格式化输出,同时展示累计计数和每条OrderId对应的序号
- 增加空行分隔每条记录,与期望输出格式完全匹配
输出效果
执行后会输出类似:
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
相关产品推荐
相关产品推荐

