refactor(engine): retire abandoned architecture scaffolding (CL15)

This commit is contained in:
Boki 2026-10-26 19:16:51 -04:00
parent 7481d02c6c
commit bc2b8a89da
31 changed files with 76 additions and 3043 deletions

View file

@ -1,153 +1,37 @@
# Migration Guide: Modular Architecture
This guide explains how to migrate from the current monolithic structure to the new modular architecture.
## Overview
The new architecture introduces:
- Domain-driven design with separated domain types
- Event-driven architecture with EventBus
- Mode-based trading engines (Backtest, Paper, Live)
- Modular API structure
- Enhanced strategy framework
## Migration Steps
### Step 1: Use Adapters for Compatibility
The `adapters` module provides compatibility between old and new implementations:
#### ExecutionHandler Adapters
```rust
use engine::adapters::{ExecutionHandlerAdapter, NewExecutionHandler};
// Wrap a new-style handler to work with old interface
let new_handler = MyNewExecutionHandler::new();
let adapted = ExecutionHandlerAdapter::new(new_handler);
// Now 'adapted' implements the old ExecutionHandler trait
// Or wrap an old handler to work with new interface
use engine::adapters::LegacyExecutionHandlerAdapter;
let old_handler = MyOldExecutionHandler::new();
let adapted = LegacyExecutionHandlerAdapter::new(old_handler);
// Now 'adapted' implements NewExecutionHandler
```
#### Event System Adapters
```rust
use engine::adapters::events::{EventBusAdapter, EventAdapter};
// Create adapter that bridges old and new event systems
let mut adapter = EventBusAdapter::new();
// Use old-style event handling
adapter.subscribe_old("market_update", |event| {
// Handle event in old format
});
// Events are automatically converted between formats
```
#### Strategy Adapters
```rust
use engine::adapters::strategy::{NewToOldStrategyAdapter, OldToNewStrategyAdapter};
// Use new strategy with old system
let new_strategy = MyNewStrategy::new();
let context = Arc::new(StrategyContext::new());
let adapted = NewToOldStrategyAdapter::new(Box::new(new_strategy), context);
// Use old strategy with new system
let old_strategy = MyOldStrategy::new();
let adapted = OldToNewStrategyAdapter::new(Box::new(old_strategy));
```
### Step 2: Gradual Module Migration
1. **Start with Domain Types** (Low Risk)
- Move from inline types to `domain::market`, `domain::orders`, etc.
- These are mostly data structures with minimal behavior
2. **Migrate Event System** (Medium Risk)
- Replace direct callbacks with EventBus subscriptions
- Use EventBusAdapter during transition
3. **Update Strategy Framework** (Medium Risk)
- Migrate strategies one at a time using adapters
- Test thoroughly before removing adapters
4. **Implement Mode-Specific Engines** (High Risk)
- Start with backtest mode (most isolated)
- Move to paper trading
- Finally implement live trading
5. **API Migration** (Final Step)
- Run old and new APIs in parallel
- Gradually move endpoints to new structure
- Deprecate old API when stable
### Step 3: Remove Adapters
Once all components are migrated:
1. Remove adapter usage
2. Delete old implementations
3. Remove adapter modules
## Testing Strategy
1. **Unit Tests**: Test adapters ensure compatibility
2. **Integration Tests**: Verify old and new systems work together
3. **Regression Tests**: Ensure no functionality is lost
4. **Performance Tests**: Verify no performance degradation
## Common Issues and Solutions
### Issue: Trait method signatures don't match
**Solution**: Use adapters to bridge the differences
### Issue: Event types are incompatible
**Solution**: Use EventAdapter for conversion
### Issue: Existing code expects synchronous behavior
**Solution**: Use `tokio::runtime::Handle::current().block_on()` temporarily
### Issue: New modules not found
**Solution**: Ensure modules are properly declared in lib.rs
## Example Migration
Here's a complete example migrating a backtest:
```rust
// Old way
use engine::backtest::BacktestEngine as OldBacktest;
let old_engine = OldBacktest::new(config);
// During migration (using adapters)
use engine::adapters::ExecutionHandlerAdapter;
use engine::modes::backtest::BacktestEngine as NewBacktest;
let new_engine = NewBacktest::new(config);
let execution_handler = ExecutionHandlerAdapter::new(new_engine.get_execution_handler());
// After migration
use engine::modes::backtest::BacktestEngine;
let engine = BacktestEngine::new(config);
```
## Timeline
1. **Week 1-2**: Implement and test adapters
2. **Week 3-4**: Migrate domain types and events
3. **Week 5-6**: Migrate strategies and modes
4. **Week 7-8**: Migrate API and cleanup
## Support
For questions or issues during migration:
1. Check adapter documentation
2. Review test examples
3. Consult team lead for architectural decisions
# Canonical execution and retained compatibility
The earlier EventBus, mode-engine and strategy-framework migration described here was abandoned. Its uncompiled alternative modules and unused placeholder adapters have been removed. Do not use that design as an implementation guide.
Current research and paper execution share the authoritative Rust core in [src/financial](src/financial/mod.rs): replay → matching → risk → ledger. TypeScript owns strategy decisions, worker supervision and durable SQL custody. [api/replay.rs](src/api/replay.rs) exposes `FinancialReplaySession`; [api/paper.rs](src/api/paper.rs) exposes `stageFinancialTransaction`. Start with the [code-reading guide](../../../docs/guide/code-reading.md) to trace a complete decision.
## Supported boundaries
| Purpose | Code | Guidance |
| --- | --- | --- |
| Current financial execution | `src/financial/`, `src/api/replay.rs`, `src/api/paper.rs` | Use for canonical research and paper execution. Preserve deterministic ordering, financial invariants and recovery contracts. |
| Indicators and analytical calculations | `src/indicators/`, `src/analytics/`, native risk/orderbook analytics, `libs/calculations/` | Retained capabilities. Reuse reviewed implementations and their fixtures. |
| Legacy native execution and accounting | `TradingEngine`, native `BacktestEngine`, `src/core/`, `src/backtest/`, `src/positions/`, `src/risk/`, `src/strategies/` | Retained for compatibility, regression tests and existing algorithm examples. They are not an alternative execution path for current admitted research jobs. |
| Uncompiled reporting reference | [reference/legacy-reporting](reference/legacy-reporting/README.md) | Original calculation prototypes retained byte-for-byte for inspection. Incomplete and unreviewed; not a runtime API or an oracle for financial results. |
The compiled `src/backtest/simple_results.rs`, `src/strategies/mean_reversion_fixed.rs`, public native constructors, and historical regression fixtures are unchanged by this cleanup. No saved dataset, strategy package, result or checkpoint is migrated or removed.
## CL15 removal and preservation inventory
Evidence baseline: `700ee09c`. Cargo's library target is `src/lib.rs`; its module declarations, `src/backtest/mod.rs` and `src/strategies/mod.rs` define the compiled graph. Repository searches found no consumers of the removed adapters outside their own implementation and this former migration guide.
| Previous path under `src/` | Disposition and reason |
| --- | --- |
| `adapters/{mod,events,strategy}.rs` | Removed unused compiled migration scaffold. The execution adapter fabricated price-100 fills without invoking the wrapped handler; status queries fabricated `Filled`. Event and strategy adapters targeted placeholder interfaces. None were NAPI exports or used by repository tests/applications. |
| `lib_new.rs`, `lib_simplified.rs`, `test_lib.rs` | Removed undeclared alternative crate roots. Cargo has no target or source inclusion for them. |
| `api_new/{mod,backtest,market_data,orders,positions,strategies,system}.rs` | Removed undeclared API skeleton targeting the abandoned root/domain/mode design; many operations were `todo!`. Current `src/api/` is retained. |
| `domain.bak/{mod,market,orders,positions,events}.rs` | Removed undeclared duplicate domain types and elementary position helpers. Compiled legacy types/accounting remain in `src/lib.rs` and `src/positions/`; canonical equivalents remain in `src/financial/`. |
| `events.bak/mod.rs` | Removed undeclared event-bus scaffold. No current event or recovery journal uses it. |
| `modes.bak/{mod,backtest,paper,live}.rs` | Removed undeclared alternate runtime, mock broker and simplistic fill/accounting implementations. These depended on the abandoned event/domain design, not the canonical matcher or any supported broker. |
| `strategies/framework/mod.rs` | Removed undeclared generic strategy trait/context and no-op base strategy. It contained no concrete signal algorithm; all compiled strategies remain. |
| `backtest/metrics_calculator.rs`, `backtest/results.rs` | Moved unchanged to `reference/legacy-reporting/`. They were not declared in `backtest/mod.rs`. Retained because they contain statistical/reporting prototypes and supporting result types worth inspecting before any later reuse. |
The archived calculator includes return, drawdown, streak and symbol-analysis logic alongside placeholder fields and incomplete methods. Moving it does not make those formulas reviewed or compilable. New calculations should enter the reviewed calculation library with explicit semantics and tests, not reactivate the abandoned financial simulator.
## Verification
Run the Rust library tests and rebuild the addon from the same checkout before native binding, market-data and terminal-equity integration tests. Existing architecture checks must continue to prevent production TypeScript entrypoints from reaching prototype financial execution. This cleanup changes the source/module surface, not economic policies, native wire contracts or existing saved artifacts.

View file

@ -0,0 +1,7 @@
# Uncompiled legacy reporting reference
These two files were moved byte-for-byte from `src/backtest/metrics_calculator.rs` and `src/backtest/results.rs` at baseline `700ee09c`. They were already outside the Rust module graph; this directory is not compiled or exported.
`metrics_calculator.rs` retains return/volatility/risk-ratio, drawdown, streak and symbol-summary prototypes. `results.rs` retains their supporting report structures. They depend on obsolete types and contain unfinished calculations, fabricated placeholder zeros, and financial/statistical assumptions that have not passed the canonical research gates. They are reference material, not supported calculations or expected-result fixtures.
Use the reviewed [calculation library](../../../../../libs/calculations/README.md) and canonical [financial reporting](../../src/financial/reporting.rs) for current work. If a prototype is useful, port only that calculation with explicit units, sampling/alignment/null semantics and independent fixtures. Do not import this directory into the engine or revive its old result model.

View file

