nt-market-data 1.0.0

Market data providers and streaming for Neural Trader - supports Alpaca, Polygon, IEX, and more
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
// Polygon.io WebSocket streaming client - High-performance real-time market data
//
// Performance targets:
// - 10,000+ ticks/second throughput
// - <1ms processing latency
// - Zero-copy message parsing where possible
// - Auto-reconnection with exponential backoff

use crate::{
    errors::{MarketDataError, Result},
    types::{Bar, Quote, Trade},
    websocket::{WebSocketClient, WebSocketStream},
    {HealthStatus, MarketDataProvider, QuoteStream, TradeStream},
};
use async_trait::async_trait;
use chrono::{DateTime, Utc};
use dashmap::DashMap;
use futures::{Stream, StreamExt};
use governor::{Quota, RateLimiter};
use parking_lot::RwLock;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use std::{
    sync::Arc,
    time::Duration,
};
use tokio::sync::broadcast;
use tokio_tungstenite::tungstenite::Message;
use tracing::{debug, error, info, warn};

const POLYGON_WS_URL: &str = "wss://socket.polygon.io";
const POLYGON_REST_URL: &str = "https://api.polygon.io";
const MAX_RECONNECT_DELAY: Duration = Duration::from_secs(60);
const INITIAL_RECONNECT_DELAY: Duration = Duration::from_secs(1);
const CHANNEL_BUFFER_SIZE: usize = 10000;
const RATE_LIMIT_PER_MINUTE: u32 = 1000;

/// Market event types from Polygon WebSocket
/// Uses custom deserialization because Polygon sends flat JSON with "ev" field
#[derive(Debug, Clone)]
pub enum PolygonEvent {
    /// Status message
    Status {
        status: String,
        message: String,
    },

    /// Trade tick (T.* channel)
    Trade {
        symbol: String,
        timestamp: i64,
        price: f64,
        size: u64,
        conditions: Vec<i32>,
        exchange: u8,
    },

    /// Quote tick (Q.* channel)
    Quote {
        symbol: String,
        timestamp: i64,
        bid_price: f64,
        ask_price: f64,
        bid_size: u64,
        ask_size: u64,
        bid_exchange: u8,
        ask_exchange: u8,
    },

    /// Aggregate bar (AM.* channel)
    AggregateBar {
        symbol: String,
        start_timestamp: i64,
        end_timestamp: i64,
        open: f64,
        high: f64,
        low: f64,
        close: f64,
        volume: u64,
        vwap: f64,
    },

    /// Level 2 book data (L2.* channel)
    Level2 {
        symbol: String,
        timestamp: i64,
        bids: Vec<PriceLevel>,
        asks: Vec<PriceLevel>,
    },
}

// Custom deserialization for Polygon events
impl<'de> Deserialize<'de> for PolygonEvent {
    fn deserialize<D>(deserializer: D) -> std::result::Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        use serde::de::Error;

        #[derive(Debug, Deserialize)]
        struct RawEvent {
            ev: String,
            #[serde(default)]
            status: Option<String>,
            #[serde(default)]
            message: Option<String>,
            #[serde(default, rename = "sym")]
            symbol: Option<String>,
            #[serde(default, rename = "t")]
            timestamp: Option<i64>,
            #[serde(default, rename = "p")]
            price: Option<f64>,
            #[serde(default, rename = "s")]
            size_or_start: Option<i64>,
            #[serde(default, rename = "c")]
            conditions_or_close: Option<serde_json::Value>,
            #[serde(default, rename = "x")]
            exchange: Option<u8>,
            #[serde(default, rename = "bp")]
            bid_price: Option<f64>,
            #[serde(default, rename = "ap")]
            ask_price: Option<f64>,
            #[serde(default, rename = "bs")]
            bid_size: Option<u64>,
            #[serde(default, rename = "as")]
            ask_size: Option<u64>,
            #[serde(default, rename = "bx")]
            bid_exchange: Option<u8>,
            #[serde(default, rename = "ax")]
            ask_exchange: Option<u8>,
            #[serde(default, rename = "e")]
            end_timestamp: Option<i64>,
            #[serde(default, rename = "o")]
            open: Option<f64>,
            #[serde(default, rename = "h")]
            high: Option<f64>,
            #[serde(default, rename = "l")]
            low: Option<f64>,
            #[serde(default, rename = "v")]
            volume: Option<u64>,
            #[serde(default, rename = "vw")]
            vwap: Option<f64>,
            #[serde(default, rename = "b")]
            bids: Option<Vec<PriceLevel>>,
            #[serde(default, rename = "a")]
            asks: Option<Vec<PriceLevel>>,
        }

