Skip to main content

nautilus_architect_ax/websocket/data/
parse.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3//  https://nautechsystems.io
4//
5//  Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6//  You may not use this file except in compliance with the License.
7//  You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9//  Unless required by applicable law or agreed to in writing, software
10//  distributed under the License is distributed on an "AS IS" BASIS,
11//  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12//  See the License for the specific language governing permissions and
13//  limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16//! Parsing functions to convert Ax WebSocket messages to Nautilus domain types.
17
18use anyhow::Context;
19use nautilus_core::nanos::UnixNanos;
20use nautilus_model::{
21    data::{Bar, BarType, BookOrder, OrderBookDelta, OrderBookDeltas, QuoteTick, TradeTick},
22    enums::{AggregationSource, AggressorSide, BookAction, OrderSide, RecordFlag},
23    identifiers::TradeId,
24    instruments::{Instrument, any::InstrumentAny},
25    types::{Price, Quantity},
26};
27use rust_decimal::Decimal;
28
29use crate::{
30    common::parse::ax_timestamp_stn_to_unix_nanos,
31    http::parse::candle_width_to_bar_spec,
32    websocket::messages::{
33        AxBookLevel, AxBookLevelL3, AxMdBookL1, AxMdBookL2, AxMdBookL3, AxMdCandle, AxMdTrade,
34    },
35};
36
37/// Converts a Decimal to Price with specified precision.
38fn decimal_to_price_dp(value: Decimal, precision: u8, field: &str) -> anyhow::Result<Price> {
39    Price::from_decimal_dp(value, precision).with_context(|| {
40        format!("Failed to construct Price for {field} with precision {precision}")
41    })
42}
43
44/// Parses an Ax L1 book message into a [`QuoteTick`].
45///
46/// L1 contains best bid/ask only, which maps directly to a quote tick.
47///
48/// # Errors
49///
50/// Returns an error if price or quantity parsing fails.
51pub fn parse_book_l1_quote(
52    book: &AxMdBookL1,
53    instrument: &InstrumentAny,
54    ts_init: UnixNanos,
55) -> anyhow::Result<QuoteTick> {
56    let price_precision = instrument.price_precision();
57    let size_precision = instrument.size_precision();
58
59    let (bid_price, bid_size) = if let Some(bid) = book.b.first() {
60        (
61            decimal_to_price_dp(bid.p, price_precision, "book.bid.price")?,
62            Quantity::new(bid.q as f64, size_precision),
63        )
64    } else {
65        (Price::zero(price_precision), Quantity::zero(size_precision))
66    };
67
68    let (ask_price, ask_size) = if let Some(ask) = book.a.first() {
69        (
70            decimal_to_price_dp(ask.p, price_precision, "book.ask.price")?,
71            Quantity::new(ask.q as f64, size_precision),
72        )
73    } else {
74        (Price::zero(price_precision), Quantity::zero(size_precision))
75    };
76
77    let ts_event = ax_timestamp_stn_to_unix_nanos(book.ts, book.tn)?;
78
79    QuoteTick::new_checked(
80        instrument.id(),
81        bid_price,
82        ask_price,
83        bid_size,
84        ask_size,
85        ts_event,
86        ts_init,
87    )
88    .context("Failed to construct QuoteTick from Ax L1 book")
89}
90
91/// Parses a book level into price and quantity.
92fn parse_book_level(
93    level: &AxBookLevel,
94    price_precision: u8,
95    size_precision: u8,
96) -> anyhow::Result<(Price, Quantity)> {
97    let price = decimal_to_price_dp(level.p, price_precision, "book.level.price")?;
98    let size = Quantity::new(level.q as f64, size_precision);
99    Ok((price, size))
100}
101
102/// Parses an Ax L2 book message into [`OrderBookDeltas`].
103///
104/// L2 contains aggregated price levels. Each message is treated as a snapshot
105/// that clears the book and adds all levels.
106///
107/// # Errors
108///
109/// Returns an error if price or quantity parsing fails.
110pub fn parse_book_l2_deltas(
111    book: &AxMdBookL2,
112    instrument: &InstrumentAny,
113    sequence: u64,
114    ts_init: UnixNanos,
115) -> anyhow::Result<OrderBookDeltas> {
116    let instrument_id = instrument.id();
117    let price_precision = instrument.price_precision();
118    let size_precision = instrument.size_precision();
119
120    let ts_event = ax_timestamp_stn_to_unix_nanos(book.ts, book.tn)?;
121
122    let total_levels = book.b.len() + book.a.len();
123    let capacity = total_levels + 1;
124
125    let mut deltas = Vec::with_capacity(capacity);
126
127    deltas.push(OrderBookDelta::clear(
128        instrument_id,
129        sequence,
130        ts_event,
131        ts_init,
132    ));
133
134    let mut processed = 0_usize;
135
136    for level in &book.b {
137        let (price, size) = parse_book_level(level, price_precision, size_precision)?;
138        processed += 1;
139
140        let mut flags = RecordFlag::F_MBP as u8;
141
142        if processed == total_levels {
143            flags |= RecordFlag::F_LAST as u8;
144        }
145
146        let order = BookOrder::new(OrderSide::Buy, price, size, 0);
147        let delta = OrderBookDelta::new_checked(
148            instrument_id,
149            BookAction::Add,
150            order,
151            flags,
152            sequence,
153            ts_event,
154            ts_init,
155        )
156        .context("Failed to construct OrderBookDelta from Ax L2 bid level")?;
157
158        deltas.push(delta);
159    }
160
161    for level in &book.a {
162        let (price, size) = parse_book_level(level, price_precision, size_precision)?;
163        processed += 1;
164
165        let mut flags = RecordFlag::F_MBP as u8;
166
167        if processed == total_levels {
168            flags |= RecordFlag::F_LAST as u8;
169        }
170
171        let order = BookOrder::new(OrderSide::Sell, price, size, 0);
172        let delta = OrderBookDelta::new_checked(
173            instrument_id,
174            BookAction::Add,
175            order,
176            flags,
177            sequence,
178            ts_event,
179            ts_init,
180        )
181        .context("Failed to construct OrderBookDelta from Ax L2 ask level")?;
182
183        deltas.push(delta);
184    }
185
186    if total_levels == 0
187        && let Some(first) = deltas.first_mut()
188    {
189        first.flags |= RecordFlag::F_LAST as u8;
190    }
191
192    OrderBookDeltas::new_checked(instrument_id, deltas)
193        .context("Failed to assemble OrderBookDeltas from Ax L2 message")
194}
195
196/// Parses a L3 book level into price and quantity.
197fn parse_book_level_l3(
198    level: &AxBookLevelL3,
199    price_precision: u8,
200    size_precision: u8,
201) -> anyhow::Result<(Price, Quantity)> {
202    let price = decimal_to_price_dp(level.p, price_precision, "book.level.price")?;
203    let size = Quantity::new(level.q as f64, size_precision);
204    Ok((price, size))
205}
206
207/// Parses an Ax L3 book message into [`OrderBookDeltas`].
208///
209/// L3 contains individual order quantities at each price level.
210/// Each message is treated as a snapshot that clears the book and adds all orders.
211///
212/// # Errors
213///
214/// Returns an error if price or quantity parsing fails.
215pub fn parse_book_l3_deltas(
216    book: &AxMdBookL3,
217    instrument: &InstrumentAny,
218    sequence: u64,
219    ts_init: UnixNanos,
220) -> anyhow::Result<OrderBookDeltas> {
221    let instrument_id = instrument.id();
222    let price_precision = instrument.price_precision();
223    let size_precision = instrument.size_precision();
224
225    let ts_event = ax_timestamp_stn_to_unix_nanos(book.ts, book.tn)?;
226
227    let total_orders: usize = book.b.iter().map(|l| l.o.len()).sum::<usize>()
228        + book.a.iter().map(|l| l.o.len()).sum::<usize>();
229    let capacity = total_orders + 1;
230
231    let mut deltas = Vec::with_capacity(capacity);
232
233    deltas.push(OrderBookDelta::clear(
234        instrument_id,
235        sequence,
236        ts_event,
237        ts_init,
238    ));
239
240    let mut processed = 0_usize;
241    let mut order_id_counter = 1_u64;
242
243    for level in &book.b {
244        let (price, _) = parse_book_level_l3(level, price_precision, size_precision)?;
245
246        for &order_qty in &level.o {
247            processed += 1;
248
249            let mut flags = 0_u8;
250
251            if processed == total_orders {
252                flags |= RecordFlag::F_LAST as u8;
253            }
254
255            let size = Quantity::new(order_qty as f64, size_precision);
256            let order = BookOrder::new(OrderSide::Buy, price, size, order_id_counter);
257            order_id_counter += 1;
258
259            let delta = OrderBookDelta::new_checked(
260                instrument_id,
261                BookAction::Add,
262                order,
263                flags,
264                sequence,
265                ts_event,
266                ts_init,
267            )
268            .context("Failed to construct OrderBookDelta from Ax L3 bid order")?;
269
270            deltas.push(delta);
271        }
272    }
273
274    for level in &book.a {
275        let (price, _) = parse_book_level_l3(level, price_precision, size_precision)?;
276
277        for &order_qty in &level.o {
278            processed += 1;
279
280            let mut flags = 0_u8;
281
282            if processed == total_orders {
283                flags |= RecordFlag::F_LAST as u8;
284            }
285
286            let size = Quantity::new(order_qty as f64, size_precision);
287            let order = BookOrder::new(OrderSide::Sell, price, size, order_id_counter);
288            order_id_counter += 1;
289
290            let delta = OrderBookDelta::new_checked(
291                instrument_id,
292                BookAction::Add,
293                order,
294                flags,
295                sequence,
296                ts_event,
297                ts_init,
298            )
299            .context("Failed to construct OrderBookDelta from Ax L3 ask order")?;
300
301            deltas.push(delta);
302        }
303    }
304
305    if total_orders == 0
306        && let Some(first) = deltas.first_mut()
307    {
308        first.flags |= RecordFlag::F_LAST as u8;
309    }
310
311    OrderBookDeltas::new_checked(instrument_id, deltas)
312        .context("Failed to assemble OrderBookDeltas from Ax L3 message")
313}
314
315/// Parses an Ax trade message into a [`TradeTick`].
316///
317/// # Errors
318///
319/// Returns an error if price or quantity parsing fails.
320pub fn parse_trade_tick(
321    trade: &AxMdTrade,
322    instrument: &InstrumentAny,
323    ts_init: UnixNanos,
324) -> anyhow::Result<TradeTick> {
325    let price_precision = instrument.price_precision();
326    let size_precision = instrument.size_precision();
327
328    let price = decimal_to_price_dp(trade.p, price_precision, "trade.price")?;
329    let size = Quantity::new(trade.q as f64, size_precision);
330    let aggressor_side: AggressorSide = trade.d.map_or(AggressorSide::NoAggressor, |d| d.into());
331
332    // Use transaction number as trade ID (stack-formatted to avoid heap alloc)
333    let mut buf = itoa::Buffer::new();
334    let trade_id = TradeId::new_checked(buf.format(trade.tn))
335        .context("Failed to create TradeId from transaction number")?;
336
337    let ts_event = ax_timestamp_stn_to_unix_nanos(trade.ts, trade.tn)?;
338
339    TradeTick::new_checked(
340        instrument.id(),
341        price,
342        size,
343        aggressor_side,
344        trade_id,
345        ts_event,
346        ts_init,
347    )
348    .context("Failed to construct TradeTick from Ax trade message")
349}
350
351/// Parses an Ax candle message into a [`Bar`].
352///
353/// # Errors
354///
355/// Returns an error if price or quantity parsing fails.
356pub fn parse_candle_bar(
357    candle: &AxMdCandle,
358    instrument: &InstrumentAny,
359    ts_init: UnixNanos,
360) -> anyhow::Result<Bar> {
361    let price_precision = instrument.price_precision();
362    let size_precision = instrument.size_precision();
363
364    let open = decimal_to_price_dp(candle.open, price_precision, "candle.open")?;
365    let high = decimal_to_price_dp(candle.high, price_precision, "candle.high")?;
366    let low = decimal_to_price_dp(candle.low, price_precision, "candle.low")?;
367    let close = decimal_to_price_dp(candle.close, price_precision, "candle.close")?;
368    let volume = Quantity::new(candle.volume as f64, size_precision);
369
370    let ts_event = ax_timestamp_stn_to_unix_nanos(candle.ts, 0)?;
371
372    let bar_spec = candle_width_to_bar_spec(candle.width);
373    let bar_type = BarType::new(instrument.id(), bar_spec, AggregationSource::External);
374
375    Bar::new_checked(bar_type, open, high, low, close, volume, ts_event, ts_init)
376        .context("Failed to construct Bar from Ax candle message")
377}
378
379#[cfg(test)]
380mod tests {
381    use nautilus_model::{
382        enums::AssetClass,
383        identifiers::{InstrumentId, Symbol},
384        instruments::PerpetualContract,
385        types::Currency,
386    };
387    use rstest::rstest;
388    use rust_decimal::Decimal;
389    use rust_decimal_macros::dec;
390    use ustr::Ustr;
391
392    use super::*;
393    use crate::{
394        common::{consts::AX_VENUE, enums::AxOrderSide},
395        websocket::messages::{AxMdBookL1, AxMdBookL2, AxMdBookL3, AxMdCandle, AxMdTrade},
396    };
397
398    fn create_test_instrument() -> InstrumentAny {
399        create_instrument_with_precision("BTC-PERP", 2, 3)
400    }
401
402    fn create_eurusd_instrument() -> InstrumentAny {
403        create_instrument_with_precision("EURUSD-PERP", 4, 0)
404    }
405
406    fn create_instrument_with_precision(
407        symbol: &str,
408        price_precision: u8,
409        size_precision: u8,
410    ) -> InstrumentAny {
411        let underlying = Ustr::from(symbol.split('-').next().unwrap_or(symbol));
412        let price_increment =
413            Price::from_decimal_dp(Decimal::new(1, price_precision as u32), price_precision)
414                .unwrap();
415        let size_increment =
416            Quantity::from_decimal_dp(Decimal::new(1, size_precision as u32), size_precision)
417                .unwrap();
418
419        let instrument = PerpetualContract::new(
420            InstrumentId::new(Symbol::new(symbol), *AX_VENUE),
421            Symbol::new(symbol),
422            underlying,
423            AssetClass::Cryptocurrency,
424            None,
425            Currency::USD(),
426            Currency::USD(),
427            false,
428            price_precision,
429            size_precision,
430            price_increment,
431            size_increment,
432            None,
433            Some(size_increment),
434            None,
435            Some(size_increment),
436            None,
437            None,
438            None,
439            None,
440            Some(Decimal::new(1, 2)),
441            Some(Decimal::new(5, 3)),
442            Some(Decimal::new(2, 4)),
443            Some(Decimal::new(5, 4)),
444            None,
445            UnixNanos::default(),
446            UnixNanos::default(),
447        );
448        InstrumentAny::PerpetualContract(instrument)
449    }
450
451    #[rstest]
452    fn test_parse_book_l1_quote() {
453        let book = AxMdBookL1 {
454            ts: 1700000000,
455            tn: 12345,
456            s: Ustr::from("BTC-PERP"),
457            b: vec![AxBookLevel {
458                p: dec!(50000.50),
459                q: 100,
460            }],
461            a: vec![AxBookLevel {
462                p: dec!(50001.00),
463                q: 150,
464            }],
465        };
466
467        let instrument = create_test_instrument();
468        let ts_init = UnixNanos::default();
469
470        let quote = parse_book_l1_quote(&book, &instrument, ts_init).unwrap();
471
472        assert_eq!(quote.bid_price.as_f64(), 50000.50);
473        assert_eq!(quote.ask_price.as_f64(), 50001.00);
474        assert_eq!(quote.bid_size.as_f64(), 100.0);
475        assert_eq!(quote.ask_size.as_f64(), 150.0);
476    }
477
478    #[rstest]
479    fn test_parse_book_l2_deltas() {
480        let book = AxMdBookL2 {
481            ts: 1700000000,
482            tn: 12345,
483            s: Ustr::from("BTC-PERP"),
484            b: vec![
485                AxBookLevel {
486                    p: dec!(50000.50),
487                    q: 100,
488                },
489                AxBookLevel {
490                    p: dec!(50000.00),
491                    q: 200,
492                },
493            ],
494            a: vec![
495                AxBookLevel {
496                    p: dec!(50001.00),
497                    q: 150,
498                },
499                AxBookLevel {
500                    p: dec!(50001.50),
501                    q: 250,
502                },
503            ],
504            st: false,
505        };
506
507        let instrument = create_test_instrument();
508        let ts_init = UnixNanos::default();
509
510        let deltas = parse_book_l2_deltas(&book, &instrument, 1, ts_init).unwrap();
511
512        // 1 clear + 4 levels
513        assert_eq!(deltas.deltas.len(), 5);
514        assert_eq!(deltas.deltas[0].action, BookAction::Clear);
515        assert_eq!(deltas.deltas[1].order.side, OrderSide::Buy);
516        assert_eq!(deltas.deltas[3].order.side, OrderSide::Sell);
517    }
518
519    #[rstest]
520    fn test_parse_book_l3_deltas() {
521        let book = AxMdBookL3 {
522            ts: 1700000000,
523            tn: 12345,
524            s: Ustr::from("BTC-PERP"),
525            b: vec![AxBookLevelL3 {
526                p: dec!(50000.50),
527                q: 300,
528                o: vec![100, 200],
529            }],
530            a: vec![AxBookLevelL3 {
531                p: dec!(50001.00),
532                q: 250,
533                o: vec![150, 100],
534            }],
535            st: false,
536        };
537
538        let instrument = create_test_instrument();
539        let ts_init = UnixNanos::default();
540
541        let deltas = parse_book_l3_deltas(&book, &instrument, 1, ts_init).unwrap();
542
543        // 1 clear + 4 individual orders
544        assert_eq!(deltas.deltas.len(), 5);
545        assert_eq!(deltas.deltas[0].action, BookAction::Clear);
546    }
547
548    #[rstest]
549    fn test_parse_trade_tick() {
550        let trade = AxMdTrade {
551            ts: 1700000000,
552            tn: 12345,
553            s: Ustr::from("BTC-PERP"),
554            p: dec!(50000.50),
555            q: 100,
556            d: Some(AxOrderSide::Buy),
557        };
558
559        let instrument = create_test_instrument();
560        let ts_init = UnixNanos::default();
561
562        let tick = parse_trade_tick(&trade, &instrument, ts_init).unwrap();
563
564        assert_eq!(tick.price.as_f64(), 50000.50);
565        assert_eq!(tick.size.as_f64(), 100.0);
566        assert_eq!(tick.aggressor_side, AggressorSide::Buyer);
567    }
568
569    #[rstest]
570    fn test_parse_book_l1_from_captured_data() {
571        let json = include_str!("../../../test_data/ws_md_book_l1_captured.json");
572        let book: AxMdBookL1 = serde_json::from_str(json).unwrap();
573
574        assert_eq!(book.s.as_str(), "EURUSD-PERP");
575        assert_eq!(book.b.len(), 1);
576        assert_eq!(book.a.len(), 1);
577
578        let instrument = create_eurusd_instrument();
579        let ts_init = UnixNanos::default();
580
581        let quote = parse_book_l1_quote(&book, &instrument, ts_init).unwrap();
582
583        assert_eq!(quote.instrument_id.symbol.as_str(), "EURUSD-PERP");
584        assert_eq!(quote.bid_price.as_f64(), 1.1712);
585        assert_eq!(quote.ask_price.as_f64(), 1.1717);
586        assert_eq!(quote.bid_size.as_f64(), 300.0);
587        assert_eq!(quote.ask_size.as_f64(), 100.0);
588    }
589
590    #[rstest]
591    fn test_parse_book_l2_from_captured_data() {
592        let json = include_str!("../../../test_data/ws_md_book_l2_captured.json");
593        let book: AxMdBookL2 = serde_json::from_str(json).unwrap();
594
595        assert_eq!(book.s.as_str(), "EURUSD-PERP");
596        assert_eq!(book.b.len(), 13);
597        assert_eq!(book.a.len(), 12);
598
599        let instrument = create_eurusd_instrument();
600        let ts_init = UnixNanos::default();
601
602        let deltas = parse_book_l2_deltas(&book, &instrument, 1, ts_init).unwrap();
603
604        // 1 clear + 13 bids + 12 asks = 26 deltas
605        assert_eq!(deltas.deltas.len(), 26);
606        assert_eq!(deltas.instrument_id.symbol.as_str(), "EURUSD-PERP");
607
608        // First delta should be clear
609        assert_eq!(deltas.deltas[0].action, BookAction::Clear);
610
611        // Check first bid level
612        let first_bid = &deltas.deltas[1];
613        assert_eq!(first_bid.order.side, OrderSide::Buy);
614        assert_eq!(first_bid.order.price.as_f64(), 1.1712);
615        assert_eq!(first_bid.order.size.as_f64(), 300.0);
616
617        // Check first ask level (after 13 bids + 1 clear = index 14)
618        let first_ask = &deltas.deltas[14];
619        assert_eq!(first_ask.order.side, OrderSide::Sell);
620        assert_eq!(first_ask.order.price.as_f64(), 1.1719);
621        assert_eq!(first_ask.order.size.as_f64(), 400.0);
622
623        // Last delta should have F_LAST flag
624        let last_delta = deltas.deltas.last().unwrap();
625        assert!(last_delta.flags & RecordFlag::F_LAST as u8 != 0);
626    }
627
628    #[rstest]
629    fn test_parse_book_l3_from_captured_data() {
630        let json = include_str!("../../../test_data/ws_md_book_l3_captured.json");
631        let book: AxMdBookL3 = serde_json::from_str(json).unwrap();
632
633        assert_eq!(book.s.as_str(), "EURUSD-PERP");
634        assert_eq!(book.b.len(), 15);
635        assert_eq!(book.a.len(), 14);
636
637        let instrument = create_eurusd_instrument();
638        let ts_init = UnixNanos::default();
639
640        let deltas = parse_book_l3_deltas(&book, &instrument, 1, ts_init).unwrap();
641
642        // 1 clear + individual orders from each level
643        // Each level has one order in the captured data
644        assert_eq!(deltas.deltas.len(), 30); // 1 clear + 15 bids + 14 asks
645        assert_eq!(deltas.instrument_id.symbol.as_str(), "EURUSD-PERP");
646
647        // First delta should be clear
648        assert_eq!(deltas.deltas[0].action, BookAction::Clear);
649
650        // Check first bid order
651        let first_bid = &deltas.deltas[1];
652        assert_eq!(first_bid.order.side, OrderSide::Buy);
653        assert_eq!(first_bid.order.price.as_f64(), 1.1714);
654        assert_eq!(first_bid.order.size.as_f64(), 100.0);
655
656        // Last delta should have F_LAST flag
657        let last_delta = deltas.deltas.last().unwrap();
658        assert!(last_delta.flags & RecordFlag::F_LAST as u8 != 0);
659    }
660
661    #[rstest]
662    fn test_parse_trade_from_captured_data() {
663        let json = include_str!("../../../test_data/ws_md_trade_captured.json");
664        let trade: AxMdTrade = serde_json::from_str(json).unwrap();
665
666        assert_eq!(trade.s.as_str(), "EURUSD-PERP");
667        assert_eq!(trade.p, dec!(1.1719));
668        assert_eq!(trade.q, 400);
669        assert_eq!(trade.d, Some(AxOrderSide::Buy));
670
671        let instrument = create_eurusd_instrument();
672        let ts_init = UnixNanos::default();
673
674        let tick = parse_trade_tick(&trade, &instrument, ts_init).unwrap();
675
676        assert_eq!(tick.instrument_id.symbol.as_str(), "EURUSD-PERP");
677        assert_eq!(tick.price.as_f64(), 1.1719);
678        assert_eq!(tick.size.as_f64(), 400.0);
679        assert_eq!(tick.aggressor_side, AggressorSide::Buyer);
680        assert_eq!(tick.trade_id.to_string(), "334589144");
681    }
682
683    #[rstest]
684    fn test_parse_book_l1_empty_sides() {
685        let book = AxMdBookL1 {
686            ts: 1700000000,
687            tn: 12345,
688            s: Ustr::from("TEST-PERP"),
689            b: vec![],
690            a: vec![],
691        };
692
693        let instrument = create_test_instrument();
694        let ts_init = UnixNanos::default();
695
696        let quote = parse_book_l1_quote(&book, &instrument, ts_init).unwrap();
697
698        assert_eq!(quote.bid_price.as_f64(), 0.0);
699        assert_eq!(quote.ask_price.as_f64(), 0.0);
700        assert_eq!(quote.bid_size.as_f64(), 0.0);
701        assert_eq!(quote.ask_size.as_f64(), 0.0);
702    }
703
704    #[rstest]
705    fn test_parse_book_l2_empty_book() {
706        let book = AxMdBookL2 {
707            ts: 1700000000,
708            tn: 12345,
709            s: Ustr::from("TEST-PERP"),
710            b: vec![],
711            a: vec![],
712            st: false,
713        };
714
715        let instrument = create_test_instrument();
716        let ts_init = UnixNanos::default();
717
718        let deltas = parse_book_l2_deltas(&book, &instrument, 1, ts_init).unwrap();
719
720        // Just clear delta with F_LAST
721        assert_eq!(deltas.deltas.len(), 1);
722        assert_eq!(deltas.deltas[0].action, BookAction::Clear);
723        assert!(deltas.deltas[0].flags & RecordFlag::F_LAST as u8 != 0);
724    }
725
726    #[rstest]
727    fn test_parse_candle_bar() {
728        use crate::common::enums::AxCandleWidth;
729
730        let candle = AxMdCandle {
731            symbol: Ustr::from("BTC-PERP"),
732            ts: 1700000000,
733            open: dec!(50000.00),
734            high: dec!(51000.00),
735            low: dec!(49500.00),
736            close: dec!(50500.00),
737            volume: 1000,
738            buy_volume: 600,
739            sell_volume: 400,
740            width: AxCandleWidth::Minutes1,
741        };
742
743        let instrument = create_test_instrument();
744        let ts_init = UnixNanos::default();
745
746        let bar = parse_candle_bar(&candle, &instrument, ts_init).unwrap();
747
748        assert_eq!(bar.open.as_f64(), 50000.00);
749        assert_eq!(bar.high.as_f64(), 51000.00);
750        assert_eq!(bar.low.as_f64(), 49500.00);
751        assert_eq!(bar.close.as_f64(), 50500.00);
752        assert_eq!(bar.volume.as_f64(), 1000.0);
753        assert_eq!(bar.bar_type.instrument_id().symbol.as_str(), "BTC-PERP");
754    }
755
756    #[rstest]
757    fn test_parse_candle_from_test_data() {
758        let json = include_str!("../../../test_data/ws_md_candle.json");
759        let candle: AxMdCandle = serde_json::from_str(json).unwrap();
760
761        assert_eq!(candle.symbol.as_str(), "EURUSD-PERP");
762        assert_eq!(candle.open, dec!(49500.00));
763        assert_eq!(candle.close, dec!(50000.00));
764
765        let instrument = create_instrument_with_precision("EURUSD-PERP", 2, 3);
766        let ts_init = UnixNanos::default();
767
768        let bar = parse_candle_bar(&candle, &instrument, ts_init).unwrap();
769
770        assert_eq!(bar.open.as_f64(), 49500.00);
771        assert_eq!(bar.high.as_f64(), 50500.00);
772        assert_eq!(bar.low.as_f64(), 49000.00);
773        assert_eq!(bar.close.as_f64(), 50000.00);
774        assert_eq!(bar.volume.as_f64(), 5000.0);
775        assert_eq!(bar.bar_type.instrument_id().symbol.as_str(), "EURUSD-PERP");
776    }
777}