@ -1,162 +0,0 @@
// Placeholder types for new event system (will be replaced when domain module is activated)
#[derive(Debug, Clone)]
pub struct NewEvent {
pub timestamp: chrono::DateTime<chrono::Utc>,
pub source: String,
pub event_type: NewEventType,
}
#[derive(Debug, Clone)]
pub enum NewEventType {
MarketData {
symbol: String,
data: crate::MarketDataType,
},
OrderFilled {
order_id: String,
fill: crate::Fill,
},
OrderSubmitted {
order_id: String,
},
OrderCancelled {
order_id: String,
},
PositionUpdated {
symbol: String,
update: crate::PositionUpdate,
},
RiskLimitExceeded {
reason: String,
},
}
use std::collections::HashMap;
use tokio::sync::mpsc;
// Placeholder EventBus type (will be replaced when events module is activated)
pub struct NewEventBus {
sender: mpsc::UnboundedSender<NewEvent>,
}
impl NewEventBus {
pub async fn publish(&mut self, event: NewEvent) {
let _ = self.sender.send(event);
}
}
/// Maps between old and new event types
pub struct EventAdapter;
impl EventAdapter {
/// Convert from new event to old event format if needed
pub fn from_new_event(event: &NewEvent) -> Option<OldEventFormat> {
match &event.event_type {
NewEventType::MarketData { symbol, data } => {
Some(OldEventFormat::MarketUpdate {
symbol: symbol.clone(),
timestamp: event.timestamp,
data: data.clone(),
})
}
NewEventType::OrderFilled { order_id, fill } => {
Some(OldEventFormat::OrderFill {
order_id: order_id.clone(),
fill: fill.clone(),
})
}
_ => None, // Other event types may not have old equivalents
}
}
/// Convert from old event to new event format
pub fn to_new_event(old_event: &OldEventFormat) -> NewEvent {
match old_event {
OldEventFormat::MarketUpdate { symbol, timestamp, data } => {
NewEvent {
timestamp: *timestamp,
source: "legacy".to_string(),
event_type: NewEventType::MarketData {
symbol: symbol.clone(),
data: data.clone(),
},
}
}
OldEventFormat::OrderFill { order_id, fill } => {
NewEvent {
timestamp: fill.timestamp,
source: "legacy".to_string(),
event_type: NewEventType::OrderFilled {
order_id: order_id.clone(),
fill: fill.clone(),
},
}
}
}
}
}
/// Placeholder for old event format (to be defined based on actual old implementation)
#[derive(Debug, Clone)]
pub enum OldEventFormat {
MarketUpdate {
symbol: String,
timestamp: chrono::DateTime<chrono::Utc>,
data: crate::MarketDataType,
},
OrderFill {
order_id: String,
fill: crate::Fill,
},
}
/// Event bus adapter to bridge old and new event systems
pub struct EventBusAdapter {
new_event_bus: Option<NewEventBus>,
old_handlers: HashMap<String, Vec<Box<dyn Fn(&OldEventFormat) + Send + Sync>>>,
}
impl EventBusAdapter {
pub fn new() -> Self {
Self {
new_event_bus: None,
old_handlers: HashMap::new(),
}
}
pub fn with_new_event_bus(mut self, event_bus: NewEventBus) -> Self {
self.new_event_bus = Some(event_bus);
self
}
/// Subscribe to events using old-style handler
pub fn subscribe_old<F>(&mut self, event_type: &str, handler: F)
where
F: Fn(&OldEventFormat) + Send + Sync + 'static,
{
self.old_handlers
.entry(event_type.to_string())
.or_insert_with(Vec::new)
.push(Box::new(handler));
}
/// Publish event in old format (converts to new format if new bus available)
pub async fn publish_old(&mut self, event: OldEventFormat) {
// Call old handlers
let event_type = match &event {
OldEventFormat::MarketUpdate { .. } => "market_update",
OldEventFormat::OrderFill { .. } => "order_fill",
};
if let Some(handlers) = self.old_handlers.get(event_type) {
for handler in handlers {
handler(&event);
}
}
// Convert and publish to new event bus if available
if let Some(ref mut new_bus) = self.new_event_bus {
let new_event = EventAdapter::to_new_event(&event);
new_bus.publish(new_event).await;
}
}
}

View file

@ -1,100 +0,0 @@
pub mod events;
pub mod strategy;
use async_trait::async_trait;
use crate::{Order, ExecutionResult, OrderStatus, Fill, OrderBookSnapshot, FillSimulator};
use std::sync::Arc;
use parking_lot::RwLock;
/// Adapter to bridge between old and new ExecutionHandler implementations
pub struct ExecutionHandlerAdapter<T> {
inner: Arc<RwLock<T>>,
}
impl<T> ExecutionHandlerAdapter<T> {
pub fn new(handler: T) -> Self {
Self {
inner: Arc::new(RwLock::new(handler)),
}
}
}
/// New-style ExecutionHandler trait (from modular design)
#[async_trait]
pub trait NewExecutionHandler: Send + Sync {
async fn submit_order(&mut self, order: Order) -> Result<String, String>;
async fn cancel_order(&mut self, order_id: &str) -> Result<(), String>;
async fn get_order_status(&self, order_id: &str) -> Result<OrderStatus, String>;
}
/// Implement old ExecutionHandler for adapters wrapping new handlers
#[async_trait]
impl<T: NewExecutionHandler + 'static> crate::ExecutionHandler for ExecutionHandlerAdapter<T> {
async fn execute_order(&mut self, order: Order) -> Result<ExecutionResult, String> {
// For now, provide a simplified implementation
// In a real implementation, you'd need proper async handling
// Create a synthetic execution result
let fill = Fill {
timestamp: chrono::Utc::now(),
price: 100.0, // This would come from market data
quantity: order.quantity,
commission: 0.0,
};
Ok(ExecutionResult {
order_id: order.id,
status: OrderStatus::Filled,
fills: vec![fill],
})
}
fn get_fill_simulator(&self) -> Option<&dyn FillSimulator> {
None
}
}
/// Simplified sync wrapper for cases where async isn't needed
pub struct SyncExecutionHandlerAdapter<T> {
inner: T,
}
impl<T> SyncExecutionHandlerAdapter<T> {
pub fn new(handler: T) -> Self {
Self { inner: handler }
}
}
#[async_trait]
impl<T: crate::ExecutionHandler + Send + Sync> NewExecutionHandler for SyncExecutionHandlerAdapter<T> {
async fn submit_order(&mut self, order: Order) -> Result<String, String> {
let result = self.inner.execute_order(order).await?;
Ok(result.order_id)
}
async fn cancel_order(&mut self, _order_id: &str) -> Result<(), String> {
// Old interface doesn't support cancellation
Err("Cancellation not supported".to_string())
}
async fn get_order_status(&self, _order_id: &str) -> Result<OrderStatus, String> {
// Old interface doesn't support status queries
Ok(OrderStatus::Filled)
}
}
// Keep the original adapter logic but commented out for future reference
/*
#[async_trait]
impl<T: NewExecutionHandler> crate::ExecutionHandler for ExecutionHandlerAdapter<T> {
async fn execute_order(&mut self, order: Order) -> Result<ExecutionResult, String> {
// This would require tokio::spawn or similar to properly handle
// the async boundaries with parking_lot RwLock
todo!("Complex async adapter implementation")
}
fn get_fill_simulator(&self) -> Option<&dyn FillSimulator> {
None
}
}
*/

View file

@ -1,231 +0,0 @@
use async_trait::async_trait;
use crate::{MarketUpdate, Fill, OrderType};
use crate::backtest::Strategy as OldStrategy;
use crate::risk::RiskLimits;
use std::sync::Arc;
use parking_lot::RwLock;
// Placeholder types for new strategy framework (will be replaced when framework module is activated)
#[async_trait]
pub trait NewStrategy: Send + Sync {
async fn init(&mut self, context: &StrategyContext) -> Result<(), String>;
async fn on_data(&mut self, data: &MarketUpdate, context: &StrategyContext) -> Vec<Signal>;
async fn on_fill(&mut self, order_id: &str, fill: &Fill, context: &StrategyContext);
async fn shutdown(&mut self, context: &StrategyContext) -> Result<(), String>;
fn get_state(&self) -> serde_json::Value;
}
pub struct StrategyContext {
pub account_id: String,
pub starting_capital: f64,
}
impl StrategyContext {
pub fn new() -> Self {
Self {
account_id: "default".to_string(),
starting_capital: 100_000.0,
}
}
}
#[derive(Debug, Clone)]
pub enum Signal {
Buy {
symbol: String,
quantity: f64,
order_type: OrderType,
},
Sell {
symbol: String,
quantity: f64,
order_type: OrderType,
},
CancelOrder {
order_id: String,
},
UpdateRiskLimits {
limits: RiskLimits,
},
}
/// Adapter to use new strategies with old interface
pub struct NewToOldStrategyAdapter {
inner: Arc<RwLock<Box<dyn NewStrategy>>>,
context: Arc<StrategyContext>,
}
impl NewToOldStrategyAdapter {
pub fn new(strategy: Box<dyn NewStrategy>, context: Arc<StrategyContext>) -> Self {
Self {
inner: Arc::new(RwLock::new(strategy)),
context,
}
}
}
impl OldStrategy for NewToOldStrategyAdapter {
fn on_market_data(&mut self, data: &crate::MarketData) -> Vec<crate::backtest::strategy::Signal> {
// Convert MarketData to MarketUpdate if needed
let market_update = data.clone(); // Assuming MarketData is type alias for MarketUpdate
// Need to block on async call since old trait is sync
let signals = tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let mut strategy = self.inner.write();
strategy.on_data(&market_update, &self.context).await
})
});
// Convert new signals to old format
signals.into_iter().map(|signal| {
match signal {
Signal::Buy { symbol, quantity, .. } => {
crate::backtest::strategy::Signal {
symbol,
signal_type: crate::backtest::strategy::SignalType::Buy,
strength: 1.0,
quantity: Some(quantity),
reason: None,
metadata: None,
}
}
Signal::Sell { symbol, quantity, .. } => {
crate::backtest::strategy::Signal {
symbol,
signal_type: crate::backtest::strategy::SignalType::Sell,
strength: 1.0,
quantity: Some(quantity),
reason: None,
metadata: None,
}
}
Signal::CancelOrder { .. } => {
// Old strategy doesn't have cancel concept, skip
crate::backtest::strategy::Signal {
symbol: String::new(),
signal_type: crate::backtest::strategy::SignalType::Close,
strength: 0.0,
quantity: None,
reason: None,
metadata: None,
}
}
_ => {
// Skip other signal types
crate::backtest::strategy::Signal {
symbol: String::new(),
signal_type: crate::backtest::strategy::SignalType::Close,
strength: 0.0,
quantity: None,
reason: None,
metadata: None,
}
}
}
}).filter(|s| !s.symbol.is_empty()).collect()
}
fn on_fill(&mut self, symbol: &str, quantity: f64, price: f64, side: &str) {
// Create a Fill object from the parameters
let fill = Fill {
timestamp: chrono::Utc::now(),
price,
quantity,
commission: 0.0,
};
// Block on async call
tokio::task::block_in_place(|| {
tokio::runtime::Handle::current().block_on(async {
let mut strategy = self.inner.write();
strategy.on_fill(&format!("order_{}", symbol), &fill, &self.context).await;
})
});
}
fn get_name(&self) -> &str {
"NewStrategyAdapter"
}
fn get_parameters(&self) -> serde_json::Value {
serde_json::json!({
"adapter": "NewToOldStrategyAdapter",
"context": {
"account_id": self.context.account_id,
"starting_capital": self.context.starting_capital,
}
})
}
}
/// Adapter to use old strategies with new interface
pub struct OldToNewStrategyAdapter {
inner: Box<dyn OldStrategy>,
}
impl OldToNewStrategyAdapter {
pub fn new(strategy: Box<dyn OldStrategy>) -> Self {
Self { inner: strategy }
}
}
#[async_trait]
impl NewStrategy for OldToNewStrategyAdapter {
async fn init(&mut self, _context: &StrategyContext) -> Result<(), String> {
// Old strategy doesn't have init
Ok(())
}
async fn on_data(&mut self, data: &MarketUpdate, _context: &StrategyContext) -> Vec<Signal> {
// Call sync method from async context
let signals = self.inner.on_market_data(data);
// Convert old signals to new format
signals.into_iter().filter_map(|signal| {
match signal.signal_type {
crate::backtest::strategy::SignalType::Buy => {
Some(Signal::Buy {
symbol: signal.symbol,
quantity: signal.quantity.unwrap_or(100.0),
order_type: OrderType::Market,
})
}
crate::backtest::strategy::SignalType::Sell => {
Some(Signal::Sell {
symbol: signal.symbol,
quantity: signal.quantity.unwrap_or(100.0),
order_type: OrderType::Market,
})
}
crate::backtest::strategy::SignalType::Close => {
// Could map to cancel, but for now skip
None
}
}
}).collect()
}
async fn on_fill(&mut self, _order_id: &str, fill: &Fill, _context: &StrategyContext) {
// Extract symbol from order_id if possible, otherwise use placeholder
let symbol = "UNKNOWN";
let side = "buy"; // Would need to track this
self.inner.on_fill(symbol, fill.quantity, fill.price, side);
}
async fn shutdown(&mut self, _context: &StrategyContext) -> Result<(), String> {
// Old strategy doesn't have shutdown
Ok(())
}
fn get_state(&self) -> serde_json::Value {
serde_json::json!({
"adapter": "OldToNewStrategyAdapter",
"inner_strategy": self.inner.get_name(),
"parameters": self.inner.get_parameters()
})
}
}

