import React, { useEffect, useMemo, useRef, useState } from "react"; import { Box, Text, useInput } from "ink"; import { tradingConfig } from "../config"; import { getExchangeDisplayName, resolveExchangeId } from "../exchanges/create-adapter"; import { buildAdapterFromEnv } from "../exchanges/resolve-from-env"; import { TrendEngine, type TrendEngineSnapshot } from "../strategy/trend-engine"; import { formatNumber, formatTrendLabel } from "../utils/format"; import { DataTable, type TableColumn } from "./components/DataTable"; import { t } from "../i18n"; const READY_MESSAGE = t("trend.readyMessage"); interface TrendAppProps { onExit: () => void; } const inputSupported = Boolean(process.stdin && (process.stdin as any).isTTY); export function TrendApp({ onExit }: TrendAppProps) { const [snapshot, setSnapshot] = useState(null); const [error, setError] = useState(null); const engineRef = useRef(null); const exchangeId = useMemo(() => resolveExchangeId(), []); const exchangeName = useMemo(() => getExchangeDisplayName(exchangeId), [exchangeId]); useInput( (input, key) => { if (key.escape) { engineRef.current?.stop(); onExit(); } }, { isActive: inputSupported } ); useEffect(() => { try { const adapter = buildAdapterFromEnv({ exchangeId, symbol: tradingConfig.symbol }); const engine = new TrendEngine(tradingConfig, adapter); engineRef.current = engine; setSnapshot(engine.getSnapshot()); const handler = (next: TrendEngineSnapshot) => { setSnapshot({ ...next, tradeLog: [...next.tradeLog] }); }; engine.on("update", handler); engine.start(); return () => { engine.off("update", handler); engine.stop(); }; } catch (err) { console.error(err); setError(err instanceof Error ? err : new Error(String(err))); } }, [exchangeId]); if (error) { return ( {t("common.startFailed", { message: error.message })} {t("common.checkEnv")} ); } if (!snapshot) { return ( {t("common.initializing", { target: t("trend.name") })} ); } const { position, tradeLog, openOrders, trend, ready, lastPrice, sma30, sessionVolume } = snapshot; const hasPosition = Math.abs(position.positionAmt) > 1e-5; const lastLogs = tradeLog.slice(-5); const sortedOrders = [...openOrders].sort((a, b) => (Number(b.updateTime ?? 0) - Number(a.updateTime ?? 0)) || Number(b.orderId) - Number(a.orderId)); const orderRows = sortedOrders.slice(0, 8).map((order) => ({ id: order.orderId, side: order.side, type: order.type, price: order.price, qty: order.origQty, filled: order.executedQty, status: order.status, })); const orderColumns: TableColumn[] = [ { key: "id", header: "ID", align: "right", minWidth: 6 }, { key: "side", header: "Side", minWidth: 4 }, { key: "type", header: "Type", minWidth: 10 }, { key: "price", header: "Price", align: "right", minWidth: 10 }, { key: "qty", header: "Qty", align: "right", minWidth: 8 }, { key: "filled", header: "Filled", align: "right", minWidth: 8 }, { key: "status", header: "Status", minWidth: 10 }, ]; return ( {t("trend.title")} {t("trend.headerLine", { exchange: exchangeName, symbol: snapshot.symbol, lastPrice: formatNumber(lastPrice, 2), sma: formatNumber(sma30, 2), trend: formatTrendLabel(trend), })} {t("trend.statusLine", { status: ready ? t("status.live") : READY_MESSAGE })} {t("common.section.position")} {hasPosition ? ( <> {t("trend.positionLine", { direction: position.positionAmt > 0 ? t("common.direction.long") : t("common.direction.short"), qty: formatNumber(Math.abs(position.positionAmt), 4), entry: formatNumber(position.entryPrice, 2), })} {t("trend.pnlLine", { pnl: formatNumber(snapshot.pnl, 4), unrealized: formatNumber(snapshot.unrealized, 4), })} ) : ( {t("common.noPosition")} )} {t("common.section.performance")} {t("trend.performanceLine", { trades: snapshot.totalTrades, profit: formatNumber(snapshot.totalProfit, 4), })} {t("trend.volumeLine", { volume: formatNumber(sessionVolume, 2) })} {snapshot.lastOpenSignal.side ? ( {t("trend.lastSignal", { side: snapshot.lastOpenSignal.side, price: formatNumber(snapshot.lastOpenSignal.price, 2), })} ) : null} {t("common.section.orders")} {orderRows.length > 0 ? ( ) : ( {t("common.noOrders")} )} {t("common.section.recentTrades")} {lastLogs.length > 0 ? ( lastLogs.map((item, index) => ( [{item.time}] [{item.type}] {item.detail} )) ) : ( {t("common.noLogs")} )} ); }