Skip to main content

perpl_sdk/types/
order.rs

1use std::fmt::Display;
2
3/// Type of the placed order.
4///
5/// Bid Order Types:
6/// * [`OrderType::OpenLong`] is used to open a long position (or to decrease,
7///   close, or invert a long position). The only restrictions applied are the
8///   user account must have sufficient collateral available.
9/// * [`OrderType::CloseShort`] is a reduce only order type and can only be used
10///   to close all or part of an existing short position on the perpetual
11///   contract.
12///
13/// Ask Order Types:
14/// * [`OrderType::OpenShort`] is used to open a short position (or to decrease,
15///   close, or invert a short position). The only restrictions applied are the
16///   user account must have sufficient collateral available.
17/// * [`OrderType::CloseLong`] is a reduce only order type and can only be used
18///   to close all or part of an existing long position on the perpetual
19///   contract.
20#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)]
21pub enum OrderType {
22    OpenLong,
23    OpenShort,
24    CloseLong,
25    CloseShort,
26}
27
28/// Side of the order.
29#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
30pub enum OrderSide {
31    Ask,
32    Bid,
33}
34
35impl OrderType {
36    pub fn side(&self) -> OrderSide {
37        match self {
38            OrderType::OpenLong | OrderType::CloseShort => OrderSide::Bid,
39            OrderType::OpenShort | OrderType::CloseLong => OrderSide::Ask,
40        }
41    }
42}
43
44impl OrderSide {
45    pub fn opposite(&self) -> OrderSide {
46        match self {
47            OrderSide::Ask => OrderSide::Bid,
48            OrderSide::Bid => OrderSide::Ask,
49        }
50    }
51}
52
53impl From<u8> for OrderType {
54    fn from(value: u8) -> Self {
55        match value {
56            0 => OrderType::OpenLong,
57            1 => OrderType::OpenShort,
58            2 => OrderType::CloseLong,
59            3 => OrderType::CloseShort,
60            _ => unreachable!(),
61        }
62    }
63}
64
65impl Display for OrderType {
66    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
67        if f.alternate() {
68            match self {
69                OrderType::OpenLong => write!(f, "OL"),
70                OrderType::OpenShort => write!(f, "OS"),
71                OrderType::CloseLong => write!(f, "CL"),
72                OrderType::CloseShort => write!(f, "CS"),
73            }
74        } else {
75            match self {
76                OrderType::OpenLong => write!(f, "Open Long"),
77                OrderType::OpenShort => write!(f, "Open Short"),
78                OrderType::CloseLong => write!(f, "Close Long"),
79                OrderType::CloseShort => write!(f, "Close Short"),
80            }
81        }
82    }
83}
84
85impl Display for OrderSide {
86    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
87        match self {
88            OrderSide::Ask => write!(f, "Ask"),
89            OrderSide::Bid => write!(f, "Bid"),
90        }
91    }
92}