longport/trade/requests/
submit_multileg.rs1use 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#[derive(Debug, Serialize, Clone)]
12pub struct SubmitMultiLegOrderLeg {
13 symbol: String,
15 ratio_quantity: Decimal,
20}
21
22impl SubmitMultiLegOrderLeg {
23 #[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#[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 #[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 #[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 #[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 #[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 #[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}