nautilus_model/ffi/events/order.rs
1// -------------------------------------------------------------------------------------------------
2// Copyright (C) 2015-2026 Nautech Systems Pty Ltd. All rights reserved.
3// https://nautechsystems.io
4//
5// Licensed under the GNU Lesser General Public License Version 3.0 (the "License");
6// You may not use this file except in compliance with the License.
7// You may obtain a copy of the License at https://www.gnu.org/licenses/lgpl-3.0.en.html
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14// -------------------------------------------------------------------------------------------------
15
16use std::ffi::c_char;
17
18use nautilus_core::{UUID4, UnixNanos, ffi::string::cstr_to_ustr};
19use ustr::Ustr;
20
21use crate::{
22 identifiers::{AccountId, ClientOrderId, InstrumentId, StrategyId, TraderId, VenueOrderId},
23 types::Price,
24};
25
26/// Represents an event where an order has been denied by the Nautilus system.
27///
28/// This could be due an unsupported feature, a risk limit exceedance, or for
29/// any other reason that an otherwise valid order is not able to be submitted.
30#[repr(C)]
31#[derive(Clone, Copy, Debug)]
32pub struct OrderDeniedFfi {
33 /// The trader ID associated with the event.
34 pub trader_id: TraderId,
35 /// The strategy ID associated with the event.
36 pub strategy_id: StrategyId,
37 /// The instrument ID associated with the event.
38 pub instrument_id: InstrumentId,
39 /// The client order ID associated with the event.
40 pub client_order_id: ClientOrderId,
41 /// The reason the order was denied.
42 pub reason: Ustr,
43 /// The unique identifier for the event.
44 pub event_id: UUID4,
45 /// UNIX timestamp (nanoseconds) when the event occurred.
46 pub ts_event: UnixNanos,
47 /// UNIX timestamp (nanoseconds) when the event was initialized.
48 pub ts_init: UnixNanos,
49}
50
51/// Represents an event where an order has become emulated by the Nautilus system.
52#[repr(C)]
53#[derive(Clone, Copy, Debug)]
54pub struct OrderEmulatedFfi {
55 /// The trader ID associated with the event.
56 pub trader_id: TraderId,
57 /// The strategy ID associated with the event.
58 pub strategy_id: StrategyId,
59 /// The instrument ID associated with the event.
60 pub instrument_id: InstrumentId,
61 /// The client order ID associated with the event.
62 pub client_order_id: ClientOrderId,
63 /// The unique identifier for the event.
64 pub event_id: UUID4,
65 /// UNIX timestamp (nanoseconds) when the event occurred.
66 pub ts_event: UnixNanos,
67 /// UNIX timestamp (nanoseconds) when the event was initialized.
68 pub ts_init: UnixNanos,
69}
70
71/// Represents an event where an order was released from the `OrderEmulated` by the Nautilus system.
72#[repr(C)]
73#[derive(Clone, Copy, Debug)]
74pub struct OrderReleasedFfi {
75 /// The trader ID associated with the event.
76 pub trader_id: TraderId,
77 /// The strategy ID associated with the event.
78 pub strategy_id: StrategyId,
79 /// The instrument ID associated with the event.
80 pub instrument_id: InstrumentId,
81 /// The client order ID associated with the event.
82 pub client_order_id: ClientOrderId,
83 pub released_price: Price,
84 /// The unique identifier for the event.
85 pub event_id: UUID4,
86 /// UNIX timestamp (nanoseconds) when the event occurred.
87 pub ts_event: UnixNanos,
88 /// UNIX timestamp (nanoseconds) when the event was initialized.
89 pub ts_init: UnixNanos,
90}
91
92/// Represents an event where an order has been submitted by the system to the
93/// trading venue.
94#[repr(C)]
95#[derive(Clone, Copy, Debug)]
96pub struct OrderSubmittedFfi {
97 /// The trader ID associated with the event.
98 pub trader_id: TraderId,
99 /// The strategy ID associated with the event.
100 pub strategy_id: StrategyId,
101 /// The instrument ID associated with the event.
102 pub instrument_id: InstrumentId,
103 /// The client order ID associated with the event.
104 pub client_order_id: ClientOrderId,
105 /// The account ID associated with the event.
106 pub account_id: AccountId,
107 /// The unique identifier for the event.
108 pub event_id: UUID4,
109 /// UNIX timestamp (nanoseconds) when the event occurred.
110 pub ts_event: UnixNanos,
111 /// UNIX timestamp (nanoseconds) when the event was initialized.
112 pub ts_init: UnixNanos,
113}
114
115/// Represents an event where an order has been accepted by the trading venue.
116///
117/// This event often corresponds to a `NEW` `OrdStatus` <39> field in FIX execution reports.
118#[repr(C)]
119#[derive(Clone, Copy, Debug)]
120pub struct OrderAcceptedFfi {
121 /// The trader ID associated with the event.
122 pub trader_id: TraderId,
123 /// The strategy ID associated with the event.
124 pub strategy_id: StrategyId,
125 /// The instrument ID associated with the event.
126 pub instrument_id: InstrumentId,
127 /// The client order ID associated with the event.
128 pub client_order_id: ClientOrderId,
129 /// The venue order ID associated with the event.
130 pub venue_order_id: VenueOrderId,
131 /// The account ID associated with the event.
132 pub account_id: AccountId,
133 /// The unique identifier for the event.
134 pub event_id: UUID4,
135 /// UNIX timestamp (nanoseconds) when the event occurred.
136 pub ts_event: UnixNanos,
137 /// UNIX timestamp (nanoseconds) when the event was initialized.
138 pub ts_init: UnixNanos,
139 /// If the event was generated during reconciliation.
140 pub reconciliation: u8,
141}
142
143/// Represents an event where an order has been rejected by the trading venue.
144#[repr(C)]
145#[derive(Clone, Copy, Debug)]
146pub struct OrderRejectedFfi {
147 /// The trader ID associated with the event.
148 pub trader_id: TraderId,
149 /// The strategy ID associated with the event.
150 pub strategy_id: StrategyId,
151 /// The instrument ID associated with the event.
152 pub instrument_id: InstrumentId,
153 /// The client order ID associated with the event.
154 pub client_order_id: ClientOrderId,
155 /// The account ID associated with the event.
156 pub account_id: AccountId,
157 /// The reason the order was rejected.
158 pub reason: Ustr,
159 /// The unique identifier for the event.
160 pub event_id: UUID4,
161 /// UNIX timestamp (nanoseconds) when the event occurred.
162 pub ts_event: UnixNanos,
163 /// UNIX timestamp (nanoseconds) when the event was initialized.
164 pub ts_init: UnixNanos,
165 /// If the event was generated during reconciliation.
166 pub reconciliation: u8,
167 /// If the order was rejected because it was post-only and would execute immediately as a taker.
168 pub due_post_only: u8,
169}
170
171/// # Safety
172///
173/// Assumes `reason_ptr` is a valid C string pointer.
174#[unsafe(no_mangle)]
175pub unsafe extern "C" fn order_denied_new(
176 trader_id: TraderId,
177 strategy_id: StrategyId,
178 instrument_id: InstrumentId,
179 client_order_id: ClientOrderId,
180 reason_ptr: *const c_char,
181 event_id: UUID4,
182 ts_event: UnixNanos,
183 ts_init: UnixNanos,
184) -> OrderDeniedFfi {
185 OrderDeniedFfi {
186 trader_id,
187 strategy_id,
188 instrument_id,
189 client_order_id,
190 reason: unsafe { cstr_to_ustr(reason_ptr) },
191 event_id,
192 ts_event,
193 ts_init,
194 }
195}
196
197#[unsafe(no_mangle)]
198pub extern "C" fn order_emulated_new(
199 trader_id: TraderId,
200 strategy_id: StrategyId,
201 instrument_id: InstrumentId,
202 client_order_id: ClientOrderId,
203 event_id: UUID4,
204 ts_event: UnixNanos,
205 ts_init: UnixNanos,
206) -> OrderEmulatedFfi {
207 OrderEmulatedFfi {
208 trader_id,
209 strategy_id,
210 instrument_id,
211 client_order_id,
212 event_id,
213 ts_event,
214 ts_init,
215 }
216}
217
218#[unsafe(no_mangle)]
219pub extern "C" fn order_released_new(
220 trader_id: TraderId,
221 strategy_id: StrategyId,
222 instrument_id: InstrumentId,
223 client_order_id: ClientOrderId,
224 released_price: Price,
225 event_id: UUID4,
226 ts_event: UnixNanos,
227 ts_init: UnixNanos,
228) -> OrderReleasedFfi {
229 OrderReleasedFfi {
230 trader_id,
231 strategy_id,
232 instrument_id,
233 client_order_id,
234 released_price,
235 event_id,
236 ts_event,
237 ts_init,
238 }
239}
240
241#[unsafe(no_mangle)]
242pub extern "C" fn order_submitted_new(
243 trader_id: TraderId,
244 strategy_id: StrategyId,
245 instrument_id: InstrumentId,
246 client_order_id: ClientOrderId,
247 account_id: AccountId,
248 event_id: UUID4,
249 ts_event: UnixNanos,
250 ts_init: UnixNanos,
251) -> OrderSubmittedFfi {
252 OrderSubmittedFfi {
253 trader_id,
254 strategy_id,
255 instrument_id,
256 client_order_id,
257 account_id,
258 event_id,
259 ts_event,
260 ts_init,
261 }
262}
263
264#[unsafe(no_mangle)]
265pub extern "C" fn order_accepted_new(
266 trader_id: TraderId,
267 strategy_id: StrategyId,
268 instrument_id: InstrumentId,
269 client_order_id: ClientOrderId,
270 venue_order_id: VenueOrderId,
271 account_id: AccountId,
272 event_id: UUID4,
273 ts_event: UnixNanos,
274 ts_init: UnixNanos,
275 reconciliation: u8,
276) -> OrderAcceptedFfi {
277 OrderAcceptedFfi {
278 trader_id,
279 strategy_id,
280 instrument_id,
281 client_order_id,
282 venue_order_id,
283 account_id,
284 event_id,
285 ts_event,
286 ts_init,
287 reconciliation,
288 }
289}
290
291/// # Safety
292///
293/// Assumes `reason_ptr` is a valid C string pointer.
294#[unsafe(no_mangle)]
295pub unsafe extern "C" fn order_rejected_new(
296 trader_id: TraderId,
297 strategy_id: StrategyId,
298 instrument_id: InstrumentId,
299 client_order_id: ClientOrderId,
300 account_id: AccountId,
301 reason_ptr: *const c_char,
302 event_id: UUID4,
303 ts_event: UnixNanos,
304 ts_init: UnixNanos,
305 reconciliation: u8,
306 due_post_only: u8,
307) -> OrderRejectedFfi {
308 OrderRejectedFfi {
309 trader_id,
310 strategy_id,
311 instrument_id,
312 client_order_id,
313 account_id,
314 reason: unsafe { cstr_to_ustr(reason_ptr) },
315 event_id,
316 ts_event,
317 ts_init,
318 reconciliation,
319 due_post_only,
320 }
321}