78fd046fb6
Co-authored-by: Cursor <cursoragent@cursor.com>
26 lines
869 B
Python
26 lines
869 B
Python
"""交易所无关的到期时刻工具。
|
|
|
|
OKX / 币安欧洲期权惯例:到期日当日 08:00 UTC(上海 16:00)。
|
|
若合约元数据带有 expiry_ms,优先使用元数据。
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
from datetime import datetime, timezone
|
|
|
|
|
|
def expiry_ms_from_ymd(ymd: str) -> int:
|
|
"""YYMMDD → 到期毫秒时间戳(UTC 08:00)。"""
|
|
ymd = (ymd or "").strip()
|
|
if len(ymd) != 6 or not ymd.isdigit():
|
|
raise ValueError(f"invalid expiry ymd: {ymd!r}")
|
|
yy, mm, dd = int(ymd[0:2]), int(ymd[2:4]), int(ymd[4:6])
|
|
dt = datetime(2000 + yy, mm, dd, 8, 0, 0, tzinfo=timezone.utc)
|
|
return int(dt.timestamp() * 1000)
|
|
|
|
|
|
def ymd_from_expiry_ms(ms: int) -> str:
|
|
"""到期毫秒 → YYMMDD(按 UTC 日历日)。"""
|
|
dt = datetime.fromtimestamp(int(ms) / 1000.0, tz=timezone.utc)
|
|
return dt.strftime("%y%m%d")
|