deribit-fix 0.3.1

This crate provides a client for the Deribit Markets API using the FIX protocol.
Documentation
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/******************************************************************************
   Author: Joaquín Béjar García
   Email: jb@taunais.com
   Date: 10/8/25
******************************************************************************/

//! Quote Request FIX Message Implementation

use crate::error::Result as DeribitFixResult;
use crate::message::builder::MessageBuilder;
use crate::message::orders::{OrderSide, TimeInForce};
use crate::model::types::MsgType;
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};

/// Quote type enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum QuoteType {
    /// Indicative quote
    Indicative,
    /// Tradeable quote
    Tradeable,
    /// Restricted tradeable quote
    RestrictedTradeable,
    /// Counter quote
    Counter,
}

impl From<QuoteType> for i32 {
    fn from(quote_type: QuoteType) -> Self {
        match quote_type {
            QuoteType::Indicative => 0,
            QuoteType::Tradeable => 1,
            QuoteType::RestrictedTradeable => 2,
            QuoteType::Counter => 3,
        }
    }
}

impl TryFrom<i32> for QuoteType {
    type Error = String;

    fn try_from(value: i32) -> Result<Self, Self::Error> {
        match value {
            0 => Ok(QuoteType::Indicative),
            1 => Ok(QuoteType::Tradeable),
            2 => Ok(QuoteType::RestrictedTradeable),
            3 => Ok(QuoteType::Counter),
            _ => Err(format!("Invalid QuoteType: {}", value)),
        }
    }
}

/// Quote Request message (MsgType = 'R')
#[derive(Clone, PartialEq, Serialize, Deserialize)]
pub struct QuoteRequest {
    /// Quote request ID
    pub quote_req_id: String,
    /// Instrument symbol
    pub symbol: String,
    /// Quote type
    pub quote_type: QuoteType,
    /// Side of quote request
    pub side: OrderSide,
    /// Order quantity
    pub order_qty: f64,
    /// Valid until time
    pub valid_until_time: Option<DateTime<Utc>>,
    /// Quote request type (0=Manual, 1=Automatic)
    pub quote_request_type: Option<i32>,
    /// Time in force
    pub time_in_force: Option<TimeInForce>,
    /// Minimum quantity
    pub min_qty: Option<f64>,
    /// Settlement type
    pub settlement_type: Option<char>,
    /// Custom label
    pub deribit_label: Option<String>,
    /// Market segment ID
    pub market_segment_id: Option<String>,
    /// Total volume traded (tag 387) - For RFQ subscription responses
    pub total_volume_traded: Option<f64>,
    /// Transaction time (tag 60) - Update time for last trade or when requested
    pub transact_time: Option<DateTime<Utc>>,
}

impl QuoteRequest {
    /// Create a new quote request
    pub fn new(
        quote_req_id: String,
        symbol: String,
        quote_type: QuoteType,
        side: OrderSide,
        order_qty: f64,
    ) -> Self {
        Self {
            quote_req_id,
            symbol,
            quote_type,
            side,
            order_qty,
            valid_until_time: None,
            quote_request_type: None,
            time_in_force: None,
            min_qty: None,
            settlement_type: None,
            deribit_label: None,
            market_segment_id: None,
            total_volume_traded: None,
            transact_time: None,
        }
    }

    /// Create a tradeable quote request
    pub fn tradeable(
        quote_req_id: String,
        symbol: String,
        side: OrderSide,
        order_qty: f64,
    ) -> Self {
        Self::new(quote_req_id, symbol, QuoteType::Tradeable, side, order_qty)
    }

    /// Create an indicative quote request
    pub fn indicative(
        quote_req_id: String,
        symbol: String,
        side: OrderSide,
        order_qty: f64,
    ) -> Self {
        Self::new(quote_req_id, symbol, QuoteType::Indicative, side, order_qty)
    }

    /// Set valid until time
    pub fn with_valid_until(mut self, valid_until: DateTime<Utc>) -> Self {
        self.valid_until_time = Some(valid_until);
        self
    }

    /// Set quote request type
    pub fn with_quote_request_type(mut self, request_type: i32) -> Self {
        self.quote_request_type = Some(request_type);
        self
    }

    /// Set time in force
    pub fn with_time_in_force(mut self, tif: TimeInForce) -> Self {
        self.time_in_force = Some(tif);
        self
    }

