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