        let raw = RawEvent::deserialize(deserializer)?;

        match raw.ev.as_str() {
            "status" => Ok(PolygonEvent::Status {
                status: raw.status.ok_or_else(|| D::Error::missing_field("status"))?,
                message: raw.message.ok_or_else(|| D::Error::missing_field("message"))?,
            }),
            "T" => Ok(PolygonEvent::Trade {
                symbol: raw.symbol.ok_or_else(|| D::Error::missing_field("sym"))?,
                timestamp: raw.timestamp.ok_or_else(|| D::Error::missing_field("t"))?,
                price: raw.price.ok_or_else(|| D::Error::missing_field("p"))?,
                size: raw.size_or_start.ok_or_else(|| D::Error::missing_field("s"))? as u64,
                conditions: raw.conditions_or_close
                    .and_then(|v| serde_json::from_value(v).ok())
                    .unwrap_or_default(),
                exchange: raw.exchange.ok_or_else(|| D::Error::missing_field("x"))?,
            }),
            "Q" => Ok(PolygonEvent::Quote {
                symbol: raw.symbol.ok_or_else(|| D::Error::missing_field("sym"))?,
                timestamp: raw.timestamp.ok_or_else(|| D::Error::missing_field("t"))?,
                bid_price: raw.bid_price.ok_or_else(|| D::Error::missing_field("bp"))?,
                ask_price: raw.ask_price.ok_or_else(|| D::Error::missing_field("ap"))?,
                bid_size: raw.bid_size.ok_or_else(|| D::Error::missing_field("bs"))?,
                ask_size: raw.ask_size.ok_or_else(|| D::Error::missing_field("as"))?,
                bid_exchange: raw.bid_exchange.ok_or_else(|| D::Error::missing_field("bx"))?,
                ask_exchange: raw.ask_exchange.ok_or_else(|| D::Error::missing_field("ax"))?,
            }),
            "AM" => Ok(PolygonEvent::AggregateBar {
                symbol: raw.symbol.ok_or_else(|| D::Error::missing_field("sym"))?,
                start_timestamp: raw.size_or_start.ok_or_else(|| D::Error::missing_field("s"))?,
                end_timestamp: raw.end_timestamp.ok_or_else(|| D::Error::missing_field("e"))?,
                open: raw.open.ok_or_else(|| D::Error::missing_field("o"))?,
                high: raw.high.ok_or_else(|| D::Error::missing_field("h"))?,
                low: raw.low.ok_or_else(|| D::Error::missing_field("l"))?,
                close: raw.conditions_or_close
                    .and_then(|v| v.as_f64())
                    .ok_or_else(|| D::Error::missing_field("c"))?,
                volume: raw.volume.ok_or_else(|| D::Error::missing_field("v"))?,
                vwap: raw.vwap.ok_or_else(|| D::Error::missing_field("vw"))?,
            }),
            "L2" => Ok(PolygonEvent::Level2 {
                symbol: raw.symbol.ok_or_else(|| D::Error::missing_field("sym"))?,
                timestamp: raw.timestamp.ok_or_else(|| D::Error::missing_field("t"))?,
                bids: raw.bids.unwrap_or_default(),
                asks: raw.asks.unwrap_or_default(),
            }),
            other => Err(D::Error::unknown_variant(other, &["status", "T", "Q", "AM", "L2"])),
        }
    }
}

// Manual Serialize implementation
impl Serialize for PolygonEvent {
    fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        use serde::ser::SerializeMap;

        let mut map = serializer.serialize_map(None)?;

