use time::OffsetDateTime;
use crate::contracts::Contract;
#[cfg(feature = "async")]
use crate::market_data::historical::r#async::TickSubscription;
use crate::market_data::historical::{TickBidAsk, TickLast, TickMidpoint, WhatToShow};
use crate::market_data::{IgnoreSize, TradingHours};
use crate::Error;
#[cfg(test)]
#[path = "ticks_tests.rs"]
mod tests;
#[must_use = "HistoricalTicksBuilder does nothing until you call .trade(), .mid_point(), or .bid_ask(...)"]
pub struct HistoricalTicksBuilder<'a, C> {
client: &'a C,
contract: &'a Contract,
number_of_ticks: i32,
start: Option<OffsetDateTime>,
end: Option<OffsetDateTime>,
trading_hours: TradingHours,
}
impl<'a, C> HistoricalTicksBuilder<'a, C> {
pub(crate) fn new(client: &'a C, contract: &'a Contract, number_of_ticks: i32) -> Self {
Self {
client,
contract,
number_of_ticks,
start: None,
end: None,
trading_hours: TradingHours::Regular,
}
}
pub fn starting(mut self, start: OffsetDateTime) -> Self {
self.start = Some(start);
self
}
pub fn ending(mut self, end: OffsetDateTime) -> Self {
self.end = Some(end);
self
}
pub fn trading_hours(mut self, trading_hours: TradingHours) -> Self {
self.trading_hours = trading_hours;
self
}
}
#[cfg(feature = "sync")]
impl<'a> HistoricalTicksBuilder<'a, crate::client::sync::Client> {
pub fn trade(self) -> Result<crate::market_data::historical::sync::TickSubscription<TickLast>, Error> {
crate::market_data::historical::sync::historical_ticks::<TickLast>(
self.client,
self.contract,
self.start,
self.end,
self.number_of_ticks,
WhatToShow::Trades,
self.trading_hours,
false,
)
}
pub fn mid_point(self) -> Result<crate::market_data::historical::sync::TickSubscription<TickMidpoint>, Error> {
crate::market_data::historical::sync::historical_ticks::<TickMidpoint>(
self.client,
self.contract,
self.start,
self.end,
self.number_of_ticks,
WhatToShow::MidPoint,
self.trading_hours,
false,
)
}
pub fn bid_ask(self, ignore_size: IgnoreSize) -> Result<crate::market_data::historical::sync::TickSubscription<TickBidAsk>, Error> {
crate::market_data::historical::sync::historical_ticks::<TickBidAsk>(
self.client,
self.contract,
self.start,
self.end,
self.number_of_ticks,
WhatToShow::BidAsk,
self.trading_hours,
matches!(ignore_size, IgnoreSize::Yes),
)
}
}
#[cfg(feature = "async")]
impl<'a> HistoricalTicksBuilder<'a, crate::client::r#async::Client> {
pub async fn trade(self) -> Result<TickSubscription<TickLast>, Error> {
crate::market_data::historical::r#async::historical_ticks::<TickLast>(
self.client,
self.contract,
self.start,
self.end,
self.number_of_ticks,
WhatToShow::Trades,
self.trading_hours,
false,
)
.await
}
pub async fn mid_point(self) -> Result<TickSubscription<TickMidpoint>, Error> {
crate::market_data::historical::r#async::historical_ticks::<TickMidpoint>(
self.client,
self.contract,
self.start,
self.end,
self.number_of_ticks,
WhatToShow::MidPoint,
self.trading_hours,
false,
)
.await
}
pub async fn bid_ask(self, ignore_size: IgnoreSize) -> Result<TickSubscription<TickBidAsk>, Error> {
crate::market_data::historical::r#async::historical_ticks::<TickBidAsk>(
self.client,
self.contract,
self.start,
self.end,
self.number_of_ticks,
WhatToShow::BidAsk,
self.trading_hours,
matches!(ignore_size, IgnoreSize::Yes),
)
.await
}
}