use std::{
collections::hash_map::DefaultHasher,
ffi::c_char,
hash::{Hash, Hasher},
str::FromStr,
};
use nautilus_core::{
ffi::string::{cstr_as_str, str_to_cstr},
nanos::UnixNanos,
};
use crate::{
data::bar::{Bar, BarSpecification, BarType},
enums::{AggregationSource, BarAggregation, PriceType},
identifiers::InstrumentId,
types::{price::Price, quantity::Quantity},
};
#[no_mangle]
pub extern "C" fn bar_specification_new(
step: usize,
aggregation: u8,
price_type: u8,
) -> BarSpecification {
let aggregation =
BarAggregation::from_repr(aggregation as usize).expect("cannot parse enum value");
let price_type = PriceType::from_repr(price_type as usize).expect("cannot parse enum value");
BarSpecification {
step,
aggregation,
price_type,
}
}
#[no_mangle]
pub extern "C" fn bar_specification_to_cstr(bar_spec: &BarSpecification) -> *const c_char {
str_to_cstr(&bar_spec.to_string())
}
#[no_mangle]
pub extern "C" fn bar_specification_hash(bar_spec: &BarSpecification) -> u64 {
let mut h = DefaultHasher::new();
bar_spec.hash(&mut h);
h.finish()
}
#[no_mangle]
pub extern "C" fn bar_specification_eq(lhs: &BarSpecification, rhs: &BarSpecification) -> u8 {
u8::from(lhs == rhs)
}
#[no_mangle]
pub extern "C" fn bar_specification_lt(lhs: &BarSpecification, rhs: &BarSpecification) -> u8 {
u8::from(lhs < rhs)
}
#[no_mangle]
pub extern "C" fn bar_specification_le(lhs: &BarSpecification, rhs: &BarSpecification) -> u8 {
u8::from(lhs <= rhs)
}
#[no_mangle]
pub extern "C" fn bar_specification_gt(lhs: &BarSpecification, rhs: &BarSpecification) -> u8 {
u8::from(lhs > rhs)
}
#[no_mangle]
pub extern "C" fn bar_specification_ge(lhs: &BarSpecification, rhs: &BarSpecification) -> u8 {
u8::from(lhs >= rhs)
}
#[no_mangle]
pub extern "C" fn bar_type_new(
instrument_id: InstrumentId,
spec: BarSpecification,
aggregation_source: u8,
) -> BarType {
let aggregation_source = AggregationSource::from_repr(aggregation_source as usize)
.expect("Error converting enum from integer");
BarType::Standard {
instrument_id,
spec,
aggregation_source,
}
}
#[no_mangle]
pub extern "C" fn bar_type_new_composite(
instrument_id: InstrumentId,
spec: BarSpecification,
aggregation_source: AggregationSource,
composite_step: usize,
composite_aggregation: BarAggregation,
composite_aggregation_source: AggregationSource,
) -> BarType {
BarType::new_composite(
instrument_id,
spec,
aggregation_source,
composite_step,
composite_aggregation,
composite_aggregation_source,
)
}
#[no_mangle]
pub extern "C" fn bar_type_is_standard(bar_type: &BarType) -> u8 {
bar_type.is_standard() as u8
}
#[no_mangle]
pub extern "C" fn bar_type_is_composite(bar_type: &BarType) -> u8 {
bar_type.is_composite() as u8
}
#[no_mangle]
pub extern "C" fn bar_type_standard(bar_type: &BarType) -> BarType {
bar_type.standard()
}
#[no_mangle]
pub extern "C" fn bar_type_composite(bar_type: &BarType) -> BarType {
bar_type.composite()
}
#[no_mangle]
pub extern "C" fn bar_type_instrument_id(bar_type: &BarType) -> InstrumentId {
bar_type.instrument_id()
}
#[no_mangle]
pub extern "C" fn bar_type_spec(bar_type: &BarType) -> BarSpecification {
bar_type.spec()
}
#[no_mangle]
pub extern "C" fn bar_type_aggregation_source(bar_type: &BarType) -> AggregationSource {
bar_type.aggregation_source()
}
#[no_mangle]
pub unsafe extern "C" fn bar_type_check_parsing(ptr: *const c_char) -> *const c_char {
match BarType::from_str(cstr_as_str(ptr)) {
Ok(_) => str_to_cstr(""),
Err(e) => str_to_cstr(&e.to_string()),
}
}
#[no_mangle]
pub unsafe extern "C" fn bar_type_from_cstr(ptr: *const c_char) -> BarType {
BarType::from(cstr_as_str(ptr))
}
#[no_mangle]
pub extern "C" fn bar_type_eq(lhs: &BarType, rhs: &BarType) -> u8 {
u8::from(lhs == rhs)
}
#[no_mangle]
pub extern "C" fn bar_type_lt(lhs: &BarType, rhs: &BarType) -> u8 {
u8::from(lhs < rhs)
}
#[no_mangle]
pub extern "C" fn bar_type_le(lhs: &BarType, rhs: &BarType) -> u8 {
u8::from(lhs <= rhs)
}
#[no_mangle]
pub extern "C" fn bar_type_gt(lhs: &BarType, rhs: &BarType) -> u8 {
u8::from(lhs > rhs)
}
#[no_mangle]
pub extern "C" fn bar_type_ge(lhs: &BarType, rhs: &BarType) -> u8 {
u8::from(lhs >= rhs)
}
#[no_mangle]
pub extern "C" fn bar_type_hash(bar_type: &BarType) -> u64 {
let mut h = DefaultHasher::new();
bar_type.hash(&mut h);
h.finish()
}
#[no_mangle]
pub extern "C" fn bar_type_to_cstr(bar_type: &BarType) -> *const c_char {
str_to_cstr(&bar_type.to_string())
}
#[no_mangle]
pub extern "C" fn bar_new(
bar_type: BarType,
open: Price,
high: Price,
low: Price,
close: Price,
volume: Quantity,
ts_event: UnixNanos,
ts_init: UnixNanos,
) -> Bar {
Bar {
bar_type,
open,
high,
low,
close,
volume,
ts_event,
ts_init,
}
}
#[no_mangle]
pub extern "C" fn bar_new_from_raw(
bar_type: BarType,
open: i64,
high: i64,
low: i64,
close: i64,
price_prec: u8,
volume: u64,
size_prec: u8,
ts_event: UnixNanos,
ts_init: UnixNanos,
) -> Bar {
Bar {
bar_type,
open: Price::from_raw(open, price_prec),
high: Price::from_raw(high, price_prec),
low: Price::from_raw(low, price_prec),
close: Price::from_raw(close, price_prec),
volume: Quantity::from_raw(volume, size_prec),
ts_event,
ts_init,
}
}
#[no_mangle]
pub extern "C" fn bar_eq(lhs: &Bar, rhs: &Bar) -> u8 {
u8::from(lhs == rhs)
}
#[no_mangle]
pub extern "C" fn bar_hash(bar: &Bar) -> u64 {
let mut h = DefaultHasher::new();
bar.hash(&mut h);
h.finish()
}
#[no_mangle]
pub extern "C" fn bar_to_cstr(bar: &Bar) -> *const c_char {
str_to_cstr(&bar.to_string())
}