86 lines
2.8 KiB
Python
86 lines
2.8 KiB
Python
from __future__ import annotations
|
|
|
|
from pathlib import Path
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
if str(ROOT) not in sys.path:
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from src.services.broker import OKXSandboxPreflightService
|
|
|
|
DEFAULT_ARTIFACT_PATH = ROOT / ".claude" / "okx_sandbox_smoke_latest.json"
|
|
|
|
|
|
def main(env=None) -> int:
|
|
env = os.environ if env is None else env
|
|
instrument_id = _get_int(env, "OKX_SANDBOX_SMOKE_INSTRUMENT_ID", 1)
|
|
timeframe = str(env.get("OKX_SANDBOX_SMOKE_TIMEFRAME") or "1m")
|
|
execute = _get_bool(env, "OKX_SANDBOX_SMOKE_EXECUTE", False)
|
|
output_json = _get_bool(env, "OKX_SANDBOX_SMOKE_OUTPUT_JSON", False)
|
|
artifact_path = _artifact_path(env)
|
|
payload = OKXSandboxPreflightService().run_isolated_smoke(
|
|
instrument_id=instrument_id,
|
|
timeframe=timeframe,
|
|
execute=execute,
|
|
order_price=_get_optional(env, "OKX_SANDBOX_SMOKE_PRICE"),
|
|
order_size=_get_optional(env, "OKX_SANDBOX_SMOKE_SIZE"),
|
|
order_side=str(env.get("OKX_SANDBOX_SMOKE_SIDE") or "buy"),
|
|
order_type=str(env.get("OKX_SANDBOX_SMOKE_ORDER_TYPE") or "post_only"),
|
|
td_mode=str(env.get("OKX_SANDBOX_SMOKE_TD_MODE") or "isolated"),
|
|
timeout_seconds=_get_float(env, "OKX_SANDBOX_SMOKE_TIMEOUT_SECONDS", 20.0),
|
|
)
|
|
artifact_path.parent.mkdir(parents=True, exist_ok=True)
|
|
artifact_path.write_text(json.dumps(payload, ensure_ascii=False, indent=2, sort_keys=True), encoding="utf-8")
|
|
if output_json:
|
|
print(json.dumps(payload, ensure_ascii=False, sort_keys=True))
|
|
else:
|
|
print(f"OKX sandbox smoke status: {payload.get('status')}")
|
|
print(f"Execute requested: {payload.get('execute_requested')}")
|
|
print(f"Blockers: {payload.get('blockers') or []}")
|
|
print(f"Artifact: {artifact_path}")
|
|
return 0 if payload.get("status") == "passed" else 2
|
|
|
|
|
|
def _artifact_path(env) -> Path:
|
|
value = str(env.get("OKX_SANDBOX_SMOKE_ARTIFACT_PATH") or "").strip()
|
|
if value:
|
|
path = Path(value)
|
|
return path if path.is_absolute() else ROOT / path
|
|
return DEFAULT_ARTIFACT_PATH
|
|
|
|
|
|
def _get_optional(env, key: str):
|
|
value = env.get(key)
|
|
if value is None:
|
|
return None
|
|
value = str(value).strip()
|
|
return value or None
|
|
|
|
|
|
def _get_int(env, key: str, default: int) -> int:
|
|
value = env.get(key)
|
|
if value is None or value == "":
|
|
return default
|
|
return int(value)
|
|
|
|
|
|
def _get_float(env, key: str, default: float) -> float:
|
|
value = env.get(key)
|
|
if value is None or value == "":
|
|
return default
|
|
return float(value)
|
|
|
|
|
|
def _get_bool(env, key: str, default: bool = False) -> bool:
|
|
value = env.get(key)
|
|
if value is None or value == "":
|
|
return default
|
|
return str(value).strip().lower() in {"1", "true", "yes", "on"}
|
|
|
|
|
|
if __name__ == "__main__":
|
|
raise SystemExit(main())
|