use crate::book::{BinaryPriceSize, BookMarketState, PriceSize};
use crate::types::*;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MarketCloseSnapshot {
pub batch_max_events: u16,
pub total_live_orders: u64,
pub cancelled_total: u64,
pub chunks_done: u32,
pub cursor_after: Option<OrderId>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct MarketSnapshot {
pub market_id: MarketId,
pub seq: u64,
pub market_model: MarketModel,
pub state: BookMarketState,
#[serde(default)]
pub close: Option<MarketCloseSnapshot>,
pub runners: Vec<RunnerSnapshot>,
#[serde(default)]
pub binary: Option<BinaryMarketSnapshot>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct BinaryMarketSnapshot {
pub max_price_ticks: u16,
pub bids: Vec<BinaryPriceSize>,
pub asks: Vec<BinaryPriceSize>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RunnerSnapshot {
pub runner_id: RunnerId,
pub available_to_back: Vec<PriceSize>,
pub available_to_lay: Vec<PriceSize>,
pub matched_volume: Money,
}
impl MarketSnapshot {
pub fn runner(&self, runner_id: RunnerId) -> Option<&RunnerSnapshot> {
self.runners.iter().find(|r| r.runner_id == runner_id)
}
pub fn runner_count(&self) -> usize {
self.runners.len()
}
}
impl RunnerSnapshot {
pub fn best_back(&self) -> Option<&PriceSize> {
self.available_to_back.first()
}
pub fn best_lay(&self) -> Option<&PriceSize> {
self.available_to_lay.first()
}
pub fn spread_ticks(&self) -> Option<u32> {
let back = self.best_back()?;
let lay = self.best_lay()?;
back.price
.ticks_between(lay.price)
.map(|t| t.unsigned_abs())
}
pub fn total_back_volume(&self) -> Money {
Money(self.available_to_back.iter().map(|p| p.size.0).sum())
}
pub fn total_lay_volume(&self) -> Money {
Money(self.available_to_lay.iter().map(|p| p.size.0).sum())
}
}