use std::fmt;
use serde::{Deserialize, Serialize};
use crate::num::{Decimal, Price, Qty, Rounding, is_multiple};
use crate::order::{OrderRequest, OrderSize, OrderType};
use crate::{ExchangeError, RejectReason};
macro_rules! id_newtype {
($(#[$m:meta])* $name:ident) => {
$(#[$m])*
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct $name(pub String);
impl $name {
pub fn new(s: impl Into<String>) -> Self {
Self(s.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for $name {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<&str> for $name {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
};
}
id_newtype!(
VenueId
);
id_newtype!(
ProductId
);
id_newtype!(
Asset
);
id_newtype!(
AccountId
);
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct Market {
pub venue: VenueId,
pub product: ProductId,
}
impl Market {
pub fn new(venue: impl Into<String>, product: impl Into<String>) -> Self {
Self { venue: VenueId::new(venue), product: ProductId::new(product) }
}
}
impl fmt::Display for Market {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}:{}", self.venue, self.product)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
#[non_exhaustive]
pub enum InstrumentKind {
Spot,
DatedFuture {
expiry_ms: i64,
contract_size: Decimal,
},
Perpetual {
contract_size: Decimal,
funding_interval_ms: i64,
},
}
impl InstrumentKind {
pub fn is_derivative(&self) -> bool {
!matches!(self, InstrumentKind::Spot)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum ProductStatus {
#[default]
Online,
PostOnly,
LimitOnly,
CancelOnly,
Halted,
Delisted,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Product {
pub market: Market,
pub base: Asset,
pub quote: Asset,
pub kind: InstrumentKind,
pub price_increment: Decimal,
pub size_increment: Decimal,
pub min_size: Qty,
pub max_size: Option<Qty>,
pub min_notional: Decimal,
pub status: ProductStatus,
}
impl Product {
pub fn spot(market: Market, base: impl Into<String>, quote: impl Into<String>) -> Self {
Self {
market,
base: Asset::new(base),
quote: Asset::new(quote),
kind: InstrumentKind::Spot,
price_increment: Decimal::ZERO,
size_increment: Decimal::ZERO,
min_size: Qty::ZERO,
max_size: None,
min_notional: Decimal::ZERO,
status: ProductStatus::Online,
}
}
pub fn with_increments(mut self, price: Decimal, size: Decimal) -> Self {
self.price_increment = price;
self.size_increment = size;
self
}
pub fn with_limits(mut self, min_size: Qty, max_size: Option<Qty>, min_notional: Decimal) -> Self {
self.min_size = min_size;
self.max_size = max_size;
self.min_notional = min_notional;
self
}
pub fn with_kind(mut self, kind: InstrumentKind) -> Self {
self.kind = kind;
self
}
pub fn with_status(mut self, status: ProductStatus) -> Self {
self.status = status;
self
}
pub fn round_price(&self, p: Price, mode: Rounding) -> Price {
p.round_to(self.price_increment, mode)
}
pub fn round_size(&self, q: Qty) -> Qty {
q.round_to(self.size_increment, Rounding::Down)
}
pub fn check_order(&self, r: &OrderRequest) -> Result<(), ExchangeError> {
let reject = |reason, msg: String| Err(ExchangeError::rejected(reason, msg));
if r.market != self.market {
return reject(RejectReason::UnknownProduct, format!("order for {} checked against {}", r.market, self.market));
}
let limit = r.limit_price();
match self.status {
ProductStatus::Online => {}
ProductStatus::LimitOnly if limit.is_some() => {}
ProductStatus::PostOnly if matches!(r.order_type, OrderType::Limit { post_only: true, .. }) => {}
s => return reject(RejectReason::TradingDisabled, format!("{} is {s:?}", self.market)),
}
for p in [limit, r.stop_price()].into_iter().flatten() {
if !p.is_positive() || !is_multiple(p.0, self.price_increment) {
return reject(RejectReason::BadIncrement, format!("price {p} is not a positive multiple of {}", self.price_increment));
}
}
match r.size {
OrderSize::Base(q) => {
if !q.is_positive() || !is_multiple(q.0, self.size_increment) {
return reject(RejectReason::BadIncrement, format!("size {q} is not a positive multiple of {}", self.size_increment));
}
if q < self.min_size {
return reject(RejectReason::BelowMinSize, format!("size {q} is below {}", self.min_size));
}
if self.max_size.is_some_and(|m| q > m) {
return reject(RejectReason::AboveMaxSize, format!("size {q} is above the maximum"));
}
if let Some(p) = limit
&& p * q < self.min_notional
{
return reject(RejectReason::BelowMinNotional, format!("notional {} is below {}", p * q, self.min_notional));
}
}
OrderSize::Quote(amount) => {
if !matches!(r.order_type, OrderType::Market) {
return Err(ExchangeError::invalid("a quote-sized order must be a market order"));
}
if amount <= Decimal::ZERO {
return reject(RejectReason::BelowMinNotional, "quote amount must be positive".into());
}
if amount < self.min_notional {
return reject(RejectReason::BelowMinNotional, format!("notional {amount} is below {}", self.min_notional));
}
}
}
Ok(())
}
}