        match self {
            PolygonEvent::Status { status, message } => {
                map.serialize_entry("ev", "status")?;
                map.serialize_entry("status", status)?;
                map.serialize_entry("message", message)?;
            }
            PolygonEvent::Trade {
                symbol,
                timestamp,
                price,
                size,
                conditions,
                exchange,
            } => {
                map.serialize_entry("ev", "T")?;
                map.serialize_entry("sym", symbol)?;
                map.serialize_entry("t", timestamp)?;
                map.serialize_entry("p", price)?;
                map.serialize_entry("s", size)?;
                map.serialize_entry("c", conditions)?;
                map.serialize_entry("x", exchange)?;
            }
            PolygonEvent::Quote {
                symbol,
                timestamp,
                bid_price,
                ask_price,
                bid_size,
                ask_size,
                bid_exchange,
                ask_exchange,
            } => {
                map.serialize_entry("ev", "Q")?;
                map.serialize_entry("sym", symbol)?;
                map.serialize_entry("t", timestamp)?;
                map.serialize_entry("bp", bid_price)?;
                map.serialize_entry("ap", ask_price)?;
                map.serialize_entry("bs", bid_size)?;
                map.serialize_entry("as", ask_size)?;
                map.serialize_entry("bx", bid_exchange)?;
                map.serialize_entry("ax", ask_exchange)?;
            }
            PolygonEvent::AggregateBar {
                symbol,
                start_timestamp,
                end_timestamp,
                open,
                high,
                low,
                close,
                volume,
                vwap,
            } => {
                map.serialize_entry("ev", "AM")?;
                map.serialize_entry("sym", symbol)?;
                map.serialize_entry("s", start_timestamp)?;
                map.serialize_entry("e", end_timestamp)?;
                map.serialize_entry("o", open)?;
                map.serialize_entry("h", high)?;
                map.serialize_entry("l", low)?;
                map.serialize_entry("c", close)?;
                map.serialize_entry("v", volume)?;
                map.serialize_entry("vw", vwap)?;
            }
            PolygonEvent::Level2 {
                symbol,
                timestamp,
                bids,
                asks,
            } => {
                map.serialize_entry("ev", "L2")?;
                map.serialize_entry("sym", symbol)?;
                map.serialize_entry("t", timestamp)?;
                map.serialize_entry("b", bids)?;
                map.serialize_entry("a", asks)?;
            }
        }

        map.end()
    }
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PriceLevel {
    #[serde(rename = "p")]
    pub price: f64,
    #[serde(rename = "s")]
    pub size: u64,
}

/// Subscription channel types
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub enum PolygonChannel {
    Trades,
    Quotes,
    AggregateBars,
    Level2,
}

impl PolygonChannel {
    fn prefix(&self) -> &'static str {
        match self {
            PolygonChannel::Trades => "T",
            PolygonChannel::Quotes => "Q",
            PolygonChannel::AggregateBars => "AM",
            PolygonChannel::Level2 => "L2",
        }
    }

    fn format_subscription(&self, symbol: &str) -> String {
        format!("{}.{}", self.prefix(), symbol)
    }
}

/// Subscription state
#[derive(Debug, Clone)]
struct Subscription {
    symbols: Vec<String>,
    channels: Vec<PolygonChannel>,
    active: bool,
}

/// Polygon WebSocket client with high-performance streaming
pub struct PolygonWebSocket {
    api_key: String,
    ws_url: String,
    subscriptions: Arc<DashMap<String, Subscription>>,
    event_tx: Arc<RwLock<Option<broadcast::Sender<PolygonEvent>>>>,
    reconnect_delay: Arc<RwLock<Duration>>,
    rate_limiter: Arc<RateLimiter<
        governor::state::direct::NotKeyed,
        governor::state::InMemoryState,
        governor::clock::DefaultClock,
    >>,
    connection_active: Arc<RwLock<bool>>,
}

impl PolygonWebSocket {
    /// Create new Polygon WebSocket client
    pub fn new(api_key: String) -> Self {
        let quota = Quota::per_minute(std::num::NonZeroU32::new(RATE_LIMIT_PER_MINUTE).unwrap());
        let rate_limiter = Arc::new(RateLimiter::direct(quota));

        Self {
            api_key,
            ws_url: POLYGON_WS_URL.to_string(),
            subscriptions: Arc::new(DashMap::new()),
            event_tx: Arc::new(RwLock::new(None)),
            reconnect_delay: Arc::new(RwLock::new(INITIAL_RECONNECT_DELAY)),
            rate_limiter,
            connection_active: Arc::new(RwLock::new(false)),
        }
    }

