使用robin_stocks获取Robinhood加密货币quantity_held_for_buy返回0问题
关于robin_stocks获取加密货币待购买持仓数量返回0的解决方案
问题根因
quantity_held_for_buy是robin_stocks旧版本适配的Robinhood接口字段,目前Robinhood官方加密货币API已经废弃该字段,所有请求统一返回固定值0,和实际持仓状态无关。
解决方案
方案1:使用接口返回的新字段替代
当前Robinhood官方接口的待购买冻结数量已调整为hold_for_buy或pending_buy_quantity字段返回,可直接从持仓接口的原始返回值中读取:
# 拉取全部加密货币持仓原始数据 crypto_positions = robin_stocks.robinhood.crypto.get_crypto_positions() for position in crypto_positions: coin_symbol = position["currency"]["code"] # 兼容两种可能的字段名 held_for_buy = position.get("hold_for_buy", position.get("pending_buy_quantity", "0")) print(f"{coin_symbol} 待购买冻结数量:{held_for_buy}")
方案2:通过未成交订单接口统计(兼容性最高)
如果以上字段均取不到值,可直接拉取所有未成交加密货币买单,自行按标的累加未成交数量:
from collections import defaultdict pending_buy_counts = defaultdict(float) # 拉取所有未成交加密货币订单 open_crypto_orders = robin_stocks.robinhood.orders.get_all_open_crypto_orders() for order in open_crypto_orders: # 仅统计买入方向的未成交部分 if order["side"] == "buy": coin_symbol = order["currency_pair_id"].split("-")[0] unfiled_qty = float(order["quantity"]) - float(order["filled_quantity"]) pending_buy_counts[coin_symbol] += unfiled_qty # 输出统计结果 for coin, qty in pending_buy_counts.items(): print(f"{coin} 待购买冻结数量:{qty}")
内容的提问来源于stack exchange,提问作者Sean
相关产品推荐
相关产品推荐

