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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
/******************************************************************************
   Author: Joaquín Béjar García
   Email: jb@taunais.com
   Date: 12/8/25
******************************************************************************/

//! FIX Position messages implementation
//!
//! This module provides functionality for creating and parsing FIX position
//! messages used in communication with Deribit, including:
//! - RequestForPositions (MsgType = "AN")
//! - PositionReport (MsgType = "AP")

use crate::error::Result;
use crate::error::{DeribitFixError, Result as DeribitFixResult};
use crate::message::MessageBuilder;
use crate::model::message::FixMessage;
use crate::model::types::MsgType;

use crate::model::position::{Direction, Position};
use serde::{Deserialize, Serialize};

/// Position request type enumeration
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum PosReqType {
    /// Positions (0)
    Positions,
    /// Trades (1)
    Trades,
    /// Exercises (2)
    Exercises,
    /// Assignments (3)
    Assignments,
}

impl From<PosReqType> for i32 {
    fn from(value: PosReqType) -> Self {
        match value {
            PosReqType::Positions => 0,
            PosReqType::Trades => 1,
            PosReqType::Exercises => 2,
            PosReqType::Assignments => 3,
        }
    }
}

impl TryFrom<i32> for PosReqType {
    type Error = DeribitFixError;

    fn try_from(value: i32) -> Result<Self> {
        match value {
            0 => Ok(PosReqType::Positions),
            1 => Ok(PosReqType::Trades),
            2 => Ok(PosReqType::Exercises),
            3 => Ok(PosReqType::Assignments),
            _ => Err(DeribitFixError::MessageParsing(format!(
                "Invalid PosReqType: {}",
                value
            ))),
        }
    }
}

/// Subscription request type for positions
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SubscriptionRequestType {
    /// Snapshot (0)
    Snapshot,
    /// Snapshot + Updates (1)
    SnapshotPlusUpdates,
    /// Disable previous snapshot + updates (2)
    DisablePreviousSnapshotPlusUpdates,
}

impl From<SubscriptionRequestType> for i32 {
    fn from(value: SubscriptionRequestType) -> Self {
        match value {
            SubscriptionRequestType::Snapshot => 0,
            SubscriptionRequestType::SnapshotPlusUpdates => 1,
            SubscriptionRequestType::DisablePreviousSnapshotPlusUpdates => 2,
        }
    }
}

impl TryFrom<i32> for SubscriptionRequestType {
    type Error = DeribitFixError;

    fn try_from(value: i32) -> Result<Self> {
        match value {
            0 => Ok(SubscriptionRequestType::Snapshot),
            1 => Ok(SubscriptionRequestType::SnapshotPlusUpdates),
            2 => Ok(SubscriptionRequestType::DisablePreviousSnapshotPlusUpdates),
            _ => Err(DeribitFixError::MessageParsing(format!(
                "Invalid SubscriptionRequestType: {}",
                value
            ))),
        }
    }
}

/// Request For Positions message (MsgType = "AN")
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RequestForPositions {
    /// Position Request ID (710)
    pub pos_req_id: String,
    /// Position Request Type (724)
    pub pos_req_type: PosReqType,
    /// Subscription Request Type (263) - optional
    pub subscription_request_type: Option<SubscriptionRequestType>,
    /// Clearing Business Date (715) - optional
    pub clearing_business_date: Option<String>,
    /// Symbols filter - optional
    pub symbols: Vec<String>,
}

impl RequestForPositions {
    /// Create a new position request for all positions
    pub fn all_positions(pos_req_id: String) -> Self {
        Self {
            pos_req_id,
            pos_req_type: PosReqType::Positions,
            subscription_request_type: Some(SubscriptionRequestType::Snapshot),
            clearing_business_date: None,
            symbols: Vec::new(),
        }
    }

    /// Create a new position request with subscription for updates
    pub fn positions_with_updates(pos_req_id: String) -> Self {
        Self {
            pos_req_id,
            pos_req_type: PosReqType::Positions,
            subscription_request_type: Some(SubscriptionRequestType::SnapshotPlusUpdates),
            clearing_business_date: None,
            symbols: Vec::new(),
        }
    }