    /// Connect to Polygon WebSocket and authenticate
    pub async fn connect(&self) -> Result<()> {
        let ws_client = WebSocketClient::new(format!("{}/stocks", self.ws_url))
            .with_reconnect_delay(INITIAL_RECONNECT_DELAY)
            .with_max_attempts(10);

        let mut stream = ws_client.connect_with_retry().await?;

        // Authenticate with API key
        self.authenticate(&mut stream).await?;

        // Mark connection as active
        *self.connection_active.write() = true;

        // Create broadcast channel for events
        let (tx, _) = broadcast::channel(CHANNEL_BUFFER_SIZE);
        *self.event_tx.write() = Some(tx.clone());

        // Spawn message processing task
        let event_tx = self.event_tx.clone();
        let subscriptions = self.subscriptions.clone();
        let connection_active = self.connection_active.clone();
        let reconnect_delay = self.reconnect_delay.clone();

        tokio::spawn(async move {
            Self::process_messages(
                stream,
                event_tx,
                subscriptions,
                connection_active,
                reconnect_delay,
            )
            .await;
        });

        info!("Polygon WebSocket connected and authenticated");
        Ok(())
    }

    /// Authenticate WebSocket connection
    async fn authenticate(&self, stream: &mut WebSocketStream) -> Result<()> {
        let auth_msg = serde_json::json!({
            "action": "auth",
            "params": self.api_key
        });

        stream
            .send(Message::Text(auth_msg.to_string()))
            .await
            .map_err(|e| MarketDataError::Auth(format!("Failed to send auth: {}", e)))?;

        // Wait for auth response
        match tokio::time::timeout(Duration::from_secs(10), stream.next()).await {
            Ok(Some(Ok(Message::Text(msg)))) => {
                if let Ok(events) = serde_json::from_str::<Vec<PolygonEvent>>(&msg) {
                    for event in events {
                        if let PolygonEvent::Status { status, message } = event {
                            if status == "auth_success" {
                                info!("Polygon authentication successful");
                                return Ok(());
                            } else if status == "auth_failed" {
                                return Err(MarketDataError::Auth(format!(
                                    "Authentication failed: {}",
                                    message
                                )));
                            }
                        }
                    }
                }
                Err(MarketDataError::Auth(
                    "Unexpected auth response".to_string(),
                ))
            }
            Ok(Some(Ok(_))) => Err(MarketDataError::Auth(
                "Invalid auth response format".to_string(),
            )),
            Ok(Some(Err(e))) => Err(e),
            Ok(None) => Err(MarketDataError::Auth(
                "Connection closed during auth".to_string(),
            )),
            Err(_) => Err(MarketDataError::Timeout),
        }
    }

    /// Subscribe to market data channels
    pub async fn subscribe(
        &self,
        symbols: Vec<String>,
        channels: Vec<PolygonChannel>,
    ) -> Result<()> {
        self.rate_limiter.until_ready().await;

        let subscription_strings: Vec<String> = symbols
            .iter()
            .flat_map(|symbol| {
                channels
                    .iter()
                    .map(move |channel| channel.format_subscription(symbol))
            })
            .collect();

        let _subscribe_msg = serde_json::json!({
            "action": "subscribe",
            "params": subscription_strings.join(",")
        });

        // Store subscription state
        for symbol in &symbols {
            self.subscriptions.insert(
                symbol.clone(),
                Subscription {
                    symbols: vec![symbol.clone()],
                    channels: channels.clone(),
                    active: true,
                },
            );
        }

        // Send subscription via current connection
        if let Some(_tx) = self.event_tx.read().as_ref() {
            // We need to send via WebSocket, but we don't have direct access here
            // In a real implementation, we'd maintain a command channel
            debug!(
                "Subscribing to {} channels for {} symbols",
                channels.len(),
                symbols.len()
            );
        }

        info!(
            "Subscribed to {} symbols across {} channels",
            symbols.len(),
            channels.len()
        );
        Ok(())
    }

