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>
impl<const N: usize> Env<N>
sourcepub fn new(
start_time: Nanos,
tick_size: Price,
step_size: Nanos,
trading: bool
) -> Self
pub fn new( start_time: Nanos, tick_size: Price, step_size: Nanos, trading: bool ) -> Self
Initialise an empty environment
§Arguments
start_time
- Simulation start timetick_size
- Market tick sizestep_size
- Simulated step time-lengthtrading
- Flag iftrue
orders will be matched, otherwise no trades will take place
sourcepub fn step<R: RngCore>(&mut self, rng: &mut R)
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
sourcepub fn enable_trading(&mut self)
pub fn enable_trading(&mut self)
Enable trading
sourcepub fn disable_trading(&mut self)
pub fn disable_trading(&mut self)
Disable trading
sourcepub fn place_order(
&mut self,
side: Side,
vol: Vol,
trader_id: TraderId,
price: Option<Price>
) -> Result<OrderId, OrderError>
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 ordervol
- Volume of the ordertrader_id
- Id of the trader/agent placing the orderprice
- Order price, if None the order will be treated as a market order
sourcepub fn cancel_order(&mut self, order_id: OrderId)
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
sourcepub fn modify_order(
&mut self,
order_id: OrderId,
new_price: Option<Price>,
new_vol: Option<Vol>
)
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 modifynew_price
- New price of the order, ifNone
the original price will be keptnew_vol
- New volume of the order, ifNone
the original price will be kept
sourcepub fn get_touch_order_counts(&self) -> (&Vec<OrderCount>, &Vec<OrderCount>)
pub fn get_touch_order_counts(&self) -> (&Vec<OrderCount>, &Vec<OrderCount>)
Get bid-ask order_count histories
sourcepub fn get_trade_vols(&self) -> &Vec<Vol>
pub fn get_trade_vols(&self) -> &Vec<Vol>
Get per step trade volume histories
sourcepub fn get_orders(&self) -> Vec<&Order>
pub fn get_orders(&self) -> Vec<&Order>
Get references to order data
sourcepub fn get_orderbook(&self) -> &OrderBook<N>
pub fn get_orderbook(&self) -> &OrderBook<N>
Get reference to the underlying orderbook
sourcepub fn get_level_2_data_history(&self) -> &Level2DataRecords<N>
pub fn get_level_2_data_history(&self) -> &Level2DataRecords<N>
Get level 2 data history
sourcepub fn get_trades(&self) -> &Vec<Trade>
pub fn get_trades(&self) -> &Vec<Trade>
Get reference to trade data
sourcepub fn order_status(&self, order_id: OrderId) -> Status
pub fn order_status(&self, order_id: OrderId) -> Status
sourcepub fn level_2_data(&self) -> &Level2Data<N>
pub fn level_2_data(&self) -> &Level2Data<N>
Reference to current level-2 market data