nautilus_model/ffi/data/
trade.rs

1// -------------------------------------------------------------------------------------------------
2//  Copyright (C) 2015-2025 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::{
17    collections::hash_map::DefaultHasher,
18    ffi::c_char,
19    hash::{Hash, Hasher},
20};
21
22use nautilus_core::ffi::string::str_to_cstr;
23
24use crate::{
25    data::TradeTick,
26    enums::AggressorSide,
27    identifiers::{InstrumentId, TradeId},
28    types::{Price, Quantity},
29};
30
31#[no_mangle]
32#[cfg_attr(feature = "high-precision", allow(improper_ctypes_definitions))]
33pub extern "C" fn trade_tick_new(
34    instrument_id: InstrumentId,
35    price: Price,
36    size: Quantity,
37    aggressor_side: AggressorSide,
38    trade_id: TradeId,
39    ts_event: u64,
40    ts_init: u64,
41) -> TradeTick {
42    TradeTick::new(
43        instrument_id,
44        price,
45        size,
46        aggressor_side,
47        trade_id,
48        ts_event.into(),
49        ts_init.into(),
50    )
51}
52
53#[no_mangle]
54pub extern "C" fn trade_tick_eq(lhs: &TradeTick, rhs: &TradeTick) -> u8 {
55    u8::from(lhs == rhs)
56}
57
58#[no_mangle]
59pub extern "C" fn trade_tick_hash(delta: &TradeTick) -> u64 {
60    let mut hasher = DefaultHasher::new();
61    delta.hash(&mut hasher);
62    hasher.finish()
63}
64
65/// Returns a [`TradeTick`] as a C string pointer.
66#[no_mangle]
67pub extern "C" fn trade_tick_to_cstr(trade: &TradeTick) -> *const c_char {
68    str_to_cstr(&trade.to_string())
69}