pub struct TradingRateLimiter { /* private fields */ }Expand description
Trading rate limiter with order lifetime penalty tracking.
Tracks orders and calculates appropriate rate limit penalties when they are cancelled based on their age.
Implementations§
Source§impl TradingRateLimiter
impl TradingRateLimiter
Sourcepub fn new(max_counter: u32, decay_rate_per_sec: f64) -> Self
pub fn new(max_counter: u32, decay_rate_per_sec: f64) -> Self
Create a new trading rate limiter.
§Arguments
max_counter- Maximum counter valuedecay_rate_per_sec- How much the counter decays per second
Sourcepub fn try_place_order(
&mut self,
order_id: &str,
info: OrderTrackingInfo,
) -> Result<(), Duration>
pub fn try_place_order( &mut self, order_id: &str, info: OrderTrackingInfo, ) -> Result<(), Duration>
Try to acquire capacity for a new order.
Returns Ok(()) if allowed, Err(wait_time) if rate limited.
Sourcepub fn track_order(
&mut self,
order_id: impl Into<String>,
info: OrderTrackingInfo,
)
pub fn track_order( &mut self, order_id: impl Into<String>, info: OrderTrackingInfo, )
Track an order that was placed (without rate limit check).
Use this when the order was already placed successfully.
Sourcepub fn cancel_penalty(age: Duration) -> u32
pub fn cancel_penalty(age: Duration) -> u32
Calculate the penalty for cancelling an order.
Returns the penalty in points based on the order’s age.
Sourcepub fn try_cancel_order(&mut self, order_id: &str) -> Result<u32, Duration>
pub fn try_cancel_order(&mut self, order_id: &str) -> Result<u32, Duration>
Try to cancel an order with rate limit penalty.
Returns Ok(penalty) if allowed (with the penalty that was applied),
or Err(wait_time) if rate limited.
Sourcepub fn order_cancelled(&mut self, order_id: &str)
pub fn order_cancelled(&mut self, order_id: &str)
Notify the limiter that an order was cancelled (without rate limit check).
Use this when the cancellation was already processed.
Sourcepub fn order_filled(&mut self, order_id: &str)
pub fn order_filled(&mut self, order_id: &str)
Notify the limiter that an order was filled.
Filled orders don’t incur cancellation penalties.
Sourcepub fn current_counter(&self) -> f64
pub fn current_counter(&self) -> f64
Get the current counter value (unscaled).
Sourcepub fn available_capacity(&self) -> f64
pub fn available_capacity(&self) -> f64
Get the available capacity (unscaled).
Sourcepub fn would_allow_place(&self) -> bool
pub fn would_allow_place(&self) -> bool
Check if placing an order would be allowed.
Sourcepub fn tracked_orders(&self) -> usize
pub fn tracked_orders(&self) -> usize
Get the number of tracked orders.