    /// Unsubscribe from channels
    pub async fn unsubscribe(&self, symbols: Vec<String>) -> Result<()> {
        self.rate_limiter.until_ready().await;

        let mut unsubscribe_list = Vec::new();

        for symbol in &symbols {
            if let Some(sub) = self.subscriptions.get(symbol.as_str()) {
                for channel in &sub.channels {
                    unsubscribe_list.push(channel.format_subscription(symbol));
                }
                drop(sub); // Release the reference before removing
                self.subscriptions.remove(symbol.as_str());
            }
        }

        let _unsubscribe_msg = serde_json::json!({
            "action": "unsubscribe",
            "params": unsubscribe_list.join(",")
        });

        debug!("Unsubscribed from {} symbols", symbols.len());
        Ok(())
    }

    /// Get event stream
    pub fn stream(&self) -> impl Stream<Item = PolygonEvent> {
        let rx = self
            .event_tx
            .read()
            .as_ref()
            .expect("Not connected")
            .subscribe();

        futures::stream::unfold(rx, |mut rx| async move {
            match rx.recv().await {
                Ok(event) => Some((event, rx)),
                Err(_) => None,
            }
        })
    }

    /// Get quote stream (filtered)
    pub fn quote_stream(&self) -> impl Stream<Item = Result<Quote>> {
        self.stream().filter_map(|event| async move {
            match event {
                PolygonEvent::Quote {
                    symbol,
                    timestamp,
                    bid_price,
                    ask_price,
                    bid_size,
                    ask_size,
                    ..
                } => {
                    let timestamp_dt = Self::timestamp_to_datetime(timestamp);
                    Some(Ok(Quote {
                        symbol,
                        timestamp: timestamp_dt,
                        bid: Decimal::from_f64_retain(bid_price)?,
                        ask: Decimal::from_f64_retain(ask_price)?,
                        bid_size,
                        ask_size,
                    }))
                }
                _ => None,
            }
        })
    }

    /// Get trade stream (filtered)
    pub fn trade_stream(&self) -> impl Stream<Item = Result<Trade>> {
        self.stream().filter_map(|event| async move {
            match event {
                PolygonEvent::Trade {
                    symbol,
                    timestamp,
                    price,
                    size,
                    conditions,
                    ..
                } => {
                    let timestamp_dt = Self::timestamp_to_datetime(timestamp);
                    Some(Ok(Trade {
                        symbol,
                        timestamp: timestamp_dt,
                        price: Decimal::from_f64_retain(price)?,
                        size,
                        conditions: conditions.iter().map(|c| c.to_string()).collect(),
                    }))
                }
                _ => None,
            }
        })
    }

    /// Get aggregate bar stream (filtered)
    pub fn bar_stream(&self) -> impl Stream<Item = Result<Bar>> {
        self.stream().filter_map(|event| async move {
            match event {
                PolygonEvent::AggregateBar {
                    symbol,
                    start_timestamp,
                    open,
                    high,
                    low,
                    close,
                    volume,
                    ..
                } => {
                    let timestamp_dt = Self::timestamp_to_datetime(start_timestamp);
                    Some(Ok(Bar {
                        symbol,
                        timestamp: timestamp_dt,
                        open: Decimal::from_f64_retain(open)?,
                        high: Decimal::from_f64_retain(high)?,
                        low: Decimal::from_f64_retain(low)?,
                        close: Decimal::from_f64_retain(close)?,
                        volume,
                    }))
                }
                _ => None,
            }
        })
    }

