apple-quant-algorithmic 0.4.0

Apple Quant's algorithmic library.
Documentation
use databento::{
	dbn::{Dataset, Schema},
	live::Subscription, DateTimeLike, LiveClient,
};

use crate::{
	backends::databento::{schema_value, Symbology},
	instrument::InstrumentTicker, schema::SchemaFlags,
};

pub(crate) struct DatabentoLive {
	pub client: LiveClient,
}

impl DatabentoLive {
	pub(crate) async fn new(
		instrument_ticker: &InstrumentTicker,
		start_timestamp: impl DateTimeLike,
		schemas: &[Schema],
		api_key: &str,
	) -> Result<(Self, SchemaFlags), ()> {
		let Some(
			highest_schema,
		) = schemas.iter().max_by_key(|&schema| schema_value(schema)) else {
			return Err(());
		};

		let mut client = LiveClient::builder()
			.key(api_key)
			.expect("no key")
			.dataset(Dataset::GlbxMdp3)
			.build().await.unwrap();

		let offset_date_time = start_timestamp.to_date_time();

		let (
			symbol,
			stype,
		) = instrument_ticker.symbol();

		for &schema in schemas {
			let subscription = Subscription::builder()
				.symbols([symbol.as_str()])
				.schema(schema)
				.stype_in(stype)
				.start(offset_date_time).build();

			client.subscribe(subscription).await.unwrap();
		}

		client.start().await.unwrap();

		Ok((
			Self { client },
			highest_schema.into(),
		))
	}
}