Skip to main content

sandbox_quant/
event.rs

1use std::collections::HashMap;
2
3use crate::model::candle::Candle;
4use crate::model::signal::Signal;
5use crate::model::tick::Tick;
6use crate::order_manager::{OrderHistorySnapshot, OrderHistoryStats, OrderUpdate};
7use crate::risk_module::RateBudgetSnapshot;
8
9#[derive(Debug, Clone)]
10pub enum WsConnectionStatus {
11    Connected,
12    Disconnected,
13    Reconnecting { attempt: u32, delay_ms: u64 },
14}
15
16#[derive(Debug, Clone)]
17pub enum AppEvent {
18    MarketTick(Tick),
19    StrategySignal {
20        signal: Signal,
21        symbol: String,
22        source_tag: String,
23        price: Option<f64>,
24        timestamp_ms: u64,
25    },
26    StrategyState {
27        fast_sma: Option<f64>,
28        slow_sma: Option<f64>,
29    },
30    OrderUpdate(OrderUpdate),
31    WsStatus(WsConnectionStatus),
32    HistoricalCandles {
33        candles: Vec<Candle>,
34        interval_ms: u64,
35        interval: String,
36    },
37    BalanceUpdate(HashMap<String, f64>),
38    OrderHistoryUpdate(OrderHistorySnapshot),
39    StrategyStatsUpdate {
40        strategy_stats: HashMap<String, OrderHistoryStats>,
41    },
42    AssetPnlUpdate {
43        by_symbol: HashMap<String, AssetPnlEntry>,
44    },
45    RiskRateSnapshot {
46        global: RateBudgetSnapshot,
47        orders: RateBudgetSnapshot,
48        account: RateBudgetSnapshot,
49        market_data: RateBudgetSnapshot,
50    },
51    TickDropped,
52    LogRecord(LogRecord),
53    LogMessage(String),
54    Error(String),
55}
56
57#[derive(Debug, Clone, Default)]
58pub struct AssetPnlEntry {
59    pub position_qty: f64,
60    pub realized_pnl_usdt: f64,
61    pub unrealized_pnl_usdt: f64,
62}
63
64#[derive(Debug, Clone, Copy, PartialEq, Eq)]
65pub enum LogLevel {
66    Debug,
67    Info,
68    Warn,
69    Error,
70}
71
72#[derive(Debug, Clone, Copy, PartialEq, Eq)]
73pub enum LogDomain {
74    Ws,
75    Strategy,
76    Risk,
77    Order,
78    Portfolio,
79    Ui,
80    System,
81}
82
83#[derive(Debug, Clone)]
84pub struct LogRecord {
85    pub ts_ms: u64,
86    pub level: LogLevel,
87    pub domain: LogDomain,
88    pub event: &'static str,
89    pub symbol: Option<String>,
90    pub strategy_tag: Option<String>,
91    pub trace_id: Option<String>,
92    pub msg: String,
93}
94
95impl LogRecord {
96    pub fn new(level: LogLevel, domain: LogDomain, event: &'static str, msg: impl Into<String>) -> Self {
97        Self {
98            ts_ms: chrono::Utc::now().timestamp_millis() as u64,
99            level,
100            domain,
101            event,
102            symbol: None,
103            strategy_tag: None,
104            trace_id: None,
105            msg: msg.into(),
106        }
107    }
108}