use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Order {
pub id: String,
pub symbol: String,
pub quantity: f64,
pub filled_quantity: f64,
pub price: Option<f64>,
pub stop_price: Option<f64>,
pub status: OrderStatus,
pub side: OrderSide,
pub order_type: OrderType,
pub time_in_force: TimeInForce,
pub extended_hours: bool,
pub created_at: DateTime<Utc>,
pub updated_at: DateTime<Utc>,
pub commission: f64,
pub rejected_reason: Option<String>,
pub average_fill_price: Option<f64>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum OrderStatus {
New,
PartiallyFilled,
Filled,
Canceled,
Rejected,
PendingCancel,
PendingNew,
PendingReplace,
Replaced,
Suspended,
Expired,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum OrderSide {
Buy,
Sell,
SellShort,
BuyToCover,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum OrderType {
Market,
Limit,
Stop,
StopLimit,
TrailingStop,
TrailingStopLimit,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "UPPERCASE")]
pub enum TimeInForce {
Day,
Gtc,
Gtd,
Ioc,
Fok,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderRequest {
pub symbol: String,
pub quantity: f64,
pub price: Option<f64>,
pub stop_price: Option<f64>,
pub side: OrderSide,
pub order_type: OrderType,
pub time_in_force: TimeInForce,
pub extended_hours: bool,
}
impl OrderRequest {
pub fn new() -> Self {
Self {
symbol: String::new(),
quantity: 0.0,
price: None,
stop_price: None,
side: OrderSide::Buy,
order_type: OrderType::Market,
time_in_force: TimeInForce::Day,
extended_hours: false,
}
}
pub fn symbol(mut self, symbol: impl Into<String>) -> Self {
self.symbol = symbol.into();
self
}
pub fn quantity(mut self, quantity: f64) -> Self {
self.quantity = quantity;
self
}
pub fn price(mut self, price: f64) -> Self {
self.price = Some(price);
self
}
pub fn stop_price(mut self, stop_price: f64) -> Self {
self.stop_price = Some(stop_price);
self
}
pub fn side(mut self, side: OrderSide) -> Self {
self.side = side;
self
}
pub fn order_type(mut self, order_type: OrderType) -> Self {
self.order_type = order_type;
self
}
pub fn time_in_force(mut self, time_in_force: TimeInForce) -> Self {
self.time_in_force = time_in_force;
self
}
pub fn extended_hours(mut self, extended_hours: bool) -> Self {
self.extended_hours = extended_hours;
self
}
}
impl Default for OrderRequest {
fn default() -> Self {
Self::new()
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderResponse {
pub id: String,
pub status: OrderStatus,
pub symbol: String,
pub quantity: f64,
pub price: Option<f64>,
pub stop_price: Option<f64>,
pub side: OrderSide,
pub order_type: OrderType,
pub time_in_force: TimeInForce,
pub extended_hours: bool,
pub created_at: DateTime<Utc>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrderQueryParams {
pub status: Option<OrderStatus>,
pub symbol: Option<String>,
pub start_date: Option<DateTime<Utc>>,
pub end_date: Option<DateTime<Utc>>,
pub limit: Option<u32>,
}
impl OrderQueryParams {
pub fn new() -> Self {
Self {
status: None,
symbol: None,
start_date: None,
end_date: None,
limit: None,
}
}
pub fn status(mut self, status: OrderStatus) -> Self {
self.status = Some(status);
self
}
pub fn symbol(mut self, symbol: impl Into<String>) -> Self {
self.symbol = Some(symbol.into());
self
}
pub fn start_date(mut self, start_date: DateTime<Utc>) -> Self {
self.start_date = Some(start_date);
self
}
pub fn end_date(mut self, end_date: DateTime<Utc>) -> Self {
self.end_date = Some(end_date);
self
}
pub fn limit(mut self, limit: u32) -> Self {
self.limit = Some(limit);
self
}
}
impl Default for OrderQueryParams {
fn default() -> Self {
Self::new()
}
}