View file

@ -1,86 +0,0 @@
use napi_derive::napi;
use napi::bindgen_prelude::*;
use std::sync::Arc;
use chrono::{DateTime, Utc};
#[napi]
pub struct BacktestAPI {
core: Arc<crate::TradingCore>,
}
impl BacktestAPI {
pub fn new(core: Arc<crate::TradingCore>) -> Self {
Self { core }
}
}
#[napi]
impl BacktestAPI {
#[napi]
pub async fn configure(
&self,
start_date: String,
end_date: String,
symbols: Vec<String>,
initial_capital: f64,
commission: f64,
slippage: f64,
) -> Result<()> {
// Parse dates
let start = DateTime::parse_from_rfc3339(&start_date)
.map_err(|e| Error::from_reason(format!("Invalid start date: {}", e)))?
.with_timezone(&Utc);
let end = DateTime::parse_from_rfc3339(&end_date)
.map_err(|e| Error::from_reason(format!("Invalid end date: {}", e)))?
.with_timezone(&Utc);
// Configure backtest parameters
if let crate::TradingMode::Backtest { .. } = self.core.get_mode() {
// Update backtest configuration
todo!("Update backtest configuration")
} else {
return Err(Error::from_reason("Not in backtest mode"));
}
}
#[napi]
pub async fn load_data(&self, data_source: String) -> Result<()> {
// Load historical data for backtest
todo!("Load historical data")
}
#[napi]
pub async fn run(&self) -> Result<JsObject> {
// Run the backtest
if let crate::TradingMode::Backtest { .. } = self.core.get_mode() {
// Execute backtest
todo!("Execute backtest")
} else {
return Err(Error::from_reason("Not in backtest mode"));
}
}
#[napi]
pub fn get_progress(&self) -> Result<f64> {
// Get backtest progress (0.0 to 1.0)
todo!("Get backtest progress")
}
#[napi]
pub fn pause(&self) -> Result<()> {
// Pause backtest execution
todo!("Pause backtest")
}
#[napi]
pub fn resume(&self) -> Result<()> {
// Resume backtest execution
todo!("Resume backtest")
}
#[napi]
pub fn get_results(&self) -> Result<JsObject> {
// Get backtest results
todo!("Get backtest results")
}
}

View file

@ -1,51 +0,0 @@
use napi_derive::napi;
use napi::bindgen_prelude::*;
use std::sync::Arc;
#[napi]
pub struct MarketDataAPI {
core: Arc<crate::TradingCore>,
}
impl MarketDataAPI {
pub fn new(core: Arc<crate::TradingCore>) -> Self {
Self { core }
}
}
#[napi]
impl MarketDataAPI {
#[napi]
pub async fn subscribe(&self, symbols: Vec<String>) -> Result<()> {
// Subscribe to market data for symbols
self.core.subscribe_market_data(symbols)
.await
.map_err(|e| Error::from_reason(e))
}
#[napi]
pub async fn unsubscribe(&self, symbols: Vec<String>) -> Result<()> {
// Unsubscribe from market data
self.core.unsubscribe_market_data(symbols)
.await
.map_err(|e| Error::from_reason(e))
}
#[napi]
pub fn get_latest_quote(&self, symbol: String) -> Result<JsObject> {
// Get latest quote for symbol
let quote = self.core.orderbook_manager
.get_best_bid_ask(&symbol)
.ok_or_else(|| Error::from_reason("No quote available"))?;
// Convert to JS object
// Note: In real implementation, would properly convert Quote to JsObject
todo!("Convert quote to JsObject")
}
#[napi]
pub fn get_latest_bar(&self, symbol: String) -> Result<JsObject> {
// Get latest bar for symbol
todo!("Get latest bar implementation")
}
}

View file

@ -1,76 +0,0 @@
use napi_derive::napi;
pub mod market_data;
pub mod orders;
pub mod positions;
pub mod backtest;
pub mod strategies;
pub mod system;
// Main API entry point
#[napi]
pub struct TradingAPI {
inner: std::sync::Arc<crate::TradingCore>,
}
#[napi]
impl TradingAPI {
#[napi(constructor)]
pub fn new(mode: String) -> napi::Result<Self> {
let trading_mode = parse_mode(&mode)?;
let core = crate::TradingCore::new(trading_mode)
.map_err(|e| napi::Error::from_reason(e))?;
Ok(Self {
inner: std::sync::Arc::new(core),
})
}
#[napi]
pub fn market_data(&self) -> market_data::MarketDataAPI {
market_data::MarketDataAPI::new(self.inner.clone())
}
#[napi]
pub fn orders(&self) -> orders::OrdersAPI {
orders::OrdersAPI::new(self.inner.clone())
}
#[napi]
pub fn positions(&self) -> positions::PositionsAPI {
positions::PositionsAPI::new(self.inner.clone())
}
#[napi]
pub fn strategies(&self) -> strategies::StrategiesAPI {
strategies::StrategiesAPI::new(self.inner.clone())
}
#[napi]
pub fn system(&self) -> system::SystemAPI {
system::SystemAPI::new(self.inner.clone())
}
#[napi]
pub fn backtest(&self) -> backtest::BacktestAPI {
backtest::BacktestAPI::new(self.inner.clone())
}
}
fn parse_mode(mode: &str) -> napi::Result<crate::TradingMode> {
match mode {
"backtest" => Ok(crate::TradingMode::Backtest {
start_time: chrono::Utc::now(),
end_time: chrono::Utc::now(),
speed_multiplier: 1.0,
}),
"paper" => Ok(crate::TradingMode::Paper {
starting_capital: 100_000.0,
}),
"live" => Ok(crate::TradingMode::Live {
broker: "default".to_string(),
account_id: "default".to_string(),
}),
_ => Err(napi::Error::from_reason(format!("Unknown mode: {}", mode))),
}
}

View file

@ -1,87 +0,0 @@
use napi_derive::napi;
use napi::bindgen_prelude::*;
use std::sync::Arc;
#[napi]
pub struct OrdersAPI {
core: Arc<crate::TradingCore>,
}
impl OrdersAPI {
pub fn new(core: Arc<crate::TradingCore>) -> Self {
Self { core }
}
}
#[napi]
impl OrdersAPI {
#[napi]
pub async fn submit_order(
&self,
symbol: String,
side: String,
quantity: f64,
order_type: String,
limit_price: Option<f64>,
stop_price: Option<f64>,
) -> Result<String> {
let side = match side.as_str() {
"buy" => crate::Side::Buy,
"sell" => crate::Side::Sell,
_ => return Err(Error::from_reason("Invalid side")),
};
let order_type = match order_type.as_str() {
"market" => crate::OrderType::Market,
"limit" => {
let price = limit_price.ok_or_else(|| Error::from_reason("Limit price required"))?;
crate::OrderType::Limit { price }
}
"stop" => {
let price = stop_price.ok_or_else(|| Error::from_reason("Stop price required"))?;
crate::OrderType::Stop { stop_price: price }
}
"stop_limit" => {
let stop = stop_price.ok_or_else(|| Error::from_reason("Stop price required"))?;
let limit = limit_price.ok_or_else(|| Error::from_reason("Limit price required"))?;
crate::OrderType::StopLimit { stop_price: stop, limit_price: limit }
}
_ => return Err(Error::from_reason("Invalid order type")),
};
let order = crate::Order {
id: uuid::Uuid::new_v4().to_string(),
symbol,
side,
quantity,
order_type,
time_in_force: crate::TimeInForce::Day,
};
let result = self.core.execution_handler
.write()
.execute_order(order)
.await
.map_err(|e| Error::from_reason(e))?;
Ok(result.order_id)
}
#[napi]
pub async fn cancel_order(&self, order_id: String) -> Result<()> {
// Cancel order implementation
todo!("Cancel order implementation")
}
#[napi]
pub fn get_pending_orders(&self) -> Result<Vec<JsObject>> {
// Get pending orders
todo!("Get pending orders implementation")
}
#[napi]
pub fn get_order_history(&self) -> Result<Vec<JsObject>> {
// Get order history
todo!("Get order history implementation")
}
}

View file

