nautilus_model/python/events/order/
filled.rs1use indexmap::IndexMap;
17use nautilus_core::{
18 UUID4,
19 python::{IntoPyObjectNautilusExt, serialization::from_dict_pyo3},
20};
21use pyo3::{basic::CompareOp, prelude::*, types::PyDict};
22
23use crate::{
24 enums::{LiquiditySide, OrderSide, OrderType},
25 events::OrderFilled,
26 identifiers::{
27 AccountId, ClientOrderId, InstrumentId, PositionId, StrategyId, TradeId, TraderId,
28 VenueOrderId,
29 },
30 orders::str_indexmap_to_ustr,
31 types::{Currency, Money, Price, Quantity},
32};
33
34#[pymethods]
35#[pyo3_stub_gen::derive::gen_stub_pymethods]
36impl OrderFilled {
37 #[expect(clippy::too_many_arguments)]
39 #[new]
40 #[pyo3(signature = (trader_id, strategy_id, instrument_id, client_order_id, venue_order_id, account_id, trade_id, order_side, order_type, last_qty, last_px, currency, liquidity_side, event_id, ts_event, ts_init, reconciliation, position_id=None, commission=None, info=None))]
41 fn py_new(
42 trader_id: TraderId,
43 strategy_id: StrategyId,
44 instrument_id: InstrumentId,
45 client_order_id: ClientOrderId,
46 venue_order_id: VenueOrderId,
47 account_id: AccountId,
48 trade_id: TradeId,
49 order_side: OrderSide,
50 order_type: OrderType,
51 last_qty: Quantity,
52 last_px: Price,
53 currency: Currency,
54 liquidity_side: LiquiditySide,
55 event_id: UUID4,
56 ts_event: u64,
57 ts_init: u64,
58 reconciliation: bool,
59 position_id: Option<PositionId>,
60 commission: Option<Money>,
61 info: Option<IndexMap<String, String>>,
62 ) -> Self {
63 Self::new(
64 trader_id,
65 strategy_id,
66 instrument_id,
67 client_order_id,
68 venue_order_id,
69 account_id,
70 trade_id,
71 order_side,
72 order_type,
73 last_qty,
74 last_px,
75 currency,
76 liquidity_side,
77 event_id,
78 ts_event.into(),
79 ts_init.into(),
80 reconciliation,
81 position_id,
82 commission,
83 info.map(str_indexmap_to_ustr),
84 )
85 }
86
87 fn __richcmp__(&self, other: &Self, op: CompareOp, py: Python<'_>) -> Py<PyAny> {
88 match op {
89 CompareOp::Eq => self.eq(other).into_py_any_unwrap(py),
90 CompareOp::Ne => self.ne(other).into_py_any_unwrap(py),
91 _ => py.NotImplemented(),
92 }
93 }
94
95 fn __repr__(&self) -> String {
96 format!("{self:?}")
97 }
98
99 fn __str__(&self) -> String {
100 self.to_string()
101 }
102
103 #[getter]
104 #[pyo3(name = "is_buy")]
105 fn py_is_buy(&self) -> bool {
106 self.is_buy()
107 }
108
109 #[getter]
110 #[pyo3(name = "is_sell")]
111 fn py_is_sell(&self) -> bool {
112 self.is_sell()
113 }
114
115 #[getter]
116 #[pyo3(name = "trader_id")]
117 fn py_trader_id(&self) -> TraderId {
118 self.trader_id
119 }
120
121 #[getter]
122 #[pyo3(name = "instrument_id")]
123 fn py_instrument_id(&self) -> InstrumentId {
124 self.instrument_id
125 }
126
127 #[getter]
128 #[pyo3(name = "strategy_id")]
129 fn py_strategy_id(&self) -> StrategyId {
130 self.strategy_id
131 }
132
133 #[getter]
134 #[pyo3(name = "client_order_id")]
135 fn py_client_order_id(&self) -> ClientOrderId {
136 self.client_order_id
137 }
138
139 #[getter]
140 #[pyo3(name = "venue_order_id")]
141 fn py_venue_order_id(&self) -> VenueOrderId {
142 self.venue_order_id
143 }
144
145 #[getter]
146 #[pyo3(name = "account_id")]
147 fn py_account_id(&self) -> AccountId {
148 self.account_id
149 }
150
151 #[getter]
152 #[pyo3(name = "trade_id")]
153 fn py_trade_id(&self) -> TradeId {
154 self.trade_id
155 }
156
157 #[getter]
158 #[pyo3(name = "order_side")]
159 fn py_order_side(&self) -> OrderSide {
160 self.order_side
161 }
162
163 #[getter]
164 #[pyo3(name = "last_qty")]
165 fn py_last_qty(&self) -> Quantity {
166 self.last_qty
167 }
168
169 #[getter]
170 #[pyo3(name = "last_px")]
171 fn py_last_px(&self) -> Price {
172 self.last_px
173 }
174
175 #[getter]
176 #[pyo3(name = "currency")]
177 fn py_currency(&self) -> Currency {
178 self.currency
179 }
180
181 #[getter]
182 #[pyo3(name = "liquidity_side")]
183 fn py_liquidity_side(&self) -> LiquiditySide {
184 self.liquidity_side
185 }
186
187 #[getter]
188 #[pyo3(name = "event_id")]
189 fn py_event_id(&self) -> UUID4 {
190 self.event_id
191 }
192
193 #[getter]
194 #[pyo3(name = "ts_event")]
195 fn py_ts_event(&self) -> u64 {
196 self.ts_event.as_u64()
197 }
198
199 #[getter]
200 #[pyo3(name = "ts_init")]
201 fn py_ts_init(&self) -> u64 {
202 self.ts_init.as_u64()
203 }
204
205 #[getter]
206 #[pyo3(name = "reconciliation")]
207 fn py_reconciliation(&self) -> bool {
208 self.reconciliation
209 }
210
211 #[getter]
212 #[pyo3(name = "position_id")]
213 fn py_position_id(&self) -> Option<PositionId> {
214 self.position_id
215 }
216
217 #[getter]
218 #[pyo3(name = "commission")]
219 fn py_commission(&self) -> Option<Money> {
220 self.commission
221 }
222
223 #[getter]
224 #[pyo3(name = "info")]
225 fn py_info(&self) -> Option<IndexMap<&str, &str>> {
226 self.info
227 .as_ref()
228 .map(|x| x.iter().map(|(k, v)| (k.as_str(), v.as_str())).collect())
229 }
230
231 #[getter]
232 #[pyo3(name = "order_type")]
233 fn py_order_type(&self) -> OrderType {
234 self.order_type
235 }
236
237 #[staticmethod]
243 #[pyo3(name = "from_dict")]
244 fn py_from_dict(py: Python<'_>, values: Py<PyDict>) -> PyResult<Self> {
245 from_dict_pyo3(py, values)
246 }
247
248 #[pyo3(name = "to_dict")]
254 pub fn py_to_dict(&self, py: Python<'_>) -> PyResult<Py<PyAny>> {
255 let dict = PyDict::new(py);
256 dict.set_item("type", stringify!(OrderFilled))?;
257 dict.set_item("trader_id", self.trader_id.to_string())?;
258 dict.set_item("strategy_id", self.strategy_id.to_string())?;
259 dict.set_item("instrument_id", self.instrument_id.to_string())?;
260 dict.set_item("client_order_id", self.client_order_id.to_string())?;
261 dict.set_item("venue_order_id", self.venue_order_id.to_string())?;
262 dict.set_item("account_id", self.account_id.to_string())?;
263 dict.set_item("trade_id", self.trade_id.to_string())?;
264 dict.set_item("order_side", self.order_side.to_string())?;
265 dict.set_item("order_type", self.order_type.to_string())?;
266 dict.set_item("last_qty", self.last_qty.to_string())?;
267 dict.set_item("last_px", self.last_px.to_string())?;
268 dict.set_item("currency", self.currency.code.to_string())?;
269 dict.set_item("liquidity_side", self.liquidity_side.to_string())?;
270 dict.set_item("event_id", self.event_id.to_string())?;
271 dict.set_item("ts_event", self.ts_event.as_u64())?;
272 dict.set_item("ts_init", self.ts_init.as_u64())?;
273 dict.set_item("reconciliation", self.reconciliation)?;
274
275 match &self.info {
276 Some(info) => {
277 let py_info = PyDict::new(py);
278 for (key, value) in info {
279 py_info.set_item(key.to_string(), value.to_string())?;
280 }
281 dict.set_item("info", py_info)?;
282 }
283 None => dict.set_item("info", py.None())?,
284 }
285
286 match self.position_id {
287 Some(position_id) => dict.set_item("position_id", position_id.to_string())?,
288 None => dict.set_item("position_id", py.None())?,
289 }
290
291 match self.commission {
292 Some(commission) => dict.set_item("commission", commission.to_string())?,
293 None => dict.set_item("commission", py.None())?,
294 }
295
296 match self.causation_id {
297 Some(causation_id) => dict.set_item("causation_id", causation_id.to_string())?,
298 None => dict.set_item("causation_id", py.None())?,
299 }
300 Ok(dict.into())
301 }
302}