Skip to content

Commit

Permalink
Refine quote and trade related method naming
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdsellers committed Oct 7, 2024
1 parent 337a8fe commit ec10de7
Show file tree
Hide file tree
Showing 48 changed files with 220 additions and 220 deletions.
28 changes: 14 additions & 14 deletions nautilus_core/backtest/src/exchange.rs
Original file line number Diff line number Diff line change
Expand Up @@ -353,57 +353,57 @@ impl SimulatedExchange {
todo!("process order book deltas")
}

pub fn process_quote_tick(&mut self, tick: &QuoteTick) {
pub fn process_quote_tick(&mut self, quote: &QuoteTick) {
for module in &self.modules {
module.pre_process(Data::Quote(tick.to_owned()));
module.pre_process(Data::Quote(quote.to_owned()));
}

if !self.matching_engines.contains_key(&tick.instrument_id) {
if !self.matching_engines.contains_key(&quote.instrument_id) {
let instrument = {
let cache = self.cache.as_ref().borrow();
cache.instrument(&tick.instrument_id).cloned()
cache.instrument(&quote.instrument_id).cloned()
};

if let Some(instrument) = instrument {
self.add_instrument(instrument).unwrap();
} else {
panic!(
"No matching engine found for instrument {}",
tick.instrument_id
quote.instrument_id
);
}
}

if let Some(matching_engine) = self.matching_engines.get_mut(&tick.instrument_id) {
matching_engine.process_quote_tick(tick);
if let Some(matching_engine) = self.matching_engines.get_mut(&quote.instrument_id) {
matching_engine.process_quote_tick(quote);
} else {
panic!("Matching engine should be initialized");
}
}

pub fn process_trade_tick(&mut self, tick: &TradeTick) {
pub fn process_trade_tick(&mut self, trade: &TradeTick) {
for module in &self.modules {
module.pre_process(Data::Trade(tick.to_owned()));
module.pre_process(Data::Trade(trade.to_owned()));
}

if !self.matching_engines.contains_key(&tick.instrument_id) {
if !self.matching_engines.contains_key(&trade.instrument_id) {
let instrument = {
let cache = self.cache.as_ref().borrow();
cache.instrument(&tick.instrument_id).cloned()
cache.instrument(&trade.instrument_id).cloned()
};

if let Some(instrument) = instrument {
self.add_instrument(instrument).unwrap();
} else {
panic!(
"No matching engine found for instrument {}",
tick.instrument_id
trade.instrument_id
);
}
}

if let Some(matching_engine) = self.matching_engines.get_mut(&tick.instrument_id) {
matching_engine.process_trade_tick(tick);
if let Some(matching_engine) = self.matching_engines.get_mut(&trade.instrument_id) {
matching_engine.process_trade_tick(trade);
} else {
panic!("Matching engine should be initialized");
}
Expand Down
18 changes: 9 additions & 9 deletions nautilus_core/backtest/src/matching_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,14 +269,14 @@ impl OrderMatchingEngine {
self.iterate(delta.ts_event);
}

pub fn process_quote_tick(&mut self, tick: &QuoteTick) {
log::debug!("Processing {tick}");
pub fn process_quote_tick(&mut self, quote: &QuoteTick) {
log::debug!("Processing {quote}");

if self.book_type == BookType::L1_MBP {
self.book.update_quote_tick(tick).unwrap();
self.book.update_quote_tick(quote).unwrap();
}

self.iterate(tick.ts_event);
self.iterate(quote.ts_event);
}

pub fn process_bar(&mut self, bar: &Bar) {
Expand Down Expand Up @@ -463,15 +463,15 @@ impl OrderMatchingEngine {
self.last_bar_ask = None;
}

pub fn process_trade_tick(&mut self, tick: &TradeTick) {
log::debug!("Processing {tick}");
pub fn process_trade_tick(&mut self, trade: &TradeTick) {
log::debug!("Processing {trade}");

if self.book_type == BookType::L1_MBP {
self.book.update_trade_tick(tick).unwrap();
self.book.update_trade_tick(trade).unwrap();
}
self.core.set_last_raw(tick.price);
self.core.set_last_raw(trade.price);

self.iterate(tick.ts_event);
self.iterate(trade.ts_event);
}

// -- TRADING COMMANDS ------------------------------------------------------------------------
Expand Down
32 changes: 16 additions & 16 deletions nautilus_core/common/src/cache/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2430,15 +2430,15 @@ impl Cache {

/// Gets all quote ticks for the given `instrument_id`.
#[must_use]
pub fn quote_ticks(&self, instrument_id: &InstrumentId) -> Option<Vec<QuoteTick>> {
pub fn quotes(&self, instrument_id: &InstrumentId) -> Option<Vec<QuoteTick>> {
self.quotes
.get(instrument_id)
.map(|quotes| quotes.iter().copied().collect())
}

/// Gets all trade ticks for the given `instrument_id`.
#[must_use]
pub fn trade_ticks(&self, instrument_id: &InstrumentId) -> Option<Vec<TradeTick>> {
pub fn trades(&self, instrument_id: &InstrumentId) -> Option<Vec<TradeTick>> {
self.trades
.get(instrument_id)
.map(|trades| trades.iter().copied().collect())
Expand All @@ -2460,15 +2460,15 @@ impl Cache {

/// Gets a reference to the latest quote tick for the given `instrument_id`.
#[must_use]
pub fn quote_tick(&self, instrument_id: &InstrumentId) -> Option<&QuoteTick> {
pub fn quote(&self, instrument_id: &InstrumentId) -> Option<&QuoteTick> {
self.quotes
.get(instrument_id)
.and_then(|quotes| quotes.front())
}

/// Gets a refernece to the latest trade tick for the given `instrument_id`.
#[must_use]
pub fn trade_tick(&self, instrument_id: &InstrumentId) -> Option<&TradeTick> {
pub fn trade(&self, instrument_id: &InstrumentId) -> Option<&TradeTick> {
self.trades
.get(instrument_id)
.and_then(|trades| trades.front())
Expand All @@ -2488,15 +2488,15 @@ impl Cache {

/// Gets the quote tick count for the given `instrument_id`.
#[must_use]
pub fn quote_tick_count(&self, instrument_id: &InstrumentId) -> usize {
pub fn quote_count(&self, instrument_id: &InstrumentId) -> usize {
self.quotes
.get(instrument_id)
.map_or(0, std::collections::VecDeque::len)
}

/// Gets the trade tick count for the given `instrument_id`.
#[must_use]
pub fn trade_tick_count(&self, instrument_id: &InstrumentId) -> usize {
pub fn trade_count(&self, instrument_id: &InstrumentId) -> usize {
self.trades
.get(instrument_id)
.map_or(0, std::collections::VecDeque::len)
Expand All @@ -2519,13 +2519,13 @@ impl Cache {
/// Returns whether the cache contains quote ticks for the given `instrument_id`.
#[must_use]
pub fn has_quote_ticks(&self, instrument_id: &InstrumentId) -> bool {
self.quote_tick_count(instrument_id) > 0
self.quote_count(instrument_id) > 0
}

/// Returns whether the cache contains trade ticks for the given `instrument_id`.
#[must_use]
pub fn has_trade_ticks(&self, instrument_id: &InstrumentId) -> bool {
self.trade_tick_count(instrument_id) > 0
self.trade_count(instrument_id) > 0
}

/// Returns whether the cache contains bars for the given `bar_type`.
Expand Down Expand Up @@ -3101,21 +3101,21 @@ mod tests {

#[rstest]
fn test_quote_tick_when_empty(cache: Cache, audusd_sim: CurrencyPair) {
let result = cache.quote_tick(&audusd_sim.id);
let result = cache.quote(&audusd_sim.id);
assert!(result.is_none());
}

#[rstest]
fn test_quote_tick_when_some(mut cache: Cache) {
let quote = QuoteTick::default();
cache.add_quote(quote).unwrap();
let result = cache.quote_tick(&quote.instrument_id);
let result = cache.quote(&quote.instrument_id);
assert_eq!(result, Some(&quote));
}

#[rstest]
fn test_quote_ticks_when_empty(cache: Cache, audusd_sim: CurrencyPair) {
let result = cache.quote_ticks(&audusd_sim.id);
let result = cache.quotes(&audusd_sim.id);
assert!(result.is_none());
}

Expand All @@ -3127,27 +3127,27 @@ mod tests {
QuoteTick::default(),
];
cache.add_quotes(&quotes).unwrap();
let result = cache.quote_ticks(&quotes[0].instrument_id);
let result = cache.quotes(&quotes[0].instrument_id);
assert_eq!(result, Some(quotes));
}

#[rstest]
fn test_trade_tick_when_empty(cache: Cache, audusd_sim: CurrencyPair) {
let result = cache.trade_tick(&audusd_sim.id);
let result = cache.trade(&audusd_sim.id);
assert!(result.is_none());
}

#[rstest]
fn test_trade_tick_when_some(mut cache: Cache) {
let trade = TradeTick::default();
cache.add_trade(trade).unwrap();
let result = cache.trade_tick(&trade.instrument_id);
let result = cache.trade(&trade.instrument_id);
assert_eq!(result, Some(&trade));
}

#[rstest]
fn test_trade_ticks_when_empty(cache: Cache, audusd_sim: CurrencyPair) {
let result = cache.trade_ticks(&audusd_sim.id);
let result = cache.trades(&audusd_sim.id);
assert!(result.is_none());
}

Expand All @@ -3159,7 +3159,7 @@ mod tests {
TradeTick::default(),
];
cache.add_trades(&trades).unwrap();
let result = cache.trade_ticks(&trades[0].instrument_id);
let result = cache.trades(&trades[0].instrument_id);
assert_eq!(result, Some(trades));
}

Expand Down
4 changes: 2 additions & 2 deletions nautilus_core/data/src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1430,7 +1430,7 @@ mod tests {
let cache = &data_engine.cache.borrow();
let messages = get_saved_messages::<QuoteTick>(handler);

assert_eq!(cache.quote_tick(&quote.instrument_id), Some(quote).as_ref());
assert_eq!(cache.quote(&quote.instrument_id), Some(quote).as_ref());
assert_eq!(messages.len(), 1);
assert!(messages.contains(&quote));
}
Expand Down Expand Up @@ -1481,7 +1481,7 @@ mod tests {
let cache = &data_engine.cache.borrow();
let messages = get_saved_messages::<TradeTick>(handler);

assert_eq!(cache.trade_tick(&trade.instrument_id), Some(trade).as_ref());
assert_eq!(cache.trade(&trade.instrument_id), Some(trade).as_ref());
assert_eq!(messages.len(), 1);
assert!(messages.contains(&trade));
}
Expand Down
16 changes: 8 additions & 8 deletions nautilus_core/indicators/src/average/ama.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ impl Indicator for AdaptiveMovingAverage {
self.initialized
}

fn handle_quote_tick(&mut self, tick: &QuoteTick) {
self.update_raw(tick.extract_price(self.price_type).into());
fn handle_quote(&mut self, quote: &QuoteTick) {
self.update_raw(quote.extract_price(self.price_type).into());
}

fn handle_trade_tick(&mut self, tick: &TradeTick) {
self.update_raw((&tick.price).into());
fn handle_trade(&mut self, trade: &TradeTick) {
self.update_raw((&trade.price).into());
}

fn handle_bar(&mut self, bar: &Bar) {
Expand Down Expand Up @@ -247,8 +247,8 @@ mod tests {
}

#[rstest]
fn test_handle_quote_tick(mut indicator_ama_10: AdaptiveMovingAverage, quote_tick: QuoteTick) {
indicator_ama_10.handle_quote_tick(&quote_tick);
fn test_handle_quote_tick(mut indicator_ama_10: AdaptiveMovingAverage, stub_quote: QuoteTick) {
indicator_ama_10.handle_quote(&stub_quote);
assert!(indicator_ama_10.has_inputs);
assert!(!indicator_ama_10.initialized);
assert_eq!(indicator_ama_10.value, 1501.0);
Expand All @@ -257,9 +257,9 @@ mod tests {
#[rstest]
fn test_handle_trade_tick_update(
mut indicator_ama_10: AdaptiveMovingAverage,
trade_tick: TradeTick,
stub_trade: TradeTick,
) {
indicator_ama_10.handle_trade_tick(&trade_tick);
indicator_ama_10.handle_trade(&stub_trade);
assert!(indicator_ama_10.has_inputs);
assert!(!indicator_ama_10.initialized);
assert_eq!(indicator_ama_10.value, 1500.0);
Expand Down
16 changes: 8 additions & 8 deletions nautilus_core/indicators/src/average/dema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,11 @@ impl Indicator for DoubleExponentialMovingAverage {
self.initialized
}

fn handle_quote_tick(&mut self, quote: &QuoteTick) {
fn handle_quote(&mut self, quote: &QuoteTick) {
self.update_raw(quote.extract_price(self.price_type).into());
}

fn handle_trade_tick(&mut self, trade: &TradeTick) {
fn handle_trade(&mut self, trade: &TradeTick) {
self.update_raw((&trade.price).into());
}

Expand Down Expand Up @@ -176,20 +176,20 @@ mod tests {
}

#[rstest]
fn test_handle_quote_tick(
fn test_handle_quote(
mut indicator_dema_10: DoubleExponentialMovingAverage,
quote_tick: QuoteTick,
stub_quote: QuoteTick,
) {
indicator_dema_10.handle_quote_tick(&quote_tick);
indicator_dema_10.handle_quote(&stub_quote);
assert_eq!(indicator_dema_10.value, 1501.0);
}

#[rstest]
fn test_handle_trade_tick(
fn test_handle_trade(
mut indicator_dema_10: DoubleExponentialMovingAverage,
trade_tick: TradeTick,
stub_trade: TradeTick,
) {
indicator_dema_10.handle_trade_tick(&trade_tick);
indicator_dema_10.handle_trade(&stub_trade);
assert_eq!(indicator_dema_10.value, 1500.0);
}

Expand Down
20 changes: 10 additions & 10 deletions nautilus_core/indicators/src/average/ema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,11 +57,11 @@ impl Indicator for ExponentialMovingAverage {
self.initialized
}

fn handle_quote_tick(&mut self, quote: &QuoteTick) {
fn handle_quote(&mut self, quote: &QuoteTick) {
self.update_raw(quote.extract_price(self.price_type).into());
}

fn handle_trade_tick(&mut self, trade: &TradeTick) {
fn handle_trade(&mut self, trade: &TradeTick) {
self.update_raw((&trade.price).into());
}

Expand Down Expand Up @@ -187,29 +187,29 @@ mod tests {
#[rstest]
fn test_handle_quote_tick_single(
indicator_ema_10: ExponentialMovingAverage,
quote_tick: QuoteTick,
stub_quote: QuoteTick,
) {
let mut ema = indicator_ema_10;
ema.handle_quote_tick(&quote_tick);
ema.handle_quote(&stub_quote);
assert!(ema.has_inputs());
assert_eq!(ema.value, 1501.0);
}

#[rstest]
fn test_handle_quote_tick_multi(mut indicator_ema_10: ExponentialMovingAverage) {
let tick1 = quote_tick("1500.0", "1502.0");
let tick2 = quote_tick("1502.0", "1504.0");
let tick1 = stub_quote("1500.0", "1502.0");
let tick2 = stub_quote("1502.0", "1504.0");

indicator_ema_10.handle_quote_tick(&tick1);
indicator_ema_10.handle_quote_tick(&tick2);
indicator_ema_10.handle_quote(&tick1);
indicator_ema_10.handle_quote(&tick2);
assert_eq!(indicator_ema_10.count, 2);
assert_eq!(indicator_ema_10.value, 1_501.363_636_363_636_3);
}

#[rstest]
fn test_handle_trade_tick(indicator_ema_10: ExponentialMovingAverage, trade_tick: TradeTick) {
fn test_handle_trade_tick(indicator_ema_10: ExponentialMovingAverage, stub_trade: TradeTick) {
let mut ema = indicator_ema_10;
ema.handle_trade_tick(&trade_tick);
ema.handle_trade(&stub_trade);
assert!(ema.has_inputs());
assert_eq!(ema.value, 1500.0);
}
Expand Down
Loading

0 comments on commit ec10de7

Please sign in to comment.