Market types
Symbol
Represents a financial symbol with exchange information.
pub struct Symbol {
pub symbol: String,
pub exchange: String,
pub asset_class: AssetClass,
}
Ticker symbol (e.g., “AAPL”, “BTC-USD”)
Exchange identifier (e.g., “NASDAQ”, “BINANCE”)
Type of financial instrument
Methods:
impl Symbol {
pub fn new(symbol: &str, exchange: &str, asset_class: AssetClass) -> Self
pub fn equity(symbol: &str) -> Self
pub fn crypto(symbol: &str) -> Self
}
Example:
let aapl = Symbol::equity("AAPL");
let btc = Symbol::crypto("BTC-USD");
let custom = Symbol::new("EURUSD", "FOREX", AssetClass::Forex);
AssetClass
Asset classes supported by the platform.
pub enum AssetClass {
Equity,
Crypto,
Forex,
Commodity,
Bond,
}
Methods:
impl AssetClass {
pub fn is_24_7(&self) -> bool
pub fn supports_fractional_quantities(&self) -> bool
pub fn default_exchange(&self) -> &'static str
}
Example:
assert!(AssetClass::Crypto.is_24_7());
assert!(AssetClass::Crypto.supports_fractional_quantities());
assert_eq!(AssetClass::Crypto.default_exchange(), "BINANCE");
Bar
OHLCV bar data with volume and timestamp.
pub struct Bar {
pub symbol: Symbol,
pub timestamp: DateTime<Utc>,
pub open: Decimal,
pub high: Decimal,
pub low: Decimal,
pub close: Decimal,
pub volume: Decimal,
pub resolution: Resolution,
}
Highest price during period
Lowest price during period
Time resolution (minute, hour, day, etc.)
Methods:
impl Bar {
pub fn new(
symbol: Symbol,
timestamp: DateTime<Utc>,
open: Decimal,
high: Decimal,
low: Decimal,
close: Decimal,
volume: Decimal,
resolution: Resolution,
) -> Self
pub fn typical_price(&self) -> Decimal
pub fn true_range(&self, prev_close: Option<Decimal>) -> Decimal
}
Resolution
Time resolution for market data.
pub enum Resolution {
Tick,
Second,
Minute,
FiveMinute,
FifteenMinute,
Hour,
FourHour,
Day,
Week,
Month,
}
Methods:
impl Resolution {
pub fn to_seconds(&self) -> Option<u64>
}
Order types
Order
Order request from strategy.
pub struct Order {
pub id: OrderId,
pub symbol: Symbol,
pub side: Side,
pub quantity: Decimal,
pub order_type: OrderType,
pub time_in_force: TimeInForce,
pub submitted_at: DateTime<Utc>,
pub status: OrderStatus,
pub filled_quantity: Decimal,
pub remaining_quantity: Decimal,
pub average_fill_price: Option<Decimal>,
pub strategy_id: String,
pub metadata: serde_json::Value,
}
Unique order identifier (UUID)
Market, Limit, Stop, or StopLimit
Methods:
impl Order {
pub fn new(
symbol: Symbol,
side: Side,
quantity: Decimal,
order_type: OrderType,
strategy_id: String,
) -> Self
pub fn market_order(symbol: Symbol, side: Side, quantity: Decimal, strategy_id: String) -> Self
pub fn limit_order(symbol: Symbol, side: Side, quantity: Decimal, price: Decimal, strategy_id: String) -> Self
pub fn stop_order(symbol: Symbol, side: Side, quantity: Decimal, stop_price: Decimal, strategy_id: String) -> Self
pub fn is_buy(&self) -> bool
pub fn is_sell(&self) -> bool
pub fn is_filled(&self) -> bool
pub fn is_active(&self) -> bool
pub fn fill(&mut self, quantity: Decimal, price: Decimal)
pub fn cancel(&mut self)
}
Example:
let order = Order::market_order(
Symbol::equity("AAPL"),
Side::Buy,
Decimal::from(100),
"my_strategy".to_string()
);
Side
Direction of an order.
pub enum Side {
Buy,
Sell,
}
Methods:
impl Side {
pub fn opposite(&self) -> Side
pub fn sign(&self) -> i32
}
OrderType
Order types supported by the engine.
pub enum OrderType {
Market,
Limit { price: Decimal },
Stop { stop_price: Decimal },
StopLimit { stop_price: Decimal, limit_price: Decimal },
}
Fill
Order execution record.
pub struct Fill {
pub id: Uuid,
pub order_id: OrderId,
pub symbol: Symbol,
pub side: Side,
pub quantity: Decimal,
pub price: Decimal,
pub commission: Decimal,
pub executed_at: DateTime<Utc>,
pub strategy_id: String,
}
Commission charged for this fill
Methods:
impl Fill {
pub fn gross_amount(&self) -> Decimal
pub fn net_amount(&self) -> Decimal
}
Portfolio types
Portfolio
Portfolio state and management.
pub struct Portfolio {
pub account_id: String,
pub initial_capital: Decimal,
pub cash: Decimal,
pub positions: HashMap<Symbol, Position>,
pub total_equity: Decimal,
pub total_pnl: Decimal,
pub total_realized_pnl: Decimal,
pub total_unrealized_pnl: Decimal,
pub total_commissions: Decimal,
pub last_updated: DateTime<Utc>,
pub daily_returns: Vec<DailyReturn>,
}
positions
HashMap<Symbol, Position>
Current positions by symbol
Total portfolio value (cash + positions)
Total profit and loss (realized + unrealized)
Methods:
impl Portfolio {
pub fn new(account_id: String, initial_capital: Decimal) -> Self
pub fn apply_fill(&mut self, fill: &Fill)
pub fn update_market_prices(&mut self, prices: &HashMap<Symbol, Decimal>)
pub fn get_position(&self, symbol: &Symbol) -> Option<&Position>
pub fn get_available_cash(&self) -> Decimal
pub fn get_total_return(&self) -> Decimal
pub fn get_sharpe_ratio(&self, risk_free_rate: Decimal) -> Option<Decimal>
pub fn get_max_drawdown(&self) -> Decimal
pub fn add_daily_return(&mut self, date: DateTime<Utc>, daily_return: Decimal)
}
Example:
let mut portfolio = Portfolio::new("my_account".to_string(), Decimal::from(100000));
// Apply a fill
let fill = Fill::new(
order_id,
Symbol::equity("AAPL"),
Side::Buy,
Decimal::from(10),
Decimal::from(150),
Decimal::from(1),
"strategy_1".to_string()
);
portfolio.apply_fill(&fill);
// Check position
if let Some(position) = portfolio.get_position(&Symbol::equity("AAPL")) {
println!("Position: {} shares at avg price {}", position.quantity, position.average_price);
}
Position
Portfolio position for a specific symbol.
pub struct Position {
pub symbol: Symbol,
pub quantity: Decimal,
pub average_price: Decimal,
pub market_value: Decimal,
pub unrealized_pnl: Decimal,
pub realized_pnl: Decimal,
pub last_updated: DateTime<Utc>,
}
Position size (positive for long, negative for short)
Current market value of position
Realized profit/loss from closed portions
Methods:
impl Position {
pub fn new(symbol: Symbol) -> Self
pub fn is_long(&self) -> bool
pub fn is_short(&self) -> bool
pub fn is_flat(&self) -> bool
pub fn apply_fill(&mut self, fill: &Fill)
pub fn update_market_price(&mut self, market_price: Decimal)
pub fn total_pnl(&self) -> Decimal
}