Fix trades API 500 by supporting sqlite Row in hold timing.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -18,10 +18,20 @@ def _ts(v: Any) -> int | None:
|
||||
return n if n > 0 else None
|
||||
|
||||
|
||||
def first_perp_close_ts_ms(fills: Sequence[Mapping[str, Any]]) -> int | None:
|
||||
def _as_map(row: Mapping[str, Any] | Any) -> Mapping[str, Any]:
|
||||
if isinstance(row, Mapping):
|
||||
return row
|
||||
try:
|
||||
return dict(row)
|
||||
except Exception:
|
||||
return {}
|
||||
|
||||
|
||||
def first_perp_close_ts_ms(fills: Sequence[Mapping[str, Any] | Any]) -> int | None:
|
||||
"""永续平仓成交时间(目标只平永续时作为持仓结束时刻)。"""
|
||||
best: int | None = None
|
||||
for f in fills:
|
||||
for raw in fills:
|
||||
f = _as_map(raw)
|
||||
if str(f.get("leg") or "") != "perp":
|
||||
continue
|
||||
if str(f.get("action") or "") != "close":
|
||||
@@ -35,7 +45,7 @@ def first_perp_close_ts_ms(fills: Sequence[Mapping[str, Any]]) -> int | None:
|
||||
|
||||
|
||||
def hold_timing(
|
||||
group: Mapping[str, Any], fills: Sequence[Mapping[str, Any]]
|
||||
group: Mapping[str, Any] | Any, fills: Sequence[Mapping[str, Any] | Any]
|
||||
) -> dict[str, Any]:
|
||||
"""
|
||||
返回展示用开仓/平仓/持仓时长。
|
||||
@@ -45,10 +55,11 @@ def hold_timing(
|
||||
- `target_perp_only` / `option_residual`:永续平仓 fill 时间
|
||||
- 其它已平:groups.close_at_ms(缺则回退成交)
|
||||
"""
|
||||
open_ms = _ts(group.get("open_at_ms"))
|
||||
status = str(group.get("status") or "")
|
||||
reason = str(group.get("close_reason") or "")
|
||||
group_close = _ts(group.get("close_at_ms"))
|
||||
g = _as_map(group)
|
||||
open_ms = _ts(g.get("open_at_ms"))
|
||||
status = str(g.get("status") or "")
|
||||
reason = str(g.get("close_reason") or "")
|
||||
group_close = _ts(g.get("close_at_ms"))
|
||||
perp_close = first_perp_close_ts_ms(fills)
|
||||
|
||||
use_perp = reason == "target_perp_only" or status == "option_residual"
|
||||
|
||||
Reference in New Issue
Block a user