    /// Add symbols filter
    pub fn with_symbols(mut self, symbols: Vec<String>) -> Self {
        self.symbols = symbols;
        self
    }

    /// Add clearing business date
    pub fn with_clearing_date(mut self, date: String) -> Self {
        self.clearing_business_date = Some(date);
        self
    }

    /// Convert to FIX message
    pub fn to_fix_message(
        &self,
        sender_comp_id: String,
        target_comp_id: String,
        msg_seq_num: u32,
    ) -> DeribitFixResult<FixMessage> {
        let mut builder = MessageBuilder::new()
            .msg_type(MsgType::RequestForPositions)
            .sender_comp_id(sender_comp_id)
            .target_comp_id(target_comp_id)
            .msg_seq_num(msg_seq_num)
            .field(710, self.pos_req_id.clone()) // PosReqID
            .field(724, i32::from(self.pos_req_type).to_string()); // PosReqType

        // Add optional subscription request type
        if let Some(subscription_type) = self.subscription_request_type {
            builder = builder.field(263, i32::from(subscription_type).to_string());
        }

        // Add optional clearing business date
        if let Some(ref date) = self.clearing_business_date {
            builder = builder.field(715, date.clone());
        }

        // Add symbols if present
        if !self.symbols.is_empty() {
            builder = builder.field(146, self.symbols.len().to_string()); // NoRelatedSym
            for symbol in &self.symbols {
                builder = builder.field(55, symbol.clone()); // Symbol
            }
        }

        builder.build()
    }
}

/// Represents a FIX Position Report message (MsgType = AP).
pub struct PositionReport;

