qualia_core_db/specialized_libs/financial_modeling/
trading.rs1use super::*;
2
3pub struct TradingEngine {
5 order_manager: OrderManager,
6 execution_engine: ExecutionEngine,
7 position_manager: PositionManager,
8}
9
10pub struct PositionManager {
12 positions: HashMap<String, Position>,
13 position_limits: HashMap<String, PositionLimit>,
14 margin_calculator: MarginCalculator,
15}
16
17#[derive(Debug, Clone)]
19pub struct Position {
20 pub position_id: String,
21 pub portfolio_id: String,
22 pub asset_id: String,
23 pub quantity: f64,
24 pub average_cost: f64,
25 pub market_value: f64,
26 pub unrealized_pnl: f64,
27 pub realized_pnl: f64,
28 pub last_updated: u64,
29}
30
31#[derive(Debug, Clone)]
33pub struct PositionLimit {
34 pub limit_id: String,
35 pub asset_id: String,
36 pub max_position: f64,
37 pub min_position: f64,
38 pub warning_threshold: f64,
39}
40
41pub struct MarginCalculator {
43 margin_methods: HashMap<String, MarginMethod>,
44 margin_requirements: MarginRequirements,
45}
46
47#[derive(Debug, Clone)]
49pub struct MarginMethod {
50 pub method_id: String,
51 pub method_type: MarginMethodType,
52 pub parameters: MarginMethodParameters,
53}
54
55#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
57pub enum MarginMethodType {
58 SPAN,
59 TIMS,
60 PortfolioMargin,
61 RegT,
62}
63
64#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct MarginMethodParameters {
67 pub volatility_multiplier: f64,
68 pub concentration_factor: f64,
69 pub stress_period: u32,
70}
71
72impl TradingEngine {
73 pub fn new() -> Self {
74 Self {
75 order_manager: OrderManager::new(),
76 execution_engine: ExecutionEngine::new(),
77 position_manager: PositionManager::new(),
78 }
79 }
80
81 pub fn initialize(&mut self) -> Result<(), FinancialError> {
82 self.order_manager.initialize()?;
83 self.execution_engine.initialize()?;
84 Ok(())
85 }
86
87 pub fn validate_order(&self, order: &Order) -> Result<(), FinancialError> {
88 if order.quantity <= 0.0 {
89 return Err(FinancialError::ValidationError(
90 "Order quantity must be positive".to_string(),
91 ));
92 }
93 if let Some(price) = order.price {
94 if price <= 0.0 {
95 return Err(FinancialError::ValidationError(
96 "Order price must be positive".to_string(),
97 ));
98 }
99 }
100 Ok(())
101 }
102
103 pub fn execute_trade(&mut self, _order: &Order) -> Result<TradeResult, FinancialError> {
104 Err(FinancialError::NotImplemented(
111 "trade execution (execute_trade): no broker/exchange connection; this system does not \
112 place orders or move money. Refusing to report a fabricated fill."
113 .to_string(),
114 ))
115 }
116
117 pub fn position_manager(&self) -> &PositionManager {
118 &self.position_manager
119 }
120
121 pub fn position_manager_mut(&mut self) -> &mut PositionManager {
122 &mut self.position_manager
123 }
124}
125
126impl PositionManager {
127 pub fn new() -> Self {
128 Self {
129 positions: HashMap::new(),
130 position_limits: HashMap::new(),
131 margin_calculator: MarginCalculator::new(),
132 }
133 }
134
135 pub fn add_position(&mut self, position: Position) {
136 self.positions
137 .insert(position.position_id.clone(), position);
138 }
139
140 pub fn get_position(&self, position_id: &str) -> Option<&Position> {
141 self.positions.get(position_id)
142 }
143
144 pub fn list_positions(&self) -> Vec<String> {
145 self.positions.keys().cloned().collect()
146 }
147
148 pub fn add_position_limit(&mut self, limit: PositionLimit) {
149 self.position_limits.insert(limit.limit_id.clone(), limit);
150 }
151
152 pub fn get_position_limit(&self, limit_id: &str) -> Option<&PositionLimit> {
153 self.position_limits.get(limit_id)
154 }
155
156 pub fn margin_calculator(&self) -> &MarginCalculator {
157 &self.margin_calculator
158 }
159
160 pub fn margin_calculator_mut(&mut self) -> &mut MarginCalculator {
161 &mut self.margin_calculator
162 }
163}
164
165impl MarginCalculator {
166 pub fn new() -> Self {
167 Self {
168 margin_methods: HashMap::new(),
169 margin_requirements: MarginRequirements::new(),
170 }
171 }
172
173 pub fn add_margin_method(&mut self, method: MarginMethod) {
174 self.margin_methods.insert(method.method_id.clone(), method);
175 }
176
177 pub fn get_margin_method(&self, method_id: &str) -> Option<&MarginMethod> {
178 self.margin_methods.get(method_id)
179 }
180
181 pub fn list_margin_methods(&self) -> Vec<String> {
182 self.margin_methods.keys().cloned().collect()
183 }
184
185 pub fn margin_requirements(&self) -> &MarginRequirements {
186 &self.margin_requirements
187 }
188}