use chrono::NaiveDate; use serde::{Deserialize, Serialize}; mod date_format { use chrono::NaiveDate; use serde::{self, Deserialize, Deserializer, Serializer}; const FORMAT: &str = "%Y-%m-%d"; pub fn serialize(date: &NaiveDate, serializer: S) -> Result where S: Serializer, { serializer.serialize_str(&date.format(FORMAT).to_string()) } pub fn deserialize<'de, D>(deserializer: D) -> Result where D: Deserializer<'de>, { let text = String::deserialize(deserializer)?; NaiveDate::parse_from_str(&text, FORMAT).map_err(serde::de::Error::custom) } } #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] pub enum OrderSide { Buy, Sell, } impl OrderSide { pub fn as_str(&self) -> &'static str { match self { Self::Buy => "buy", Self::Sell => "sell", } } } #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq)] pub enum OrderStatus { Pending, Filled, PartiallyFilled, Canceled, Rejected, } impl OrderStatus { pub fn as_str(&self) -> &'static str { match self { Self::Pending => "pending", Self::Filled => "filled", Self::PartiallyFilled => "partially_filled", Self::Canceled => "canceled", Self::Rejected => "rejected", } } } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct OrderEvent { #[serde(with = "date_format")] pub date: NaiveDate, #[serde(default)] pub order_id: Option, pub symbol: String, pub side: OrderSide, pub requested_quantity: u32, pub filled_quantity: u32, pub status: OrderStatus, pub reason: String, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct FillEvent { #[serde(with = "date_format")] pub date: NaiveDate, #[serde(default)] pub order_id: Option, pub symbol: String, pub side: OrderSide, pub quantity: u32, pub price: f64, pub gross_amount: f64, pub commission: f64, pub stamp_tax: f64, pub net_cash_flow: f64, pub reason: String, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct PositionEvent { #[serde(with = "date_format")] pub date: NaiveDate, pub symbol: String, pub delta_quantity: i32, pub quantity_after: u32, pub average_cost: f64, pub realized_pnl_delta: f64, pub reason: String, } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct AccountEvent { #[serde(with = "date_format")] pub date: NaiveDate, pub cash_before: f64, pub cash_after: f64, pub total_equity: f64, pub note: String, } #[derive(Debug, Clone, Copy, Serialize, Deserialize, PartialEq, Eq, PartialOrd, Ord)] pub enum ProcessEventKind { PreBeforeTrading, BeforeTrading, PostBeforeTrading, PreOpenAuction, OpenAuction, PostOpenAuction, PreBar, Bar, PostBar, PreTick, Tick, PostTick, PreScheduled, PostScheduled, PreOnDay, OnDay, PostOnDay, PreAfterTrading, AfterTrading, PostAfterTrading, PreSettlement, Settlement, PostSettlement, OrderPendingNew, OrderCreationPass, OrderCreationReject, OrderPendingCancel, OrderCancellationPass, OrderCancellationReject, OrderUnsolicitedUpdate, Trade, UniverseUpdated, UniverseSubscribed, UniverseUnsubscribed, AccountDepositWithdraw, AccountFinanceRepay, AccountManagementFee, } impl ProcessEventKind { pub fn as_str(&self) -> &'static str { match self { Self::PreBeforeTrading => "pre_before_trading", Self::BeforeTrading => "before_trading", Self::PostBeforeTrading => "post_before_trading", Self::PreOpenAuction => "pre_open_auction", Self::OpenAuction => "open_auction", Self::PostOpenAuction => "post_open_auction", Self::PreBar => "pre_bar", Self::Bar => "bar", Self::PostBar => "post_bar", Self::PreTick => "pre_tick", Self::Tick => "tick", Self::PostTick => "post_tick", Self::PreScheduled => "pre_scheduled", Self::PostScheduled => "post_scheduled", Self::PreOnDay => "pre_on_day", Self::OnDay => "on_day", Self::PostOnDay => "post_on_day", Self::PreAfterTrading => "pre_after_trading", Self::AfterTrading => "after_trading", Self::PostAfterTrading => "post_after_trading", Self::PreSettlement => "pre_settlement", Self::Settlement => "settlement", Self::PostSettlement => "post_settlement", Self::OrderPendingNew => "order_pending_new", Self::OrderCreationPass => "order_creation_pass", Self::OrderCreationReject => "order_creation_reject", Self::OrderPendingCancel => "order_pending_cancel", Self::OrderCancellationPass => "order_cancellation_pass", Self::OrderCancellationReject => "order_cancellation_reject", Self::OrderUnsolicitedUpdate => "order_unsolicited_update", Self::Trade => "trade", Self::UniverseUpdated => "universe_updated", Self::UniverseSubscribed => "universe_subscribed", Self::UniverseUnsubscribed => "universe_unsubscribed", Self::AccountDepositWithdraw => "account_deposit_withdraw", Self::AccountFinanceRepay => "account_finance_repay", Self::AccountManagementFee => "account_management_fee", } } } #[derive(Debug, Clone, Serialize, Deserialize)] pub struct ProcessEvent { #[serde(with = "date_format")] pub date: NaiveDate, pub kind: ProcessEventKind, #[serde(default)] pub order_id: Option, #[serde(default)] pub symbol: Option, #[serde(default)] pub side: Option, pub detail: String, }