deribit_base/model/order.rs
1/******************************************************************************
2 Author: Joaquín Béjar García
3 Email: jb@taunais.com
4 Date: 21/7/25
5******************************************************************************/
6use crate::{impl_json_debug_pretty, impl_json_display};
7use serde::{Deserialize, Serialize};
8
9/// Time in force enumeration
10#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
11pub enum TimeInForce {
12 /// Order remains active until explicitly cancelled
13 #[serde(rename = "good_til_cancelled")]
14 GoodTilCancelled,
15 /// Order expires at the end of the trading day
16 #[serde(rename = "good_til_day")]
17 GoodTilDay,
18 /// Order must be filled immediately and completely or cancelled
19 #[serde(rename = "fill_or_kill")]
20 FillOrKill,
21 /// Order must be filled immediately, partial fills allowed, remaining cancelled
22 #[serde(rename = "immediate_or_cancel")]
23 ImmediateOrCancel,
24}
25
26impl TimeInForce {
27 /// Returns the string representation of the time in force value
28 pub fn as_str(&self) -> &'static str {
29 match self {
30 TimeInForce::GoodTilCancelled => "good_til_cancelled",
31 TimeInForce::GoodTilDay => "good_til_day",
32 TimeInForce::FillOrKill => "fill_or_kill",
33 TimeInForce::ImmediateOrCancel => "immediate_or_cancel",
34 }
35 }
36}
37
38/// Order side enumeration
39#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
40pub enum OrderSide {
41 /// Buy order
42 Buy,
43 /// Sell order
44 Sell,
45}
46
47/// Order type enum
48#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
49pub enum OrderType {
50 /// Limit order - executes at specified price or better
51 #[serde(rename = "limit")]
52 Limit,
53 /// Market order - executes immediately at best available price
54 #[serde(rename = "market")]
55 Market,
56 /// Stop limit order - becomes limit order when stop price is reached
57 #[serde(rename = "stop_limit")]
58 StopLimit,
59 /// Stop market order - becomes market order when stop price is reached
60 #[serde(rename = "stop_market")]
61 StopMarket,
62 /// Take limit order - limit order to take profit
63 #[serde(rename = "take_limit")]
64 TakeLimit,
65 /// Take market order - market order to take profit
66 #[serde(rename = "take_market")]
67 TakeMarket,
68 /// Market limit order - market order with limit price protection
69 #[serde(rename = "market_limit")]
70 MarketLimit,
71 /// Trailing stop order - stop order that trails the market price
72 #[serde(rename = "trailing_stop")]
73 TrailingStop,
74}
75
76impl OrderType {
77 /// Returns the string representation of the order type
78 pub fn as_str(&self) -> &'static str {
79 match self {
80 OrderType::Limit => "limit",
81 OrderType::Market => "market",
82 OrderType::StopLimit => "stop_limit",
83 OrderType::StopMarket => "stop_market",
84 OrderType::TakeLimit => "take_limit",
85 OrderType::TakeMarket => "take_market",
86 OrderType::MarketLimit => "market_limit",
87 OrderType::TrailingStop => "trailing_stop",
88 }
89 }
90}
91
92/// New order request structure
93#[derive(Clone, Serialize, Deserialize)]
94pub struct NewOrderRequest {
95 /// Trading symbol/instrument name
96 pub symbol: String,
97 /// Order side (buy or sell)
98 pub side: OrderSide,
99 /// Type of order
100 pub order_type: OrderType,
101 /// Order quantity
102 pub quantity: f64,
103 /// Order price (required for limit orders)
104 pub price: Option<f64>,
105 /// Time in force specification
106 pub time_in_force: TimeInForce,
107 /// Client-specified order identifier
108 pub client_order_id: Option<String>,
109}
110
111/// Order status enumeration
112#[derive(Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
113pub enum OrderStatus {
114 /// Order has been accepted by the system
115 New,
116 /// Order has been partially filled
117 PartiallyFilled,
118 /// Order has been completely filled
119 Filled,
120 /// Order is done for the day
121 DoneForDay,
122 /// Order has been cancelled
123 Canceled,
124 /// Order has been replaced
125 Replaced,
126 /// Order cancellation is pending
127 PendingCancel,
128 /// Order has been stopped
129 Stopped,
130 /// Order has been rejected
131 Rejected,
132 /// Order has been suspended
133 Suspended,
134 /// Order is pending acceptance
135 PendingNew,
136 /// Order has been calculated
137 Calculated,
138 /// Order has expired
139 Expired,
140 /// Order has been accepted for bidding
141 AcceptedForBidding,
142 /// Order replacement is pending
143 PendingReplace,
144}
145
146/// Order information
147#[derive(Clone, Serialize, Deserialize)]
148pub struct OrderInfo {
149 /// Order amount
150 pub amount: f64,
151 /// Whether order was placed via API
152 pub api: bool,
153 /// Average execution price
154 pub average_price: f64,
155 /// Order creation timestamp
156 pub creation_timestamp: u64,
157 /// Order direction (buy/sell)
158 pub direction: String,
159 /// Amount that has been filled
160 pub filled_amount: f64,
161 /// Instrument name
162 pub instrument_name: String,
163 /// Whether this is a liquidation order
164 pub is_liquidation: bool,
165 /// Order label
166 pub label: String,
167 /// Last update timestamp
168 pub last_update_timestamp: u64,
169 /// Maximum amount to show in order book (optional)
170 pub max_show: Option<f64>,
171 /// Unique order identifier
172 pub order_id: String,
173 /// Current order state
174 pub order_state: String,
175 /// Type of order
176 pub order_type: String,
177 /// Original order type before any modifications
178 pub original_order_type: Option<String>,
179 /// Whether this is a post-only order
180 pub post_only: bool,
181 /// Order price
182 pub price: f64,
183 /// Current profit/loss on the order
184 pub profit_loss: Option<f64>,
185 /// Whether this order only reduces position
186 pub reduce_only: bool,
187 /// Whether this order has been replaced
188 pub replaced: bool,
189 /// Whether this order reduces risk
190 pub risk_reducing: bool,
191 /// Time in force specification
192 pub time_in_force: String,
193 /// Whether the order has been triggered
194 pub triggered: Option<bool>,
195 /// Trigger condition for the order
196 pub trigger: Option<String>,
197 /// USD value of the order
198 pub usd: Option<f64>,
199 /// Whether order was placed via web interface
200 pub web: bool,
201}
202
203impl_json_debug_pretty!(
204 TimeInForce,
205 OrderSide,
206 OrderType,
207 NewOrderRequest,
208 OrderStatus,
209 OrderInfo
210);
211impl_json_display!(
212 TimeInForce,
213 OrderSide,
214 OrderType,
215 NewOrderRequest,
216 OrderStatus,
217 OrderInfo
218);