106 lines
3.8 KiB
Python
106 lines
3.8 KiB
Python
from pathlib import Path
|
|
import json
|
|
import os
|
|
import sys
|
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
os.environ.setdefault("DATABASE_URL", "postgresql+psycopg://user:pass@localhost:5432/ai_ict_test")
|
|
if str(ROOT) not in sys.path:
|
|
sys.path.insert(0, str(ROOT))
|
|
|
|
from src.services.broker import LiveBrokerService
|
|
|
|
|
|
def main(env=None) -> int:
|
|
env = os.environ if env is None else env
|
|
output_json = _get_bool(env, "LIVE_ADAPTER_OUTPUT_JSON", False)
|
|
operation = str(env.get("LIVE_ADAPTER_OPERATION") or "reconcile").strip().lower()
|
|
adapter_name = str(env.get("LIVE_ADAPTER_NAME") or "okx").strip().lower()
|
|
requested_by = str(env.get("LIVE_REQUESTED_BY") or "local_operator")
|
|
explicit_live_confirmation = _get_bool(env, "LIVE_EXPLICIT_CONFIRMATION", False)
|
|
instrument_id = _get_optional_int(env, "LIVE_INSTRUMENT_ID")
|
|
execution_ticket_id = _get_optional_int(env, "LIVE_EXECUTION_TICKET_ID")
|
|
kill_switch_reason = str(env.get("LIVE_KILL_SWITCH_REASON") or "manual_kill_switch")
|
|
service = LiveBrokerService()
|
|
try:
|
|
if operation == "submit":
|
|
if execution_ticket_id is None:
|
|
raise ValueError("missing_required_env:LIVE_EXECUTION_TICKET_ID")
|
|
result = service.submit_confirmed_ticket(
|
|
execution_ticket_id,
|
|
requested_by=requested_by,
|
|
explicit_live_confirmation=explicit_live_confirmation,
|
|
adapter_name=adapter_name,
|
|
)
|
|
elif operation == "reconcile":
|
|
result = service.reconcile_orders(
|
|
requested_by=requested_by,
|
|
instrument_id=instrument_id,
|
|
adapter_name=adapter_name,
|
|
)
|
|
elif operation == "cancel_all":
|
|
result = service.cancel_all(
|
|
requested_by=requested_by,
|
|
instrument_id=instrument_id,
|
|
explicit_live_confirmation=explicit_live_confirmation,
|
|
adapter_name=adapter_name,
|
|
)
|
|
elif operation == "kill_switch":
|
|
result = service.activate_kill_switch(
|
|
requested_by=requested_by,
|
|
reason=kill_switch_reason,
|
|
explicit_live_confirmation=explicit_live_confirmation,
|
|
adapter_name=adapter_name,
|
|
)
|
|
else:
|
|
raise ValueError(f"unsupported_live_adapter_operation:{operation}")
|
|
except SQLAlchemyError as exc:
|
|
payload = {
|
|
"passed": False,
|
|
"operation": operation,
|
|
"adapter_name": adapter_name,
|
|
"error_type": exc.__class__.__name__,
|
|
"error": str(exc),
|
|
}
|
|
if output_json:
|
|
print(json.dumps(payload, default=str, ensure_ascii=False, sort_keys=True))
|
|
else:
|
|
print(f"Database unavailable for live adapter sample: {exc.__class__.__name__}")
|
|
return 2
|
|
|
|
payload = {
|
|
"passed": True,
|
|
"operation": operation,
|
|
"adapter_name": adapter_name,
|
|
"result": result.to_dict() if result is not None else None,
|
|
}
|
|
if output_json:
|
|
print(json.dumps(payload, default=str, ensure_ascii=False, sort_keys=True))
|
|
else:
|
|
print(
|
|
f"Live adapter operation: {operation}; "
|
|
f"status: {(result.status if result else 'missing')}; "
|
|
f"reasons: {(result.reason_codes if result else [])}"
|
|
)
|
|
return 0
|
|
|
|
|
|
def _get_optional_int(env, key: str):
|
|
value = env.get(key)
|
|
if value is None or value == "":
|
|
return None
|
|
return int(value)
|
|
|
|
|
|
def _get_bool(env, key: str, default: bool) -> 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())
|