Skip to content

Commit

Permalink
Fix clippy warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed May 5, 2023
1 parent 96b8ca9 commit 26eee7d
Show file tree
Hide file tree
Showing 32 changed files with 188 additions and 155 deletions.
2 changes: 1 addition & 1 deletion nautilus_core/backtest/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ impl TimeEventAccumulator {

impl Default for TimeEventAccumulator {
fn default() -> Self {
TimeEventAccumulator::new()
Self::new()
}
}

Expand Down
7 changes: 4 additions & 3 deletions nautilus_core/indicators/src/ema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,9 @@ pub struct ExponentialMovingAverage {
#[pymethods]
impl ExponentialMovingAverage {
#[new]
#[must_use]
pub fn new(period: usize, price_type: Option<PriceType>) -> Self {
ExponentialMovingAverage {
Self {
period,
price_type: price_type.unwrap_or(PriceType::Last),
alpha: 2.0 / (period as f64 + 1.0),
Expand All @@ -58,7 +59,7 @@ impl ExponentialMovingAverage {
self.value = value;
}

self.value = self.alpha * value + ((1.0 - self.alpha) * self.value);
self.value = self.alpha.mul_add(value, (1.0 - self.alpha) * self.value);
self.count += 1;

// Initialization logic
Expand Down Expand Up @@ -107,7 +108,7 @@ mod tests {
#[test]
fn test_ema_initialized() {
let ema = ExponentialMovingAverage::new(20, Some(PriceType::Mid));
let display_str = format!("{:?}", ema);
let display_str = format!("{ema:?}");
assert_eq!(display_str, "ExponentialMovingAverage { period: 20, price_type: Mid, alpha: 0.09523809523809523, value: 0.0, count: 0, _has_inputs: false, _is_initialized: false }");
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use iai::black_box;
use nautilus_model::types::fixed::f64_to_fixed_i64;

fn iai_fixed_precision_benchmark() -> i64 {
f64_to_fixed_i64(black_box(-0.000000001), black_box(9))
f64_to_fixed_i64(black_box(-0.000_000_001), black_box(9))
}

iai::main!(iai_fixed_precision_benchmark);
1 change: 1 addition & 0 deletions nautilus_core/model/src/currencies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use std::{collections::HashMap, sync::Arc};

use crate::{enums::CurrencyType, types::currency::Currency};

#[must_use]
pub fn currency_map() -> HashMap<String, Currency> {
[
// Fiat currencies
Expand Down
16 changes: 9 additions & 7 deletions nautilus_core/model/src/data/tick.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl QuoteTick {
"bid_size.precision",
"ask_size.precision",
);
QuoteTick {
Self {
instrument_id,
bid,
ask,
Expand All @@ -75,6 +75,7 @@ impl QuoteTick {
}
}

#[must_use]
pub fn extract_price(&self, price_type: PriceType) -> Price {
match price_type {
PriceType::Bid => self.bid.clone(),
Expand Down Expand Up @@ -122,7 +123,7 @@ impl TradeTick {
ts_event: UnixNanos,
ts_init: UnixNanos,
) -> Self {
TradeTick {
Self {
instrument_id,
price,
size,
Expand Down Expand Up @@ -157,10 +158,11 @@ pub enum Data {
}

impl Data {
#[must_use]
pub fn get_ts_init(&self) -> UnixNanos {
match self {
Data::Trade(t) => t.ts_init,
Data::Quote(q) => q.ts_init,
Self::Trade(t) => t.ts_init,
Self::Quote(q) => q.ts_init,
}
}
}
Expand Down Expand Up @@ -325,9 +327,9 @@ mod tests {
#[rstest(
input,
expected,
case(PriceType::Bid, 10000000000000),
case(PriceType::Ask, 10001000000000),
case(PriceType::Mid, 10000500000000)
case(PriceType::Bid, 10_000_000_000_000),
case(PriceType::Ask, 10_001_000_000_000),
case(PriceType::Mid, 10_000_500_000_000)
)]
fn test_quote_tick_extract_price(input: PriceType, expected: i64) {
let tick = QuoteTick {
Expand Down
4 changes: 2 additions & 2 deletions nautilus_core/model/src/identifiers/account_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ impl AccountId {
correctness::valid_string(s, "`AccountId` value");
correctness::string_contains(s, "-", "`TraderId` value");

AccountId {
Self {
value: Box::new(Rc::new(s.to_string())),
}
}
}

impl Default for AccountId {
fn default() -> Self {
AccountId {
Self {
value: Box::new(Rc::new(String::from("SIM-001"))),
}
}
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/identifiers/client_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl ClientId {
pub fn new(s: &str) -> Self {
correctness::valid_string(s, "`ClientId` value");

ClientId {
Self {
value: Box::new(Rc::new(s.to_string())),
}
}
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/identifiers/client_order_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl ClientOrderId {
pub fn new(s: &str) -> Self {
correctness::valid_string(s, "`ClientOrderId` value");

ClientOrderId {
Self {
value: Box::new(Rc::new(s.to_string())),
}
}
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/identifiers/component_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl ComponentId {
pub fn new(s: &str) -> Self {
correctness::valid_string(s, "`ComponentId` value");

ComponentId {
Self {
value: Box::new(Rc::new(s.to_string())),
}
}
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/identifiers/exec_algorithm_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl ExecAlgorithmId {
pub fn new(s: &str) -> Self {
correctness::valid_string(s, "`ExecAlgorithmId` value");

ExecAlgorithmId {
Self {
value: Box::new(Rc::new(s.to_string())),
}
}
Expand Down
4 changes: 2 additions & 2 deletions nautilus_core/model/src/identifiers/instrument_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub struct InstrumentId {
impl From<&str> for InstrumentId {
fn from(s: &str) -> Self {
let pieces = s.rsplit_once('.').expect("rsplit_once failed");
InstrumentId {
Self {
symbol: Symbol::new(pieces.0),
venue: Venue::new(pieces.1),
}
Expand All @@ -50,7 +50,7 @@ impl Display for InstrumentId {
impl InstrumentId {
#[must_use]
pub fn new(symbol: Symbol, venue: Venue) -> Self {
InstrumentId { symbol, venue }
Self { symbol, venue }
}
}

Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/identifiers/order_list_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl OrderListId {
pub fn new(s: &str) -> Self {
correctness::valid_string(s, "`OrderListId` value");

OrderListId {
Self {
value: Box::new(Rc::new(s.to_string())),
}
}
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/identifiers/position_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl PositionId {
pub fn new(s: &str) -> Self {
correctness::valid_string(s, "`PositionId` value");

PositionId {
Self {
value: Box::new(Rc::new(s.to_string())),
}
}
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/identifiers/strategy_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ impl StrategyId {
correctness::string_contains(s, "-", "`StrategyId` value");
}

StrategyId {
Self {
value: Box::new(Rc::new(s.to_string())),
}
}
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/identifiers/symbol.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Symbol {
pub fn new(s: &str) -> Self {
correctness::valid_string(s, "`Symbol` value");

Symbol {
Self {
value: Box::new(Rc::new(s.to_string())),
}
}
Expand Down
4 changes: 2 additions & 2 deletions nautilus_core/model/src/identifiers/trade_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl TradeId {
pub fn new(s: &str) -> Self {
correctness::valid_string(s, "`TradeId` value");

TradeId {
Self {
value: Box::new(Rc::new(s.to_string())),
}
}
Expand Down Expand Up @@ -70,7 +70,7 @@ pub extern "C" fn trade_id_drop(trade_id: TradeId) {
drop(trade_id); // Memory freed here
}

/// Returns [TradeId] as a C string pointer.
/// Returns [`TradeId`] as a C string pointer.
#[no_mangle]
pub extern "C" fn trade_id_to_cstr(trade_id: &TradeId) -> *const c_char {
string_to_cstr(&trade_id.value)
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/identifiers/trader_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ impl TraderId {
correctness::valid_string(s, "`TraderId` value");
correctness::string_contains(s, "-", "`TraderId` value");

TraderId {
Self {
value: Box::new(Rc::new(s.to_string())),
}
}
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/identifiers/venue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Venue {
pub fn new(s: &str) -> Self {
correctness::valid_string(s, "`Venue` value");

Venue {
Self {
value: Box::new(Rc::new(s.to_string())),
}
}
Expand Down
4 changes: 2 additions & 2 deletions nautilus_core/model/src/identifiers/venue_order_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,15 @@ impl VenueOrderId {
pub fn new(s: &str) -> Self {
correctness::valid_string(s, "`VenueOrderId` value");

VenueOrderId {
Self {
value: Box::new(Rc::new(s.to_string())),
}
}
}

impl Default for VenueOrderId {
fn default() -> Self {
VenueOrderId {
Self {
value: Box::new(Rc::new(String::from("001"))),
}
}
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/model/src/orderbook/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct OrderBook {
impl OrderBook {
#[must_use]
pub fn new(instrument_id: InstrumentId, book_level: BookType) -> Self {
OrderBook {
Self {
bids: Ladder::new(OrderSide::Buy),
asks: Ladder::new(OrderSide::Sell),
instrument_id,
Expand Down
17 changes: 11 additions & 6 deletions nautilus_core/model/src/orderbook/ladder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ pub struct BookPrice {
impl BookPrice {
#[must_use]
pub fn new(value: Price, side: OrderSide) -> Self {
BookPrice { value, side }
Self { value, side }
}
}

Expand Down Expand Up @@ -72,17 +72,19 @@ pub struct Ladder {
impl Ladder {
#[must_use]
pub fn new(side: OrderSide) -> Self {
Ladder {
Self {
side,
levels: Box::<BTreeMap<BookPrice, Level>>::default(),
cache: Box::<HashMap<u64, BookPrice>>::default(),
}
}

#[must_use]
pub fn len(&self) -> usize {
self.levels.len()
}

#[must_use]
pub fn is_empty(&self) -> bool {
self.levels.len() == 0
}
Expand Down Expand Up @@ -141,14 +143,17 @@ impl Ladder {
}
}

#[must_use]
pub fn volumes(&self) -> f64 {
return self.levels.iter().map(|(_, l)| l.volume()).sum();
}

#[must_use]
pub fn exposures(&self) -> f64 {
return self.levels.iter().map(|(_, l)| l.exposure()).sum();
}

#[must_use]
pub fn top(&self) -> Option<&Level> {
match self.levels.iter().next() {
None => Option::None,
Expand Down Expand Up @@ -302,10 +307,10 @@ mod tests {
ladder.update(order);
assert_eq!(ladder.len(), 1);
assert_eq!(ladder.volumes(), 20.0);
assert_eq!(ladder.exposures(), 222.00000000000003);
assert_eq!(ladder.exposures(), 222.000_000_000_000_03);
assert_eq!(
ladder.top().unwrap().price.value.as_f64(),
11.100000000000001
11.100_000_000_000_001
)
}

Expand All @@ -331,10 +336,10 @@ mod tests {
ladder.update(order);
assert_eq!(ladder.len(), 1);
assert_eq!(ladder.volumes(), 20.0);
assert_eq!(ladder.exposures(), 222.00000000000003);
assert_eq!(ladder.exposures(), 222.000_000_000_000_03);
assert_eq!(
ladder.top().unwrap().price.value.as_f64(),
11.100000000000001
11.100_000_000_000_001
)
}

Expand Down
9 changes: 7 additions & 2 deletions nautilus_core/model/src/orderbook/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,28 @@ pub struct Level {
impl Level {
#[must_use]
pub fn new(price: BookPrice) -> Self {
Level {
Self {
price,
orders: Box::<Vec<BookOrder>>::default(),
}
}

#[must_use]
pub fn from_order(order: BookOrder) -> Self {
let mut level = Level {
let mut level = Self {
price: order.to_book_price(),
orders: Box::<Vec<BookOrder>>::default(),
};
level.add(order);
level
}

#[must_use]
pub fn len(&self) -> usize {
self.orders.len()
}

#[must_use]
pub fn is_empty(&self) -> bool {
self.orders.len() == 0
}
Expand Down Expand Up @@ -88,6 +91,7 @@ impl Level {
self.orders.remove(index);
}

#[must_use]
pub fn volume(&self) -> f64 {
let mut sum: f64 = 0.0;
for o in self.orders.iter() {
Expand All @@ -96,6 +100,7 @@ impl Level {
sum
}

#[must_use]
pub fn exposure(&self) -> f64 {
let mut sum: f64 = 0.0;
for o in self.orders.iter() {
Expand Down
Loading

0 comments on commit 26eee7d

Please sign in to comment.