/** * Database Entity Interfaces * * These TypeScript interfaces mirror the actual database tables in the trader system. * They are read-only contracts that define the structure of data as it exists in the database. * * IMPORTANT: These are NOT Zod schemas for validation - they represent the actual * database schema structure. For validation, use the schemas in src/schemas/. * * Database Ownership: The Infra Team owns all database schemas and migrations. * This file only provides TypeScript interfaces for type safety when consuming data. * * Naming Convention: Use snake_case to match actual database column names */ /** * Trading Signal Interface (for ML service usage) * * This interface represents a trading signal as used by the ML service * for signal generation and persistence. It's a simplified version of * TradingSignalsTable that focuses on the core signal data. * * Usage: ML service signal generation, API responses, frontend consumption */ export interface TradingSignal { id?: string; model_id: string; symbol: string; timeframe: string; signal: string; confidence: number; price: number; created_at?: string; timestamp?: string; // Frontend compatibility metadata?: Record; } /** * Trading Signals Table Interface * * Represents the signals table in the trading schema where all ML-generated * trading signals are stored for historical analysis and real-time consumption. * * Database Columns: * - id: Primary key, UUID v4 * - model_id: Foreign key to trading.models table * - symbol: Trading pair identifier (e.g., 'EURUSD', 'GBPUSD') * - timeframe: Market data timeframe used (e.g., '1m', '5m', '1h') * - signal: Trading action ('BUY', 'SELL', 'HOLD') * - confidence: Model confidence score (0.0-1.0) * - price: Entry price when signal was generated * - created_at: Record creation timestamp * - metadata: Optional JSON field for model-specific data * * Usage: ML service writes signals, Frontend reads for trading decisions */ export interface TradingSignalsTable extends TradingSignal { id: string; created_at: string; } /** * Trading Models Table Interface * * Represents the trading.models table that tracks all ML models used * for signal generation, including their versions and performance metrics. * * Database Columns: * - id: Primary key, UUID v4 * - name: Human-readable model name (e.g., 'LSTM_v1', 'RandomForest_v2') * - version: Semantic version string (e.g., '1.0.0', '2.1.3') * - status: Current model state ('active', 'inactive', 'training') * - accuracy: Optional model accuracy score (0.0-1.0) * - last_training: Optional timestamp of last training run * - created_at: Record creation timestamp * - updated_at: Last modification timestamp * * Usage: ML service manages model lifecycle, Frontend displays model status */ export interface TradingModelsTable { id: string; name: string; version: string; status: 'active' | 'inactive' | 'training'; accuracy?: number; last_training?: string; created_at: string; updated_at: string; } /** * Trading Metrics Table Interface * * Represents the trading.metrics table that stores aggregated daily * performance metrics for monitoring and analytics. * * Database Columns: * - id: Primary key, UUID v4 * - date: Date for the metrics (YYYY-MM-DD format) * - total_signals: Number of signals generated on this date * - active_models: Number of active models on this date * - average_confidence: Average confidence score across all signals * - created_at: Record creation timestamp * * Usage: Dashboard analytics, performance monitoring, historical trends */ export interface TradingMetricsTable { id: string; date: string; total_signals: number; active_models: number; average_confidence: number; created_at: string; } /** * Trading Trades Table Interface * * Represents the trading.trades table that stores all executed trades * based on ML signals, providing a complete audit trail. * * Database Columns: * - id: Primary key, UUID v4 * - signal_id: Foreign key to signals table in trading schema * - symbol: Trading pair that was traded * - side: Trade direction ('BUY' or 'SELL') * - quantity: Number of units traded * - price: Execution price per unit * - executed_at: When the trade was executed * - created_at: Record creation timestamp * * Usage: Trade history, performance analysis, compliance reporting */ export interface TradingTradesTable { id: string; signal_id: string; symbol: string; side: 'BUY' | 'SELL'; quantity: number; price: number; executed_at: string; created_at: string; } /** * IG Trading Account Interface * * Represents IG trading account information */ export interface IgAccount { id: string; account_id: string; account_name: string; currency: string; balance: number; available: number; margin: number; equity: number; created_at: string; } /** * IG Trading Position Interface * * Represents IG trading position information */ export interface IgPosition { id: string; epic: string; size: number; direction: 'BUY' | 'SELL'; entry_price: number; current_price: number; profit_loss: number; created_at: string; } /** * IG Trading Trade Interface * * Represents IG trading trade information */ export interface IgTrade { id: string; epic: string; size: number; direction: 'BUY' | 'SELL'; entry_price: number; exit_price: number; profit_loss: number; opened_at: string; closed_at?: string; } /** * Portfolio Funds Interface * * Represents portfolio funds information */ export interface PortfolioFunds { id: string; account_id: string; balance: number; available: number; margin: number; equity: number; created_at: string; } /** * Wealth Summary Interface * * Represents wealth summary information */ export interface WealthSummary { id: string; total_balance: number; total_equity: number; total_profit_loss: number; created_at: string; } /** * Database Profile Types for PostgREST * * Defines the different database access profiles used by PostgREST * to enforce Row Level Security (RLS) policies. * * Profiles: * - trading: Frontend service access (read signals, write trades) * - ml: ML service access (write signals, read models) * - admin: Administrative access (full read/write access) */ export type DatabaseProfile = 'trading' | 'ml' | 'admin'; /** * Table Access Patterns Interface * * Defines the data access patterns for each database profile. * This interface ensures type safety when working with PostgREST * responses that are filtered by RLS policies. * * Access Patterns: * - trading: Can read signals and models, write trades and metrics * - ml: Can read/write signals and models (training data) * - admin: Full access to all tables for monitoring and maintenance * * Usage: Type PostgREST responses based on the profile being used */ export interface TableAccess { trading: { signals: TradingSignalsTable[]; models: TradingModelsTable[]; metrics: TradingMetricsTable[]; trades: TradingTradesTable[]; }; ml: { signals: TradingSignalsTable[]; models: TradingModelsTable[]; }; admin: { signals: TradingSignalsTable[]; models: TradingModelsTable[]; metrics: TradingMetricsTable[]; trades: TradingTradesTable[]; }; }