Files
crypto_monitor/lib/hub/price_snapshot_lib.py
T
2026-07-06 22:04:19 +08:00

76 lines
2.4 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""price_snapshot 共用:订单行情价兜底,避免 get_price 失败时整单不入 order_prices。"""
from __future__ import annotations
from typing import Any, Callable, Mapping, Optional
from lib.hub.hub_position_metrics import parse_position_mark_price
def resolve_order_snapshot_price(
symbol: str,
prices: Mapping[str, float],
*,
position_row: Optional[dict[str, Any]] = None,
order_leverage=None,
parse_position_metrics_fn: Callable[..., dict[str, Any] | None] | None = None,
get_mark_price_fn: Callable[[str], float | None] | None = None,
fallback_entry: float | None = None,
) -> float | None:
"""
解析下单监控轮询用的现价/标记价,优先级:
1. 已批量拉取的 ticker last
2. get_symbol_mark_price(含 mark
3. 交易所持仓 markparse_ccxt_position_metrics / parse_position_mark_price
4. 计划成交价 trigger_price
"""
sym = (symbol or "").strip()
if not sym:
return None
cached = prices.get(sym)
if cached is not None:
try:
v = float(cached)
if v > 0:
return v
except (TypeError, ValueError):
pass
if get_mark_price_fn is not None:
try:
mp = get_mark_price_fn(sym)
if mp is not None and float(mp) > 0:
return float(mp)
except Exception:
pass
if position_row:
mark = None
if parse_position_metrics_fn is not None:
try:
metrics = parse_position_metrics_fn(
position_row, order_leverage=order_leverage
)
if isinstance(metrics, dict) and metrics.get("mark_price") is not None:
mark = float(metrics["mark_price"])
except Exception:
mark = None
if mark is None or mark <= 0:
try:
mp = parse_position_mark_price(position_row)
if mp is not None and mp > 0:
mark = float(mp)
except Exception:
mark = None
if mark is not None and mark > 0:
return mark
if fallback_entry is not None:
try:
entry = float(fallback_entry)
if entry > 0:
return entry
except (TypeError, ValueError):
pass
return None