币本位顶栏资金显示USDT+ETH,单笔期权本位改为下拉选择
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -509,8 +509,8 @@ def fetch_account_balances_by_type(
|
||||
ex: ccxt.okx,
|
||||
account_type: str,
|
||||
) -> tuple[dict[str, float | None], dict[str, float | None]]:
|
||||
out: dict[str, float | None] = {"USDT": None, "USDC": None, "USDG": None}
|
||||
avail: dict[str, float | None] = {"USDT": None, "USDC": None, "USDG": None}
|
||||
out: dict[str, float | None] = {"USDT": None, "USDC": None, "USDG": None, "ETH": None, "BTC": None}
|
||||
avail: dict[str, float | None] = {"USDT": None, "USDC": None, "USDG": None, "ETH": None, "BTC": None}
|
||||
try:
|
||||
bal = ex.fetch_balance(params={"type": account_type})
|
||||
for c in out:
|
||||
@@ -525,8 +525,8 @@ def fetch_funding_balances_via_asset_api(
|
||||
ex: ccxt.okx,
|
||||
) -> tuple[dict[str, float | None], dict[str, float | None]]:
|
||||
"""OKX 资金账户余额(GET /api/v5/asset/balances),比 ccxt fetch_balance 更准确."""
|
||||
out: dict[str, float | None] = {"USDT": None, "USDC": None, "USDG": None}
|
||||
avail: dict[str, float | None] = {"USDT": None, "USDC": None, "USDG": None}
|
||||
out: dict[str, float | None] = {"USDT": None, "USDC": None, "USDG": None, "ETH": None, "BTC": None}
|
||||
avail: dict[str, float | None] = {"USDT": None, "USDC": None, "USDG": None, "ETH": None, "BTC": None}
|
||||
try:
|
||||
resp = ex.private_get_asset_balances({})
|
||||
for row in (resp or {}).get("data") or []:
|
||||
@@ -620,13 +620,21 @@ def fetch_options_balances(
|
||||
"funding_usdt": funding.get("USDT"),
|
||||
"funding_usdc": funding.get("USDC"),
|
||||
"funding_usdg": funding.get("USDG"),
|
||||
"funding_eth": funding.get("ETH"),
|
||||
"funding_btc": funding.get("BTC"),
|
||||
"funding_usdt_avail": funding_avail.get("USDT"),
|
||||
"funding_usdc_avail": funding_avail.get("USDC"),
|
||||
"funding_eth_avail": funding_avail.get("ETH"),
|
||||
"funding_btc_avail": funding_avail.get("BTC"),
|
||||
"trading_usdt": trading.get("USDT"),
|
||||
"trading_usdc": trading.get("USDC"),
|
||||
"trading_usdg": trading.get("USDG"),
|
||||
"trading_eth": trading.get("ETH"),
|
||||
"trading_btc": trading.get("BTC"),
|
||||
"trading_usdt_avail": trading_avail.get("USDT"),
|
||||
"trading_usdc_avail": trading_avail.get("USDC"),
|
||||
"trading_eth_avail": trading_avail.get("ETH"),
|
||||
"trading_btc_avail": trading_avail.get("BTC"),
|
||||
}
|
||||
_OPTIONS_BALANCE_CACHE["updated_at"] = now
|
||||
_OPTIONS_BALANCE_CACHE["data"] = result
|
||||
@@ -642,22 +650,63 @@ def options_header_balances(
|
||||
|
||||
返回:(trading_usdc, funding_usdc, funding_usdt, trading_usdt)
|
||||
"""
|
||||
pack = options_header_balance_pack(ex, force=force)
|
||||
return (
|
||||
pack.get("trading_usdc"),
|
||||
pack.get("funding_usdc"),
|
||||
pack.get("funding_usdt"),
|
||||
pack.get("trading_usdt"),
|
||||
)
|
||||
|
||||
|
||||
def options_header_balance_pack(
|
||||
ex: ccxt.okx,
|
||||
*,
|
||||
force: bool = False,
|
||||
) -> dict[str, Any]:
|
||||
"""顶栏/快照用期权资金包(含币本位 ETH/BTC)."""
|
||||
import os
|
||||
|
||||
bal = fetch_options_balances(ex, force=force)
|
||||
|
||||
def _round(v: Any) -> float | None:
|
||||
def _round(v: Any, nd: int = 2) -> float | None:
|
||||
if v is None:
|
||||
return None
|
||||
try:
|
||||
return round(float(v), 2)
|
||||
return round(float(v), nd)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
return (
|
||||
_round(bal.get("trading_usdc")),
|
||||
_round(bal.get("funding_usdc")),
|
||||
_round(bal.get("funding_usdt")),
|
||||
_round(bal.get("trading_usdt")),
|
||||
)
|
||||
def _round_coin(v: Any) -> float | None:
|
||||
if v is None:
|
||||
return None
|
||||
try:
|
||||
return round(float(v), 8)
|
||||
except (TypeError, ValueError):
|
||||
return None
|
||||
|
||||
try:
|
||||
from lib.options.options_margin_mode_lib import normalize_options_margin_mode
|
||||
|
||||
margin_mode = normalize_options_margin_mode()
|
||||
except Exception:
|
||||
margin_mode = "usdc"
|
||||
underly = (os.getenv("OKX_OPTIONS_DEFAULT_UNDERLY") or "ETH").strip().upper() or "ETH"
|
||||
coin_key = "btc" if underly == "BTC" else "eth"
|
||||
return {
|
||||
"trading_usdc": _round(bal.get("trading_usdc")),
|
||||
"funding_usdc": _round(bal.get("funding_usdc")),
|
||||
"funding_usdt": _round(bal.get("funding_usdt")),
|
||||
"trading_usdt": _round(bal.get("trading_usdt")),
|
||||
"funding_eth": _round_coin(bal.get("funding_eth")),
|
||||
"trading_eth": _round_coin(bal.get("trading_eth")),
|
||||
"funding_btc": _round_coin(bal.get("funding_btc")),
|
||||
"trading_btc": _round_coin(bal.get("trading_btc")),
|
||||
"options_margin_mode": margin_mode,
|
||||
"options_underly": underly,
|
||||
"funding_coin": _round_coin(bal.get(f"funding_{coin_key}")),
|
||||
"trading_coin": _round_coin(bal.get(f"trading_{coin_key}")),
|
||||
}
|
||||
|
||||
|
||||
def fetch_index_price(ex: ccxt.okx, uly: str) -> float | None:
|
||||
|
||||
Reference in New Issue
Block a user