use crate::types::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum Command {
PlaceOrder(PlaceOrder),
PlaceOrders(Vec<PlaceOrder>),
CancelOrder(CancelOrder),
CancelAll(CancelAll),
StopStrategy(StopStrategy),
}
impl Command {
pub fn client_id(&self) -> Option<&ClientOrderId> {
match self {
Command::PlaceOrder(c) => Some(&c.client_id),
Command::PlaceOrders(orders) => orders.first().map(|o| &o.client_id),
Command::CancelOrder(c) => Some(&c.client_id),
Command::CancelAll(_) => None,
Command::StopStrategy(_) => None,
}
}
pub fn instrument(&self) -> Option<&InstrumentId> {
match self {
Command::PlaceOrder(c) => Some(&c.instrument),
Command::PlaceOrders(orders) => orders.first().map(|o| &o.instrument),
Command::CancelOrder(_) => None,
Command::CancelAll(c) => c.instrument.as_ref(),
Command::StopStrategy(_) => None,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PlaceOrder {
pub client_id: ClientOrderId,
pub exchange: ExchangeInstance,
pub instrument: InstrumentId,
pub side: OrderSide,
pub price: Price,
pub qty: Qty,
pub tif: TimeInForce,
pub post_only: bool,
pub reduce_only: bool,
}
impl PlaceOrder {
pub fn limit(
exchange: ExchangeInstance,
instrument: InstrumentId,
side: OrderSide,
price: Price,
qty: Qty,
) -> Self {
Self {
client_id: ClientOrderId::generate(),
exchange,
instrument,
side,
price,
qty,
tif: TimeInForce::Gtc,
post_only: false,
reduce_only: false,
}
}
pub fn with_tif(mut self, tif: TimeInForce) -> Self {
self.tif = tif;
self
}
pub fn post_only(mut self) -> Self {
self.post_only = true;
self
}
pub fn reduce_only(mut self) -> Self {
self.reduce_only = true;
self
}
pub fn with_client_id(mut self, client_id: ClientOrderId) -> Self {
self.client_id = client_id;
self
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CancelOrder {
pub exchange: ExchangeInstance,
pub client_id: ClientOrderId,
}
impl CancelOrder {
pub fn new(exchange: ExchangeInstance, client_id: ClientOrderId) -> Self {
Self {
exchange,
client_id,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CancelAll {
pub exchange: ExchangeInstance,
pub instrument: Option<InstrumentId>,
}
impl CancelAll {
pub fn new(exchange: ExchangeInstance) -> Self {
Self {
exchange,
instrument: None,
}
}
pub fn for_instrument(exchange: ExchangeInstance, instrument: InstrumentId) -> Self {
Self {
exchange,
instrument: Some(instrument),
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct StopStrategy {
pub strategy_id: StrategyId,
pub reason: String,
}
impl StopStrategy {
pub fn new(strategy_id: StrategyId, reason: impl Into<String>) -> Self {
Self {
strategy_id,
reason: reason.into(),
}
}
}