Files
aura-trade/backend/app/broker/cdp_bridge.py
T

97 lines
3.9 KiB
Python

import logging
import httpx
from typing import Dict, Any, Optional
logger = logging.getLogger("AuraTrade.CDPBridge")
BRIDGE_HTTP_URL = "http://10.0.1.1:9225"
class CDPBridge:
def __init__(self, bridge_url: str = BRIDGE_HTTP_URL):
self.bridge_url = bridge_url
async def get_telemetry(self) -> Dict[str, Any]:
try:
async with httpx.AsyncClient(timeout=3.0) as client:
res = await client.get(f"{self.bridge_url}/status")
if res.status_code == 200:
data = res.json()
if data.get("connected"):
telem = data.get("telemetry", {})
return {
"connected": True,
"mode": "cdp_exness",
"account_id": data.get("account_id", 425437997),
"server": data.get("server", "Exness-MT5Real15"),
"balance": telem.get("balance", 15.19),
"equity": telem.get("equity", 15.19),
"bid": telem.get("bid", 4009.0),
"ask": telem.get("ask", 4009.24),
"spread_pips": telem.get("spread_pips", 0.2),
"floating_pnl": round(telem.get("equity", 15.19) - telem.get("balance", 15.19), 2),
"trade_allowed": True
}
else:
return {
"connected": False,
"mode": "awaiting_login",
"status": data.get("status", "DISCONNECTED"),
"balance": 15.19,
"equity": 15.19,
"floating_pnl": 0.0,
"trade_allowed": False
}
except Exception as e:
logger.error(f"CDP Bridge query failed: {e}")
return {
"connected": False,
"mode": "offline",
"status": "BRIDGE_OFFLINE",
"balance": 15.19,
"equity": 15.19,
"floating_pnl": 0.0,
"trade_allowed": False
}
async def execute_order(self, symbol: str, order_type: str, lot: float, sl: float, tp: float) -> Dict[str, Any]:
try:
async with httpx.AsyncClient(timeout=10.0) as client:
payload = {
"symbol": symbol,
"type": order_type.upper(),
"lot": lot,
"sl": sl,
"tp": tp
}
res = await client.post(f"{self.bridge_url}/trade", json=payload)
if res.status_code == 200:
result = res.json()
if result.get("success"):
return {
"success": True,
"ticket": result.get("ticket"),
"fill_price": result.get("fill_price", 0.0),
"message": result.get("message")
}
return {
"success": False,
"error": "EXECUTION_REJECTED",
"reason": result.get("reason", "Order rejected by broker terminal")
}
else:
return {
"success": False,
"error": "HTTP_ERROR",
"reason": f"Bridge returned status {res.status_code}: {res.text}"
}
except Exception as e:
logger.error(f"CDP Bridge execution call failed: {e}")
return {
"success": False,
"error": "EXECUTION_EXCEPTION",
"reason": str(e)
}
cdp_bridge = CDPBridge()