@ -1,67 +0,0 @@
use napi_derive::napi;
use napi::bindgen_prelude::*;
use std::sync::Arc;
#[napi]
pub struct PositionsAPI {
core: Arc<crate::TradingCore>,
}
impl PositionsAPI {
pub fn new(core: Arc<crate::TradingCore>) -> Self {
Self { core }
}
}
#[napi]
impl PositionsAPI {
#[napi]
pub fn get_position(&self, symbol: String) -> Result<JsObject> {
let position = self.core.position_tracker
.get_position(&symbol)
.ok_or_else(|| Error::from_reason("No position found"))?;
// Convert position to JsObject
todo!("Convert position to JsObject")
}
#[napi]
pub fn get_all_positions(&self) -> Result<Vec<JsObject>> {
let positions = self.core.position_tracker.get_all_positions();
// Convert positions to JsObjects
todo!("Convert positions to JsObjects")
}
#[napi]
pub fn get_closed_trades(&self) -> Result<Vec<JsObject>> {
let trades = self.core.position_tracker.get_closed_trades();
// Convert trades to JsObjects
todo!("Convert trades to JsObjects")
}
#[napi]
pub fn get_pnl(&self, symbol: Option<String>) -> Result<f64> {
if let Some(sym) = symbol {
// Get P&L for specific symbol
let position = self.core.position_tracker
.get_position(&sym)
.ok_or_else(|| Error::from_reason("No position found"))?;
Ok(position.realized_pnl + position.unrealized_pnl)
} else {
// Get total P&L
let positions = self.core.position_tracker.get_all_positions();
let total_pnl = positions.into_iter()
.map(|p| p.realized_pnl + p.unrealized_pnl)
.sum();
Ok(total_pnl)
}
}
#[napi]
pub fn get_portfolio_value(&self) -> Result<f64> {
// Calculate total portfolio value
todo!("Calculate portfolio value")
}
}

View file

@ -1,76 +0,0 @@
use napi_derive::napi;
use napi::bindgen_prelude::*;
use std::sync::Arc;
#[napi]
pub struct StrategiesAPI {
core: Arc<crate::TradingCore>,
}
impl StrategiesAPI {
pub fn new(core: Arc<crate::TradingCore>) -> Self {
Self { core }
}
}
#[napi]
impl StrategiesAPI {
#[napi]
pub async fn add_strategy(
&self,
name: String,
strategy_type: String,
parameters: String,
) -> Result<String> {
// Parse parameters from JSON string
let params: serde_json::Value = serde_json::from_str(&parameters)
.map_err(|e| Error::from_reason(format!("Invalid parameters: {}", e)))?;
// Create strategy based on type
let strategy = match strategy_type.as_str() {
"sma_crossover" => {
// Create SMA crossover strategy
todo!("Create SMA strategy")
}
"momentum" => {
// Create momentum strategy
todo!("Create momentum strategy")
}
_ => return Err(Error::from_reason("Unknown strategy type")),
};
// Add strategy to core
todo!("Add strategy to core")
}
#[napi]
pub async fn remove_strategy(&self, strategy_id: String) -> Result<()> {
// Remove strategy
todo!("Remove strategy implementation")
}
#[napi]
pub fn get_strategies(&self) -> Result<Vec<JsObject>> {
// Get all active strategies
todo!("Get strategies implementation")
}
#[napi]
pub async fn update_strategy_parameters(
&self,
strategy_id: String,
parameters: String,
) -> Result<()> {
// Parse and update parameters
let params: serde_json::Value = serde_json::from_str(&parameters)
.map_err(|e| Error::from_reason(format!("Invalid parameters: {}", e)))?;
todo!("Update strategy parameters")
}
#[napi]
pub fn get_strategy_performance(&self, strategy_id: String) -> Result<JsObject> {
// Get performance metrics for strategy
todo!("Get strategy performance")
}
}

View file

@ -1,77 +0,0 @@
use napi_derive::napi;
use napi::bindgen_prelude::*;
use std::sync::Arc;
#[napi]
pub struct SystemAPI {
core: Arc<crate::TradingCore>,
}
impl SystemAPI {
pub fn new(core: Arc<crate::TradingCore>) -> Self {
Self { core }
}
}
#[napi]
impl SystemAPI {
#[napi]
pub async fn start(&self) -> Result<()> {
// Start the trading system
match self.core.get_mode() {
crate::TradingMode::Backtest { .. } => {
// Start backtest processing
todo!("Start backtest")
}
crate::TradingMode::Paper { .. } => {
// Start paper trading
todo!("Start paper trading")
}
crate::TradingMode::Live { .. } => {
// Start live trading
todo!("Start live trading")
}
}
}
#[napi]
pub async fn stop(&self) -> Result<()> {
// Stop the trading system
todo!("Stop trading system")
}
#[napi]
pub fn get_mode(&self) -> String {
match self.core.get_mode() {
crate::TradingMode::Backtest { .. } => "backtest".to_string(),
crate::TradingMode::Paper { .. } => "paper".to_string(),
crate::TradingMode::Live { .. } => "live".to_string(),
}
}
#[napi]
pub fn get_current_time(&self) -> String {
self.core.get_time().to_rfc3339()
}
#[napi]
pub fn set_risk_limits(&self, limits: String) -> Result<()> {
// Parse and set risk limits
let limits: serde_json::Value = serde_json::from_str(&limits)
.map_err(|e| Error::from_reason(format!("Invalid limits: {}", e)))?;
todo!("Set risk limits")
}
#[napi]
pub fn get_risk_metrics(&self) -> Result<JsObject> {
// Get current risk metrics
todo!("Get risk metrics")
}
#[napi]
pub fn get_analytics(&self) -> Result<JsObject> {
// Get trading analytics
todo!("Get analytics")
}
}

View file

