Skip to main content

deribit_base/model/
order_management.rs

1/******************************************************************************
2   Author: Joaquín Béjar García
3   Email: jb@taunais.com
4   Date: 21/7/25
5******************************************************************************/
6use crate::model::order::{OrderSide, OrderType, TimeInForce};
7use pretty_simple_display::{DebugPretty, DisplaySimple};
8
9use crate::model::request::{LinkedOrderType, TriggerFillCondition};
10use crate::prelude::{AdvancedOrderType, TriggerType};
11use serde::{Deserialize, Serialize};
12
13/// Buy order request
14#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
15pub struct BuyOrderRequest {
16    /// Name of the instrument to trade
17    pub instrument_name: String,
18    /// Amount/quantity to buy
19    pub amount: Option<f64>,
20    /// Number of contracts to buy
21    pub contracts: Option<f64>,
22    /// Type of order to place
23    pub type_: Option<OrderType>,
24    /// User-defined label for the order
25    pub label: Option<String>,
26    /// Order price (required for limit orders)
27    pub price: Option<f64>,
28    /// Time in force specification
29    pub time_in_force: Option<TimeInForce>,
30    /// Amount to display in the order book (iceberg orders)
31    pub display_amount: Option<f64>,
32    /// Whether this is a post-only order
33    pub post_only: Option<bool>,
34    /// If true, reject order if it would be a taker
35    pub reject_post_only: Option<bool>,
36    /// Whether this order only reduces position
37    pub reduce_only: Option<bool>,
38    /// Trigger price for stop/take orders
39    pub trigger_price: Option<f64>,
40    /// Trigger offset from mark price
41    pub trigger_offset: Option<f64>,
42    /// Trigger type (index_price, mark_price, last_price)
43    pub trigger: Option<TriggerType>,
44    /// Advanced order type (usd, implv)
45    pub advanced: Option<AdvancedOrderType>,
46    /// Market maker protection flag
47    pub mmp: Option<bool>,
48    /// Order validity timestamp in milliseconds
49    pub valid_until: Option<i64>,
50    /// Type of linked order (OTO, OCO, OTOCO)
51    pub linked_order_type: Option<LinkedOrderType>,
52    /// Trigger fill condition for linked orders
53    pub trigger_fill_condition: Option<TriggerFillCondition>,
54    /// OTOCO configuration for linked orders
55    pub otoco_config: Option<Vec<String>>,
56}
57
58/// Sell order request
59#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
60pub struct SellOrderRequest {
61    /// Name of the instrument to trade
62    pub instrument_name: String,
63    /// Amount/quantity to sell
64    pub amount: f64,
65    /// Order price (required for limit orders)
66    pub price: Option<f64>,
67    /// User-defined label for the order
68    pub label: Option<String>,
69    /// Time in force specification
70    pub time_in_force: Option<TimeInForce>,
71    /// Whether this order only reduces position
72    pub reduce_only: Option<bool>,
73    /// Whether this is a post-only order
74    pub post_only: Option<bool>,
75    /// Type of order to place
76    pub type_: Option<OrderType>,
77}
78
79/// Edit order request
80#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
81pub struct EditOrderRequest {
82    /// Unique identifier of the order to edit
83    pub order_id: String,
84    /// New order amount/quantity
85    pub amount: Option<f64>,
86    /// New order price
87    pub price: Option<f64>,
88    /// Whether this is a post-only order
89    pub post_only: Option<bool>,
90    /// Whether this order only reduces position
91    pub reduce_only: Option<bool>,
92    /// Time in force specification
93    pub time_in_force: Option<TimeInForce>,
94}
95
96/// Mass quote request item
97#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
98pub struct MassQuoteItem {
99    /// Name of the instrument to quote
100    pub instrument_name: String,
101    /// Order side (buy or sell)
102    pub side: OrderSide,
103    /// Quote amount/quantity
104    pub amount: f64,
105    /// Quote price
106    pub price: f64,
107}
108
109/// Mass quote request
110#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
111pub struct MassQuoteRequest {
112    /// List of quote items
113    pub items: Vec<MassQuoteItem>,
114    /// User-defined label for the mass quote
115    pub label: Option<String>,
116}
117
118/// Transfer result for order-related transfers (e.g., fee rebates)
119#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
120pub struct TransferResult {
121    /// Transfer identifier
122    pub id: String,
123    /// Transfer status
124    pub status: String,
125}
126
127/// Quote result
128#[derive(DebugPretty, DisplaySimple, Clone, Serialize, Deserialize)]
129pub struct QuoteResult {
130    /// Name of the instrument that was quoted
131    pub instrument_name: String,
132    /// Whether the quote was successful
133    pub success: bool,
134    /// Error message if quote failed
135    pub error: Option<String>,
136}
137
138#[cfg(test)]
139mod tests {
140    use super::*;
141
142    #[test]
143    fn test_mass_quote_request() {
144        let item = MassQuoteItem {
145            instrument_name: "BTC-PERPETUAL".to_string(),
146            side: OrderSide::Buy,
147            amount: 10.0,
148            price: 50000.0,
149        };
150        let req = MassQuoteRequest {
151            items: vec![item],
152            label: Some("test".to_string()),
153        };
154        let json = serde_json::to_string(&req).unwrap();
155        let de: MassQuoteRequest = serde_json::from_str(&json).unwrap();
156        assert_eq!(de.items.len(), 1);
157    }
158}