impl PositionReport {
    /// Parse a Position from a FIX message (Position Report-like payload).
    ///
    /// This function extracts Deribit position information from a FixMessage by
    /// reading standard FIX tags and Deribit extensions. It computes derived fields
    /// such as net size and direction, and maps several optional numeric fields into
    /// greeks and margin metrics.
    ///
    /// Behavior:
    /// - Instrument name (tag 55) is mandatory; absence results in an error.
    /// - LongQty (704) and ShortQty (705) are read and netted as `size = long - short`.
    /// - `direction` is Buy if `size > 0.0`, otherwise Sell.
    /// - Many numeric fields are optional; if missing or unparsable, they default to:
    ///   - f64 options: None
    ///   - Aggregated numeric values used directly (e.g., `average_price`) default to 0.0
    /// - Settlement price (730) is reused as both `average_price` and `settlement_price`.
    ///
    /// Tag mapping:
    /// - 55 (Symbol) -> Position.instrument_name
    /// - 704 (LongQty) and 705 (ShortQty) -> Position.size (long - short)
    /// - 730 (SettlPx) -> Position.average_price and Position.settlement_price
    /// - 731 (IndexPx) -> Position.index_price
    /// - 732 (MarkPx) -> Position.mark_price
    /// - 898 (MaintenanceMargin) -> Position.maintenance_margin
    /// - 899 (InitialMargin) -> Position.initial_margin
    /// - 706 (RealizedPnL) -> Position.realized_profit_loss
    /// - 707 (UnrealizedPnL) -> Position.floating_profit_loss and Position.unrealized_profit_loss
    /// - 708 (TotalPnL) -> Position.total_profit_loss
    /// - 811 (Delta), 812 (Gamma), 813 (Theta), 814 (Vega) -> Position greeks
    /// - 461 (CFICode) -> Position.kind
    /// - 100088 (DeribitLiquidationPrice) -> Position.estimated_liquidation_price
    /// - 100089 (DeribitSizeInCurrency) -> Position.size_currency
    ///
    /// Errors:
    /// - Returns DeribitFixError::Generic when tag 55 (Symbol) is missing.
    ///
    /// Note:
    /// - Fields like `average_price_usd`, `interest_value`, `leverage`, `open_orders_margin`,
    ///   `realized_funding`, and `size_currency` are not provided by this parser and remain `None`.
    ///
    /// Returns:
    /// - Ok(Position) when parsing succeeds
    /// - Err(DeribitFixError) if the required symbol (tag 55) is missing
    pub fn try_from_fix_message(message: &FixMessage) -> Result<Position> {
        let get_f64 = |tag| message.get_field(tag).and_then(|s| s.parse::<f64>().ok());
        let get_string = |tag| message.get_field(tag).map(|s| s.to_string());

        let instrument_name = get_string(55).ok_or_else(|| {
            DeribitFixError::Generic("Missing instrument name (tag 55)".to_string())
        })?;
        let long_qty = get_f64(704).unwrap_or(0.0);
        let short_qty = get_f64(705).unwrap_or(0.0);
        let size = long_qty - short_qty;
        let direction = if size > 0.0 {
            Direction::Buy
        } else {
            Direction::Sell
        };
        let average_price = get_f64(730).unwrap_or(0.0);

        Ok(Position {
            instrument_name,
            size,
            direction,
            average_price,
            average_price_usd: None,
            delta: get_f64(811),                          // Greeks delta
            estimated_liquidation_price: get_f64(100088), // DeribitLiquidationPrice
            floating_profit_loss: get_f64(707),           // Unrealized PnL
            floating_profit_loss_usd: None,
            gamma: get_f64(812),          // Greeks gamma
            index_price: get_f64(731),    // Index price
            initial_margin: get_f64(899), // Initial margin
            interest_value: None,
            kind: get_string(461), // CFICode for instrument type
            leverage: None,
            maintenance_margin: get_f64(898), // Maintenance margin
            mark_price: get_f64(732),         // Mark price
            open_orders_margin: None,
            realized_funding: None,
            realized_profit_loss: get_f64(706),   // Realized PnL
            settlement_price: get_f64(730),       // Settlement price (same as avg price for now)
            size_currency: get_f64(100089),       // DeribitSizeInCurrency
            theta: get_f64(813),                  // Greeks theta
            total_profit_loss: get_f64(708),      // Total PnL
            vega: get_f64(814),                   // Greeks vega
            unrealized_profit_loss: get_f64(707), // Same as floating PnL
        })
    }

