Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/hypercore/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1193,7 +1193,7 @@ impl Client {
sz: size,
reduce_only: false,
order_type: OrderTypePlacement::Limit {
tif: TimeInForce::Gtc,
tif: TimeInForce::FrontendMarket,
},
cloid: Default::default(),
}],
Expand Down
25 changes: 24 additions & 1 deletion src/hypercore/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,7 +714,10 @@ impl PriceTick {
/// See the PriceTick documentation for calculation details.
pub fn tick_for(&self, price: Decimal) -> Option<Decimal> {
let sig_figs = price.log10();
let sig_figs_n = sig_figs.ceil().to_i32()? as i64;
// Integer digits = floor(log10(price)) + 1. ceil() and floor+1 agree
// except when log10(price) is exact (price a power of ten), where
// ceil() undercounts by one and yields a tick ten times too fine.
let sig_figs_n = sig_figs.floor().to_i32()? as i64 + 1;
let decimals = 5_i64 - sig_figs_n;
let max_decimals = decimals.clamp(0, self.max_decimals);
Some(Decimal::TEN.powi(-max_decimals))
Expand Down Expand Up @@ -1155,6 +1158,26 @@ mod tick_tests {
);
}
}

#[test]
fn power_of_ten_prices_keep_five_sig_figs() {
// Prices that are exact powers of ten exercise the difference
// between ceil(log10) and floor(log10) + 1. The latter is the
// documented algorithm; the former undercounts the integer digits
// by one and yields a tick ten times too fine.
let table = PriceTick::for_perp(0); // max_decimals = 6
for (price, expected_tick) in [
(dec!(1000), dec!(0.1)),
(dec!(100), dec!(0.01)),
(dec!(10), dec!(0.001)),
(dec!(1), dec!(0.0001)),
] {
assert_eq!(table.tick_for(price), Some(expected_tick), "{price}");
}

let spot = PriceTick::for_spot(0); // max_decimals = 8
assert_eq!(spot.tick_for(dec!(1000)), Some(dec!(0.1)));
}
}

/// Spot token on HyperCore.
Expand Down
Loading