diff --git a/Cargo.lock b/Cargo.lock index 64393e87f..42549e321 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -7802,7 +7802,6 @@ dependencies = [ "tower 0.4.13", "tracing", "tracing-subscriber", - "trading_engine", "uuid 1.18.1", ] diff --git a/common/src/lib.rs b/common/src/lib.rs index c23e604ed..1f29e8f73 100644 --- a/common/src/lib.rs +++ b/common/src/lib.rs @@ -30,25 +30,8 @@ pub mod traits; pub mod trading; pub mod types; -// Prelude module for common imports -pub mod prelude; - -// Re-export commonly used types at crate root -pub use types::{ - // Core types - Decimal, Quantity, Volume, Price, HftTimestamp, Timestamp, - // Trading types - Order, Position, Execution, OrderSide, OrderStatus, OrderType, TimeInForce, - // ID types - OrderId, ExecutionId, Symbol, Currency, Exchange, AssetId, - // Additional types - MarketTick, BrokerType, TradeEvent, QuoteEvent, - // Market data types - MarketDataEvent, BarEvent, OrderBookEvent, Level2Update, MarketStatus, - ConnectionEvent, ErrorEvent, Aggregate, DataType, Subscription, PriceLevel, ConnectionStatus, - // Error types - CommonTypeError, -}; +// REMOVED prelude module - violates type governance and creates hidden coupling +// All imports must be explicit to maintain clear architectural boundaries // Re-export trading types from trading module pub use trading::MarketRegime; diff --git a/common/src/prelude.rs b/common/src/prelude.rs deleted file mode 100644 index 917f3802a..000000000 --- a/common/src/prelude.rs +++ /dev/null @@ -1,22 +0,0 @@ -//! Common prelude module -//! -//! This module provides convenient imports for commonly used types -//! across the Foxhunt HFT trading system. - -// Re-export all commonly used types -pub use crate::types::{ - // Core types - Decimal, Quantity, Volume, Price, HftTimestamp, - // Trading types - Order, Position, Execution, OrderSide, OrderStatus, OrderType, TimeInForce, - // ID types - OrderId, ExecutionId, Symbol, Currency, Exchange, - // Error types - CommonTypeError, -}; - -// Re-export trading module types -pub use crate::trading::*; - -// Re-export error types -pub use crate::error::{CommonError, ErrorCategory}; \ No newline at end of file diff --git a/common/src/types.rs b/common/src/types.rs index 934afcaaf..3875b0d3b 100644 --- a/common/src/types.rs +++ b/common/src/types.rs @@ -76,6 +76,45 @@ pub type ModelRegistry = SharedHashMap; /// Generic configuration cache pub type ConfigCache = SharedHashMap; +// ============================================================================= +// Event Types - Moved from trading_engine to enforce pure client architecture +// ============================================================================= + +/// Order events for the complete order lifecycle +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct OrderEvent { + pub order_id: OrderId, + pub symbol: Symbol, + pub order_type: OrderType, + pub side: OrderSide, + pub quantity: Quantity, + pub price: Option, + pub timestamp: DateTime, + /// Strategy or client identifier + pub strategy_id: String, + /// Order event type (placed, modified, cancelled) + pub event_type: OrderEventType, + /// Previous quantity for modifications + pub previous_quantity: Option, + /// Previous price for modifications + pub previous_price: Option, + /// Reason for cancellation or modification + pub reason: Option, +} + +/// Types of order events +#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +pub enum OrderEventType { + /// Order was placed + Placed, + /// Order was modified + Modified, + /// Order was cancelled + Cancelled, + /// Order was rejected + Rejected, +} + // ============================================================================= // Core Data Types // ============================================================================= diff --git a/ml/src/common/mod.rs b/ml/src/common/mod.rs index d33af4c9f..5c0a646bb 100644 --- a/ml/src/common/mod.rs +++ b/ml/src/common/mod.rs @@ -137,23 +137,24 @@ pub mod conversions { use super::*; /// Convert canonical Price to liquid submodule FixedPoint (8-decimal to 6-decimal precision) - pub fn price_to_liquid_fixed_point(price: Price) -> crate::liquid::FixedPoint { + pub fn price_to_liquid_fixed_point(price: Price) -> Result> { let liquid_precision = 1_000_000_i64; // 6 decimal places let canonical_precision = 100_000_000_i64; // 8 decimal places - // Scale down from 8-decimal to 6-decimal precision - let scaled_value = (price.to_f64().unwrap_or(0.0) * liquid_precision as f64) as i64; - crate::liquid::FixedPoint(scaled_value) + // Scale down from 8-decimal to 6-decimal precision with proper error handling + let price_f64 = price.to_f64().ok_or("Failed to convert Price to f64")?; + let scaled_value = (price_f64 * liquid_precision as f64) as i64; + Ok(crate::liquid::FixedPoint(scaled_value)) } /// Convert liquid submodule FixedPoint to canonical Price (6-decimal to 8-decimal precision) - pub fn liquid_fixed_point_to_price(fixed_point: crate::liquid::FixedPoint) -> Price { + pub fn liquid_fixed_point_to_price(fixed_point: crate::liquid::FixedPoint) -> Result> { let liquid_precision = 1_000_000_i64; // 6 decimal places let canonical_precision = 100_000_000_i64; // 8 decimal places - // Scale up from 6-decimal to 8-decimal precision + // Scale up from 6-decimal to 8-decimal precision with proper error handling let value_f64 = fixed_point.0 as f64 / liquid_precision as f64; - Price::from_f64(value_f64).unwrap_or(Price::ZERO) + Price::from_f64(value_f64).ok_or_else(|| "Failed to convert f64 to Price".into()) } /// Convert `f64` to canonical Price with full 8-decimal precision @@ -163,8 +164,8 @@ pub mod conversions { } /// Convert canonical Price to `f64` for ML model inputs - pub fn price_to_f64(price: Price) -> f64 { - price.to_f64().unwrap_or(0.0) + pub fn price_to_f64(price: Price) -> Result> { + price.to_f64().ok_or_else(|| "Failed to convert Price to f64 for ML model".into()) } /// SYSTEMATIC CONVERSION TRAITS: Eliminate IntegerPrice usage throughout ML @@ -181,8 +182,8 @@ pub mod conversions { } /// Convert Volume to f64 for ML model inputs - pub fn volume_to_f64(volume: Volume) -> f64 { - volume.to_f64().unwrap_or(0.0) + pub fn volume_to_f64(volume: Volume) -> Result> { + volume.to_f64().ok_or_else(|| "Failed to convert Volume to f64 for ML model".into()) } /// Convert f64 to Volume with validation @@ -194,8 +195,9 @@ pub mod conversions { } /// Convert Quantity to i64 for efficient processing - pub fn quantity_to_i64(quantity: Quantity) -> i64 { - (quantity.to_f64().unwrap_or(0.0) * 100_000_000.0) as i64 // Convert to integer with 8 decimal precision + pub fn quantity_to_i64(quantity: Quantity) -> Result> { + let quantity_f64 = quantity.to_f64().ok_or("Failed to convert Quantity to f64")?; + Ok((quantity_f64 * 100_000_000.0) as i64) // Convert to integer with 8 decimal precision } /// Convert i64 to Quantity with validation diff --git a/tli/Cargo.toml b/tli/Cargo.toml index e6190ca11..cdd0865bd 100644 --- a/tli/Cargo.toml +++ b/tli/Cargo.toml @@ -44,11 +44,9 @@ rand.workspace = true tokio-stream.workspace = true futures-util.workspace = true -# Core types from trading engine (for canonical event types) -trading_engine.workspace = true - -# Canonical types from common crate +# Canonical types from common crate ONLY - TLI is a pure client common.workspace = true +# REMOVED trading_engine dependency - violates pure client architecture # Note: Database-related imports removed to enforce clean service architecture # - SQLite pools should only exist in services diff --git a/tli/src/dashboard/events.rs b/tli/src/dashboard/events.rs index f9e07a2b3..6d06e7d73 100644 --- a/tli/src/dashboard/events.rs +++ b/tli/src/dashboard/events.rs @@ -5,7 +5,8 @@ use crate::dashboard::DashboardType; use serde::{Deserialize, Serialize}; -pub use trading_engine::types::events::OrderEvent; +// Use OrderEvent from common crate - TLI is a pure client +pub use common::types::OrderEvent; /// Main event type for dashboard communication #[derive(Debug, Clone)] diff --git a/tli/src/dashboard/observability.rs b/tli/src/dashboard/observability.rs index 31e9e2089..b7edf4b25 100644 --- a/tli/src/dashboard/observability.rs +++ b/tli/src/dashboard/observability.rs @@ -7,14 +7,12 @@ //! - System-wide metrics across all critical paths use crate::error::TliResult; -use common::types::get_order_ack_percentiles; -use common::types::LatencyPercentiles; -use common::types::MarketDataEvent; -use common::types::MARKET_DATA_BUFFER; -use common::types::TELEMETRY_TRACER; -use common::types::ORDER_ACK_LATENCY; -use common::types::; -use trading_engine::timing::{HardwareTimestamp, LatencyStats, HftLatencyTracker}; +// All types from common crate - TLI is a pure client +use common::types::{ + get_order_ack_percentiles, LatencyPercentiles, MarketDataEvent, + MARKET_DATA_BUFFER, TELEMETRY_TRACER, ORDER_ACK_LATENCY, + HardwareTimestamp, LatencyStats, HftLatencyTracker +}; use ratatui::{ backend::Backend, layout::{Alignment, Constraint, Direction, Layout, Rect},