Skip to content

Commit

Permalink
Port RiskEngine to Rust (#2035)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pushkarm029 authored Nov 12, 2024
1 parent 9e26d37 commit 78fdd19
Show file tree
Hide file tree
Showing 11 changed files with 3,961 additions and 73 deletions.
2 changes: 1 addition & 1 deletion nautilus_core/adapters/src/tardis/http/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ impl TardisHttpClient {
Ok(Self {
base_url,
api_key,
normalize_symbols,
client,
normalize_symbols,
})
}

Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/common/src/msgbus/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,7 +552,7 @@ mod tests {
let handler = get_call_check_shareable_handler(None);

msgbus.register(endpoint, handler.clone());
assert!(msgbus.get_endpoint(&endpoint).is_some());
assert!(msgbus.get_endpoint(endpoint).is_some());
assert!(!check_handler_was_called(handler.clone()));

// Send a message to the endpoint
Expand Down
12 changes: 10 additions & 2 deletions nautilus_core/common/src/throttler/callbacks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@ impl<T, F> ThrottlerResume<T, F> {
}
}

impl<T: 'static, F: Fn(T) + 'static> From<ThrottlerResume<T, F>> for TimeEventCallback {
impl<T, F> From<ThrottlerResume<T, F>> for TimeEventCallback
where
T: 'static,
F: Fn(T) + 'static,
{
fn from(value: ThrottlerResume<T, F>) -> Self {
Self::Rust(Rc::new(move |_event: TimeEvent| {
value.inner.borrow_mut().is_limiting = false;
Expand All @@ -51,7 +55,11 @@ impl<T, F> ThrottlerProcess<T, F> {
}
}

impl<T: 'static, F: Fn(T) + 'static> From<ThrottlerProcess<T, F>> for TimeEventCallback {
impl<T, F> From<ThrottlerProcess<T, F>> for TimeEventCallback
where
T: 'static,
F: Fn(T) + 'static,
{
fn from(value: ThrottlerProcess<T, F>) -> Self {
Self::Rust(Rc::new(move |_event: TimeEvent| {
let process_clone = ThrottlerProcess {
Expand Down
2 changes: 1 addition & 1 deletion nautilus_core/common/src/throttler/inner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,8 +160,8 @@ impl<T, F> InnerThrottler<T, F> {

impl<T, F> InnerThrottler<T, F>
where
F: Fn(T) + 'static,
T: 'static,
F: Fn(T) + 'static,
{
#[inline]
pub fn send_msg(&mut self, msg: T) {
Expand Down
3 changes: 2 additions & 1 deletion nautilus_core/common/src/throttler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use inner::InnerThrottler;
use crate::clock::Clock;

/// Represents a throttling limit per interval.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RateLimit {
pub limit: usize,
pub interval_ns: u64,
Expand Down Expand Up @@ -100,8 +101,8 @@ impl<T, F> Throttler<T, F> {

impl<T, F> Throttler<T, F>
where
F: Fn(T) + 'static,
T: 'static,
F: Fn(T) + 'static,
{
pub fn send(&self, msg: T) {
let throttler_clone = Self {
Expand Down
13 changes: 13 additions & 0 deletions nautilus_core/model/src/data/stubs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,19 @@ pub fn quote_ethusdt_binance() -> QuoteTick {
}
}

#[fixture]
pub fn quote_audusd() -> QuoteTick {
QuoteTick {
instrument_id: InstrumentId::from("AUD/USD.SIM"),
bid_price: Price::from("100.0000"),
ask_price: Price::from("101.0000"),
bid_size: Quantity::from("1.00000000"),
ask_size: Quantity::from("1.00000000"),
ts_event: UnixNanos::default(),
ts_init: UnixNanos::from(1),
}
}

#[fixture]
pub fn stub_trade_ethusdt_buyer() -> TradeTick {
TradeTick {
Expand Down
60 changes: 60 additions & 0 deletions nautilus_core/model/src/instruments/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,66 @@ impl InstrumentAny {
}
}

pub fn max_quantity(&self) -> Option<Quantity> {
match self {
Self::Betting(inst) => inst.max_quantity(),
Self::BinaryOption(inst) => inst.max_quantity(),
Self::CryptoFuture(inst) => inst.max_quantity(),
Self::CryptoPerpetual(inst) => inst.max_quantity(),
Self::CurrencyPair(inst) => inst.max_quantity(),
Self::Equity(inst) => inst.max_quantity(),
Self::FuturesContract(inst) => inst.max_quantity(),
Self::FuturesSpread(inst) => inst.max_quantity(),
Self::OptionsContract(inst) => inst.max_quantity(),
Self::OptionsSpread(inst) => inst.max_quantity(),
}
}

pub fn min_quantity(&self) -> Option<Quantity> {
match self {
Self::Betting(inst) => inst.min_quantity(),
Self::BinaryOption(inst) => inst.min_quantity(),
Self::CryptoFuture(inst) => inst.min_quantity(),
Self::CryptoPerpetual(inst) => inst.min_quantity(),
Self::CurrencyPair(inst) => inst.min_quantity(),
Self::Equity(inst) => inst.min_quantity(),
Self::FuturesContract(inst) => inst.min_quantity(),
Self::FuturesSpread(inst) => inst.min_quantity(),
Self::OptionsContract(inst) => inst.min_quantity(),
Self::OptionsSpread(inst) => inst.min_quantity(),
}
}

pub fn max_notional(&self) -> Option<Money> {
match self {
Self::Betting(inst) => inst.max_notional(),
Self::BinaryOption(inst) => inst.max_notional(),
Self::CryptoFuture(inst) => inst.max_notional(),
Self::CryptoPerpetual(inst) => inst.max_notional(),
Self::CurrencyPair(inst) => inst.max_notional(),
Self::Equity(inst) => inst.max_notional(),
Self::FuturesContract(inst) => inst.max_notional(),
Self::FuturesSpread(inst) => inst.max_notional(),
Self::OptionsContract(inst) => inst.max_notional(),
Self::OptionsSpread(inst) => inst.max_notional(),
}
}

pub fn min_notional(&self) -> Option<Money> {
match self {
Self::Betting(inst) => inst.min_notional(),
Self::BinaryOption(inst) => inst.min_notional(),
Self::CryptoFuture(inst) => inst.min_notional(),
Self::CryptoPerpetual(inst) => inst.min_notional(),
Self::CurrencyPair(inst) => inst.min_notional(),
Self::Equity(inst) => inst.min_notional(),
Self::FuturesContract(inst) => inst.min_notional(),
Self::FuturesSpread(inst) => inst.min_notional(),
Self::OptionsContract(inst) => inst.min_notional(),
Self::OptionsSpread(inst) => inst.min_notional(),
}
}

pub fn make_price(&self, value: f64) -> Price {
match self {
Self::Betting(inst) => inst.make_price(value),
Expand Down
6 changes: 6 additions & 0 deletions nautilus_core/model/src/orders/any.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ pub enum OrderAny {
TrailingStopMarket(TrailingStopMarketOrder),
}

impl Default for OrderAny {
fn default() -> Self {
Self::Limit(LimitOrder::default())
}
}

impl OrderAny {
/// Applies the given `event` to the order.
pub fn apply(&mut self, event: OrderEventAny) -> Result<(), OrderError> {
Expand Down
1 change: 1 addition & 0 deletions nautilus_core/risk/src/engine/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use nautilus_core::datetime::NANOSECONDS_IN_SECOND;
use nautilus_model::identifiers::InstrumentId;
use rust_decimal::Decimal;

#[derive(Debug)]
/// Configuration for `RiskEngineConfig` instances.
pub struct RiskEngineConfig {
pub bypass: bool,
Expand Down
Loading

0 comments on commit 78fdd19

Please sign in to comment.