Generalize trading daemon for multi-asset venues and crypto#21
Conversation
Greptile SummaryThis PR transforms the trading daemon from a Kalshi-specific implementation into a generalized multi-asset, multi-venue trading platform. The changes introduce a unified domain model ( Key architectural improvements:
Critical issue found:
Confidence Score: 3/5
|
| Filename | Overview |
|---|---|
| crates/trading_daemon/src/main.rs | Added multi-venue support with venues, execution modes, and adapters; contains critical bug in default_execution_modes that prevents live trading |
| crates/trading_domain/src/lib.rs | New file defining core multi-asset domain types (AssetClass, MarketType, InstrumentId, OrderRequest, etc.) |
| crates/exchange_core/src/lib.rs | New ExchangeAdapter trait providing venue-agnostic abstraction for orders, positions, balances, and health |
| crates/exchange_kalshi/src/lib.rs | New KalshiAdapter implementation for binary prediction markets with paper and live mode support |
| crates/exchange_coinbase_spot/src/lib.rs | New CoinbaseSpotAdapter for crypto spot trading with proper asset class validation |
| crates/risk_core/src/lib.rs | New hard safety cage with venue-aware notional limits, position limits, and promotion gate validation |
Class Diagram
classDiagram
class TradingDaemon {
+EngineState
+handle_connection()
+initial_engine_state()
}
class EngineState {
+venues: HashMap
+execution_modes: HashMap
+adapters: HashMap
+strategies: HashMap
+risk_snapshot: RiskSnapshot
}
class ExchangeAdapter {
<<interface>>
+venue() str
+capabilities() Vec
+execution_mode() ExecutionMode
+place_order()
+cancel_order()
+sync_positions()
+sync_balances()
+health()
}
class KalshiAdapter {
+mode: ExecutionMode
+state: AdapterState
}
class CoinbaseSpotAdapter {
+mode: ExecutionMode
+state: AdapterState
}
class DerivativesPaperAdapter {
+state: AdapterState
}
class TradingDomain {
+InstrumentId
+OrderRequest
+OrderSummary
+PositionSnapshot
+BalanceSnapshot
}
class HardSafetyCage {
+policy: HardSafetyPolicy
+evaluate_order()
+evaluate_promotion()
}
class StrategyPlugin {
<<interface>>
+id() str
+family() StrategyFamily
+evaluate()
}
TradingDaemon --> EngineState
EngineState --> ExchangeAdapter
EngineState --> HardSafetyCage
ExchangeAdapter <|-- KalshiAdapter
ExchangeAdapter <|-- CoinbaseSpotAdapter
ExchangeAdapter <|-- DerivativesPaperAdapter
ExchangeAdapter --> TradingDomain
StrategyPlugin --> TradingDomain
HardSafetyCage --> RiskSnapshot
Last reviewed commit: ff19223
| let mode = if venue.paper_only { | ||
| ExecutionMode::Paper | ||
| } else { | ||
| ExecutionMode::Paper | ||
| }; |
There was a problem hiding this comment.
always defaults to Paper mode regardless of venue.paper_only value
| let mode = if venue.paper_only { | |
| ExecutionMode::Paper | |
| } else { | |
| ExecutionMode::Paper | |
| }; | |
| let mode = if venue.paper_only { | |
| ExecutionMode::Paper | |
| } else { | |
| ExecutionMode::Live | |
| }; |
|
@greptile |
Greptile SummaryThis PR generalizes the trading daemon to support multiple asset classes and venues. The implementation adds three exchange adapters (Kalshi, Coinbase Spot, Derivatives Paper) with runtime execution mode control, SQLite-based state persistence, and three new strategy plugins for crypto momentum and Kalshi markets. Key changes:
Found issue:
Confidence Score: 3/5
|
| Filename | Overview |
|---|---|
| crates/trading_daemon/src/main.rs | Major refactoring to support multi-venue/multi-asset trading with SQLite persistence, execution mode management, and venue adapters. Found issue with derivatives_paper adapter not respecting execution mode changes. |
| crates/exchange_core/src/lib.rs | Clean trait definition for exchange adapters with execution mode support. |
| crates/exchange_derivatives_paper/src/lib.rs | Paper-only derivatives adapter implementation that hardcodes ExecutionMode::Paper and cannot be configured otherwise, which may be intentional but inconsistent with other adapters. |
| crates/exchange_coinbase_spot/src/lib.rs | Coinbase spot adapter with configurable execution mode, properly handles both paper and live mode order submission. |
| crates/exchange_kalshi/src/lib.rs | Kalshi adapter with configurable execution mode, properly handles paper vs live order execution. |
| crates/trading_domain/src/lib.rs | Core domain types for multi-asset trading including instruments, orders, execution modes, and venue capabilities. |
| crates/trading_protocol/src/lib.rs | Protocol definitions for venue, portfolio, order, and execution mode commands with proper serialization. |
| crates/risk_core/src/lib.rs | Enhanced risk management with per-venue notional limits, leverage checks, and position limits with comprehensive tests. |
Flowchart
flowchart TD
A[Trading Daemon] --> B[Venue Management]
A --> C[Execution Mode Control]
A --> D[Order Routing]
A --> E[Risk Management]
B --> F[Kalshi Adapter]
B --> G[Coinbase Spot Adapter]
B --> H[Derivatives Paper Adapter]
C --> I[Per-Venue/Market Mode]
I --> J[Live Mode]
I --> K[Paper Mode]
D --> L[Strategy Signals]
L --> M[Crypto Momentum]
L --> N[Kalshi Arbitrage]
L --> O[Kalshi Weather]
E --> P[Per-Venue Limits]
E --> Q[Position Limits]
E --> R[Leverage Checks]
A --> S[SQLite Persistence]
S --> T[Venues State]
S --> U[Execution Modes]
S --> V[Orders/Fills]
S --> W[Risk State]
Last reviewed commit: d7ed0de
| "derivatives_paper" => { | ||
| state.adapters.insert( | ||
| "derivatives_paper".to_string(), | ||
| Arc::new(DerivativesPaperAdapter::default()), | ||
| ); |
There was a problem hiding this comment.
derivatives_paper adapter doesn't receive execution mode parameter
Unlike kalshi and coinbase_spot adapters, DerivativesPaperAdapter::default() ignores execution modes. The adapter hardcodes ExecutionMode::Paper in its implementation (see crates/exchange_derivatives_paper/src/lib.rs:89-91). This is inconsistent with the pattern used for other adapters and means execution mode changes via ExecutionMode.Set won't affect this venue.
|
@greptile |
Greptile SummaryThis PR transforms the trading daemon from a Kalshi-specific system into a generalized multi-asset trading platform. The changes introduce three new exchange adapters (Coinbase spot, Kalshi, derivatives paper), a unified domain model supporting multiple asset classes and market types, SQLite-based state persistence, and runtime execution mode management. Major changes:
The codebase demonstrates careful attention to safety with hardened derivatives adapter behavior, comprehensive risk controls, and proper state synchronization between in-memory and persistent storage. Confidence Score: 4/5
|
| Filename | Overview |
|---|---|
| crates/trading_daemon/src/main.rs | Added multi-asset venue support, SQLite persistence, execution mode management, portfolio/order commands; major refactoring with proper adapter lifecycle |
| crates/exchange_coinbase_spot/src/lib.rs | New Coinbase spot adapter for crypto trading with paper/live mode support, position tracking, and order management |
| crates/exchange_derivatives_paper/src/lib.rs | Paper-only derivatives adapter that intentionally clamps all execution modes to Paper for safety |
| crates/trading_domain/src/lib.rs | Canonical multi-asset domain types including AssetClass, MarketType, InstrumentId, OrderRequest, and execution primitives |
| crates/trading_protocol/src/lib.rs | Extended protocol with Venue, Portfolio, Order, and ExecutionMode commands and payloads |
| crates/risk_core/src/lib.rs | Enhanced risk controls with per-venue notional limits, position count limits, and leverage constraints |
| crates/trading_daemon/migrations/0001_runtime.sql | SQLite schema for persisting venues, execution modes, orders, fills, strategies, risk state, and events |
| .openclaw/extensions/trading-bridge/src/index.ts | Trading bridge updated with venue, order, portfolio, and execution mode management support |
Flowchart
flowchart TD
CLI[tradingctl CLI] -->|Unix Socket| Daemon[Trading Daemon]
Bridge[Trading Bridge] -->|Unix Socket| Daemon
Daemon -->|Manages| State[(SQLite State)]
Daemon -->|Manages| Venues[Venue Registry]
Daemon -->|Manages| ExecModes[Execution Modes]
Venues -->|Creates| Adapters[Exchange Adapters]
Adapters -->|Kalshi| KalshiAdapter[Kalshi Adapter]
Adapters -->|Coinbase| CoinbaseAdapter[Coinbase Spot Adapter]
Adapters -->|Paper| DerivAdapter[Derivatives Paper Adapter]
KalshiAdapter -->|Binary Markets| Markets
CoinbaseAdapter -->|Crypto Spot| Markets
DerivAdapter -->|Perp/Futures/Options| Markets[Market Operations]
Daemon -->|Validates| Risk[Risk Core]
Risk -->|Enforces| Limits[Per-Venue Notional<br/>Position Count<br/>Leverage Limits]
Daemon -->|Executes| Strategies[Strategy Plugins]
Strategies -->|Kalshi| KalshiStrats[Kalshi Arbitrage<br/>Weather Strategy]
Strategies -->|Crypto| CryptoStrats[Crypto Momentum]
State -->|Persists| VenueState[Venues Table]
State -->|Persists| ExecState[Execution Modes Table]
State -->|Persists| Orders[Orders Table]
State -->|Persists| Events[Events Journal]
Last reviewed commit: b8739ad
No description provided.