use super::triggers::TrailingStop;
use super::validate_instrument;
use crate::{
error::RithmicError,
types::{ManualOrAutoEntry, OrderSide, OrderType, TimeInForce},
};
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
#[must_use = "a leg does nothing until added to an OCO group"]
pub struct RithmicOcoOrderLeg {
pub symbol: String,
pub exchange: String,
pub quantity: i32,
pub price: Option<f64>,
pub trigger_price: Option<f64>,
pub transaction_type: OrderSide,
pub duration: TimeInForce,
pub price_type: OrderType,
pub user_tag: String,
pub trailing_stop: Option<TrailingStop>,
pub trade_route: Option<String>,
pub manual_or_auto: ManualOrAutoEntry,
pub window_name: Option<String>,
}
impl RithmicOcoOrderLeg {
pub fn new() -> Self {
Self::default()
}
pub fn symbol(mut self, symbol: impl Into<String>) -> Self {
self.symbol = symbol.into();
self
}
pub fn exchange(mut self, exchange: impl Into<String>) -> Self {
self.exchange = exchange.into();
self
}
pub fn quantity(mut self, quantity: i32) -> Self {
self.quantity = quantity;
self
}
pub fn transaction_type(mut self, transaction_type: OrderSide) -> Self {
self.transaction_type = transaction_type;
self
}
pub fn price_type(mut self, price_type: OrderType) -> Self {
self.price_type = price_type;
self
}
pub fn price(mut self, price: f64) -> Self {
self.price = Some(price);
self
}
pub fn trigger_price(mut self, trigger_price: f64) -> Self {
self.trigger_price = Some(trigger_price);
self
}
pub fn duration(mut self, duration: TimeInForce) -> Self {
self.duration = duration;
self
}
pub fn user_tag(mut self, user_tag: impl Into<String>) -> Self {
self.user_tag = user_tag.into();
self
}
pub fn trailing_stop(mut self, trailing_stop: TrailingStop) -> Self {
self.trailing_stop = Some(trailing_stop);
self
}
pub fn trailing_stop_by(self, trail_by_ticks: i32, trail_by_price_id: i32) -> Self {
self.trailing_stop(
TrailingStop::new()
.trail_by_ticks(trail_by_ticks)
.trail_by_price_id(trail_by_price_id),
)
}
pub fn trade_route(mut self, trade_route: impl Into<String>) -> Self {
self.trade_route = Some(trade_route.into());
self
}
pub fn manual_or_auto(mut self, manual_or_auto: ManualOrAutoEntry) -> Self {
self.manual_or_auto = manual_or_auto;
self
}
pub fn window_name(mut self, window_name: impl Into<String>) -> Self {
self.window_name = Some(window_name.into());
self
}
pub fn validate(&self) -> Result<(), RithmicError> {
validate_instrument(&self.symbol, &self.exchange, self.quantity)?;
if matches!(
self.price_type,
OrderType::MarketIfTouched | OrderType::LimitIfTouched
) {
return Err(RithmicError::InvalidArgument(format!(
"price_type {} is not available on an OCO leg",
self.price_type.as_str_name()
)));
}
super::require_prices(self.price_type, self.price, self.trigger_price)
}
pub fn build(self) -> Result<Self, RithmicError> {
self.validate()?;
Ok(self)
}
}
#[derive(Debug, Clone, Default, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[non_exhaustive]
#[must_use = "an order does nothing until passed to a plant handle"]
pub struct RithmicOcoOrder {
pub legs: Vec<RithmicOcoOrderLeg>,
pub cancel_at_ssboe: Option<i32>,
pub cancel_at_usecs: Option<i32>,
pub cancel_after_secs: Option<i32>,
}
impl RithmicOcoOrder {
pub fn new() -> Self {
Self::default()
}
pub fn leg(mut self, leg: RithmicOcoOrderLeg) -> Self {
self.legs.push(leg);
self
}
pub fn legs(mut self, legs: impl IntoIterator<Item = RithmicOcoOrderLeg>) -> Self {
self.legs.extend(legs);
self
}
pub fn cancel_at_ssboe(mut self, ssboe: i32) -> Self {
self.cancel_at_ssboe = Some(ssboe);
self
}
pub fn cancel_at_usecs(mut self, usecs: i32) -> Self {
self.cancel_at_usecs = Some(usecs);
self
}
pub fn cancel_at(self, ssboe: i32, usecs: i32) -> Self {
self.cancel_at_ssboe(ssboe).cancel_at_usecs(usecs)
}
pub fn cancel_after_secs(mut self, secs: i32) -> Self {
self.cancel_after_secs = Some(secs);
self
}
pub fn validate(&self) -> Result<(), RithmicError> {
for leg in &self.legs {
leg.validate()?;
}
Ok(())
}
pub fn build(self) -> Result<Self, RithmicError> {
self.validate()?;
Ok(self)
}
pub(crate) fn cancel_timing(&self) -> OcoCancelTiming {
OcoCancelTiming {
cancel_at_ssboe: self.cancel_at_ssboe,
cancel_at_usecs: self.cancel_at_usecs,
cancel_after_secs: self.cancel_after_secs,
}
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub(crate) struct OcoCancelTiming {
pub(crate) cancel_at_ssboe: Option<i32>,
pub(crate) cancel_at_usecs: Option<i32>,
pub(crate) cancel_after_secs: Option<i32>,
}
#[cfg(test)]
mod tests {
use super::*;
fn leg(price_type: OrderType) -> RithmicOcoOrderLeg {
RithmicOcoOrderLeg {
symbol: "ESM6".to_string(),
exchange: "CME".to_string(),
quantity: 1,
price_type,
..Default::default()
}
}
#[test]
fn an_oco_leg_validates_on_the_same_rules() {
let mut leg = leg(OrderType::Limit);
assert!(leg.validate().is_err());
leg.price = Some(5000.0);
assert!(leg.validate().is_ok());
}
#[test]
fn an_oco_leg_rejects_the_if_touched_price_types() {
let leg = RithmicOcoOrderLeg {
price: Some(5000.0),
trigger_price: Some(5000.0),
..leg(OrderType::LimitIfTouched)
};
let err = leg.validate().unwrap_err().to_string();
assert!(err.contains("LIMIT_IF_TOUCHED"), "{err}");
assert!(err.contains("is not available on an OCO leg"), "{err}");
}
#[test]
fn an_oco_order_validates_each_leg_but_not_the_count() {
let ok = leg(OrderType::Market);
assert!(RithmicOcoOrder::default().validate().is_ok());
assert!(
RithmicOcoOrder {
legs: vec![ok.clone()],
..Default::default()
}
.validate()
.is_ok()
);
let bad = leg(OrderType::Limit);
assert!(
RithmicOcoOrder {
legs: vec![ok, bad],
..Default::default()
}
.validate()
.is_err()
);
}
}