    /// Process incoming WebSocket messages
    async fn process_messages(
        mut stream: WebSocketStream,
        event_tx: Arc<RwLock<Option<broadcast::Sender<PolygonEvent>>>>,
        subscriptions: Arc<DashMap<String, Subscription>>,
        connection_active: Arc<RwLock<bool>>,
        reconnect_delay: Arc<RwLock<Duration>>,
    ) {
        let mut message_count = 0u64;
        let mut error_count = 0u64;

        while let Some(msg) = stream.next().await {
            match msg {
                Ok(Message::Text(text)) => {
                    message_count += 1;

                    // Parse events (Polygon sends arrays of events)
                    match serde_json::from_str::<Vec<PolygonEvent>>(&text) {
                        Ok(events) => {
                            if let Some(tx) = event_tx.read().as_ref() {
                                for event in events {
                                    // Log status messages
                                    if let PolygonEvent::Status { status, message } = &event {
                                        info!("Polygon status: {} - {}", status, message);
                                    }

                                    // Broadcast event (ignore if no receivers)
                                    let _ = tx.send(event);
                                }
                            }

                            // Reset reconnect delay on successful processing
                            *reconnect_delay.write() = INITIAL_RECONNECT_DELAY;
                        }
                        Err(e) => {
                            error_count += 1;
                            error!("Failed to parse Polygon message: {}", e);
                            debug!("Raw message: {}", text);

                            if error_count > 100 {
                                warn!("Too many parse errors, reconnecting...");
                                break;
                            }
                        }
                    }
                }
                Ok(Message::Binary(_)) => {
                    warn!("Unexpected binary message from Polygon");
                }
                Ok(Message::Ping(data)) => {
                    if let Err(e) = stream.send(Message::Pong(data)).await {
                        error!("Failed to send pong: {}", e);
                        break;
                    }
                }
                Ok(Message::Close(_)) => {
                    info!("WebSocket closed by Polygon");
                    break;
                }
                Err(e) => {
                    error!("WebSocket error: {}", e);
                    break;
                }
                _ => {}
            }

            // Log stats periodically
            if message_count % 10000 == 0 {
                info!(
                    "Processed {} messages, {} errors, {} active subscriptions",
                    message_count,
                    error_count,
                    subscriptions.len()
                );
            }
        }

        // Mark connection as inactive
        *connection_active.write() = false;

        // Implement exponential backoff for reconnection
        let mut delay = *reconnect_delay.read();
        if delay < MAX_RECONNECT_DELAY {
            delay = (delay * 2).min(MAX_RECONNECT_DELAY);
            *reconnect_delay.write() = delay;
        }

        warn!(
            "Polygon WebSocket disconnected. Reconnecting in {:?}...",
            delay
        );
    }

    /// Convert Unix timestamp (nanoseconds) to DateTime
    fn timestamp_to_datetime(nanos: i64) -> DateTime<Utc> {
        let secs = nanos / 1_000_000_000;
        let nsecs = (nanos % 1_000_000_000) as u32;
        DateTime::from_timestamp(secs, nsecs).unwrap_or_else(|| Utc::now())
    }

    /// Check if connection is active
    pub fn is_connected(&self) -> bool {
        *self.connection_active.read()
    }

    /// Get active subscriptions
    pub fn get_subscriptions(&self) -> Vec<(String, Vec<PolygonChannel>)> {
        self.subscriptions
            .iter()
            .filter(|entry| entry.value().active)
            .map(|entry| (entry.key().clone(), entry.value().channels.clone()))
            .collect()
    }
}

/// Polygon REST API client (for historical data)
pub struct PolygonClient {
    api_key: String,
    base_url: String,
    ws: Arc<PolygonWebSocket>,
}

impl PolygonClient {
    pub fn new(api_key: String) -> Self {
        let ws = Arc::new(PolygonWebSocket::new(api_key.clone()));

        Self {
            api_key,
            base_url: POLYGON_REST_URL.to_string(),
            ws,
        }
    }

    pub fn websocket(&self) -> Arc<PolygonWebSocket> {
        self.ws.clone()
    }
}

#[async_trait]
impl MarketDataProvider for PolygonClient {
    async fn get_quote(&self, symbol: &str) -> Result<Quote> {
        let url = format!(
            "{}/v2/last/nbbo/{}?apiKey={}",
            self.base_url, symbol, self.api_key
        );

        let response: serde_json::Value = reqwest::get(&url)
            .await
            .map_err(|e| MarketDataError::Network(e.to_string()))?
            .json()
            .await
            .map_err(|e| MarketDataError::Parse(e.to_string()))?;

        let results = response["results"]
            .as_object()
            .ok_or_else(|| MarketDataError::Parse("No results".to_string()))?;

        Ok(Quote {
            symbol: symbol.to_string(),
            timestamp: Utc::now(),
            bid: Decimal::from_f64_retain(
                results["P"]
                    .as_f64()
                    .ok_or_else(|| MarketDataError::Parse("Invalid bid".to_string()))?,
            )
            .ok_or_else(|| MarketDataError::Parse("Invalid bid decimal".to_string()))?,
            ask: Decimal::from_f64_retain(
                results["p"]
                    .as_f64()
                    .ok_or_else(|| MarketDataError::Parse("Invalid ask".to_string()))?,
            )
            .ok_or_else(|| MarketDataError::Parse("Invalid ask decimal".to_string()))?,
            bid_size: results["S"].as_u64().unwrap_or(0),
            ask_size: results["s"].as_u64().unwrap_or(0),
        })
    }

