Add depth-based option close flow.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -176,6 +176,38 @@ def _fetch_book_bid_ask(ex: ccxt.okx, inst_id: str) -> tuple[float | None, float
|
||||
return bid, ask
|
||||
|
||||
|
||||
def _normalize_book_levels(rows: list[Any], depth: int) -> list[dict[str, float]]:
|
||||
levels: list[dict[str, float]] = []
|
||||
for row in rows[: max(0, int(depth))]:
|
||||
if not isinstance(row, (list, tuple)) or len(row) < 2:
|
||||
continue
|
||||
px = _safe_float(row[0])
|
||||
sz = _safe_float(row[1])
|
||||
if px is None or sz is None or px <= 0 or sz <= 0:
|
||||
continue
|
||||
levels.append({"px": px, "sz": sz})
|
||||
return levels
|
||||
|
||||
|
||||
def fetch_option_book_depth(ex: ccxt.okx, inst_id: str, depth: int = 5) -> dict[str, list[dict[str, float]]]:
|
||||
"""获取期权盘口深度,sz 为 OKX 返回的张数口径."""
|
||||
inst_id = (inst_id or "").strip()
|
||||
if not inst_id:
|
||||
return {"bids": [], "asks": []}
|
||||
try:
|
||||
sz = str(max(1, min(int(depth), 10)))
|
||||
rows = ex.public_get_market_books({"instId": inst_id, "sz": sz}).get("data") or []
|
||||
if not rows:
|
||||
return {"bids": [], "asks": []}
|
||||
row = rows[0]
|
||||
return {
|
||||
"bids": _normalize_book_levels(row.get("bids") or [], int(depth)),
|
||||
"asks": _normalize_book_levels(row.get("asks") or [], int(depth)),
|
||||
}
|
||||
except Exception:
|
||||
return {"bids": [], "asks": []}
|
||||
|
||||
|
||||
def _fetch_book_top(
|
||||
ex: ccxt.okx, inst_id: str
|
||||
) -> tuple[float | None, float | None, float | None, float | None]:
|
||||
|
||||
Reference in New Issue
Block a user