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>

source

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 book
  • tick_size - Tick size
  • trading - Flag to indicate if trades will be executed
source

pub fn get_time(&self) -> Nanos

Get the order book time

source

pub fn set_time(&mut self, t: Nanos)

Manually set the time of the orderbook

  • t - Time to set
source

pub fn enable_trading(&mut self)

Enable trade execution

source

pub fn disable_trading(&mut self)

Disable trade execution

NOTE: Currently there is not a un-crossing algorithm implemented

source

pub fn get_trade_vol(&self) -> Vol

Get the current cumulative trade_volume

source

pub fn reset_trade_vol(&mut self)

Reset cumulative trade vol to 0

source

pub fn ask_vol(&self) -> Vol

Get the current total ask volume

source

pub fn ask_best_vol(&self) -> Vol

Get the current touch ask volume

source

pub fn ask_best_vol_and_orders(&self) -> (Vol, OrderCount)

Get the current touch ask volume and order count

source

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.

source

pub fn bid_vol(&self) -> Vol

Get the current total bid volume

source

pub fn bid_best_vol(&self) -> Vol

Get current touch bid volume

source

pub fn bid_best_vol_and_orders(&self) -> (Vol, OrderCount)

Get the current touch bid volume and order count

source

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.

source

pub fn bid_ask(&self) -> (Price, Price)

Get current bid-ask price

source

pub fn mid_price(&self) -> f64

Get current mid-price (as a float)

source

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
source

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)
source

pub fn order(&self, order_id: OrderId) -> &Order

Get a reference to the order data stored at the id

§Arguments
  • order_id - Id of the order
source

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 side
  • vol - Order volume
  • trader_id - Id of the trader placing the order
  • price - Price of the order, if None the order is treated as a market order
source

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 side
  • vol - Order volume
  • trader_id - Id of the trader placing the order
  • price - Price of the order, if None the order is treated as a market order
source

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
source

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
source

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 modify
  • new_price - New price of the order, `None`` keeps the same price
  • new_vol - New volume of the order, `None`` keeps the same volume
source

pub fn process_event(&mut self, event: Event)

Process an Event order instruction

Processes an order instruction to place, cancel or modify an order

§Arguments
  • event - Order instruction
source

pub fn get_orders(&self) -> Vec<&Order>

Reference to list of created orders

source

pub fn get_trades(&self) -> &Vec<Trade>

Reference to trade records

source

pub fn save_json<P: AsRef<Path>>(&self, path: P, pretty: bool) -> Result<()>

Save a snapshot of the order-book to JSON

§Argument
  • path - Path to write snapshot JSON to
  • pretty - If True JSON will be pretty printed
source

pub fn load_json<P: AsRef<Path>>(path: P) -> Result<Self>

Load an order-book from a JSON snapshot

§Argument
  • path - Path to read snapshot JSON from

Trait Implementations§

source§

impl<'de, const N: usize> Deserialize<'de> for OrderBook<N>

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl<const N: usize> Serialize for OrderBook<N>

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl<const N: usize> Freeze for OrderBook<N>

§

impl<const N: usize> RefUnwindSafe for OrderBook<N>

§

impl<const N: usize> Send for OrderBook<N>

§

impl<const N: usize> Sync for OrderBook<N>

§

impl<const N: usize> Unpin for OrderBook<N>

§

impl<const N: usize> UnwindSafe for OrderBook<N>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,