af_iperps/
order_helpers.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
use std::ops::Not;

use serde::{Deserialize, Serialize};

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
pub struct OrderDetails {
    pub account_id: u64,
    pub price: u64,
    pub size: u64,
}

#[derive(Clone, Copy, Debug, clap::ValueEnum, Serialize, Deserialize, Eq, PartialEq)]
#[serde(into = "bool")]
pub enum Side {
    Bid,
    Ask,
}

impl Not for Side {
    type Output = Self;

    fn not(self) -> Self::Output {
        match self {
            Self::Bid => Self::Ask,
            Self::Ask => Self::Bid,
        }
    }
}

impl From<Side> for bool {
    fn from(value: Side) -> Self {
        match value {
            Side::Bid => false,
            Side::Ask => true,
        }
    }
}

impl From<bool> for Side {
    fn from(value: bool) -> Self {
        match value {
            false => Self::Bid,
            true => Self::Ask,
        }
    }
}

#[derive(Clone, Copy, Debug, clap::ValueEnum, Serialize, Deserialize)]
#[serde(into = "u64")]
pub enum OrderType {
    Standard,
    /// Mandates that the entire order size be filled in the current transaction. Otherwise, the
    /// order is canceled.
    FillOrKill,
    /// Mandates that the entire order not be filled at all in the current transaction. Otherwise,
    /// cancel the order.
    PostOnly,
    /// Mandates that maximal possible part of an order will be filled in the current transaction.
    /// The rest of the order canceled.
    ImmediateOrCancel,
}

impl From<OrderType> for u64 {
    fn from(value: OrderType) -> Self {
        match value {
            OrderType::Standard => 0,
            OrderType::FillOrKill => 1,
            OrderType::PostOnly => 2,
            OrderType::ImmediateOrCancel => 3,
        }
    }
}

#[derive(thiserror::Error, Debug)]
#[error("Invalid order type value")]
pub struct InvalidOrderTypeValue;

impl TryFrom<u64> for OrderType {
    type Error = InvalidOrderTypeValue;

    fn try_from(value: u64) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(Self::Standard),
            1 => Ok(Self::FillOrKill),
            2 => Ok(Self::PostOnly),
            3 => Ok(Self::ImmediateOrCancel),
            _ => Err(InvalidOrderTypeValue),
        }
    }
}