Skip to main content

longbridge/trade/requests/
replace_order.rs

1use rust_decimal::Decimal;
2use serde::Serialize;
3
4use crate::trade::{AttachedOrderType, OrderType, OutsideRTH, TimeInForceType};
5
6/// Options for replace order request
7#[derive(Debug, Serialize, Clone)]
8pub struct ReplaceOrderOptions {
9    order_id: String,
10    quantity: Decimal,
11    #[serde(skip_serializing_if = "Option::is_none")]
12    price: Option<Decimal>,
13    #[serde(skip_serializing_if = "Option::is_none")]
14    trigger_price: Option<Decimal>,
15    #[serde(skip_serializing_if = "Option::is_none")]
16    limit_offset: Option<Decimal>,
17    #[serde(skip_serializing_if = "Option::is_none")]
18    trailing_amount: Option<Decimal>,
19    #[serde(skip_serializing_if = "Option::is_none")]
20    trailing_percent: Option<Decimal>,
21    #[serde(skip_serializing_if = "Option::is_none")]
22    limit_depth_level: Option<i32>,
23    #[serde(skip_serializing_if = "Option::is_none")]
24    trigger_count: Option<i32>,
25    #[serde(skip_serializing_if = "Option::is_none")]
26    monitor_price: Option<Decimal>,
27    #[serde(skip_serializing_if = "Option::is_none")]
28    remark: Option<String>,
29    #[serde(skip_serializing_if = "Option::is_none")]
30    attached_params: Option<ReplaceAttachedParams>,
31}
32
33impl ReplaceOrderOptions {
34    /// Create a new `ReplaceOrderOptions`
35    #[inline]
36    pub fn new(order_id: impl Into<String>, quantity: Decimal) -> Self {
37        Self {
38            order_id: order_id.into(),
39            quantity,
40            price: None,
41            trigger_price: None,
42            limit_offset: None,
43            trailing_amount: None,
44            trailing_percent: None,
45            limit_depth_level: None,
46            trigger_count: None,
47            monitor_price: None,
48            remark: None,
49            attached_params: None,
50        }
51    }
52
53    /// Set the price
54    #[inline]
55    #[must_use]
56    pub fn price(self, price: Decimal) -> Self {
57        Self {
58            price: Some(price),
59            ..self
60        }
61    }
62
63    /// Set the trigger price
64    #[inline]
65    #[must_use]
66    pub fn trigger_price(self, trigger_price: Decimal) -> Self {
67        Self {
68            trigger_price: Some(trigger_price),
69            ..self
70        }
71    }
72
73    /// Set the limit offset
74    #[inline]
75    #[must_use]
76    pub fn limit_offset(self, limit_offset: Decimal) -> Self {
77        Self {
78            limit_offset: Some(limit_offset),
79            ..self
80        }
81    }
82
83    /// Set the trailing amount
84    #[inline]
85    #[must_use]
86    pub fn trailing_amount(self, trailing_amount: Decimal) -> Self {
87        Self {
88            trailing_amount: Some(trailing_amount),
89            ..self
90        }
91    }
92
93    /// Set the trailing percent
94    #[inline]
95    #[must_use]
96    pub fn trailing_percent(self, trailing_percent: Decimal) -> Self {
97        Self {
98            trailing_percent: Some(trailing_percent),
99            ..self
100        }
101    }
102
103    /// Set the limit depth level
104    #[inline]
105    #[must_use]
106    pub fn limit_depth_level(self, limit_depth_level: i32) -> Self {
107        Self {
108            limit_depth_level: Some(limit_depth_level),
109            ..self
110        }
111    }
112
113    /// Set the trigger count
114    #[inline]
115    #[must_use]
116    pub fn trigger_count(self, trigger_count: i32) -> Self {
117        Self {
118            trigger_count: Some(trigger_count),
119            ..self
120        }
121    }
122
123    /// Set the monitor price
124    #[inline]
125    #[must_use]
126    pub fn monitor_price(self, monitor_price: Decimal) -> Self {
127        Self {
128            monitor_price: Some(monitor_price),
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
143    /// Set attached order parameters
144    pub fn attached_params(self, params: ReplaceAttachedParams) -> Self {
145        Self {
146            attached_params: Some(params),
147            ..self
148        }
149    }
150}
151
152/// Attached order parameters for replace order
153#[derive(Debug, Serialize, Clone)]
154pub struct ReplaceAttachedParams {
155    attached_order_type: AttachedOrderType,
156    #[serde(skip_serializing_if = "Option::is_none")]
157    profit_taker_price: Option<Decimal>,
158    #[serde(skip_serializing_if = "Option::is_none")]
159    stop_loss_price: Option<Decimal>,
160    #[serde(skip_serializing_if = "Option::is_none")]
161    time_in_force: Option<TimeInForceType>,
162    #[serde(skip_serializing_if = "Option::is_none")]
163    expire_time: Option<i64>,
164    #[serde(skip_serializing_if = "Option::is_none")]
165    activate_order_type: Option<OrderType>,
166    #[serde(skip_serializing_if = "Option::is_none")]
167    profit_taker_submit_price: Option<Decimal>,
168    #[serde(skip_serializing_if = "Option::is_none")]
169    stop_loss_submit_price: Option<Decimal>,
170    #[serde(skip_serializing_if = "Option::is_none")]
171    activate_rth: Option<OutsideRTH>,
172    #[serde(skip_serializing_if = "Option::is_none")]
173    profit_taker_id: Option<i64>,
174    #[serde(skip_serializing_if = "Option::is_none")]
175    stop_loss_id: Option<i64>,
176    #[serde(skip_serializing_if = "Option::is_none")]
177    cancel_all_attached: Option<bool>,
178    #[serde(skip_serializing_if = "Option::is_none")]
179    main_id: Option<i64>,
180    #[serde(skip_serializing_if = "Option::is_none")]
181    quantity: Option<Decimal>,
182    #[serde(skip_serializing_if = "Option::is_none")]
183    market_price: Option<Decimal>,
184}
185
186impl ReplaceAttachedParams {
187    /// Create new ReplaceAttachedParams
188    pub fn new(attached_order_type: AttachedOrderType) -> Self {
189        Self {
190            attached_order_type,
191            profit_taker_price: None,
192            stop_loss_price: None,
193            time_in_force: None,
194            expire_time: None,
195            activate_order_type: None,
196            profit_taker_submit_price: None,
197            stop_loss_submit_price: None,
198            activate_rth: None,
199            profit_taker_id: None,
200            stop_loss_id: None,
201            cancel_all_attached: None,
202            main_id: None,
203            quantity: None,
204            market_price: None,
205        }
206    }
207    /// Set the take-profit trigger price
208    pub fn profit_taker_price(self, v: Decimal) -> Self {
209        Self {
210            profit_taker_price: Some(v),
211            ..self
212        }
213    }
214    /// Set the stop-loss trigger price
215    pub fn stop_loss_price(self, v: Decimal) -> Self {
216        Self {
217            stop_loss_price: Some(v),
218            ..self
219        }
220    }
221    /// Set the time in force type
222    pub fn time_in_force(self, v: TimeInForceType) -> Self {
223        Self {
224            time_in_force: Some(v),
225            ..self
226        }
227    }
228    /// Set the expiry time (unix timestamp seconds)
229    pub fn expire_time(self, v: i64) -> Self {
230        Self {
231            expire_time: Some(v),
232            ..self
233        }
234    }
235    /// Set the order type to submit after trigger
236    pub fn activate_order_type(self, v: OrderType) -> Self {
237        Self {
238            activate_order_type: Some(v),
239            ..self
240        }
241    }
242    /// Set the take-profit limit price
243    pub fn profit_taker_submit_price(self, v: Decimal) -> Self {
244        Self {
245            profit_taker_submit_price: Some(v),
246            ..self
247        }
248    }
249    /// Set the stop-loss limit price
250    pub fn stop_loss_submit_price(self, v: Decimal) -> Self {
251        Self {
252            stop_loss_submit_price: Some(v),
253            ..self
254        }
255    }
256    /// Set the RTH setting for the activated order
257    pub fn activate_rth(self, v: OutsideRTH) -> Self {
258        Self {
259            activate_rth: Some(v),
260            ..self
261        }
262    }
263    /// Set the take-profit order ID (for modifying existing attached order)
264    pub fn profit_taker_id(self, v: i64) -> Self {
265        Self {
266            profit_taker_id: Some(v),
267            ..self
268        }
269    }
270    /// Set the stop-loss order ID (for modifying existing attached order)
271    pub fn stop_loss_id(self, v: i64) -> Self {
272        Self {
273            stop_loss_id: Some(v),
274            ..self
275        }
276    }
277    /// Cancel all attached orders
278    pub fn cancel_all_attached(self) -> Self {
279        Self {
280            cancel_all_attached: Some(true),
281            ..self
282        }
283    }
284    /// Set the main order ID
285    pub fn main_id(self, v: i64) -> Self {
286        Self {
287            main_id: Some(v),
288            ..self
289        }
290    }
291    /// Set the quantity
292    pub fn quantity(self, v: Decimal) -> Self {
293        Self {
294            quantity: Some(v),
295            ..self
296        }
297    }
298    /// Set the market price
299    pub fn market_price(self, v: Decimal) -> Self {
300        Self {
301            market_price: Some(v),
302            ..self
303        }
304    }
305}