Struct bourse_de::Env

source ·
pub struct Env<const N: usize = 10> { /* private fields */ }
Expand description

Discrete event simulation environment

Simulation environment designed for use in a discrete event simulation. Allows agents/users to submit order instructions, update the state of the simulation, and record the market data.

§Examples

use bourse_de;
use bourse_de::{types, Env};
use rand_xoshiro::Xoroshiro128StarStar;
use rand_xoshiro::rand_core::SeedableRng;

let mut env: Env = Env::new(0, 1, 1_000, true);
let mut rng = Xoroshiro128StarStar::seed_from_u64(101);

// Submit a new order instruction
let order_id = env.place_order(
    types::Side::Ask,
    100,
    101,
    Some(50),
);

// Update the state of the market
env.step(&mut rng)

Implementations§

source§

impl<const N: usize> Env<N>

source

pub const N_LEVELS: usize = N

Number of price levels recorded during simulation

source

pub fn new( start_time: Nanos, tick_size: Price, step_size: Nanos, trading: bool ) -> Self

Initialise an empty environment

§Arguments
  • start_time - Simulation start time
  • tick_size - Market tick size
  • step_size - Simulated step time-length
  • trading - Flag if true orders will be matched, otherwise no trades will take place
source

pub fn step<R: RngCore>(&mut self, rng: &mut R)

Update the state of the simulation

Each step of the simulation:

  • The cumulative trade volume is reset
  • The transaction queue is shuffled
  • The transactions are processed, updating the state of the market
  • Time is jumped forward to the next step
  • Market data for the step is recorded

Note that when each event is processed time is incremented by 1 time unit (to ensure orders have a unique index).

§Arguments
  • rng - Random generator
source

pub fn enable_trading(&mut self)

Enable trading

source

pub fn disable_trading(&mut self)

Disable trading

source

pub fn place_order( &mut self, side: Side, vol: Vol, trader_id: TraderId, price: Option<Price> ) -> Result<OrderId, OrderError>

Create a new order

Note that this creates an order but does not immediately place the order on the market, rather it submits an instruction to place the order on the market that will be executed during the next update.

Returns the id of the newly create order.

§Arguments
  • side - Side to place order
  • vol - Volume of the order
  • trader_id - Id of the trader/agent placing the order
  • price - Order price, if None the order will be treated as a market order
source

pub fn cancel_order(&mut self, order_id: OrderId)

Submit an instruction to cancel an order

Note that this does not immediately delete the order but submits an instruction to cancel the order that will be processed during the next update

§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<Vol> )

Submit an instruction to modify an order

Note that this does not immediately modify the order but submits an instruction to modify the order that will be processed during the next update

§Arguments
  • order_id - Id of the order to modify
  • new_price - New price of the order, if None the original price will be kept
  • new_vol - New volume of the order, if None the original price will be kept
source

pub fn get_prices(&self) -> &(Vec<Price>, Vec<Price>)

Get reference to bid-ask price histories

source

pub fn get_volumes(&self) -> &(Vec<Vol>, Vec<Vol>)

Get bid-ask volume histories

source

pub fn get_touch_volumes(&self) -> (&Vec<Vol>, &Vec<Vol>)

Get bid-ask touch histories

source

pub fn get_touch_order_counts(&self) -> (&Vec<OrderCount>, &Vec<OrderCount>)

Get bid-ask order_count histories

source

pub fn get_trade_vols(&self) -> &Vec<Vol>

Get per step trade volume histories

source

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

Get references to order data

source

pub fn get_orderbook(&self) -> &OrderBook<N>

Get reference to the underlying orderbook

source

pub fn get_level_2_data_history(&self) -> &Level2DataRecords<N>

Get level 2 data history

source

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

Get reference to trade data

source

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

Get a reference to an order by id

§Arguments
  • order_id - Id of an order
source

pub fn order_status(&self, order_id: OrderId) -> Status

Get the status of an order

§Arguments
  • order_id - Id of an order
source

pub fn level_2_data(&self) -> &Level2Data<N>

Reference to current level-2 market data

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<const N: usize> UnwindSafe for Env<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<V, T> VZip<V> for T
where V: MultiLane<T>,

source§

fn vzip(self) -> V