    /// Set minimum quantity
    pub fn with_min_qty(mut self, min_qty: f64) -> Self {
        self.min_qty = Some(min_qty);
        self
    }

    /// Set settlement type
    pub fn with_settlement_type(mut self, settlement_type: char) -> Self {
        self.settlement_type = Some(settlement_type);
        self
    }

    /// Set custom label
    pub fn with_label(mut self, label: String) -> Self {
        self.deribit_label = Some(label);
        self
    }

    /// Set market segment ID
    pub fn with_market_segment_id(mut self, segment_id: String) -> Self {
        self.market_segment_id = Some(segment_id);
        self
    }

    /// Set total volume traded (tag 387)
    #[must_use]
    pub fn with_total_volume_traded(mut self, volume: f64) -> Self {
        self.total_volume_traded = Some(volume);
        self
    }

    /// Set transaction time (tag 60)
    #[must_use]
    pub fn with_transact_time(mut self, time: DateTime<Utc>) -> Self {
        self.transact_time = Some(time);
        self
    }

    /// Convert to FIX message
    pub fn to_fix_message(
        &self,
        sender_comp_id: &str,
        target_comp_id: &str,
        msg_seq_num: u32,
    ) -> DeribitFixResult<String> {
        let mut builder = MessageBuilder::new()
            .msg_type(MsgType::QuoteRequest)
            .sender_comp_id(sender_comp_id.to_string())
            .target_comp_id(target_comp_id.to_string())
            .msg_seq_num(msg_seq_num)
            .sending_time(Utc::now());

        // Required fields
        builder = builder
            .field(131, self.quote_req_id.clone()) // QuoteReqID
            .field(55, self.symbol.clone()) // Symbol
            .field(537, i32::from(self.quote_type).to_string()) // QuoteType
            .field(54, char::from(self.side).to_string()) // Side
            .field(38, self.order_qty.to_string()); // OrderQty

        // Optional fields
        if let Some(valid_until_time) = &self.valid_until_time {
            builder = builder.field(
                62,
                valid_until_time.format("%Y%m%d-%H:%M:%S%.3f").to_string(),
            );
        }

        if let Some(quote_request_type) = &self.quote_request_type {
            builder = builder.field(303, quote_request_type.to_string());
        }

        if let Some(time_in_force) = &self.time_in_force {
            builder = builder.field(59, char::from(*time_in_force).to_string());
        }

        if let Some(min_qty) = &self.min_qty {
            builder = builder.field(110, min_qty.to_string());
        }

        if let Some(settlement_type) = &self.settlement_type {
            builder = builder.field(63, settlement_type.to_string());
        }

        if let Some(deribit_label) = &self.deribit_label {
            builder = builder.field(100010, deribit_label.clone());
        }

        if let Some(market_segment_id) = &self.market_segment_id {
            builder = builder.field(1300, market_segment_id.clone());
        }

        if let Some(total_volume_traded) = self.total_volume_traded {
            builder = builder.field(387, total_volume_traded.to_string());
        }

        if let Some(transact_time) = &self.transact_time {
            builder = builder.field(60, transact_time.format("%Y%m%d-%H:%M:%S%.3f").to_string());
        }

        Ok(builder.build()?.to_string())
    }
}

