feat: initial commit of AuraTrade quant terminal and sentinel risk engine
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
"use client";
|
||||
|
||||
import { useEffect, useState, useRef, useCallback } from "react";
|
||||
|
||||
export interface LiveTick {
|
||||
symbol: string;
|
||||
bid: number;
|
||||
ask: number;
|
||||
spread_pips: number;
|
||||
time: number;
|
||||
}
|
||||
|
||||
export interface LiveTelemetry {
|
||||
status: string;
|
||||
account_id: number;
|
||||
server: string;
|
||||
balance: number;
|
||||
equity: number;
|
||||
margin?: number;
|
||||
free_margin?: number;
|
||||
floating_pnl: number;
|
||||
trade_allowed?: boolean;
|
||||
}
|
||||
|
||||
export interface MarketStreamPacket {
|
||||
type: string;
|
||||
symbol: string;
|
||||
tick: LiveTick;
|
||||
telemetry: LiveTelemetry;
|
||||
news_quarantine: {
|
||||
is_active: boolean;
|
||||
reason: string;
|
||||
};
|
||||
circuit_breaker: {
|
||||
is_tripped: boolean;
|
||||
reason: string;
|
||||
};
|
||||
}
|
||||
|
||||
export function useExnessWebSocket(endpointUrl: string = "ws://localhost:8085/ws/stream") {
|
||||
const [isConnected, setIsConnected] = useState(false);
|
||||
const [packet, setPacket] = useState<MarketStreamPacket | null>(null);
|
||||
const [ping, setPing] = useState<number>(18);
|
||||
const wsRef = useRef<WebSocket | null>(null);
|
||||
const reconnectTimeoutRef = useRef<NodeJS.Timeout | null>(null);
|
||||
|
||||
const connect = useCallback(() => {
|
||||
try {
|
||||
const ws = new WebSocket(endpointUrl);
|
||||
wsRef.current = ws;
|
||||
|
||||
ws.onopen = () => {
|
||||
setIsConnected(true);
|
||||
// Measure ping
|
||||
const start = performance.now();
|
||||
ws.send("ping");
|
||||
ws.onmessage = (event) => {
|
||||
if (event.data === "pong") {
|
||||
setPing(Math.round(performance.now() - start));
|
||||
} else {
|
||||
try {
|
||||
const data = JSON.parse(event.data);
|
||||
setPacket(data);
|
||||
} catch (err) {
|
||||
// Ignore non-json ping responses
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
ws.onclose = () => {
|
||||
setIsConnected(false);
|
||||
// Exponential reconnect after 2 seconds
|
||||
reconnectTimeoutRef.current = setTimeout(() => {
|
||||
connect();
|
||||
}, 2000);
|
||||
};
|
||||
|
||||
ws.onerror = () => {
|
||||
ws.close();
|
||||
};
|
||||
} catch (err) {
|
||||
setIsConnected(false);
|
||||
}
|
||||
}, [endpointUrl]);
|
||||
|
||||
useEffect(() => {
|
||||
connect();
|
||||
return () => {
|
||||
if (wsRef.current) wsRef.current.close();
|
||||
if (reconnectTimeoutRef.current) clearTimeout(reconnectTimeoutRef.current);
|
||||
};
|
||||
}, [connect]);
|
||||
|
||||
return { isConnected, packet, ping };
|
||||
}
|
||||
Reference in New Issue
Block a user