af_iperps/
order_helpers.rs

1use std::ops::Not;
2
3use serde::{Deserialize, Serialize};
4
5#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
6pub struct OrderDetails {
7    pub account_id: u64,
8    pub price: u64,
9    pub size: u64,
10}
11
12#[derive(Clone, Copy, Debug, clap::ValueEnum, Serialize, Deserialize, Eq, PartialEq)]
13#[serde(into = "bool")]
14pub enum Side {
15    Bid,
16    Ask,
17}
18
19impl Not for Side {
20    type Output = Self;
21
22    fn not(self) -> Self::Output {
23        match self {
24            Self::Bid => Self::Ask,
25            Self::Ask => Self::Bid,
26        }
27    }
28}
29
30impl From<Side> for bool {
31    fn from(value: Side) -> Self {
32        match value {
33            Side::Bid => false,
34            Side::Ask => true,
35        }
36    }
37}
38
39impl From<bool> for Side {
40    fn from(value: bool) -> Self {
41        match value {
42            false => Self::Bid,
43            true => Self::Ask,
44        }
45    }
46}
47
48#[derive(Clone, Copy, Debug, clap::ValueEnum, Serialize, Deserialize)]
49#[serde(into = "u64")]
50pub enum OrderType {
51    Standard,
52    /// Mandates that the entire order size be filled in the current transaction. Otherwise, the
53    /// order is canceled.
54    FillOrKill,
55    /// Mandates that the entire order not be filled at all in the current transaction. Otherwise,
56    /// cancel the order.
57    PostOnly,
58    /// Mandates that maximal possible part of an order will be filled in the current transaction.
59    /// The rest of the order canceled.
60    ImmediateOrCancel,
61}
62
63impl From<OrderType> for u64 {
64    fn from(value: OrderType) -> Self {
65        match value {
66            OrderType::Standard => 0,
67            OrderType::FillOrKill => 1,
68            OrderType::PostOnly => 2,
69            OrderType::ImmediateOrCancel => 3,
70        }
71    }
72}
73
74#[derive(thiserror::Error, Debug)]
75#[error("Invalid order type value")]
76pub struct InvalidOrderTypeValue;
77
78impl TryFrom<u64> for OrderType {
79    type Error = InvalidOrderTypeValue;
80
81    fn try_from(value: u64) -> Result<Self, Self::Error> {
82        match value {
83            0 => Ok(Self::Standard),
84            1 => Ok(Self::FillOrKill),
85            2 => Ok(Self::PostOnly),
86            3 => Ok(Self::ImmediateOrCancel),
87            _ => Err(InvalidOrderTypeValue),
88        }
89    }
90}