use std::{collections::HashMap, fmt::Display};
use indexmap::IndexMap;
use nautilus_core::{UnixNanos, serialization::Serializable};
use serde::{Deserialize, Serialize};
use super::HasTsInit;
use crate::{
identifiers::InstrumentId,
types::{Price, fixed::FIXED_SIZE_BINARY},
};
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(tag = "type")]
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
)]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
)]
pub struct MarkPriceUpdate {
pub instrument_id: InstrumentId,
pub value: Price,
pub ts_event: UnixNanos,
pub ts_init: UnixNanos,
}
impl MarkPriceUpdate {
#[must_use]
pub fn new(
instrument_id: InstrumentId,
value: Price,
ts_event: UnixNanos,
ts_init: UnixNanos,
) -> Self {
Self {
instrument_id,
value,
ts_event,
ts_init,
}
}
#[must_use]
pub fn get_metadata(
instrument_id: &InstrumentId,
price_precision: u8,
) -> HashMap<String, String> {
let mut metadata = HashMap::new();
metadata.insert("instrument_id".to_string(), instrument_id.to_string());
metadata.insert("price_precision".to_string(), price_precision.to_string());
metadata
}
#[must_use]
pub fn get_fields() -> IndexMap<String, String> {
let mut metadata = IndexMap::new();
metadata.insert("value".to_string(), FIXED_SIZE_BINARY.to_string());
metadata.insert("ts_event".to_string(), "UInt64".to_string());
metadata.insert("ts_init".to_string(), "UInt64".to_string());
metadata
}
}
impl Display for MarkPriceUpdate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{},{},{},{}",
self.instrument_id, self.value, self.ts_event, self.ts_init
)
}
}
impl Serializable for MarkPriceUpdate {}
impl HasTsInit for MarkPriceUpdate {
fn ts_init(&self) -> UnixNanos {
self.ts_init
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(tag = "type")]
#[cfg_attr(
feature = "python",
pyo3::pyclass(module = "nautilus_trader.core.nautilus_pyo3.model", from_py_object)
)]
#[cfg_attr(
feature = "python",
pyo3_stub_gen::derive::gen_stub_pyclass(module = "nautilus_trader.model")
)]
pub struct IndexPriceUpdate {
pub instrument_id: InstrumentId,
pub value: Price,
pub ts_event: UnixNanos,
pub ts_init: UnixNanos,
}
impl IndexPriceUpdate {
#[must_use]
pub fn new(
instrument_id: InstrumentId,
value: Price,
ts_event: UnixNanos,
ts_init: UnixNanos,
) -> Self {
Self {
instrument_id,
value,
ts_event,
ts_init,
}
}
#[must_use]
pub fn get_metadata(
instrument_id: &InstrumentId,
price_precision: u8,
) -> HashMap<String, String> {
let mut metadata = HashMap::new();
metadata.insert("instrument_id".to_string(), instrument_id.to_string());
metadata.insert("price_precision".to_string(), price_precision.to_string());
metadata
}
#[must_use]
pub fn get_fields() -> IndexMap<String, String> {
let mut metadata = IndexMap::new();
metadata.insert("value".to_string(), FIXED_SIZE_BINARY.to_string());
metadata.insert("ts_event".to_string(), "UInt64".to_string());
metadata.insert("ts_init".to_string(), "UInt64".to_string());
metadata
}
}
impl Display for IndexPriceUpdate {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(
f,
"{},{},{},{}",
self.instrument_id, self.value, self.ts_event, self.ts_init
)
}
}
impl Serializable for IndexPriceUpdate {}
impl HasTsInit for IndexPriceUpdate {
fn ts_init(&self) -> UnixNanos {
self.ts_init
}
}
#[cfg(test)]
mod tests {
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
};
use nautilus_core::serialization::{
Serializable,
msgpack::{FromMsgPack, ToMsgPack},
};
use rstest::{fixture, rstest};
use serde_json;
use super::*;
#[fixture]
fn instrument_id() -> InstrumentId {
InstrumentId::from("BTC-USDT.OKX")
}
#[fixture]
fn price() -> Price {
Price::from("150_500.10")
}
#[rstest]
fn test_mark_price_update_new(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let mark_price = MarkPriceUpdate::new(instrument_id, price, ts_event, ts_init);
assert_eq!(mark_price.instrument_id, instrument_id);
assert_eq!(mark_price.value, price);
assert_eq!(mark_price.ts_event, ts_event);
assert_eq!(mark_price.ts_init, ts_init);
}
#[rstest]
fn test_mark_price_update_display(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let mark_price = MarkPriceUpdate::new(instrument_id, price, ts_event, ts_init);
assert_eq!(format!("{mark_price}"), "BTC-USDT.OKX,150500.10,1,2");
}
#[rstest]
fn test_mark_price_update_get_ts_init(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let mark_price = MarkPriceUpdate::new(instrument_id, price, ts_event, ts_init);
assert_eq!(mark_price.ts_init(), ts_init);
}
#[rstest]
fn test_mark_price_update_eq_hash(instrument_id: InstrumentId, price: Price) {
use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
};
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let mark_price1 = MarkPriceUpdate::new(instrument_id, price, ts_event, ts_init);
let mark_price2 = MarkPriceUpdate::new(instrument_id, price, ts_event, ts_init);
let mark_price3 =
MarkPriceUpdate::new(instrument_id, Price::from("143_500.50"), ts_event, ts_init);
assert_eq!(mark_price1, mark_price2);
assert_ne!(mark_price1, mark_price3);
let mut hasher1 = DefaultHasher::new();
let mut hasher2 = DefaultHasher::new();
mark_price1.hash(&mut hasher1);
mark_price2.hash(&mut hasher2);
assert_eq!(hasher1.finish(), hasher2.finish());
}
#[rstest]
fn test_mark_price_update_json_serialization(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let mark_price = MarkPriceUpdate::new(instrument_id, price, ts_event, ts_init);
let serialized = mark_price.to_json_bytes().unwrap();
let deserialized = MarkPriceUpdate::from_json_bytes(&serialized).unwrap();
assert_eq!(mark_price, deserialized);
}
#[rstest]
fn test_mark_price_update_msgpack_serialization(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let mark_price = MarkPriceUpdate::new(instrument_id, price, ts_event, ts_init);
let serialized = mark_price.to_msgpack_bytes().unwrap();
let deserialized = MarkPriceUpdate::from_msgpack_bytes(&serialized).unwrap();
assert_eq!(mark_price, deserialized);
}
#[rstest]
fn test_mark_price_update_clone(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let mark_price = MarkPriceUpdate::new(instrument_id, price, ts_event, ts_init);
let cloned = mark_price;
assert_eq!(mark_price, cloned);
}
#[rstest]
fn test_mark_price_update_serde_json(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let mark_price = MarkPriceUpdate::new(instrument_id, price, ts_event, ts_init);
let json_str = serde_json::to_string(&mark_price).unwrap();
let deserialized: MarkPriceUpdate = serde_json::from_str(&json_str).unwrap();
assert_eq!(mark_price, deserialized);
}
#[rstest]
fn test_index_price_update_new(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let index_price = IndexPriceUpdate::new(instrument_id, price, ts_event, ts_init);
assert_eq!(index_price.instrument_id, instrument_id);
assert_eq!(index_price.value, price);
assert_eq!(index_price.ts_event, ts_event);
assert_eq!(index_price.ts_init, ts_init);
}
#[rstest]
fn test_index_price_update_display(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let index_price = IndexPriceUpdate::new(instrument_id, price, ts_event, ts_init);
assert_eq!(format!("{index_price}"), "BTC-USDT.OKX,150500.10,1,2");
}
#[rstest]
fn test_index_price_update_get_ts_init(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let index_price = IndexPriceUpdate::new(instrument_id, price, ts_event, ts_init);
assert_eq!(index_price.ts_init(), ts_init);
}
#[rstest]
fn test_index_price_update_eq_hash(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let index_price1 = IndexPriceUpdate::new(instrument_id, price, ts_event, ts_init);
let index_price2 = IndexPriceUpdate::new(instrument_id, price, ts_event, ts_init);
let index_price3 = IndexPriceUpdate::new(instrument_id, price, UnixNanos::from(3), ts_init);
assert_eq!(index_price1, index_price2);
assert_ne!(index_price1, index_price3);
let mut hasher1 = DefaultHasher::new();
let mut hasher2 = DefaultHasher::new();
index_price1.hash(&mut hasher1);
index_price2.hash(&mut hasher2);
assert_eq!(hasher1.finish(), hasher2.finish());
}
#[rstest]
fn test_index_price_update_json_serialization(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let index_price = IndexPriceUpdate::new(instrument_id, price, ts_event, ts_init);
let serialized = index_price.to_json_bytes().unwrap();
let deserialized = IndexPriceUpdate::from_json_bytes(&serialized).unwrap();
assert_eq!(index_price, deserialized);
}
#[rstest]
fn test_index_price_update_msgpack_serialization(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let index_price = IndexPriceUpdate::new(instrument_id, price, ts_event, ts_init);
let serialized = index_price.to_msgpack_bytes().unwrap();
let deserialized = IndexPriceUpdate::from_msgpack_bytes(&serialized).unwrap();
assert_eq!(index_price, deserialized);
}
#[rstest]
fn test_index_price_update_serde_json(instrument_id: InstrumentId, price: Price) {
let ts_event = UnixNanos::from(1);
let ts_init = UnixNanos::from(2);
let index_price = IndexPriceUpdate::new(instrument_id, price, ts_event, ts_init);
let json_str = serde_json::to_string(&index_price).unwrap();
let deserialized: IndexPriceUpdate = serde_json::from_str(&json_str).unwrap();
assert_eq!(index_price, deserialized);
}
}