impl_json_display!(QuoteRequest);
impl_json_debug_pretty!(QuoteRequest);

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_quote_request_creation() {
        let request = QuoteRequest::new(
            "QR123".to_string(),
            "BTC-PERPETUAL".to_string(),
            QuoteType::Tradeable,
            OrderSide::Buy,
            10.0,
        );

        assert_eq!(request.quote_req_id, "QR123");
        assert_eq!(request.symbol, "BTC-PERPETUAL");
        assert_eq!(request.quote_type, QuoteType::Tradeable);
        assert_eq!(request.side, OrderSide::Buy);
        assert_eq!(request.order_qty, 10.0);
    }

    #[test]
    fn test_quote_request_tradeable() {
        let request = QuoteRequest::tradeable(
            "QR456".to_string(),
            "ETH-PERPETUAL".to_string(),
            OrderSide::Sell,
            5.0,
        );

        assert_eq!(request.quote_type, QuoteType::Tradeable);
        assert_eq!(request.side, OrderSide::Sell);
        assert_eq!(request.order_qty, 5.0);
    }

    #[test]
    fn test_quote_request_indicative() {
        let request = QuoteRequest::indicative(
            "QR789".to_string(),
            "BTC-PERPETUAL".to_string(),
            OrderSide::Buy,
            15.0,
        );

        assert_eq!(request.quote_type, QuoteType::Indicative);
        assert_eq!(request.order_qty, 15.0);
    }

    #[test]
    fn test_quote_request_with_options() {
        let valid_until = Utc::now() + chrono::Duration::hours(1);
        let request = QuoteRequest::new(
            "QR999".to_string(),
            "ETH-PERPETUAL".to_string(),
            QuoteType::RestrictedTradeable,
            OrderSide::Buy,
            20.0,
        )
        .with_valid_until(valid_until)
        .with_quote_request_type(1)
        .with_time_in_force(TimeInForce::GoodTillCancelled)
        .with_min_qty(5.0)
        .with_settlement_type('0')
        .with_label("test-quote".to_string())
        .with_market_segment_id("DERIBIT".to_string());

        assert_eq!(request.valid_until_time, Some(valid_until));
        assert_eq!(request.quote_request_type, Some(1));
        assert_eq!(request.time_in_force, Some(TimeInForce::GoodTillCancelled));
        assert_eq!(request.min_qty, Some(5.0));
        assert_eq!(request.settlement_type, Some('0'));
        assert_eq!(request.deribit_label, Some("test-quote".to_string()));
        assert_eq!(request.market_segment_id, Some("DERIBIT".to_string()));
    }

    #[test]
    fn test_quote_request_to_fix_message() {
        let request = QuoteRequest::tradeable(
            "QR123".to_string(),
            "BTC-PERPETUAL".to_string(),
            OrderSide::Buy,
            10.0,
        )
        .with_label("test-label".to_string());

        let fix_message = request.to_fix_message("SENDER", "TARGET", 1).unwrap();

        // Check that the message contains required fields
        assert!(fix_message.contains("35=R")); // MsgType
        assert!(fix_message.contains("131=QR123")); // QuoteReqID
        assert!(fix_message.contains("55=BTC-PERPETUAL")); // Symbol
        assert!(fix_message.contains("537=1")); // QuoteType=Tradeable
        assert!(fix_message.contains("54=1")); // Side=Buy
        assert!(fix_message.contains("38=10")); // OrderQty
        assert!(fix_message.contains("100010=test-label")); // Custom label
    }

    #[test]
    fn test_quote_type_conversions() {
        assert_eq!(i32::from(QuoteType::Indicative), 0);
        assert_eq!(i32::from(QuoteType::Tradeable), 1);
        assert_eq!(i32::from(QuoteType::RestrictedTradeable), 2);
        assert_eq!(i32::from(QuoteType::Counter), 3);

        assert_eq!(QuoteType::try_from(0).unwrap(), QuoteType::Indicative);
        assert_eq!(QuoteType::try_from(1).unwrap(), QuoteType::Tradeable);
        assert_eq!(
            QuoteType::try_from(2).unwrap(),
            QuoteType::RestrictedTradeable
        );
        assert_eq!(QuoteType::try_from(3).unwrap(), QuoteType::Counter);

        assert!(QuoteType::try_from(99).is_err());
    }

    #[test]
    fn test_quote_request_with_rfq_fields() {
        let transact_time = Utc::now();
        let request = QuoteRequest::tradeable(
            "QR_RFQ".to_string(),
            "BTC-PERPETUAL".to_string(),
            OrderSide::Buy,
            10.0,
        )
        .with_total_volume_traded(1000.5)
        .with_transact_time(transact_time);

        assert_eq!(request.total_volume_traded, Some(1000.5));
        assert_eq!(request.transact_time, Some(transact_time));
    }

    #[test]
    fn test_quote_request_rfq_fields_in_fix_message() {
        let transact_time = Utc::now();
        let request = QuoteRequest::tradeable(
            "QR_FIX".to_string(),
            "ETH-PERPETUAL".to_string(),
            OrderSide::Sell,
            5.0,
        )
        .with_total_volume_traded(500.25)
        .with_transact_time(transact_time);

        let fix_message = request.to_fix_message("SENDER", "TARGET", 1).unwrap();

        // Check that new fields are present
        assert!(fix_message.contains("387=500.25")); // TotalVolumeTraded
        assert!(fix_message.contains("60=")); // TransactTime
    }
}