Skip to content

Commit

Permalink
Finish Instrument trait and implement sqlx trait FromRow for instrume…
Browse files Browse the repository at this point in the history
…nts (#1600)
  • Loading branch information
filipmacek authored Apr 20, 2024
1 parent 30aff27 commit fbf67eb
Show file tree
Hide file tree
Showing 13 changed files with 989 additions and 48 deletions.
1 change: 1 addition & 0 deletions nautilus_core/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions nautilus_core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ tracing = "0.1.40"
tokio = { version = "1.37.0", features = ["full"] }
ustr = { version = "1.0.0", features = ["serde"] }
uuid = { version = "1.8.0", features = ["v4"] }
sqlx = { version = "0.7.4", features = ["sqlite", "postgres", "any", "runtime-tokio"] }

# dev-dependencies
criterion = "0.5.1"
Expand Down
1 change: 1 addition & 0 deletions nautilus_core/model/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ ustr = { workspace = true }
chrono = { workspace = true }
evalexpr = "11.3.0"
tabled = "0.15.0"
sqlx = { workspace = true}

[dev-dependencies]
criterion = { workspace = true }
Expand Down
153 changes: 151 additions & 2 deletions nautilus_core/model/src/instruments/crypto_future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,23 @@
// limitations under the License.
// -------------------------------------------------------------------------------------------------

use std::hash::{Hash, Hasher};
use std::{
hash::{Hash, Hasher},
str::FromStr,
};

use nautilus_core::{
correctness::{check_equal_u8, check_positive_i64, check_positive_u64},
nanos::UnixNanos,
};
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};
use sqlx::{postgres::PgRow, FromRow, Row};
use ustr::Ustr;

use super::{Instrument, InstrumentAny};
use crate::{
enums::{AssetClass, InstrumentClass},
enums::{AssetClass, InstrumentClass, OptionKind},
identifiers::{instrument_id::InstrumentId, symbol::Symbol},
types::{currency::Currency, money::Money, price::Price, quantity::Quantity},
};
Expand Down Expand Up @@ -173,6 +178,10 @@ impl Instrument for CryptoFuture {
InstrumentClass::Future
}

fn underlying(&self) -> Option<Ustr> {
Some(self.underlying.code)
}

fn quote_currency(&self) -> Currency {
self.quote_currency
}
Expand All @@ -185,6 +194,18 @@ impl Instrument for CryptoFuture {
self.settlement_currency
}

fn isin(&self) -> Option<Ustr> {
None
}

fn exchange(&self) -> Option<Ustr> {
None
}

fn option_kind(&self) -> Option<OptionKind> {
None
}

fn is_inverse(&self) -> bool {
self.is_inverse
}
Expand Down Expand Up @@ -237,6 +258,134 @@ impl Instrument for CryptoFuture {
fn ts_init(&self) -> UnixNanos {
self.ts_init
}

fn strike_price(&self) -> Option<Price> {
None
}

fn activation_ns(&self) -> Option<UnixNanos> {
Some(self.activation_ns)
}

fn expiration_ns(&self) -> Option<UnixNanos> {
Some(self.expiration_ns)
}

fn max_notional(&self) -> Option<Money> {
self.max_notional
}

fn min_notional(&self) -> Option<Money> {
self.min_notional
}
}

impl<'r> FromRow<'r, PgRow> for CryptoFuture {
fn from_row(row: &'r PgRow) -> Result<Self, sqlx::Error> {
let id = row
.try_get::<String, _>("id")
.map(|res| InstrumentId::from(res.as_str()))?;
let raw_symbol = row
.try_get::<String, _>("raw_symbol")
.map(|res| Symbol::from(res.as_str()))?;
let underlying = row
.try_get::<String, _>("underlying")
.map(|res| Currency::from(res.as_str()))?;
let quote_currency = row
.try_get::<String, _>("quote_currency")
.map(|res| Currency::from(res.as_str()))?;
let settlement_currency = row
.try_get::<String, _>("settlement_currency")
.map(|res| Currency::from(res.as_str()))?;
let is_inverse = row.try_get::<bool, _>("is_inverse")?;
let activation_ns = row
.try_get::<String, _>("activation_ns")
.map(|res| UnixNanos::from(res.as_str()))?;
let expiration_ns = row
.try_get::<String, _>("expiration_ns")
.map(|res| UnixNanos::from(res.as_str()))?;
let price_precision = row.try_get::<i32, _>("price_precision")?;
let size_precision = row.try_get::<i32, _>("size_precision")?;
let price_increment = row
.try_get::<String, _>("price_increment")
.map(|res| Price::from_str(res.as_str()).unwrap())?;
let size_increment = row
.try_get::<String, _>("size_increment")
.map(|res| Quantity::from_str(res.as_str()).unwrap())?;
let maker_fee = row
.try_get::<String, _>("maker_fee")
.map(|res| Decimal::from_str(res.as_str()).unwrap())?;
let taker_fee = row
.try_get::<String, _>("taker_fee")
.map(|res| Decimal::from_str(res.as_str()).unwrap())?;
let margin_init = row
.try_get::<String, _>("margin_init")
.map(|res| Decimal::from_str(res.as_str()).unwrap())?;
let margin_maint = row
.try_get::<String, _>("margin_maint")
.map(|res| Decimal::from_str(res.as_str()).unwrap())?;
let lot_size = row
.try_get::<String, _>("lot_size")
.map(|res| Quantity::from(res.as_str()))?;
let max_quantity = row
.try_get::<Option<String>, _>("max_quantity")
.ok()
.and_then(|res| res.map(|value| Quantity::from(value.as_str())));
let min_quantity = row
.try_get::<Option<String>, _>("min_quantity")
.ok()
.and_then(|res| res.map(|value| Quantity::from(value.as_str())));
let max_notional = row
.try_get::<Option<String>, _>("max_notional")
.ok()
.and_then(|res| res.map(|value| Money::from(value.as_str())));
let min_notional = row
.try_get::<Option<String>, _>("min_notional")
.ok()
.and_then(|res| res.map(|value| Money::from(value.as_str())));
let max_price = row
.try_get::<Option<String>, _>("max_price")
.ok()
.and_then(|res| res.map(|value| Price::from(value.as_str())));
let min_price = row
.try_get::<Option<String>, _>("min_price")
.ok()
.and_then(|res| res.map(|value| Price::from(value.as_str())));
let ts_event = row
.try_get::<String, _>("ts_event")
.map(|res| UnixNanos::from(res.as_str()))?;
let ts_init = row
.try_get::<String, _>("ts_init")
.map(|res| UnixNanos::from(res.as_str()))?;
Ok(Self::new(
id,
raw_symbol,
underlying,
quote_currency,
settlement_currency,
is_inverse,
activation_ns,
expiration_ns,
price_precision as u8,
size_precision as u8,
price_increment,
size_increment,
maker_fee,
taker_fee,
margin_init,
margin_maint,
Some(lot_size),
max_quantity,
min_quantity,
max_notional,
min_notional,
max_price,
min_price,
ts_event,
ts_init,
)
.unwrap())
}
}

////////////////////////////////////////////////////////////////////////////////
Expand Down
Loading

0 comments on commit fbf67eb

Please sign in to comment.