use rustrade_instrument::instrument::name::InstrumentNameExchange;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IbkrSubscription<K> {
pub key: K,
pub instrument: InstrumentNameExchange,
pub kind: IbkrSubscriptionKind,
}
impl<K> IbkrSubscription<K> {
pub fn quotes(key: K, instrument: InstrumentNameExchange) -> Self {
Self {
key,
instrument,
kind: IbkrSubscriptionKind::Quotes,
}
}
pub fn depth(key: K, instrument: InstrumentNameExchange, rows: i32) -> Self {
assert!(
(1..=20).contains(&rows),
"IB depth rows must be 1-20, got {rows}"
);
Self {
key,
instrument,
kind: IbkrSubscriptionKind::Depth { rows },
}
}
pub fn trades(key: K, instrument: InstrumentNameExchange) -> Self {
Self {
key,
instrument,
kind: IbkrSubscriptionKind::Trades,
}
}
pub fn option_greeks(key: K, instrument: InstrumentNameExchange) -> Self {
Self {
key,
instrument,
kind: IbkrSubscriptionKind::OptionGreeks,
}
}
}
#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
pub enum IbkrSubscriptionKind {
Quotes,
Depth {
rows: i32,
},
Trades,
OptionGreeks,
}
#[cfg(test)]
mod tests {
use super::*;
use rustrade_instrument::instrument::name::InstrumentNameExchange;
#[test]
#[should_panic(expected = "IB depth rows must be 1-20")]
fn depth_panics_on_zero_rows() {
let instrument = InstrumentNameExchange::new("AAPL");
IbkrSubscription::<()>::depth((), instrument, 0);
}
#[test]
#[should_panic(expected = "IB depth rows must be 1-20")]
fn depth_panics_on_rows_over_20() {
let instrument = InstrumentNameExchange::new("AAPL");
IbkrSubscription::<()>::depth((), instrument, 21);
}
}