a1abe159fa
Add deploy/manage.sh bootstrap for git.bz121.com/dekun/crypto_okx and point docs at this repo. Co-authored-by: Cursor <cursoragent@cursor.com>
80 lines
1.9 KiB
Python
80 lines
1.9 KiB
Python
"""成交价与手续费: 滑点 = 1×fee_rate."""
|
||
|
||
from __future__ import annotations
|
||
|
||
import os
|
||
from dataclasses import asdict, dataclass
|
||
|
||
|
||
@dataclass(slots=True)
|
||
class PriceResult:
|
||
base_px: float
|
||
fill_px: float
|
||
fee: float
|
||
slip: float
|
||
notional: float
|
||
|
||
def to_dict(self) -> dict[str, float]:
|
||
return asdict(self)
|
||
|
||
|
||
def sim_fee_rate(override: float | None = None) -> float:
|
||
if override is not None:
|
||
return float(override)
|
||
try:
|
||
return float(os.getenv("SIM_FEE_RATE") or "0.0005")
|
||
except (TypeError, ValueError):
|
||
return 0.0005
|
||
|
||
|
||
def perp_fill(
|
||
*,
|
||
side: str,
|
||
action: str,
|
||
bid: float,
|
||
ask: float,
|
||
qty: float,
|
||
fee_rate: float,
|
||
) -> PriceResult:
|
||
"""
|
||
side: long|short
|
||
action: open|close
|
||
开多/平空: 吃卖一 ×(1+f)
|
||
开空/平多: 吃买一 ×(1-f)
|
||
qty: 标的数量(合约张数 × 合约面值)
|
||
"""
|
||
f = float(fee_rate)
|
||
buying = (action == "open" and side == "long") or (action == "close" and side == "short")
|
||
if buying:
|
||
base = float(ask)
|
||
fill = base * (1.0 + f)
|
||
else:
|
||
base = float(bid)
|
||
fill = base * (1.0 - f)
|
||
notional = abs(fill * float(qty))
|
||
fee = notional * f
|
||
slip = abs(fill - base) * float(qty)
|
||
return PriceResult(base_px=base, fill_px=fill, fee=fee, slip=slip, notional=notional)
|
||
|
||
|
||
def option_fill(
|
||
*,
|
||
action: str,
|
||
bid: float,
|
||
ask: float,
|
||
qty: float,
|
||
fee_rate: float,
|
||
) -> PriceResult:
|
||
"""开仓买入吃卖一; 平仓卖出吃买一. qty = sheets × ct_mult."""
|
||
f = float(fee_rate)
|
||
if action == "open":
|
||
base = float(ask)
|
||
fill = base * (1.0 + f)
|
||
else:
|
||
base = float(bid)
|
||
fill = base * (1.0 - f)
|
||
notional = abs(fill * float(qty))
|
||
fee = notional * f
|
||
slip = abs(fill - base) * float(qty)
|
||
return PriceResult(base_px=base, fill_px=fill, fee=fee, slip=slip, notional=notional)
|