@ -1,65 +0,0 @@
use chrono::{DateTime, Utc};
use serde::{Serialize, Deserialize};
use super::{Order, Fill, MarketUpdate, Position};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum EventType {
MarketData(MarketUpdate),
OrderSubmitted(Order),
OrderFilled { order_id: String, fill: Fill },
OrderCancelled { order_id: String },
OrderRejected { order_id: String, reason: String },
PositionUpdate(Position),
RiskAlert { message: String, severity: RiskSeverity },
SystemStatus { status: SystemStatus },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum RiskSeverity {
Info,
Warning,
Critical,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SystemStatus {
Starting,
Running,
Paused,
Stopping,
Error(String),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Event {
pub id: String,
pub timestamp: DateTime<Utc>,
pub event_type: EventType,
}
impl Event {
pub fn new(event_type: EventType) -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
timestamp: Utc::now(),
event_type,
}
}
pub fn market_data(data: MarketUpdate) -> Self {
Self::new(EventType::MarketData(data))
}
pub fn order_submitted(order: Order) -> Self {
Self::new(EventType::OrderSubmitted(order))
}
pub fn order_filled(order_id: String, fill: Fill) -> Self {
Self::new(EventType::OrderFilled { order_id, fill })
}
}
pub trait EventHandler: Send + Sync {
fn handle_event(&self, event: &Event);
fn event_types(&self) -> Vec<String>; // Which event types this handler is interested in
}

View file

@ -1,44 +0,0 @@
use chrono::{DateTime, Utc};
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Quote {
pub bid: f64,
pub ask: f64,
pub bid_size: f64,
pub ask_size: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bar {
pub open: f64,
pub high: f64,
pub low: f64,
pub close: f64,
pub volume: f64,
pub vwap: Option<f64>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Trade {
pub price: f64,
pub size: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum MarketDataType {
Quote(Quote),
Bar(Bar),
Trade(Trade),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketUpdate {
pub symbol: String,
pub timestamp: DateTime<Utc>,
pub data: MarketDataType,
}
// Type alias for compatibility
pub type MarketData = MarketUpdate;

View file

@ -1,10 +0,0 @@
pub mod market;
pub mod orders;
pub mod positions;
pub mod events;
// Re-export commonly used types
pub use market::{Quote, Bar, Trade, MarketUpdate, MarketDataType};
pub use orders::{Order, OrderType, OrderStatus, TimeInForce, Side, Fill};
pub use positions::{Position, PositionUpdate};
pub use events::{Event, EventType, EventHandler};

View file

@ -1,104 +0,0 @@
use chrono::{DateTime, Utc};
use serde::{Serialize, Deserialize};
use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Side {
Buy,
Sell,
}
impl fmt::Display for Side {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Side::Buy => write!(f, "buy"),
Side::Sell => write!(f, "sell"),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OrderType {
Market,
Limit,
Stop,
StopLimit,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum OrderStatus {
Pending,
Submitted,
PartiallyFilled,
Filled,
Cancelled,
Rejected,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum TimeInForce {
Day,
GTC, // Good Till Cancelled
IOC, // Immediate or Cancel
FOK, // Fill or Kill
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Order {
pub id: String,
pub symbol: String,
pub side: Side,
pub quantity: f64,
pub order_type: OrderType,
pub limit_price: Option<f64>,
pub stop_price: Option<f64>,
pub time_in_force: TimeInForce,
pub status: OrderStatus,
pub submitted_at: Option<DateTime<Utc>>,
pub filled_quantity: f64,
pub average_fill_price: Option<f64>,
}
impl Order {
pub fn new_market_order(symbol: String, side: Side, quantity: f64) -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
symbol,
side,
quantity,
order_type: OrderType::Market,
limit_price: None,
stop_price: None,
time_in_force: TimeInForce::Day,
status: OrderStatus::Pending,
submitted_at: None,
filled_quantity: 0.0,
average_fill_price: None,
}
}
pub fn new_limit_order(symbol: String, side: Side, quantity: f64, limit_price: f64) -> Self {
Self {
id: uuid::Uuid::new_v4().to_string(),
symbol,
side,
quantity,
order_type: OrderType::Limit,
limit_price: Some(limit_price),
stop_price: None,
time_in_force: TimeInForce::Day,
status: OrderStatus::Pending,
submitted_at: None,
filled_quantity: 0.0,
average_fill_price: None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Fill {
pub timestamp: DateTime<Utc>,
pub price: f64,
pub quantity: f64,
pub commission: f64,
}

View file

@ -1,59 +0,0 @@
use chrono::{DateTime, Utc};
use serde::{Serialize, Deserialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Position {
pub symbol: String,
pub quantity: f64,
pub average_price: f64,
pub realized_pnl: f64,
pub unrealized_pnl: f64,
pub last_update: DateTime<Utc>,
}
impl Position {
pub fn new(symbol: String) -> Self {
Self {
symbol,
quantity: 0.0,
average_price: 0.0,
realized_pnl: 0.0,
unrealized_pnl: 0.0,
last_update: Utc::now(),
}
}
pub fn is_long(&self) -> bool {
self.quantity > 0.0
}
pub fn is_short(&self) -> bool {
self.quantity < 0.0
}
pub fn is_flat(&self) -> bool {
self.quantity.abs() < f64::EPSILON
}
pub fn market_value(&self, current_price: f64) -> f64 {
self.quantity * current_price
}
pub fn calculate_unrealized_pnl(&self, current_price: f64) -> f64 {
if self.is_flat() {
0.0
} else if self.is_long() {
(current_price - self.average_price) * self.quantity
} else {
(self.average_price - current_price) * self.quantity.abs()
}
}
}
#[derive(Debug, Clone)]
pub struct PositionUpdate {
pub symbol: String,
pub previous_position: Option<Position>,
pub resulting_position: Position,
pub realized_pnl: f64,
}

View file

@ -1,91 +0,0 @@
use std::sync::Arc;
use parking_lot::RwLock;
use std::collections::HashMap;
use crate::domain::{Event, EventHandler};
use tokio::sync::mpsc;
pub struct EventBus {
handlers: Arc<RwLock<HashMap<String, Vec<Arc<dyn EventHandler>>>>>,
sender: mpsc::UnboundedSender<Event>,
}
impl EventBus {
pub fn new() -> (Self, mpsc::UnboundedReceiver<Event>) {
let (sender, receiver) = mpsc::unbounded_channel();
let bus = Self {
handlers: Arc::new(RwLock::new(HashMap::new())),
sender,
};
(bus, receiver)
}
pub fn subscribe(&self, event_type: String, handler: Arc<dyn EventHandler>) {
let mut handlers = self.handlers.write();
handlers.entry(event_type).or_insert_with(Vec::new).push(handler);
}
pub fn publish(&self, event: Event) -> Result<(), String> {
// Send to async handler
self.sender.send(event.clone())
.map_err(|_| "Failed to send event".to_string())?;
// Also handle synchronously for immediate handlers
let event_type = match &event.event_type {
crate::domain::EventType::MarketData(_) => "market_data",
crate::domain::EventType::OrderSubmitted(_) => "order_submitted",
crate::domain::EventType::OrderFilled { .. } => "order_filled",
crate::domain::EventType::OrderCancelled { .. } => "order_cancelled",
crate::domain::EventType::OrderRejected { .. } => "order_rejected",
crate::domain::EventType::PositionUpdate(_) => "position_update",
crate::domain::EventType::RiskAlert { .. } => "risk_alert",
crate::domain::EventType::SystemStatus { .. } => "system_status",
};
let handlers = self.handlers.read();
if let Some(event_handlers) = handlers.get(event_type) {
for handler in event_handlers {
handler.handle_event(&event);
}
}
Ok(())
}
}
// Simple event processor that runs in the background
pub struct EventProcessor {
receiver: mpsc::UnboundedReceiver<Event>,
handlers: Arc<RwLock<HashMap<String, Vec<Arc<dyn EventHandler>>>>>,
}
impl EventProcessor {
pub fn new(
receiver: mpsc::UnboundedReceiver<Event>,
handlers: Arc<RwLock<HashMap<String, Vec<Arc<dyn EventHandler>>>>>,
) -> Self {
Self { receiver, handlers }
}
pub async fn run(mut self) {
while let Some(event) = self.receiver.recv().await {
// Process event asynchronously
let event_type = match &event.event_type {
crate::domain::EventType::MarketData(_) => "market_data",
crate::domain::EventType::OrderSubmitted(_) => "order_submitted",
crate::domain::EventType::OrderFilled { .. } => "order_filled",
crate::domain::EventType::OrderCancelled { .. } => "order_cancelled",
crate::domain::EventType::OrderRejected { .. } => "order_rejected",
crate::domain::EventType::PositionUpdate(_) => "position_update",
crate::domain::EventType::RiskAlert { .. } => "risk_alert",
crate::domain::EventType::SystemStatus { .. } => "system_status",
};
let handlers = self.handlers.read();
if let Some(event_handlers) = handlers.get(event_type) {
for handler in event_handlers {
handler.handle_event(&event);
}
}
}
}
}

View file

@ -1,8 +1,8 @@
#![deny(clippy::all)]
// Existing modules
// Canonical financial execution lives in financial/. The other modules retain
// reviewed calculations and legacy API compatibility; see ../MIGRATION.md.
pub mod core;
pub mod adapters;
pub mod orderbook;
pub mod risk;
pub mod positions;

View file

@ -1,67 +0,0 @@
#![deny(clippy::all)]
// Domain modules
pub mod domain;
pub mod events;
pub mod modes;
// Core functionality modules
pub mod core;
pub mod orderbook;
pub mod risk;
pub mod positions;
pub mod analytics;
pub mod indicators;
pub mod backtest;
pub mod strategies;
// API layer
pub mod api;
// Re-export commonly used types from domain
pub use domain::{
// Market types
Quote, Bar, Trade, MarketUpdate, MarketDataType,
// Order types
Order, OrderType, OrderStatus, TimeInForce, Side, Fill,
// Position types
Position, PositionUpdate,
// Event types
Event, EventType, EventHandler,
};
// Re-export mode types
pub use modes::{TradingMode, TradingEngine};
// Re-export other commonly used types
pub use positions::{PositionTracker, TradeRecord, ClosedTrade};
pub use risk::{RiskLimits, RiskCheckResult, RiskMetrics};
// Core traits that define the system's abstractions
use chrono::{DateTime, Utc};
use std::sync::Arc;
use async_trait::async_trait;
#[async_trait]
pub trait MarketDataSource: Send + Sync {
async fn get_next_update(&mut self) -> Option<MarketUpdate>;
fn seek_to_time(&mut self, timestamp: DateTime<Utc>) -> Result<(), String>;
fn as_any(&self) -> &dyn std::any::Any;
fn as_any_mut(&mut self) -> &mut dyn std::any::Any;
}
#[async_trait]
pub trait ExecutionHandler: Send + Sync {
async fn submit_order(&self, order: &Order) -> Result<String, String>;
async fn cancel_order(&self, order_id: &str) -> Result<(), String>;
async fn get_order_status(&self, order_id: &str) -> Result<OrderStatus, String>;
}
pub trait TimeProvider: Send + Sync {
fn now(&self) -> DateTime<Utc>;
fn as_any(&self) -> &dyn std::any::Any;
}
pub trait FillSimulator: Send + Sync {
fn simulate_fill(&self, order: &Order, market_price: f64) -> Option<Fill>;
}

View file

@ -1,123 +0,0 @@
#![deny(clippy::all)]
// Domain modules - core types and abstractions
pub mod domain;
pub mod events;
pub mod modes;
pub mod strategies;
// Core functionality modules
pub mod core;
pub mod orderbook;
pub mod risk;
pub mod positions;
pub mod analytics;
pub mod indicators;
pub mod backtest;
// API layer
pub mod api;
// Re-export all domain types
pub use domain::*;
// Re-export event system
pub use events::{Event, EventType, EventBus, EventHandler};
// Re-export mode types and engine trait
pub use modes::{TradingMode, TradingEngine};
// Re-export strategy framework
pub use strategies::framework::{Strategy, StrategyContext, Signal, SignalAction};
// Re-export position tracking
pub use positions::{PositionTracker, TradeRecord, ClosedTrade};
// Re-export risk management
pub use risk::{RiskLimits, RiskCheckResult, RiskMetrics, RiskEngine};
// Re-export analytics
pub use analytics::{AnalyticsEngine, PerformanceMetrics};
// Re-export indicators
pub use indicators::{Indicator, IndicatorSet};
// Re-export backtest types
pub use backtest::{BacktestEngine, BacktestResults};
// Re-export API
pub use api::TradingAPI;
// Core system traits
use async_trait::async_trait;
use chrono::{DateTime, Utc};
/// Source of market data - can be historical files, live feed, or simulated
#[async_trait]
pub trait MarketDataSource: Send + Sync {
async fn get_next_update(&mut self) -> Option<MarketUpdate>;
fn seek_to_time(&mut self, timestamp: DateTime<Utc>) -> Result<(), String>;
}
/// Handles order execution - can be simulated, paper, or live broker
#[async_trait]
pub trait ExecutionHandler: Send + Sync {
async fn submit_order(&self, order: &Order) -> Result<String, String>;
async fn cancel_order(&self, order_id: &str) -> Result<(), String>;
async fn get_order_status(&self, order_id: &str) -> Result<OrderStatus, String>;
}
/// Provides current time - can be simulated time for backtest or real time
pub trait TimeProvider: Send + Sync {
fn now(&self) -> DateTime<Utc>;
fn sleep_until(&self, target: DateTime<Utc>) -> Result<(), String>;
}
/// Simulates order fills for backtest/paper trading
pub trait FillSimulator: Send + Sync {
fn simulate_fill(&self, order: &Order, market_price: f64, spread: f64) -> Option<Fill>;
}
/// Main trading system that coordinates all components
pub struct TradingCore {
mode: TradingMode,
engine: Box<dyn TradingEngine>,
event_bus: EventBus,
orderbook_manager: OrderBookManager,
position_tracker: PositionTracker,
risk_engine: RiskEngine,
analytics_engine: AnalyticsEngine,
}
impl TradingCore {
/// Create a new trading system for the specified mode
pub fn new(mode: TradingMode) -> Result<Self, String> {
let engine = modes::create_engine_for_mode(&mode)?;
let event_bus = EventBus::new();
Ok(Self {
mode,
engine,
event_bus,
orderbook_manager: OrderBookManager::new(),
position_tracker: PositionTracker::new(),
risk_engine: RiskEngine::new(),
analytics_engine: AnalyticsEngine::new(),
})
}
/// Start the trading system
pub async fn start(&mut self) -> Result<(), String> {
self.engine.start(&mut self.event_bus).await
}
/// Stop the trading system
pub async fn stop(&mut self) -> Result<(), String> {
self.engine.stop().await
}
/// Get current trading mode
pub fn mode(&self) -> &TradingMode {
&self.mode
}
}

View file

@ -1,291 +0,0 @@
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use std::sync::Arc;
use parking_lot::RwLock;
use crate::{
MarketDataSource, ExecutionHandler, TimeProvider, FillSimulator,
MarketUpdate, Order, OrderStatus, Fill, OrderBookSnapshot,
};
use crate::events::EventBus;
use crate::domain::{Event, EventType};
use super::TradingEngine;
/// Backtest-specific trading engine
pub struct BacktestEngine {
start_time: DateTime<Utc>,
end_time: DateTime<Utc>,
current_time: DateTime<Utc>,
speed_multiplier: f64,
market_data: Box<dyn MarketDataSource>,
execution: BacktestExecutor,
is_running: Arc<RwLock<bool>>,
}
impl BacktestEngine {
pub fn new(
start_time: DateTime<Utc>,
end_time: DateTime<Utc>,
speed_multiplier: f64,
market_data: Box<dyn MarketDataSource>,
) -> Self {
Self {
start_time,
end_time,
current_time: start_time,
speed_multiplier,
market_data,
execution: BacktestExecutor::new(),
is_running: Arc::new(RwLock::new(false)),
}
}
}
#[async_trait]
impl TradingEngine for BacktestEngine {
async fn start(&mut self, event_bus: &mut EventBus) -> Result<(), String> {
*self.is_running.write() = true;
self.current_time = self.start_time;
// Seek market data to start time
self.market_data.seek_to_time(self.start_time)?;
// Emit start event
event_bus.publish(Event {
event_type: EventType::SystemStart,
timestamp: self.current_time,
data: serde_json::json!({
"mode": "backtest",
"start_time": self.start_time,
"end_time": self.end_time,
}),
}).await;
// Main backtest loop
while *self.is_running.read() && self.current_time < self.end_time {
// Get next market update
if let Some(update) = self.market_data.get_next_update().await {
// Update current time
self.current_time = update.timestamp;
// Publish market data event
event_bus.publish(Event {
event_type: EventType::MarketData(update.clone()),
timestamp: self.current_time,
data: serde_json::Value::Null,
}).await;
// Process any pending orders
self.execution.process_orders(&update, event_bus).await?;
} else {
// No more data
break;
}
// Simulate time passing (for UI updates, etc.)
if self.speed_multiplier > 0.0 {
tokio::time::sleep(std::time::Duration::from_millis(
(10.0 / self.speed_multiplier) as u64
)).await;
}
}
// Emit stop event
event_bus.publish(Event {
event_type: EventType::SystemStop,
timestamp: self.current_time,
data: serde_json::json!({
"reason": "backtest_complete",
"end_time": self.current_time,
}),
}).await;
Ok(())
}
async fn stop(&mut self) -> Result<(), String> {
*self.is_running.write() = false;
Ok(())
}
fn get_execution_handler(&self) -> Arc<dyn ExecutionHandler> {
Arc::new(self.execution.clone())
}
fn get_time_provider(&self) -> Arc<dyn TimeProvider> {
Arc::new(BacktestTimeProvider {
current_time: self.current_time,
})
}
}
/// Backtest order executor
#[derive(Clone)]
struct BacktestExecutor {
pending_orders: Arc<RwLock<Vec<Order>>>,
order_history: Arc<RwLock<Vec<(Order, OrderStatus)>>>,
fill_simulator: BacktestFillSimulator,
}
impl BacktestExecutor {
fn new() -> Self {
Self {
pending_orders: Arc::new(RwLock::new(Vec::new())),
order_history: Arc::new(RwLock::new(Vec::new())),
fill_simulator: BacktestFillSimulator::new(),
}
}
async fn process_orders(
&self,
market_update: &MarketUpdate,
event_bus: &mut EventBus,
) -> Result<(), String> {
let mut orders = self.pending_orders.write();
let mut filled_indices = Vec::new();
for (idx, order) in orders.iter().enumerate() {
if order.symbol != market_update.symbol {
continue;
}
// Try to fill the order
if let Some(fill) = self.fill_simulator.try_fill(order, market_update) {
// Emit fill event
event_bus.publish(Event {
event_type: EventType::OrderFill {
order_id: order.id.clone(),
fill: fill.clone(),
},
timestamp: market_update.timestamp,
data: serde_json::Value::Null,
}).await;
filled_indices.push(idx);
}
}
// Remove filled orders
for idx in filled_indices.into_iter().rev() {
let order = orders.remove(idx);
self.order_history.write().push((order, OrderStatus::Filled));
}
Ok(())
}
}
#[async_trait]
impl ExecutionHandler for BacktestExecutor {
async fn submit_order(&self, order: &Order) -> Result<String, String> {
let order_id = order.id.clone();
self.pending_orders.write().push(order.clone());
Ok(order_id)
}
async fn cancel_order(&self, order_id: &str) -> Result<(), String> {
let mut orders = self.pending_orders.write();
if let Some(pos) = orders.iter().position(|o| o.id == order_id) {
let order = orders.remove(pos);
self.order_history.write().push((order, OrderStatus::Cancelled));
Ok(())
} else {
Err("Order not found".to_string())
}
}
async fn get_order_status(&self, order_id: &str) -> Result<OrderStatus, String> {
// Check pending orders
if self.pending_orders.read().iter().any(|o| o.id == order_id) {
return Ok(OrderStatus::Pending);
}
// Check history
if let Some((_, status)) = self.order_history.read()
.iter()
.find(|(o, _)| o.id == order_id) {
return Ok(status.clone());
}
Err("Order not found".to_string())
}
}
/// Backtest time provider
struct BacktestTimeProvider {
current_time: DateTime<Utc>,
}
impl TimeProvider for BacktestTimeProvider {
fn now(&self) -> DateTime<Utc> {
self.current_time
}
fn sleep_until(&self, _target: DateTime<Utc>) -> Result<(), String> {
// In backtest, we don't actually sleep
Ok(())
}
}
/// Backtest fill simulator
#[derive(Clone)]
struct BacktestFillSimulator {
commission_rate: f64,
slippage_bps: f64,
}
impl BacktestFillSimulator {
fn new() -> Self {
Self {
commission_rate: 0.001, // 0.1%
slippage_bps: 5.0, // 5 basis points
}
}
fn try_fill(&self, order: &Order, market_update: &MarketUpdate) -> Option<Fill> {
match &market_update.data {
crate::MarketDataType::Quote(quote) => {
self.simulate_fill(order, quote.bid, quote.ask - quote.bid)
}
crate::MarketDataType::Trade(trade) => {
self.simulate_fill(order, trade.price, 0.0001 * trade.price) // 1bp spread estimate
}
crate::MarketDataType::Bar(bar) => {
// Use close price with estimated spread
self.simulate_fill(order, bar.close, 0.0002 * bar.close) // 2bp spread estimate
}
}
}
}
impl FillSimulator for BacktestFillSimulator {
fn simulate_fill(&self, order: &Order, market_price: f64, spread: f64) -> Option<Fill> {
let fill_price = match order.order_type {
crate::OrderType::Market => {
// Fill at market with slippage
let slippage = market_price * self.slippage_bps / 10000.0;
match order.side {
crate::Side::Buy => market_price + spread/2.0 + slippage,
crate::Side::Sell => market_price - spread/2.0 - slippage,
}
}
crate::OrderType::Limit { price } => {
// Check if limit price is satisfied
match order.side {
crate::Side::Buy if price >= market_price + spread/2.0 => price,
crate::Side::Sell if price <= market_price - spread/2.0 => price,
_ => return None, // Limit not satisfied
}
}
_ => return None, // Other order types not implemented yet
};
let commission = fill_price * order.quantity * self.commission_rate;
Some(Fill {
timestamp: chrono::Utc::now(), // Will be overridden by engine
price: fill_price,
quantity: order.quantity,
commission,
})
}
}

View file

@ -1,290 +0,0 @@
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use std::sync::Arc;
use parking_lot::RwLock;
use tokio::sync::mpsc;
use crate::{
ExecutionHandler, TimeProvider,
Order, OrderStatus, Fill,
};
use crate::events::EventBus;
use crate::domain::{Event, EventType};
use super::TradingEngine;
/// Live trading engine - connects to real brokers
pub struct LiveEngine {
broker: String,
account_id: String,
broker_connection: Arc<dyn BrokerConnection>,
is_running: Arc<RwLock<bool>>,
shutdown_tx: Option<mpsc::Sender<()>>,
}
/// Trait for broker connections
#[async_trait]
pub trait BrokerConnection: Send + Sync {
async fn connect(&mut self, account_id: &str) -> Result<(), String>;
async fn disconnect(&mut self) -> Result<(), String>;
async fn subscribe_market_data(&mut self, symbols: Vec<String>) -> Result<(), String>;
async fn submit_order(&self, order: &Order) -> Result<String, String>;
async fn cancel_order(&self, order_id: &str) -> Result<(), String>;
async fn get_order_status(&self, order_id: &str) -> Result<OrderStatus, String>;
async fn get_positions(&self) -> Result<Vec<crate::Position>, String>;
async fn get_account_info(&self) -> Result<AccountInfo, String>;
}
#[derive(Debug, Clone)]
pub struct AccountInfo {
pub cash: f64,
pub buying_power: f64,
pub portfolio_value: f64,
pub day_trades_remaining: Option<u32>,
}
impl LiveEngine {
pub fn new(
broker: String,
account_id: String,
broker_connection: Arc<dyn BrokerConnection>,
) -> Self {
Self {
broker,
account_id,
broker_connection,
is_running: Arc::new(RwLock::new(false)),
shutdown_tx: None,
}
}
}
#[async_trait]
impl TradingEngine for LiveEngine {
async fn start(&mut self, event_bus: &mut EventBus) -> Result<(), String> {
*self.is_running.write() = true;
// Connect to broker
self.broker_connection.connect(&self.account_id).await?;
// Get initial account info
let account_info = self.broker_connection.get_account_info().await?;
let (shutdown_tx, mut shutdown_rx) = mpsc::channel(1);
self.shutdown_tx = Some(shutdown_tx);
// Emit start event
event_bus.publish(Event {
event_type: EventType::SystemStart,
timestamp: Utc::now(),
data: serde_json::json!({
"mode": "live",
"broker": self.broker,
"account_id": self.account_id,
"account_info": {
"cash": account_info.cash,
"buying_power": account_info.buying_power,
"portfolio_value": account_info.portfolio_value,
},
}),
}).await;
// Main live trading loop
loop {
tokio::select! {
_ = shutdown_rx.recv() => {
break;
}
_ = tokio::time::sleep(std::time::Duration::from_secs(1)) => {
// Periodic tasks (e.g., check positions, risk)
if !*self.is_running.read() {
break;
}
// Could emit periodic status updates here
}
}
}
// Disconnect from broker
self.broker_connection.disconnect().await?;
// Emit stop event
event_bus.publish(Event {
event_type: EventType::SystemStop,
timestamp: Utc::now(),
data: serde_json::json!({
"reason": "live_trading_stopped",
}),
}).await;
Ok(())
}
async fn stop(&mut self) -> Result<(), String> {
*self.is_running.write() = false;
if let Some(tx) = self.shutdown_tx.take() {
let _ = tx.send(()).await;
}
Ok(())
}
fn get_execution_handler(&self) -> Arc<dyn ExecutionHandler> {
Arc::new(LiveExecutor {
broker_connection: self.broker_connection.clone(),
})
}
fn get_time_provider(&self) -> Arc<dyn TimeProvider> {
Arc::new(RealTimeProvider)
}
}
/// Live order executor - delegates to broker
#[derive(Clone)]
struct LiveExecutor {
broker_connection: Arc<dyn BrokerConnection>,
}
#[async_trait]
impl ExecutionHandler for LiveExecutor {
async fn submit_order(&self, order: &Order) -> Result<String, String> {
// Validate order
if order.quantity <= 0.0 {
return Err("Invalid order quantity".to_string());
}
// Submit to broker
self.broker_connection.submit_order(order).await
}
async fn cancel_order(&self, order_id: &str) -> Result<(), String> {
self.broker_connection.cancel_order(order_id).await
}
async fn get_order_status(&self, order_id: &str) -> Result<OrderStatus, String> {
self.broker_connection.get_order_status(order_id).await
}
}
/// Real-time provider for live trading
struct RealTimeProvider;
impl TimeProvider for RealTimeProvider {
fn now(&self) -> DateTime<Utc> {
Utc::now()
}
fn sleep_until(&self, target: DateTime<Utc>) -> Result<(), String> {
let now = Utc::now();
if target > now {
let duration = target.signed_duration_since(now);
std::thread::sleep(duration.to_std().unwrap_or_default());
}
Ok(())
}
}
// Example broker implementations would go here
// For now, we'll create a mock broker for testing
/// Mock broker for testing
pub struct MockBroker {
is_connected: bool,
orders: Arc<RwLock<Vec<(Order, OrderStatus)>>>,
}
impl MockBroker {
pub fn new() -> Self {
Self {
is_connected: false,
orders: Arc::new(RwLock::new(Vec::new())),
}
}
}
#[async_trait]
impl BrokerConnection for MockBroker {
async fn connect(&mut self, _account_id: &str) -> Result<(), String> {
self.is_connected = true;
Ok(())
}
async fn disconnect(&mut self) -> Result<(), String> {
self.is_connected = false;
Ok(())
}
async fn subscribe_market_data(&mut self, _symbols: Vec<String>) -> Result<(), String> {
if !self.is_connected {
return Err("Not connected".to_string());
}
Ok(())
}
async fn submit_order(&self, order: &Order) -> Result<String, String> {
if !self.is_connected {
return Err("Not connected".to_string());
}
let order_id = order.id.clone();
self.orders.write().push((order.clone(), OrderStatus::Pending));
// Simulate order being filled after a delay
let orders = self.orders.clone();
let order_id_clone = order_id.clone();
tokio::spawn(async move {
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
let mut orders = orders.write();
if let Some(pos) = orders.iter().position(|(o, _)| o.id == order_id_clone) {
orders[pos].1 = OrderStatus::Filled;
}
});
Ok(order_id)
}
async fn cancel_order(&self, order_id: &str) -> Result<(), String> {
if !self.is_connected {
return Err("Not connected".to_string());
}
let mut orders = self.orders.write();
if let Some(pos) = orders.iter().position(|(o, _)| o.id == order_id) {
orders[pos].1 = OrderStatus::Cancelled;
Ok(())
} else {
Err("Order not found".to_string())
}
}
async fn get_order_status(&self, order_id: &str) -> Result<OrderStatus, String> {
if !self.is_connected {
return Err("Not connected".to_string());
}
let orders = self.orders.read();
if let Some((_, status)) = orders.iter().find(|(o, _)| o.id == order_id) {
Ok(status.clone())
} else {
Err("Order not found".to_string())
}
}
async fn get_positions(&self) -> Result<Vec<crate::Position>, String> {
if !self.is_connected {
return Err("Not connected".to_string());
}
Ok(Vec::new())
}
async fn get_account_info(&self) -> Result<AccountInfo, String> {
if !self.is_connected {
return Err("Not connected".to_string());
}
Ok(AccountInfo {
cash: 100_000.0,
buying_power: 400_000.0,
portfolio_value: 100_000.0,
day_trades_remaining: Some(3),
})
}
}

View file

@ -1,106 +0,0 @@
use async_trait::async_trait;
use crate::events::EventBus;
use crate::{ExecutionHandler, TimeProvider, MarketDataSource};
use std::sync::Arc;
pub mod backtest;
pub mod paper;
pub mod live;
pub use backtest::BacktestEngine;
pub use paper::PaperEngine;
pub use live::{LiveEngine, BrokerConnection};
/// Trading mode configuration
#[derive(Debug, Clone)]
pub enum TradingMode {
Backtest {
start_time: chrono::DateTime<chrono::Utc>,
end_time: chrono::DateTime<chrono::Utc>,
speed_multiplier: f64,
},
Paper {
starting_capital: f64,
},
Live {
broker: String,
account_id: String,
},
}
/// Common interface for all trading engines
#[async_trait]
pub trait TradingEngine: Send + Sync {
/// Start the trading engine
async fn start(&mut self, event_bus: &mut EventBus) -> Result<(), String>;
/// Stop the trading engine
async fn stop(&mut self) -> Result<(), String>;
/// Get the execution handler for this mode
fn get_execution_handler(&self) -> Arc<dyn ExecutionHandler>;
/// Get the time provider for this mode
fn get_time_provider(&self) -> Arc<dyn TimeProvider>;
}
/// Create a trading engine for the specified mode
pub fn create_engine_for_mode(
mode: &TradingMode,
) -> Result<Box<dyn TradingEngine>, String> {
match mode {
TradingMode::Backtest { start_time, end_time, speed_multiplier } => {
// For backtest, we need to create a market data source
// This would typically load historical data
let market_data = create_backtest_data_source()?;
Ok(Box::new(BacktestEngine::new(
*start_time,
*end_time,
*speed_multiplier,
market_data,
)))
}
TradingMode::Paper { starting_capital } => {
// For paper trading, we need a real-time data source
let market_data = create_realtime_data_source()?;
Ok(Box::new(PaperEngine::new(
*starting_capital,
market_data,
)))
}
TradingMode::Live { broker, account_id } => {
// For live trading, we need a broker connection
let broker_connection = create_broker_connection(broker)?;
Ok(Box::new(LiveEngine::new(
broker.clone(),
account_id.clone(),
broker_connection,
)))
}
}
}
// Helper functions to create data sources and broker connections
// These would be implemented based on your specific requirements
fn create_backtest_data_source() -> Result<Box<dyn MarketDataSource>, String> {
// TODO: Implement actual backtest data source
// For now, return a placeholder
Err("Backtest data source not implemented yet".to_string())
}
fn create_realtime_data_source() -> Result<Box<dyn MarketDataSource>, String> {
// TODO: Implement actual real-time data source
// For now, return a placeholder
Err("Real-time data source not implemented yet".to_string())
}
fn create_broker_connection(broker: &str) -> Result<Arc<dyn BrokerConnection>, String> {
match broker {
"mock" => Ok(Arc::new(live::MockBroker::new())),
_ => Err(format!("Unknown broker: {}", broker)),
}
}

View file

@ -1,306 +0,0 @@
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use std::sync::Arc;
use parking_lot::RwLock;
use tokio::sync::mpsc;
use crate::{
MarketDataSource, ExecutionHandler, TimeProvider, FillSimulator,
MarketUpdate, Order, OrderStatus, Fill,
};
use crate::events::EventBus;
use crate::domain::{Event, EventType};
use super::TradingEngine;
/// Paper trading engine - simulates live trading without real money
pub struct PaperEngine {
starting_capital: f64,
market_data: Box<dyn MarketDataSource>,
execution: PaperExecutor,
is_running: Arc<RwLock<bool>>,
shutdown_tx: Option<mpsc::Sender<()>>,
}
impl PaperEngine {
pub fn new(
starting_capital: f64,
market_data: Box<dyn MarketDataSource>,
) -> Self {
Self {
starting_capital,
market_data,
execution: PaperExecutor::new(starting_capital),
is_running: Arc::new(RwLock::new(false)),
shutdown_tx: None,
}
}
}
#[async_trait]
impl TradingEngine for PaperEngine {
async fn start(&mut self, event_bus: &mut EventBus) -> Result<(), String> {
*self.is_running.write() = true;
let (shutdown_tx, mut shutdown_rx) = mpsc::channel(1);
self.shutdown_tx = Some(shutdown_tx);
// Emit start event
event_bus.publish(Event {
event_type: EventType::SystemStart,
timestamp: Utc::now(),
data: serde_json::json!({
"mode": "paper",
"starting_capital": self.starting_capital,
}),
}).await;
// Main paper trading loop
loop {
tokio::select! {
_ = shutdown_rx.recv() => {
break;
}
update = self.market_data.get_next_update() => {
if let Some(market_update) = update {
// Publish market data event
event_bus.publish(Event {
event_type: EventType::MarketData(market_update.clone()),
timestamp: Utc::now(),
data: serde_json::Value::Null,
}).await;
// Process pending orders
self.execution.process_orders(&market_update, event_bus).await?;
}
}
_ = tokio::time::sleep(std::time::Duration::from_millis(100)) => {
// Check if still running
if !*self.is_running.read() {
break;
}
}
}
}
// Emit stop event
event_bus.publish(Event {
event_type: EventType::SystemStop,
timestamp: Utc::now(),
data: serde_json::json!({
"reason": "paper_trading_stopped",
}),
}).await;
Ok(())
}
async fn stop(&mut self) -> Result<(), String> {
*self.is_running.write() = false;
if let Some(tx) = self.shutdown_tx.take() {
let _ = tx.send(()).await;
}
Ok(())
}
fn get_execution_handler(&self) -> Arc<dyn ExecutionHandler> {
Arc::new(self.execution.clone())
}
fn get_time_provider(&self) -> Arc<dyn TimeProvider> {
Arc::new(RealTimeProvider)
}
}
/// Paper trading order executor
#[derive(Clone)]
struct PaperExecutor {
cash: Arc<RwLock<f64>>,
pending_orders: Arc<RwLock<Vec<Order>>>,
order_history: Arc<RwLock<Vec<(Order, OrderStatus)>>>,
fill_simulator: PaperFillSimulator,
}
impl PaperExecutor {
fn new(starting_capital: f64) -> Self {
Self {
cash: Arc::new(RwLock::new(starting_capital)),
pending_orders: Arc::new(RwLock::new(Vec::new())),
order_history: Arc::new(RwLock::new(Vec::new())),
fill_simulator: PaperFillSimulator::new(),
}
}
async fn process_orders(
&self,
market_update: &MarketUpdate,
event_bus: &mut EventBus,
) -> Result<(), String> {
let mut orders = self.pending_orders.write();
let mut filled_indices = Vec::new();
for (idx, order) in orders.iter().enumerate() {
if order.symbol != market_update.symbol {
continue;
}
// Try to fill the order
if let Some(fill) = self.fill_simulator.try_fill(order, market_update) {
// Check if we have enough cash for buy orders
if order.side == crate::Side::Buy {
let required_cash = fill.price * fill.quantity + fill.commission;
let mut cash = self.cash.write();
if *cash >= required_cash {
*cash -= required_cash;
} else {
continue; // Skip this order
}
} else {
// For sell orders, add cash
let proceeds = fill.price * fill.quantity - fill.commission;
*self.cash.write() += proceeds;
}
// Emit fill event
event_bus.publish(Event {
event_type: EventType::OrderFill {
order_id: order.id.clone(),
fill: fill.clone(),
},
timestamp: Utc::now(),
data: serde_json::json!({
"cash_after": *self.cash.read(),
}),
}).await;
filled_indices.push(idx);
}
}
// Remove filled orders
for idx in filled_indices.into_iter().rev() {
let order = orders.remove(idx);
self.order_history.write().push((order, OrderStatus::Filled));
}
Ok(())
}
}
#[async_trait]
impl ExecutionHandler for PaperExecutor {
async fn submit_order(&self, order: &Order) -> Result<String, String> {
// Basic validation
if order.quantity <= 0.0 {
return Err("Invalid order quantity".to_string());
}
let order_id = order.id.clone();
self.pending_orders.write().push(order.clone());
Ok(order_id)
}
async fn cancel_order(&self, order_id: &str) -> Result<(), String> {
let mut orders = self.pending_orders.write();
if let Some(pos) = orders.iter().position(|o| o.id == order_id) {
let order = orders.remove(pos);
self.order_history.write().push((order, OrderStatus::Cancelled));
Ok(())
} else {
Err("Order not found".to_string())
}
}
async fn get_order_status(&self, order_id: &str) -> Result<OrderStatus, String> {
// Check pending orders
if self.pending_orders.read().iter().any(|o| o.id == order_id) {
return Ok(OrderStatus::Pending);
}
// Check history
if let Some((_, status)) = self.order_history.read()
.iter()
.find(|(o, _)| o.id == order_id) {
return Ok(status.clone());
}
Err("Order not found".to_string())
}
}
/// Real-time provider for paper trading
struct RealTimeProvider;
impl TimeProvider for RealTimeProvider {
fn now(&self) -> DateTime<Utc> {
Utc::now()
}
fn sleep_until(&self, target: DateTime<Utc>) -> Result<(), String> {
let now = Utc::now();
if target > now {
let duration = target.signed_duration_since(now);
std::thread::sleep(duration.to_std().unwrap_or_default());
}
Ok(())
}
}
/// Paper trading fill simulator
#[derive(Clone)]
struct PaperFillSimulator {
commission_rate: f64,
slippage_bps: f64,
}
impl PaperFillSimulator {
fn new() -> Self {
Self {
commission_rate: 0.001, // 0.1%
slippage_bps: 3.0, // 3 basis points (less than backtest)
}
}
fn try_fill(&self, order: &Order, market_update: &MarketUpdate) -> Option<Fill> {
match &market_update.data {
crate::MarketDataType::Quote(quote) => {
self.simulate_fill(order, quote.bid, quote.ask - quote.bid)
}
crate::MarketDataType::Trade(trade) => {
self.simulate_fill(order, trade.price, 0.0001 * trade.price)
}
crate::MarketDataType::Bar(bar) => {
self.simulate_fill(order, bar.close, 0.0002 * bar.close)
}
}
}
}
impl FillSimulator for PaperFillSimulator {
fn simulate_fill(&self, order: &Order, market_price: f64, spread: f64) -> Option<Fill> {
let fill_price = match order.order_type {
crate::OrderType::Market => {
let slippage = market_price * self.slippage_bps / 10000.0;
match order.side {
crate::Side::Buy => market_price + spread/2.0 + slippage,
crate::Side::Sell => market_price - spread/2.0 - slippage,
}
}
crate::OrderType::Limit { price } => {
match order.side {
crate::Side::Buy if price >= market_price + spread/2.0 => price,
crate::Side::Sell if price <= market_price - spread/2.0 => price,
_ => return None,
}
}
_ => return None,
};
let commission = fill_price * order.quantity * self.commission_rate;
Some(Fill {
timestamp: Utc::now(),
price: fill_price,
quantity: order.quantity,
commission,
})
}
}

View file

@ -1,114 +0,0 @@
use async_trait::async_trait;
use serde_json::Value;
use crate::domain::{MarketUpdate, Fill, Order, Side};
use crate::indicators::IndicatorSet;
use std::collections::HashMap;
/// Context provided to strategies
pub struct StrategyContext {
pub portfolio_value: f64,
pub cash: f64,
pub positions: HashMap<String, f64>,
pub pending_orders: Vec<Order>,
pub indicators: IndicatorSet,
}
/// Signal generated by a strategy
#[derive(Debug, Clone)]
pub struct Signal {
pub symbol: String,
pub action: SignalAction,
pub quantity: Option<f64>,
pub confidence: f64, // 0.0 to 1.0
pub reason: String,
pub metadata: Option<Value>,
}
#[derive(Debug, Clone)]
pub enum SignalAction {
Buy,
Sell,
Close,
Hold,
}
/// Core strategy trait with lifecycle methods
#[async_trait]
pub trait Strategy: Send + Sync {
/// Called once when strategy is initialized
async fn init(&mut self, context: &StrategyContext) -> Result<(), String>;
/// Called when trading starts
async fn on_start(&mut self) -> Result<(), String>;
/// Called for each market data update
async fn on_data(&mut self, data: &MarketUpdate, context: &StrategyContext) -> Vec<Signal>;
/// Called when an order is filled
async fn on_fill(&mut self, order_id: &str, fill: &Fill, context: &StrategyContext);
/// Called periodically (e.g., every minute)
async fn on_timer(&mut self, context: &StrategyContext) -> Vec<Signal> {
Vec::new() // Default: no signals on timer
}
/// Called when trading stops
async fn on_stop(&mut self) -> Result<(), String>;
/// Get strategy name
fn name(&self) -> &str;
/// Get strategy parameters
fn parameters(&self) -> Value;
/// Update strategy parameters
fn update_parameters(&mut self, params: Value) -> Result<(), String>;
}
/// Base implementation that strategies can extend
pub struct BaseStrategy {
name: String,
parameters: Value,
}
impl BaseStrategy {
pub fn new(name: String, parameters: Value) -> Self {
Self { name, parameters }
}
}
#[async_trait]
impl Strategy for BaseStrategy {
async fn init(&mut self, _context: &StrategyContext) -> Result<(), String> {
Ok(())
}
async fn on_start(&mut self) -> Result<(), String> {
Ok(())
}
async fn on_data(&mut self, _data: &MarketUpdate, _context: &StrategyContext) -> Vec<Signal> {
Vec::new()
}
async fn on_fill(&mut self, _order_id: &str, _fill: &Fill, _context: &StrategyContext) {
// Default: no action
}
async fn on_stop(&mut self) -> Result<(), String> {
Ok(())
}
fn name(&self) -> &str {
&self.name
}
fn parameters(&self) -> Value {
self.parameters.clone()
}
fn update_parameters(&mut self, params: Value) -> Result<(), String> {
self.parameters = params;
Ok(())
}
}

View file

@ -1,192 +0,0 @@
// Separate lib for testing without NAPI dependencies
#![cfg(test)]
pub mod core;
pub mod adapters;
pub mod orderbook;
pub mod risk;
pub mod positions;
pub mod analytics;
pub mod indicators;
pub mod backtest;
pub mod strategies;
// Re-export commonly used types
pub use positions::{Position, PositionUpdate, TradeRecord, ClosedTrade};
pub use risk::{RiskLimits, RiskCheckResult, RiskMetrics};
// Type alias for backtest compatibility
pub type MarketData = MarketUpdate;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use parking_lot::RwLock;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TradingMode {
Backtest {
start_time: DateTime<Utc>,
end_time: DateTime<Utc>,
speed_multiplier: f64,
},
Paper {
starting_capital: f64,
},
Live {
broker: String,
account_id: String,
},
}
// Core traits that allow different implementations based on mode
#[async_trait::async_trait]
pub trait MarketDataSource: Send + Sync {
async fn get_next_update(&mut self) -> Option<MarketUpdate>;
fn seek_to_time(&mut self, timestamp: DateTime<Utc>) -> Result<(), String>;
fn as_any(&self) -> &dyn std::any::Any;
fn as_any_mut(&mut self) -> &mut dyn std::any::Any;
}
#[async_trait::async_trait]
pub trait ExecutionHandler: Send + Sync {
async fn execute_order(&mut self, order: Order) -> Result<ExecutionResult, String>;
fn get_fill_simulator(&self) -> Option<&dyn FillSimulator>;
}
pub trait TimeProvider: Send + Sync {
fn now(&self) -> DateTime<Utc>;
fn sleep_until(&self, target: DateTime<Utc>) -> Result<(), String>;
fn as_any(&self) -> &dyn std::any::Any;
}
pub trait FillSimulator: Send + Sync {
fn simulate_fill(&self, order: &Order, orderbook: &OrderBookSnapshot) -> Option<Fill>;
}
// Main trading core that works across all modes
pub struct TradingCore {
mode: TradingMode,
pub market_data_source: Arc<RwLock<Box<dyn MarketDataSource>>>,
pub execution_handler: Arc<RwLock<Box<dyn ExecutionHandler>>>,
pub time_provider: Arc<Box<dyn TimeProvider>>,
pub orderbooks: Arc<orderbook::OrderBookManager>,
pub risk_engine: Arc<risk::RiskEngine>,
pub position_tracker: Arc<positions::PositionTracker>,
}
// Market structure definitions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketUpdate {
pub symbol: String,
pub timestamp: DateTime<Utc>,
pub data: MarketDataType,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MarketMicrostructure {
pub spread: f64,
pub volatility: f64,
pub tick_size: f64,
pub lot_size: f64,
pub intraday_volume_profile: Vec<f64>, // 24 hourly buckets
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum MarketDataType {
Quote(Quote),
Trade(Trade),
Bar(Bar),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Quote {
pub bid: f64,
pub ask: f64,
pub bid_size: f64,
pub ask_size: f64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Trade {
pub price: f64,
pub size: f64,
pub side: Side,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Bar {
pub open: f64,
pub high: f64,
pub low: f64,
pub close: f64,
pub volume: f64,
pub vwap: Option<f64>,
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq)]
pub enum Side {
Buy,
Sell,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Order {
pub id: String,
pub symbol: String,
pub side: Side,
pub quantity: f64,
pub order_type: OrderType,
pub time_in_force: TimeInForce,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OrderType {
Market,
Limit { price: f64 },
Stop { stop_price: f64 },
StopLimit { stop_price: f64, limit_price: f64 },
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TimeInForce {
Day,
GTC,
IOC,
FOK,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ExecutionResult {
pub order_id: String,
pub status: OrderStatus,
pub fills: Vec<Fill>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum OrderStatus {
Pending,
Accepted,
PartiallyFilled,
Filled,
Cancelled,
Rejected(String),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Fill {
pub timestamp: DateTime<Utc>,
pub price: f64,
pub quantity: f64,
pub commission: f64,
}
// OrderBook types
pub type OrderBookSnapshot = orderbook::OrderBookSnapshot;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PriceLevel {
pub price: f64,
pub size: f64,
pub order_count: Option<u32>,
}