Skip to main content

longport/trade/requests/
submit_multileg.rs

1use rust_decimal::Decimal;
2use serde::Serialize;
3use time::OffsetDateTime;
4
5use crate::{
6    trade::{MultiLegStrategy, OrderSide, OrderType},
7    types::normalize_symbol_owned,
8};
9
10/// A leg of a multi-leg combination order to submit
11#[derive(Debug, Serialize, Clone)]
12pub struct SubmitMultiLegOrderLeg {
13    /// Option symbol, in `ticker.region` format (e.g. `QQQ260731C764000.US`)
14    symbol: String,
15    /// Leg ratio quantity — must be a positive number.  The direction of each
16    /// leg is implied by `strategy` together with the order `side`, not by the
17    /// sign of this value; a negative or zero ratio is rejected by the server
18    /// with `602001`.
19    ratio_quantity: Decimal,
20}
21
22impl SubmitMultiLegOrderLeg {
23    /// Create a new `SubmitMultiLegOrderLeg`
24    #[inline]
25    pub fn new(symbol: impl Into<String>, ratio_quantity: Decimal) -> Self {
26        Self {
27            symbol: normalize_symbol_owned(symbol),
28            ratio_quantity,
29        }
30    }
31}
32
33/// Options for submit multi-leg order request
34#[derive(Debug, Serialize, Clone)]
35pub struct SubmitMultiLegOrderOptions {
36    side: OrderSide,
37    order_type: OrderType,
38    submitted_quantity: Decimal,
39    strategy: MultiLegStrategy,
40    legs: Vec<SubmitMultiLegOrderLeg>,
41    #[serde(skip_serializing_if = "Option::is_none")]
42    submitted_price: Option<Decimal>,
43    #[serde(skip_serializing_if = "Option::is_none")]
44    remark: Option<String>,
45    #[serde(skip_serializing_if = "Option::is_none")]
46    client_request_id: Option<String>,
47    #[serde(skip_serializing_if = "Option::is_none")]
48    delayed_until: Option<i64>,
49}
50
51impl SubmitMultiLegOrderOptions {
52    /// Create a new `SubmitMultiLegOrderOptions`
53    #[inline]
54    pub fn new(
55        side: OrderSide,
56        order_type: OrderType,
57        submitted_quantity: Decimal,
58        strategy: MultiLegStrategy,
59        legs: impl IntoIterator<Item = SubmitMultiLegOrderLeg>,
60    ) -> Self {
61        Self {
62            side,
63            order_type,
64            submitted_quantity,
65            strategy,
66            legs: legs.into_iter().collect(),
67            submitted_price: None,
68            remark: None,
69            client_request_id: None,
70            delayed_until: None,
71        }
72    }
73
74    /// Set the submitted price
75    ///
76    /// Required for limit order types such as `LO`.
77    #[inline]
78    #[must_use]
79    pub fn submitted_price(self, submitted_price: Decimal) -> Self {
80        Self {
81            submitted_price: Some(submitted_price),
82            ..self
83        }
84    }
85
86    /// Set the remark
87    #[inline]
88    #[must_use]
89    pub fn remark(self, remark: impl Into<String>) -> Self {
90        Self {
91            remark: Some(remark.into()),
92            ..self
93        }
94    }
95
96    /// Set the client request ID for idempotency control.
97    /// If not specified, idempotency control is skipped.
98    /// The server caches this ID for 10 minutes; requests with the same ID
99    /// within that period return the original response without creating a new
100    /// order.
101    #[inline]
102    #[must_use]
103    pub fn client_request_id(self, id: impl Into<String>) -> Self {
104        Self {
105            client_request_id: Some(id.into()),
106            ..self
107        }
108    }
109
110    /// Set the time-condition submit time.
111    ///
112    /// The order is held in `DelayedNotReported` status and only submitted at
113    /// this time, which must be later than now and no more than 90 days ahead.
114    /// Only `LO` / `MO` support time conditions.
115    #[inline]
116    #[must_use]
117    pub fn delayed_until(self, delayed_until: OffsetDateTime) -> Self {
118        Self {
119            delayed_until: Some(super::delayed_until_timestamp(delayed_until)),
120            ..self
121        }
122    }
123}
124
125#[cfg(test)]
126mod tests {
127    use time::macros::datetime;
128
129    use super::*;
130
131    #[test]
132    fn delayed_until() {
133        let opts = SubmitMultiLegOrderOptions::new(
134            OrderSide::Buy,
135            OrderType::MO,
136            Decimal::from(1),
137            MultiLegStrategy::Straddle,
138            [],
139        );
140        assert!(
141            serde_json::to_value(&opts)
142                .unwrap()
143                .get("delayed_until")
144                .is_none()
145        );
146
147        let opts = opts.delayed_until(datetime!(2026-01-01 00:00 UTC));
148        assert_eq!(
149            serde_json::to_value(&opts).unwrap()["delayed_until"],
150            serde_json::json!(1767225600)
151        );
152    }
153}