-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Improve implementation, validations and testing for Rust instruments #2733
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
c4dff38
to
8012dd1
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for all the work including the tests @nicolad 🙏
I'll follow up on the comments after merge to establish some more patterns.
We can address the info
field specifically on another pass.
|
||
/// # Errors | ||
/// | ||
/// Returns `anyhow::Error` if the value is not finite or cannot be converted to a `Price`. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to qualify the error type in the description here. "Returns an error if ..." is enough.
@@ -556,7 +636,7 @@ pub fn betting() -> BettingInstrument { | |||
); | |||
let selection_id = 50214; | |||
let selection_name = Ustr::from("Kansas City Chiefs"); | |||
let selection_handicap = 0.0; // As per betting convention, no handicap | |||
let selection_handicap = 0.0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is still reasonably informative.
@@ -501,7 +581,7 @@ pub fn option_spread() -> OptionSpread { | |||
Symbol::from("UD:U$: GN 2534559"), | |||
AssetClass::FX, | |||
Some(Ustr::from("XCME")), | |||
Ustr::from("SR3"), // British Pound futures (option on futures) | |||
Ustr::from("SR3"), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is still reasonably informative.
@@ -464,7 +544,7 @@ pub fn option_contract_appl() -> OptionContract { | |||
InstrumentId::from("AAPL211217C00150000.OPRA"), | |||
Symbol::from("AAPL211217C00150000"), | |||
AssetClass::Equity, | |||
Some(Ustr::from("GMNI")), // Nasdaq GEMX | |||
Some(Ustr::from("GMNI")), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This comment is still reasonably informative.
#[case(2.345_5, true)] | ||
#[case(0.000_999_999, false)] | ||
#[case(0.000_999_999, true)] | ||
fn quantity_rounding_grid( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good amount of cases here 👌
let step = 10f64.powi(-(price_precision as i32)); | ||
Price::new(step, price_precision) | ||
} | ||
|
||
macro_rules! check_positive { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This macro isn't necessary.
ensure!(price_precision >= 0, "price_precision negative"); | ||
ensure!(size_precision >= 0, "size_precision negative"); | ||
check_predicate_true(price_precision >= 0, "price_precision negative")?; | ||
check_predicate_true(size_precision >= 0, "size_precision negative")?; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We have more specialized correctness functions which could be used for many of these, e.g.
https://github.com/nautechsystems/nautilus_trader/blob/develop/crates/model/src/data/trade.rs#L76
check_positive_quantity(size, stringify!(size))?;
Instrument
→ Rust (make_price
,make_qty
, tick helpers, notional math, base-qty math)try_make_price
/try_make_qty
/try_calculate_base_quantity
returnanyhow::Result
, while legacy panicking versions call theirtry_
cousins.info
metadata in PyO3HashMap<String, PyObject>
plus getter/setter.to_dict
/from_dict
from RustFixed
,Crypto0_01
, …)enum_dispatch
, case-insensitiveFromStr
.price < 0
; tests added.rust_decimal::round_dp_with_strategy
for clarityamount
,currency
, …)validate_instrument_common
)ensure!
+ helper macrocheck_positive!
; covers paired-field, precision and bound checks.rstest
casesproptest
) for price/qty factoriesinfo
exposure.cargo clippy -D warnings
, multi-OS)try_*
fallible variants