Struct bourse_book::OrderBook
source · pub struct OrderBook<const N: usize = 10> { /* private fields */ }
Expand description
Order book with order and trade history
§Examples
use bourse_book;
use bourse_book::{types, OrderBook};
let mut book: OrderBook = OrderBook::new(0, 1, true);
let order_id = book.create_order(
types::Side::Bid, 50, 101, Some(50)
).unwrap();
book.place_order(order_id);
let (bid, ask) = book.bid_ask();
book.cancel_order(order_id);
Implementations§
source§impl<const N: usize> OrderBook<N>
impl<const N: usize> OrderBook<N>
sourcepub fn new(start_time: Nanos, tick_size: Price, trading: bool) -> Self
pub fn new(start_time: Nanos, tick_size: Price, trading: bool) -> Self
Initialise a new orderbook
Creates a new order empty order book ( i.e. one with 0 volume and 0-MAX_PRICE prices)
§Arguments
start_time
- Simulated time to assign to the order booktick_size
- Tick sizetrading
- Flag to indicate if trades will be executed
sourcepub fn enable_trading(&mut self)
pub fn enable_trading(&mut self)
Enable trade execution
sourcepub fn disable_trading(&mut self)
pub fn disable_trading(&mut self)
Disable trade execution
NOTE: Currently there is not a un-crossing algorithm implemented
sourcepub fn get_trade_vol(&self) -> Vol
pub fn get_trade_vol(&self) -> Vol
Get the current cumulative trade_volume
sourcepub fn reset_trade_vol(&mut self)
pub fn reset_trade_vol(&mut self)
Reset cumulative trade vol to 0
sourcepub fn ask_best_vol(&self) -> Vol
pub fn ask_best_vol(&self) -> Vol
Get the current touch ask volume
sourcepub fn ask_best_vol_and_orders(&self) -> (Vol, OrderCount)
pub fn ask_best_vol_and_orders(&self) -> (Vol, OrderCount)
Get the current touch ask volume and order count
sourcepub fn ask_levels(&self) -> [(Vol, OrderCount); N]
pub fn ask_levels(&self) -> [(Vol, OrderCount); N]
Get volumes and number of orders at N price levels
Returns an array of tuples containing the volume and number of orders at each price level from the touch.
sourcepub fn bid_best_vol(&self) -> Vol
pub fn bid_best_vol(&self) -> Vol
Get current touch bid volume
sourcepub fn bid_best_vol_and_orders(&self) -> (Vol, OrderCount)
pub fn bid_best_vol_and_orders(&self) -> (Vol, OrderCount)
Get the current touch bid volume and order count
sourcepub fn bid_levels(&self) -> [(Vol, OrderCount); N]
pub fn bid_levels(&self) -> [(Vol, OrderCount); N]
Get volumes and number of orders at N price levels
Returns an array of tuples containing the volume and number of orders at each price level from the touch.
sourcepub fn level_1_data(&self) -> Level1Data
pub fn level_1_data(&self) -> Level1Data
Get current level 1 market data
Returns level 1 data which includes
- Best bid and ask prices
- Total bid and ask side volumes
- Bid and ask volumes at the touch
- Number of bid and ask orders at the touch
sourcepub fn level_2_data(&self) -> Level2Data<N>
pub fn level_2_data(&self) -> Level2Data<N>
Get current level 2 market data
In this case level 2 data contains additional order information at a fixed number of ticks from the best price
- Best bid and ask prices
- Total bid and ask volumes
- Volume and number of orders at N levels (ticks) above/below the bid ask (where N is 10 by default)
sourcepub fn create_order(
&mut self,
side: Side,
vol: Vol,
trader_id: TraderId,
price: Option<Price>
) -> Result<OrderId, OrderError>
pub fn create_order( &mut self, side: Side, vol: Vol, trader_id: TraderId, price: Option<Price> ) -> Result<OrderId, OrderError>
Create a new order
Create a new order in the order list, but this order is not automatically placed on the market. Returns the id of the newly created order.
§Arguments
side
- Order sidevol
- Order volumetrader_id
- Id of the trader placing the orderprice
- Price of the order, ifNone
the order is treated as a market order
sourcepub fn create_and_place_order(
&mut self,
side: Side,
vol: Vol,
trader_id: TraderId,
price: Option<Price>
) -> Result<OrderId, OrderError>
pub fn create_and_place_order( &mut self, side: Side, vol: Vol, trader_id: TraderId, price: Option<Price> ) -> Result<OrderId, OrderError>
Convenience function to create and immediately place an order
Create a new order in the order list and place it on the market. Returns the id of the newly created order.
§Arguments
side
- Order sidevol
- Order volumetrader_id
- Id of the trader placing the orderprice
- Price of the order, ifNone
the order is treated as a market order
sourcepub fn place_order(&mut self, order_id: OrderId)
pub fn place_order(&mut self, order_id: OrderId)
Place an order on the market
Place an order that has been created on the market
§Arguments
order_id
- Id of the order to place
sourcepub fn cancel_order(&mut self, order_id: OrderId)
pub fn cancel_order(&mut self, order_id: OrderId)
Cancel an order
Attempts to cancel an order, if the order is already filled or rejected then no change is made
§Arguments
order_id
- Id of the order to cancel
sourcepub fn modify_order(
&mut self,
order_id: OrderId,
new_price: Option<Price>,
new_vol: Option<Price>
)
pub fn modify_order( &mut self, order_id: OrderId, new_price: Option<Price>, new_vol: Option<Price> )
Modify the price and/or volume of an order
If only the volume is reduced, then the order maintains its price-time priority. Otherwise the order is replaced. The modified order maintains the same id.
If the price/vol are None then the original price/vol are kept.
§Arguments
order_id
- Id of the order to modifynew_price
- New price of the order, `None`` keeps the same pricenew_vol
- New volume of the order, `None`` keeps the same volume
sourcepub fn process_event(&mut self, event: Event)
pub fn process_event(&mut self, event: Event)
sourcepub fn get_orders(&self) -> Vec<&Order>
pub fn get_orders(&self) -> Vec<&Order>
Reference to list of created orders
sourcepub fn get_trades(&self) -> &Vec<Trade>
pub fn get_trades(&self) -> &Vec<Trade>
Reference to trade records