finance_query_core/websocket/
mod.rs

1//! WebSocket support types for real-time data streaming.
2//!
3//! This module provides framework-agnostic data structures for WebSocket
4//! subscriptions. These types can be used with any WebSocket framework.
5
6use chrono::{DateTime, Utc};
7use serde::{Deserialize, Serialize};
8
9use crate::models::{Quote, SimpleQuote, News};
10use crate::models::movers::MarketMover;
11use crate::models::sectors::MarketSector;
12
13/// Real-time quote update for streaming stock quotes.
14///
15/// This type supports streaming a single quote or multiple quotes at once.
16#[derive(Debug, Clone, Serialize, Deserialize)]
17pub struct QuotesUpdate {
18    /// List of updated quotes
19    pub quotes: Vec<SimpleQuote>,
20    /// Timestamp of the update
21    pub timestamp: DateTime<Utc>,
22}
23
24impl QuotesUpdate {
25    /// Create a new QuotesUpdate with a single quote.
26    pub fn single(quote: SimpleQuote) -> Self {
27        Self {
28            quotes: vec![quote],
29            timestamp: Utc::now(),
30        }
31    }
32
33    /// Create a new QuotesUpdate with multiple quotes.
34    pub fn multiple(quotes: Vec<SimpleQuote>) -> Self {
35        Self {
36            quotes,
37            timestamp: Utc::now(),
38        }
39    }
40
41    /// Create a new QuotesUpdate with a specific timestamp.
42    pub fn with_timestamp(quotes: Vec<SimpleQuote>, timestamp: DateTime<Utc>) -> Self {
43        Self { quotes, timestamp }
44    }
45
46    /// Check if this update contains a specific symbol.
47    pub fn contains_symbol(&self, symbol: &str) -> bool {
48        self.quotes.iter().any(|q| q.symbol == symbol)
49    }
50
51    /// Get a quote by symbol if present.
52    pub fn get_quote(&self, symbol: &str) -> Option<&SimpleQuote> {
53        self.quotes.iter().find(|q| q.symbol == symbol)
54    }
55
56    /// Returns true if this update contains no quotes.
57    pub fn is_empty(&self) -> bool {
58        self.quotes.is_empty()
59    }
60
61    /// Returns the number of quotes in this update.
62    pub fn len(&self) -> usize {
63        self.quotes.len()
64    }
65}
66
67/// Profile update containing quote, similar stocks, sector performance, and news.
68#[derive(Debug, Clone, Serialize, Deserialize)]
69pub struct ProfileUpdate {
70    /// Current quote data for the symbol
71    pub quote: Option<Quote>,
72    /// Similar stocks to the symbol
73    pub similar: Option<Vec<SimpleQuote>>,
74    /// Sector performance data
75    pub sector_performance: Option<MarketSector>,
76    /// Recent news for the symbol
77    pub news: Option<Vec<News>>,
78}
79
80/// Market movers update containing actives, gainers, and losers.
81#[derive(Debug, Clone, Serialize, Deserialize)]
82pub struct MoversUpdate {
83    /// Most active stocks
84    pub actives: Option<Vec<MarketMover>>,
85    /// Top gaining stocks
86    pub gainers: Option<Vec<MarketMover>>,
87    /// Top losing stocks
88    pub losers: Option<Vec<MarketMover>>,
89}
90
91/// Market hours status update.
92#[derive(Debug, Clone, Serialize, Deserialize)]
93pub struct MarketHours {
94    /// Current market status (e.g., "open", "closed", "pre-market", "after-hours")
95    pub status: String,
96    /// Optional reason for the status (e.g., holiday name)
97    pub reason: Option<String>,
98    /// Timestamp of the status update
99    pub timestamp: DateTime<Utc>,
100}
101
102/// Moving average update for real-time indicator streaming.
103#[derive(Debug, Clone, Serialize, Deserialize)]
104pub struct MovingAverageUpdate {
105    /// Stock symbol
106    pub symbol: String,
107    /// Indicator type (e.g., "SMA", "EMA")
108    pub indicator_type: String,
109    /// Period for the moving average
110    pub period: i32,
111    /// Calculated value
112    pub value: f64,
113    /// Timestamp of the calculation
114    pub timestamp: DateTime<Utc>,
115}