    /// Builds a FIX Position Report (MsgType=AP) from a Deribit `Position`.
    ///
    /// This function converts a Deribit position into a FIX message using
    /// the provided message metadata (sender/target IDs and sequence number).
    /// Only fields present in the input position are included in the resulting
    /// message, and position-side determines whether LongQty or ShortQty is used.
    ///
    /// FIX tags populated:
    /// - 35 (MsgType): AP (PositionReport)
    /// - 49 (SenderCompID): from `sender_comp_id`
    /// - 56 (TargetCompID): from `target_comp_id`
    /// - 34 (MsgSeqNum): from `msg_seq_num`
    /// - 55 (Symbol): from `position.instrument_name`
    /// - 730 (SettlPx): from `position.average_price`
    /// - 704 (LongQty): if `position.direction` is Buy, with `position.size`
    /// - 705 (ShortQty): if `position.direction` is Sell, with `abs(position.size)`
    /// - 706 (PosAmt Realized PnL): from `position.realized_profit_loss` (if set)
    /// - 707 (PosAmt Floating/Unrealized PnL): from `position.floating_profit_loss` (if set)
    /// - 708 (PosAmt Total PnL): from `position.total_profit_loss` (if set)
    /// - 811 (Delta): from `position.delta` (if set)
    /// - 812 (Gamma): from `position.gamma` (if set)
    /// - 813 (Theta): from `position.theta` (if set)
    /// - 814 (Vega): from `position.vega` (if set)
    /// - 731 (IndexPx): from `position.index_price` (if set)
    /// - 732 (MarkPx): from `position.mark_price` (if set)
    /// - 899 (InitialMargin): from `position.initial_margin` (if set)
    /// - 898 (MaintenanceMargin): from `position.maintenance_margin` (if set)
    /// - 979 (PosAmtType): constant "FMTM"
    ///
    /// Parameters:
    /// - `position`: Source Deribit position to translate.
    /// - `sender_comp_id`: Value for FIX tag 49 (SenderCompID).
    /// - `target_comp_id`: Value for FIX tag 56 (TargetCompID).
    /// - `msg_seq_num`: Value for FIX tag 34 (MsgSeqNum).
    ///
    /// Returns:
    /// - `Ok(String)`: The serialized FIX message string when message building succeeds.
    /// - `Err(DeribitFixError)`: If the underlying builder fails to construct the message.
    ///
    /// Notes:
    /// - Quantity tag selection depends on `position.direction`:
    ///   - Buy -> 704=LongQty
    ///   - Sell -> 705=ShortQty (absolute value)
    /// - Optional numeric fields are only included when present in `position`.
    pub fn from_deribit_position(
        position: &Position,
        sender_comp_id: String,
        target_comp_id: String,
        msg_seq_num: u32,
    ) -> Result<String> {
        let msg = MessageBuilder::new()
            .msg_type(MsgType::PositionReport)
            .sender_comp_id(sender_comp_id)
            .target_comp_id(target_comp_id)
            .msg_seq_num(msg_seq_num);

        // Add position-specific fields
        let msg = msg.field(55, position.instrument_name.clone()); // Symbol
        let msg = msg.field(730, position.average_price.to_string()); // SettlPx

        // Add position quantity based on direction
        let msg = match position.direction {
            Direction::Buy => msg.field(704, position.size.to_string()), // LongQty
            Direction::Sell => msg.field(705, position.size.abs().to_string()), // ShortQty (absolute value)
        };

        // Add other position fields (only if they exist)
        let msg = if let Some(realized_pnl) = position.realized_profit_loss {
            msg.field(706, realized_pnl.to_string())
        } else {
            msg
        };

        let msg = if let Some(floating_pnl) = position.floating_profit_loss {
            msg.field(707, floating_pnl.to_string())
        } else {
            msg
        };

        let msg = if let Some(total_pnl) = position.total_profit_loss {
            msg.field(708, total_pnl.to_string())
        } else {
            msg
        };

        // Add Greeks if available
        let msg = if let Some(delta) = position.delta {
            msg.field(811, delta.to_string())
        } else {
            msg
        };

        let msg = if let Some(gamma) = position.gamma {
            msg.field(812, gamma.to_string())
        } else {
            msg
        };

        let msg = if let Some(theta) = position.theta {
            msg.field(813, theta.to_string())
        } else {
            msg
        };

        let msg = if let Some(vega) = position.vega {
            msg.field(814, vega.to_string())
        } else {
            msg
        };

        // Add other optional fields
        let msg = if let Some(index_price) = position.index_price {
            msg.field(731, index_price.to_string())
        } else {
            msg
        };

        let msg = if let Some(mark_price) = position.mark_price {
            msg.field(732, mark_price.to_string())
        } else {
            msg
        };

        let msg = if let Some(initial_margin) = position.initial_margin {
            msg.field(899, initial_margin.to_string())
        } else {
            msg
        };

        let msg = if let Some(maintenance_margin) = position.maintenance_margin {
            msg.field(898, maintenance_margin.to_string())
        } else {
            msg
        };

        let msg = msg.field(979, "FMTM".to_string()); // PosAmtType

        // Deribit custom tags
        let msg = if let Some(liquidation_price) = position.estimated_liquidation_price {
            msg.field(100088, liquidation_price.to_string()) // DeribitLiquidationPrice
        } else {
            msg
        };

        let msg = if let Some(size_currency) = position.size_currency {
            msg.field(100089, size_currency.to_string()) // DeribitSizeInCurrency
        } else {
            msg
        };

        Ok(msg.build()?.to_string())
    }
}
#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::message::FixMessage;

    #[test]
    fn test_pos_req_type_conversion() {
        assert_eq!(i32::from(PosReqType::Positions), 0);
        assert_eq!(i32::from(PosReqType::Trades), 1);

        assert_eq!(PosReqType::try_from(0).unwrap(), PosReqType::Positions);
        assert_eq!(PosReqType::try_from(1).unwrap(), PosReqType::Trades);

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

    #[test]
    fn test_subscription_request_type_conversion() {
        assert_eq!(i32::from(SubscriptionRequestType::Snapshot), 0);
        assert_eq!(i32::from(SubscriptionRequestType::SnapshotPlusUpdates), 1);

        assert_eq!(
            SubscriptionRequestType::try_from(0).unwrap(),
            SubscriptionRequestType::Snapshot
        );
        assert_eq!(
            SubscriptionRequestType::try_from(1).unwrap(),
            SubscriptionRequestType::SnapshotPlusUpdates
        );

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

    #[test]
    fn test_request_for_positions_creation() {
        let request = RequestForPositions::all_positions("POS_123".to_string());
        assert_eq!(request.pos_req_id, "POS_123");
        assert_eq!(request.pos_req_type, PosReqType::Positions);
        assert_eq!(
            request.subscription_request_type,
            Some(SubscriptionRequestType::Snapshot)
        );
    }

    #[test]
    fn test_request_for_positions_with_symbols() {
        let request = RequestForPositions::all_positions("POS_123".to_string()).with_symbols(vec![
            "BTC-PERPETUAL".to_string(),
            "ETH-PERPETUAL".to_string(),
        ]);

        assert_eq!(request.symbols.len(), 2);
        assert!(request.symbols.contains(&"BTC-PERPETUAL".to_string()));
    }

    #[test]
    fn test_request_for_positions_to_fix_message() {
        let request = RequestForPositions::all_positions("POS_123".to_string());
        let fix_message = request
            .to_fix_message("SENDER".to_string(), "TARGET".to_string(), 1)
            .unwrap();

        // Test field values directly
        assert_eq!(fix_message.get_field(35), Some(&"AN".to_string())); // MsgType
        assert_eq!(fix_message.get_field(710), Some(&"POS_123".to_string())); // PosReqID
        assert_eq!(fix_message.get_field(724), Some(&"0".to_string())); // PosReqType
        assert_eq!(fix_message.get_field(263), Some(&"0".to_string())); // SubscriptionRequestType
    }

    #[test]
    fn test_position_report_try_from_fix_message() {
        // Create a FixMessage manually by setting fields
        let mut fix_message = FixMessage::new();
        fix_message.set_field(55, "BTC-PERPETUAL".to_string()); // Symbol
        fix_message.set_field(704, "1.5".to_string()); // LongQty
        fix_message.set_field(705, "0.0".to_string()); // ShortQty
        fix_message.set_field(730, "50000.0".to_string()); // SettlPx (average price)
        fix_message.set_field(707, "100.0".to_string()); // Unrealized PnL
        fix_message.set_field(706, "50.0".to_string()); // Realized PnL

        let position = PositionReport::try_from_fix_message(&fix_message).unwrap();

        assert_eq!(position.instrument_name, "BTC-PERPETUAL");
        assert_eq!(position.size, 1.5);
        assert_eq!(position.average_price, 50000.0);
        assert!(matches!(position.direction, Direction::Buy));
        assert_eq!(position.floating_profit_loss, Some(100.0));
        assert_eq!(position.realized_profit_loss, Some(50.0));
    }

    #[test]
    fn test_position_report_from_deribit_position() {
        // Create a Position struct
        let position = Position {
            instrument_name: "ETH-PERPETUAL".to_string(),
            size: 2.0,
            direction: Direction::Buy,
            average_price: 3500.0,
            average_price_usd: None,
            delta: Some(0.5),
            estimated_liquidation_price: None,
            floating_profit_loss: Some(150.0),
            floating_profit_loss_usd: None,
            gamma: Some(0.001),
            index_price: Some(3520.0),
            initial_margin: Some(100.0),
            interest_value: None,
            kind: Some("future".to_string()),
            leverage: None,
            maintenance_margin: Some(50.0),
            mark_price: Some(3510.0),
            open_orders_margin: None,
            realized_funding: None,
            realized_profit_loss: Some(50.0),
            settlement_price: Some(3500.0),
            size_currency: None,
            theta: Some(-0.1),
            total_profit_loss: Some(200.0),
            vega: Some(0.05),
            unrealized_profit_loss: Some(150.0),
        };

        let fix_message = PositionReport::from_deribit_position(
            &position,
            "SENDER".to_string(),
            "TARGET".to_string(),
            1,
        )
        .unwrap();

        // Verify the FIX message contains expected fields
        assert!(fix_message.contains("55=ETH-PERPETUAL")); // Symbol
        assert!(fix_message.contains("704=2")); // LongQty
        assert!(fix_message.contains("730=3500")); // SettlPx
    }

    #[test]
    fn test_position_direction_sell() {
        // Create a FixMessage with negative position (sell)
        let mut fix_message = FixMessage::new();
        fix_message.set_field(55, "BTC-PERPETUAL".to_string()); // Symbol
        fix_message.set_field(704, "0.0".to_string()); // LongQty
        fix_message.set_field(705, "1.0".to_string()); // ShortQty
        fix_message.set_field(730, "45000.0".to_string()); // SettlPx

        let position = PositionReport::try_from_fix_message(&fix_message).unwrap();

        assert_eq!(position.instrument_name, "BTC-PERPETUAL");
        assert_eq!(position.size, -1.0); // Negative size for sell
        assert!(matches!(position.direction, Direction::Sell));
        assert_eq!(position.average_price, 45000.0);
    }

    #[test]
    fn test_position_report_with_deribit_custom_tags() {
        // Create a FixMessage with Deribit custom tags
        let mut fix_message = FixMessage::new();
        fix_message.set_field(55, "ETH-PERPETUAL".to_string()); // Symbol
        fix_message.set_field(704, "2.5".to_string()); // LongQty
        fix_message.set_field(730, "3500.0".to_string()); // SettlPx
        fix_message.set_field(100088, "3000.0".to_string()); // DeribitLiquidationPrice
        fix_message.set_field(100089, "8750.0".to_string()); // DeribitSizeInCurrency

        let position = PositionReport::try_from_fix_message(&fix_message).unwrap();

        assert_eq!(position.instrument_name, "ETH-PERPETUAL");
        assert_eq!(position.size, 2.5);
        assert_eq!(position.estimated_liquidation_price, Some(3000.0));
        assert_eq!(position.size_currency, Some(8750.0));
    }

    #[test]
    fn test_position_report_emits_deribit_custom_tags() {
        // Create a Position with Deribit custom fields
        let position = Position {
            instrument_name: "BTC-PERPETUAL".to_string(),
            size: 1.0,
            direction: Direction::Buy,
            average_price: 50000.0,
            average_price_usd: None,
            delta: None,
            estimated_liquidation_price: Some(45000.0),
            floating_profit_loss: None,
            floating_profit_loss_usd: None,
            gamma: None,
            index_price: None,
            initial_margin: None,
            interest_value: None,
            kind: None,
            leverage: None,
            maintenance_margin: None,
            mark_price: None,
            open_orders_margin: None,
            realized_funding: None,
            realized_profit_loss: None,
            settlement_price: None,
            size_currency: Some(50000.0),
            theta: None,
            total_profit_loss: None,
            vega: None,
            unrealized_profit_loss: None,
        };

        let fix_message = PositionReport::from_deribit_position(
            &position,
            "SENDER".to_string(),
            "TARGET".to_string(),
            1,
        )
        .unwrap();

        // Check that Deribit custom tags are present
        assert!(fix_message.contains("100088=45000")); // DeribitLiquidationPrice
        assert!(fix_message.contains("100089=50000")); // DeribitSizeInCurrency
    }
}