1use rust_decimal::Decimal;
2use serde::Serialize;
3use time::{Date, OffsetDateTime};
4
5use crate::{
6 serde_utils,
7 trade::{AttachedOrderType, OrderSide, OrderType, OutsideRTH, TimeInForceType},
8 types::normalize_symbol_owned,
9};
10
11#[derive(Debug, Serialize, Clone)]
13pub struct SubmitOrderOptions {
14 symbol: String,
15 order_type: OrderType,
16 side: OrderSide,
17 submitted_quantity: Decimal,
18 time_in_force: TimeInForceType,
19 #[serde(skip_serializing_if = "Option::is_none")]
20 submitted_price: Option<Decimal>,
21 #[serde(skip_serializing_if = "Option::is_none")]
22 trigger_price: Option<Decimal>,
23 #[serde(skip_serializing_if = "Option::is_none")]
24 limit_offset: Option<Decimal>,
25 #[serde(skip_serializing_if = "Option::is_none")]
26 trailing_amount: Option<Decimal>,
27 #[serde(skip_serializing_if = "Option::is_none")]
28 trailing_percent: Option<Decimal>,
29 #[serde(
30 with = "serde_utils::date_opt",
31 skip_serializing_if = "Option::is_none"
32 )]
33 expire_date: Option<Date>,
34 #[serde(skip_serializing_if = "Option::is_none")]
35 outside_rth: Option<OutsideRTH>,
36 #[serde(skip_serializing_if = "Option::is_none")]
37 limit_depth_level: Option<i32>,
38 #[serde(skip_serializing_if = "Option::is_none")]
39 trigger_count: Option<i32>,
40 #[serde(skip_serializing_if = "Option::is_none")]
41 monitor_price: Option<Decimal>,
42 #[serde(skip_serializing_if = "Option::is_none")]
43 remark: Option<String>,
44 #[serde(skip_serializing_if = "Option::is_none")]
45 client_request_id: Option<String>,
46 #[serde(skip_serializing_if = "Option::is_none")]
47 delayed_until: Option<i64>,
48 #[serde(skip_serializing_if = "Option::is_none")]
49 attached_params: Option<SubmitAttachedParams>,
50}
51
52impl SubmitOrderOptions {
53 #[inline]
55 pub fn new(
56 symbol: impl Into<String>,
57 order_type: OrderType,
58 side: OrderSide,
59 submitted_quantity: Decimal,
60 time_in_force: TimeInForceType,
61 ) -> Self {
62 Self {
63 symbol: normalize_symbol_owned(symbol),
64 order_type,
65 side,
66 submitted_quantity,
67 time_in_force,
68 submitted_price: None,
69 trigger_price: None,
70 limit_offset: None,
71 trailing_amount: None,
72 trailing_percent: None,
73 expire_date: None,
74 outside_rth: None,
75 limit_depth_level: None,
76 trigger_count: None,
77 monitor_price: None,
78 remark: None,
79 client_request_id: None,
80 delayed_until: None,
81 attached_params: None,
82 }
83 }
84
85 #[inline]
87 #[must_use]
88 pub fn submitted_price(self, submitted_price: Decimal) -> Self {
89 Self {
90 submitted_price: Some(submitted_price),
91 ..self
92 }
93 }
94
95 #[inline]
97 #[must_use]
98 pub fn trigger_price(self, trigger_price: Decimal) -> Self {
99 Self {
100 trigger_price: Some(trigger_price),
101 ..self
102 }
103 }
104
105 #[inline]
107 #[must_use]
108 pub fn limit_offset(self, limit_offset: Decimal) -> Self {
109 Self {
110 limit_offset: Some(limit_offset),
111 ..self
112 }
113 }
114
115 #[inline]
117 #[must_use]
118 pub fn trailing_amount(self, trailing_amount: Decimal) -> Self {
119 Self {
120 trailing_amount: Some(trailing_amount),
121 ..self
122 }
123 }
124
125 #[inline]
127 #[must_use]
128 pub fn trailing_percent(self, trailing_percent: Decimal) -> Self {
129 Self {
130 trailing_percent: Some(trailing_percent),
131 ..self
132 }
133 }
134
135 #[inline]
137 #[must_use]
138 pub fn expire_date(self, expire_date: Date) -> Self {
139 Self {
140 expire_date: Some(expire_date),
141 ..self
142 }
143 }
144
145 #[inline]
147 #[must_use]
148 pub fn outside_rth(self, outside_rth: OutsideRTH) -> Self {
149 Self {
150 outside_rth: Some(outside_rth),
151 ..self
152 }
153 }
154
155 pub fn limit_depth_level(self, level: i32) -> Self {
157 Self {
158 limit_depth_level: Some(level),
159 ..self
160 }
161 }
162
163 pub fn trigger_count(self, count: i32) -> Self {
165 Self {
166 trigger_count: Some(count),
167 ..self
168 }
169 }
170
171 pub fn monitor_price(self, price: Decimal) -> Self {
173 Self {
174 monitor_price: Some(price),
175 ..self
176 }
177 }
178
179 #[inline]
181 #[must_use]
182 pub fn remark(self, remark: impl Into<String>) -> Self {
183 Self {
184 remark: Some(remark.into()),
185 ..self
186 }
187 }
188
189 pub fn client_request_id(self, id: impl Into<String>) -> Self {
195 Self {
196 client_request_id: Some(id.into()),
197 ..self
198 }
199 }
200
201 #[inline]
206 #[must_use]
207 pub fn delayed_until(self, delayed_until: OffsetDateTime) -> Self {
208 Self {
209 delayed_until: Some(super::delayed_until_timestamp(delayed_until)),
210 ..self
211 }
212 }
213
214 pub fn attached_params(self, params: SubmitAttachedParams) -> Self {
216 Self {
217 attached_params: Some(params),
218 ..self
219 }
220 }
221}
222
223#[derive(Debug, Serialize, Clone)]
225pub struct SubmitAttachedParams {
226 attached_order_type: AttachedOrderType,
227 #[serde(skip_serializing_if = "Option::is_none")]
228 profit_taker_price: Option<Decimal>,
229 #[serde(skip_serializing_if = "Option::is_none")]
230 stop_loss_price: Option<Decimal>,
231 #[serde(skip_serializing_if = "Option::is_none")]
232 time_in_force: Option<TimeInForceType>,
233 #[serde(skip_serializing_if = "Option::is_none")]
234 expire_time: Option<i64>,
235 #[serde(skip_serializing_if = "Option::is_none")]
236 activate_order_type: Option<OrderType>,
237 #[serde(skip_serializing_if = "Option::is_none")]
238 profit_taker_submit_price: Option<Decimal>,
239 #[serde(skip_serializing_if = "Option::is_none")]
240 stop_loss_submit_price: Option<Decimal>,
241 #[serde(skip_serializing_if = "Option::is_none")]
242 activate_rth: Option<OutsideRTH>,
243 #[serde(skip_serializing_if = "Option::is_none")]
244 profit_taker_depth_level: Option<i32>,
245 #[serde(skip_serializing_if = "Option::is_none")]
246 stop_loss_depth_level: Option<i32>,
247}
248
249impl SubmitAttachedParams {
250 pub fn new(attached_order_type: AttachedOrderType) -> Self {
252 Self {
253 attached_order_type,
254 profit_taker_price: None,
255 stop_loss_price: None,
256 time_in_force: None,
257 expire_time: None,
258 activate_order_type: None,
259 profit_taker_submit_price: None,
260 stop_loss_submit_price: None,
261 activate_rth: None,
262 profit_taker_depth_level: None,
263 stop_loss_depth_level: None,
264 }
265 }
266 pub fn profit_taker_price(self, v: Decimal) -> Self {
268 Self {
269 profit_taker_price: Some(v),
270 ..self
271 }
272 }
273 pub fn stop_loss_price(self, v: Decimal) -> Self {
275 Self {
276 stop_loss_price: Some(v),
277 ..self
278 }
279 }
280 pub fn time_in_force(self, v: TimeInForceType) -> Self {
282 Self {
283 time_in_force: Some(v),
284 ..self
285 }
286 }
287 pub fn expire_time(self, v: i64) -> Self {
289 Self {
290 expire_time: Some(v),
291 ..self
292 }
293 }
294 pub fn activate_order_type(self, v: OrderType) -> Self {
296 Self {
297 activate_order_type: Some(v),
298 ..self
299 }
300 }
301 pub fn profit_taker_submit_price(self, v: Decimal) -> Self {
303 Self {
304 profit_taker_submit_price: Some(v),
305 ..self
306 }
307 }
308 pub fn stop_loss_submit_price(self, v: Decimal) -> Self {
310 Self {
311 stop_loss_submit_price: Some(v),
312 ..self
313 }
314 }
315 pub fn activate_rth(self, v: OutsideRTH) -> Self {
317 Self {
318 activate_rth: Some(v),
319 ..self
320 }
321 }
322
323 pub fn profit_taker_depth_level(self, v: i32) -> Self {
326 Self {
327 profit_taker_depth_level: Some(v),
328 ..self
329 }
330 }
331
332 pub fn stop_loss_depth_level(self, v: i32) -> Self {
335 Self {
336 stop_loss_depth_level: Some(v),
337 ..self
338 }
339 }
340}
341
342#[cfg(test)]
343mod tests {
344 use time::macros::datetime;
345
346 use super::*;
347
348 fn options() -> SubmitOrderOptions {
349 SubmitOrderOptions::new(
350 "700.HK",
351 OrderType::LO,
352 OrderSide::Buy,
353 Decimal::from(100),
354 TimeInForceType::Day,
355 )
356 }
357
358 #[test]
359 fn delayed_until() {
360 assert!(
361 serde_json::to_value(options())
362 .unwrap()
363 .get("delayed_until")
364 .is_none()
365 );
366
367 let opts = options().delayed_until(datetime!(2026-01-01 00:00 UTC));
368 assert_eq!(
369 serde_json::to_value(&opts).unwrap()["delayed_until"],
370 serde_json::json!(1767225600)
371 );
372
373 let opts = options().delayed_until(OffsetDateTime::UNIX_EPOCH);
374 assert_eq!(
375 serde_json::to_value(&opts).unwrap()["delayed_until"],
376 serde_json::json!(2),
377 "a time that maps to 0 must not be sent as a regular order"
378 );
379 }
380
381 #[test]
382 fn attached_params_depth_level() {
383 let params = SubmitAttachedParams::new(AttachedOrderType::Bracket);
384 let value = serde_json::to_value(¶ms).unwrap();
385 assert!(value.get("profit_taker_depth_level").is_none());
386 assert!(value.get("stop_loss_depth_level").is_none());
387
388 let params = params.profit_taker_depth_level(2).stop_loss_depth_level(5);
389 let value = serde_json::to_value(¶ms).unwrap();
390 assert_eq!(value["profit_taker_depth_level"], serde_json::json!(2));
391 assert_eq!(value["stop_loss_depth_level"], serde_json::json!(5));
392 }
393}