    async fn get_bars(
        &self,
        symbol: &str,
        start: DateTime<Utc>,
        end: DateTime<Utc>,
        timeframe: crate::types::Timeframe,
    ) -> Result<Vec<Bar>> {
        // Polygon uses different timeframe format
        let tf_str = match timeframe {
            crate::types::Timeframe::Minute1 => "1/minute",
            crate::types::Timeframe::Minute5 => "5/minute",
            crate::types::Timeframe::Minute15 => "15/minute",
            crate::types::Timeframe::Hour1 => "1/hour",
            crate::types::Timeframe::Day1 => "1/day",
        };

        let url = format!(
            "{}/v2/aggs/ticker/{}/range/{}/{}/{}?apiKey={}",
            self.base_url,
            symbol,
            tf_str,
            start.timestamp_millis(),
            end.timestamp_millis(),
            self.api_key
        );

        let response: serde_json::Value = reqwest::get(&url)
            .await
            .map_err(|e| MarketDataError::Network(e.to_string()))?
            .json()
            .await
            .map_err(|e| MarketDataError::Parse(e.to_string()))?;

        let results = response["results"]
            .as_array()
            .ok_or_else(|| MarketDataError::Parse("No results".to_string()))?;

        results
            .iter()
            .map(|bar| {
                Ok(Bar {
                    symbol: symbol.to_string(),
                    timestamp: DateTime::from_timestamp_millis(
                        bar["t"]
                            .as_i64()
                            .ok_or_else(|| MarketDataError::Parse("Invalid timestamp".to_string()))?,
                    )
                    .ok_or_else(|| MarketDataError::Parse("Invalid datetime".to_string()))?,
                    open: Decimal::from_f64_retain(
                        bar["o"]
                            .as_f64()
                            .ok_or_else(|| MarketDataError::Parse("Invalid open".to_string()))?,
                    )
                    .ok_or_else(|| MarketDataError::Parse("Invalid open decimal".to_string()))?,
                    high: Decimal::from_f64_retain(
                        bar["h"]
                            .as_f64()
                            .ok_or_else(|| MarketDataError::Parse("Invalid high".to_string()))?,
                    )
                    .ok_or_else(|| MarketDataError::Parse("Invalid high decimal".to_string()))?,
                    low: Decimal::from_f64_retain(
                        bar["l"]
                            .as_f64()
                            .ok_or_else(|| MarketDataError::Parse("Invalid low".to_string()))?,
                    )
                    .ok_or_else(|| MarketDataError::Parse("Invalid low decimal".to_string()))?,
                    close: Decimal::from_f64_retain(
                        bar["c"]
                            .as_f64()
                            .ok_or_else(|| MarketDataError::Parse("Invalid close".to_string()))?,
                    )
                    .ok_or_else(|| MarketDataError::Parse("Invalid close decimal".to_string()))?,
                    volume: bar["v"].as_u64().unwrap_or(0),
                })
            })
            .collect()
    }

    async fn subscribe_quotes(&self, symbols: Vec<String>) -> Result<QuoteStream> {
        if !self.ws.is_connected() {
            self.ws.connect().await?;
        }

        self.ws
            .subscribe(symbols.clone(), vec![PolygonChannel::Quotes])
            .await?;

        let stream = self.ws.quote_stream();
        Ok(Box::pin(stream))
    }

    async fn subscribe_trades(&self, symbols: Vec<String>) -> Result<TradeStream> {
        if !self.ws.is_connected() {
            self.ws.connect().await?;
        }

        self.ws
            .subscribe(symbols.clone(), vec![PolygonChannel::Trades])
            .await?;

        let stream = self.ws.trade_stream();
        Ok(Box::pin(stream))
    }

