-
Notifications
You must be signed in to change notification settings - Fork 60
历史订单有漏单,无法对齐所有交易 #511
Copy link
Copy link
Open
Description
问题描述
history_orders 接口返回的历史订单不完整,存在漏单现象。
具体现象:对同一标的在同一时间段内调用 history_orders,返回的买单与卖单数量之和的净头寸,与 stock_positions 接口返回的当前持仓(为 0)不一致。按理说如果实际持仓为 0,那历史所有买卖单数量应该精确轧平,但 API 返回的数据却存在缺口。
以下为复现到的具体标的和数量差异(所有标的当前 stock_positions 均为 0):
| 标的 | 同步周期 | history_orders 返回净数量 |
stock_positions 实际持仓 |
漏单估算 |
|---|---|---|---|---|
| GGLL.US | 2025-05 ~ 2026-01 | 买入 9130 股,卖出 8630 股,净 +500 | 0 | 约 500 股卖单缺失 |
| TSLL.US | 2025-03 ~ 2026-03 | 净 +1500(买多于卖) | 0 | 约 1500 股卖单缺失 |
| BIDU.US | 2025 ~ 2026-01 | 净 -700(卖多于买) | 0 | 约 700 股买单缺失 |
| SOXL.US | 2025 ~ 2026-01 | 净 -1000(卖多于买) | 0 | 约 1000 股买单缺失 |
| 多只港股(如 1276.HK 等) | 近一年 | 只有卖单,无对应买单 | 0 | 开仓买单全部缺失 |
所有漏单标的的共同特征:实际账户已完全平仓,但 history_orders 返回的历史数据无法轧平,导致系统无法正确还原交易记录。
拉取方式为按 90 天窗口分批请求,已覆盖上述所有时间范围,无遗漏窗口。
代码例子
import asyncio
from datetime import datetime, timezone, timedelta
from longport.openapi import Config, TradeContext, Market
APP_KEY = "YOUR_APP_KEY"
APP_SECRET = "YOUR_APP_SECRET"
ACCESS_TOKEN = "YOUR_ACCESS_TOKEN"
config = Config.from_apikey(
app_key=APP_KEY,
app_secret=APP_SECRET,
access_token=ACCESS_TOKEN,
)
ctx = TradeContext(config)
# 检查某标的历史买卖是否轧平
SYMBOL = "GGLL.US"
WINDOW_DAYS = 90
start = datetime(2025, 5, 1, tzinfo=timezone.utc)
end = datetime(2026, 2, 1, tzinfo=timezone.utc)
all_orders = []
cursor = start
while cursor < end:
batch_end = min(cursor + timedelta(days=WINDOW_DAYS), end)
resp = ctx.history_orders(market=Market.US, start_at=cursor, end_at=batch_end)
for o in (resp or []):
if str(o.symbol) == SYMBOL and str(o.status) in ("Filled", "PartialFilled", "PartialWithdrawal"):
all_orders.append(o)
cursor = batch_end
buy_qty = sum(int(o.executed_quantity) for o in all_orders if str(o.side) == "Buy")
sell_qty = sum(int(o.executed_quantity) for o in all_orders if str(o.side) == "Sell")
net = buy_qty - sell_qty
print(f"{SYMBOL}: buy={buy_qty}, sell={sell_qty}, net={net}")
# 再查当前持仓
positions = ctx.stock_positions()
held = 0
for ch in (positions.channels or []):
for pos in (ch.positions or []):
if str(pos.symbol) == SYMBOL:
held = int(pos.quantity)
print(f"{SYMBOL}: stock_positions 持仓 = {held}")
print(f"差异 = {net - held}(理论上应为 0)")预期输出示例:
GGLL.US: buy=9130, sell=8630, net=500
GGLL.US: stock_positions 持仓 = 0
差异 = 500(理论上应为 0)
错误信息或结果截图
无报错,接口调用本身正常返回 200。问题在于返回数据不完整:部分已成交订单未出现在 history_orders 的任何一个时间窗口响应中,但对应的持仓已在实际账户中被平仓(stock_positions 为 0 可证明)。
你的环境信息
- 操作系统:Windows 10
- 开发语言:Python 3.12
- SDK 版本号:
longport 3.0.14
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels