longport/trade/requests/
submit_order.rs

1use rust_decimal::Decimal;
2use serde::Serialize;
3use time::Date;
4
5use crate::{
6    serde_utils,
7    trade::{OrderSide, OrderType, OutsideRTH, TimeInForceType},
8};
9
10/// Options for submit order request
11#[derive(Debug, Serialize, Clone)]
12pub struct SubmitOrderOptions {
13    symbol: String,
14    order_type: OrderType,
15    side: OrderSide,
16    submitted_quantity: Decimal,
17    time_in_force: TimeInForceType,
18    #[serde(skip_serializing_if = "Option::is_none")]
19    submitted_price: Option<Decimal>,
20    #[serde(skip_serializing_if = "Option::is_none")]
21    trigger_price: Option<Decimal>,
22    #[serde(skip_serializing_if = "Option::is_none")]
23    limit_offset: Option<Decimal>,
24    #[serde(skip_serializing_if = "Option::is_none")]
25    trailing_amount: Option<Decimal>,
26    #[serde(skip_serializing_if = "Option::is_none")]
27    trailing_percent: Option<Decimal>,
28    #[serde(with = "serde_utils::date_opt")]
29    expire_date: Option<Date>,
30    #[serde(skip_serializing_if = "Option::is_none")]
31    outside_rth: Option<OutsideRTH>,
32    #[serde(skip_serializing_if = "Option::is_none")]
33    remark: Option<String>,
34}
35
36impl SubmitOrderOptions {
37    /// Create a new `SubmitOrderOptions`
38    #[inline]
39    pub fn new(
40        symbol: impl Into<String>,
41        order_type: OrderType,
42        side: OrderSide,
43        submitted_quantity: Decimal,
44        time_in_force: TimeInForceType,
45    ) -> Self {
46        Self {
47            symbol: symbol.into(),
48            order_type,
49            side,
50            submitted_quantity,
51            time_in_force,
52            submitted_price: None,
53            trigger_price: None,
54            limit_offset: None,
55            trailing_amount: None,
56            trailing_percent: None,
57            expire_date: None,
58            outside_rth: None,
59            remark: None,
60        }
61    }
62
63    /// Set the submitted price
64    #[inline]
65    #[must_use]
66    pub fn submitted_price(self, submitted_price: Decimal) -> Self {
67        Self {
68            submitted_price: Some(submitted_price),
69            ..self
70        }
71    }
72
73    /// Set the trigger price
74    #[inline]
75    #[must_use]
76    pub fn trigger_price(self, trigger_price: Decimal) -> Self {
77        Self {
78            trigger_price: Some(trigger_price),
79            ..self
80        }
81    }
82
83    /// Set the limit offset
84    #[inline]
85    #[must_use]
86    pub fn limit_offset(self, limit_offset: Decimal) -> Self {
87        Self {
88            limit_offset: Some(limit_offset),
89            ..self
90        }
91    }
92
93    /// Set the trailing amount
94    #[inline]
95    #[must_use]
96    pub fn trailing_amount(self, trailing_amount: Decimal) -> Self {
97        Self {
98            trailing_amount: Some(trailing_amount),
99            ..self
100        }
101    }
102
103    /// Set the trailing percent
104    #[inline]
105    #[must_use]
106    pub fn trailing_percent(self, trailing_percent: Decimal) -> Self {
107        Self {
108            trailing_percent: Some(trailing_percent),
109            ..self
110        }
111    }
112
113    /// Set the expire date
114    #[inline]
115    #[must_use]
116    pub fn expire_date(self, expire_date: Date) -> Self {
117        Self {
118            expire_date: Some(expire_date),
119            ..self
120        }
121    }
122
123    /// Enable or disable outside regular trading hours
124    #[inline]
125    #[must_use]
126    pub fn outside_rth(self, outside_rth: OutsideRTH) -> Self {
127        Self {
128            outside_rth: Some(outside_rth),
129            ..self
130        }
131    }
132
133    /// Set the remark
134    #[inline]
135    #[must_use]
136    pub fn remark(self, remark: impl Into<String>) -> Self {
137        Self {
138            remark: Some(remark.into()),
139            ..self
140        }
141    }
142}