Merge OKX dual APIs into one OKX_API_* account for perp and options.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
dekun
2026-08-05 21:28:33 +08:00
parent 22e6b68e6d
commit 09a763e47d
26 changed files with 166 additions and 473 deletions
+16 -44
View File
@@ -72,16 +72,22 @@ def td_mode_for_option_buy(configured: str | None = None) -> str:
def create_options_exchange(
api_key: str,
api_secret: str,
passphrase: str,
api_key: str = "",
api_secret: str = "",
passphrase: str = "",
proxies: dict[str, str] | None = None,
) -> ccxt.okx:
"""创建 option 客户端.未传密钥时读 OKX_API_*(与永续同源)."""
import os
key = (api_key or os.getenv("OKX_API_KEY") or "").strip()
secret = (api_secret or os.getenv("OKX_API_SECRET") or "").strip()
password = (passphrase or os.getenv("OKX_API_PASSPHRASE") or "").strip()
ex = ccxt.okx(
{
"apiKey": api_key,
"secret": api_secret,
"password": passphrase,
"apiKey": key,
"secret": secret,
"password": password,
"enableRateLimit": True,
"options": {"defaultType": "option"},
}
@@ -600,7 +606,10 @@ def options_header_balances(
*,
force: bool = False,
) -> tuple[float | None, float | None, float | None, float | None]:
"""顶栏四格:交易 USDC/USDT,资金 USDC/USDT(单次拉取 + 缓存)."""
"""顶栏期权两格用 USDC;顺带返回同账户 USDT(调用方勿再计入总资金,避免与永续栏重复).
返回:(trading_usdc, funding_usdc, funding_usdt, trading_usdt)
"""
bal = fetch_options_balances(ex, force=force)
def _round(v: Any) -> float | None:
@@ -1562,43 +1571,6 @@ def spot_market_swap_usdt_usdc(
return {"ok": False, "msg": _okx_trade_error_message(e)}
def transfer_main_sub_account(
ex: ccxt.okx,
*,
ccy: str,
amount: float,
sub_acct: str,
main_to_sub: bool,
from_account: str = "funding",
to_account: str = "funding",
) -> dict[str, Any]:
"""主账户与子账户之间划转(须主账户 API)."""
if amount <= 0:
return {"ok": False, "msg": "划转金额须大于 0"}
sub = (sub_acct or "").strip()
if not sub:
return {"ok": False, "msg": "未配置子账户名称 OKX_SUB_ACCOUNT_NAME"}
from_code = _OKX_ACCT_CODE.get((from_account or "funding").lower(), "6")
to_code = _OKX_ACCT_CODE.get((to_account or "funding").lower(), "6")
try:
resp = ex.private_post_asset_transfer(
{
"type": "1" if main_to_sub else "2",
"ccy": str(ccy).upper(),
"amt": str(amount),
"from": from_code,
"to": to_code,
"subAcct": sub,
}
)
data = (resp or {}).get("data") or []
if data and str(data[0].get("sCode", "0")) == "0":
return {"ok": True, "data": data[0], "raw": resp}
return {"ok": False, "msg": _okx_trade_error_message(resp=resp), "raw": resp}
except Exception as e:
return {"ok": False, "msg": _okx_trade_error_message(e)}
def format_position_row(
pos: dict[str, Any],
ct_mult: float = 0.01,