kiteconnect_async_wasm/models/market_data/
market_depth.rs1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize)]
5pub struct MarketDepthFull {
6 pub buy: Vec<DepthLevel>,
8
9 pub sell: Vec<DepthLevel>,
11}
12
13#[derive(Debug, Clone, Serialize, Deserialize)]
15pub struct DepthLevel {
16 pub price: f64,
18
19 pub quantity: u32,
21
22 pub orders: u32,
24}
25
26#[derive(Debug, Clone, Serialize, Deserialize)]
28pub struct Level2Data {
29 pub bid_price: f64,
31
32 pub bid_quantity: u32,
34
35 pub ask_price: f64,
37
38 pub ask_quantity: u32,
40
41 pub timestamp: chrono::DateTime<chrono::Utc>,
43}
44
45impl MarketDepthFull {
46 pub fn best_bid(&self) -> Option<&DepthLevel> {
48 self.buy.first()
49 }
50
51 pub fn best_ask(&self) -> Option<&DepthLevel> {
53 self.sell.first()
54 }
55
56 pub fn spread(&self) -> Option<f64> {
58 match (self.best_bid(), self.best_ask()) {
59 (Some(bid), Some(ask)) => Some(ask.price - bid.price),
60 _ => None,
61 }
62 }
63
64 pub fn mid_price(&self) -> Option<f64> {
66 match (self.best_bid(), self.best_ask()) {
67 (Some(bid), Some(ask)) => Some((bid.price + ask.price) / 2.0),
68 _ => None,
69 }
70 }
71
72 pub fn total_bid_volume(&self) -> u64 {
74 self.buy.iter().map(|level| level.quantity as u64).sum()
75 }
76
77 pub fn total_ask_volume(&self) -> u64 {
79 self.sell.iter().map(|level| level.quantity as u64).sum()
80 }
81}
82
83impl Level2Data {
84 pub fn spread(&self) -> f64 {
86 self.ask_price - self.bid_price
87 }
88
89 pub fn mid_price(&self) -> f64 {
91 (self.bid_price + self.ask_price) / 2.0
92 }
93
94 pub fn spread_percentage(&self) -> f64 {
96 if self.bid_price > 0.0 {
97 (self.spread() / self.bid_price) * 100.0
98 } else {
99 0.0
100 }
101 }
102}