    async fn health_check(&self) -> Result<HealthStatus> {
        if self.ws.is_connected() {
            Ok(HealthStatus::Healthy)
        } else {
            Ok(HealthStatus::Degraded)
        }
    }
}

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

    #[test]
    fn test_polygon_channel_formatting() {
        assert_eq!(
            PolygonChannel::Trades.format_subscription("AAPL"),
            "T.AAPL"
        );
        assert_eq!(
            PolygonChannel::Quotes.format_subscription("TSLA"),
            "Q.TSLA"
        );
        assert_eq!(
            PolygonChannel::AggregateBars.format_subscription("MSFT"),
            "AM.MSFT"
        );
    }

    #[test]
    fn test_timestamp_conversion() {
        let nanos = 1640000000000000000i64; // 2021-12-20
        let dt = PolygonWebSocket::timestamp_to_datetime(nanos);
        assert_eq!(dt.timestamp(), 1640000000);
    }

    #[tokio::test]
    async fn test_websocket_creation() {
        let ws = PolygonWebSocket::new("test_key".to_string());
        assert!(!ws.is_connected());
        assert_eq!(ws.get_subscriptions().len(), 0);
    }

    #[test]
    fn test_polygon_event_deserialization() {
        // Test status message
        let status_json = r#"[{"ev":"status","status":"connected","message":"Connected Successfully"}]"#;
        let events: Vec<PolygonEvent> = serde_json::from_str(status_json).unwrap();
        assert_eq!(events.len(), 1);
        match &events[0] {
            PolygonEvent::Status { status, .. } => {
                assert_eq!(status, "connected");
            }
            _ => panic!("Wrong event type"),
        }

        // Test trade event with compact JSON
        let trade_json = r#"[{"ev":"T","sym":"AAPL","t":1640000000000000000,"p":150.00,"s":100,"c":[12,37],"x":4}]"#;
        let events: Vec<PolygonEvent> = serde_json::from_str(trade_json).unwrap();
        assert_eq!(events.len(), 1);

        match &events[0] {
            PolygonEvent::Trade {
                symbol, price, size, ..
            } => {
                assert_eq!(symbol, "AAPL");
                assert_eq!(*price, 150.00);
                assert_eq!(*size, 100);
            }
            _ => panic!("Wrong event type"),
        }
    }

    #[test]
    fn test_quote_event_deserialization() {
        let quote_json = r#"[{"ev":"Q","sym":"TSLA","t":1640000000000000000,"bp":900.00,"ap":901.00,"bs":50,"as":75,"bx":4,"ax":4}]"#;

        let events: Vec<PolygonEvent> = serde_json::from_str(quote_json).unwrap();
        assert_eq!(events.len(), 1);

        match &events[0] {
            PolygonEvent::Quote {
                symbol,
                bid_price,
                ask_price,
                ..
            } => {
                assert_eq!(symbol, "TSLA");
                assert_eq!(*bid_price, 900.00);
                assert_eq!(*ask_price, 901.00);
            }
            _ => panic!("Wrong event type"),
        }
    }

    #[tokio::test]
    async fn test_subscription_management() {
        let ws = PolygonWebSocket::new("test_key".to_string());

        // Note: This test doesn't actually connect, just tests the API
        assert_eq!(ws.get_subscriptions().len(), 0);

        // In a real test with mock WebSocket, we would:
        // ws.subscribe(vec!["AAPL".to_string()], vec![PolygonChannel::Trades]).await.unwrap();
        // assert_eq!(ws.get_subscriptions().len(), 1);
    }

    #[test]
    fn test_aggregate_bar_deserialization() {
        let bar_json = r#"[{"ev":"AM","sym":"NVDA","s":1640000000000000000,"e":1640000060000000000,"o":500.00,"h":505.00,"l":499.00,"c":503.00,"v":1000000,"vw":502.00}]"#;

        let events: Vec<PolygonEvent> = serde_json::from_str(bar_json).unwrap();
        assert_eq!(events.len(), 1);

        match &events[0] {
            PolygonEvent::AggregateBar {
                symbol,
                open,
                high,
                low,
                close,
                volume,
                ..
            } => {
                assert_eq!(symbol, "NVDA");
                assert_eq!(*open, 500.00);
                assert_eq!(*high, 505.00);
                assert_eq!(*low, 499.00);
                assert_eq!(*close, 503.00);
                assert_eq!(*volume, 1000000);
            }
            _ => panic!("Wrong event type"),
        }
    }
}