Markets package
Base Market
Base module for financial markets.
- class pymicrostructure.markets.base.Market[source]
Bases:
objectRepresents a financial market with order management and trade history.
This class serves as a base class for specific market implementations. It provides the basic structure for managing orders, market participants, and trade history.
Attributes:
- orderslist
A list to store all orders submitted to the market.
- participantslist
A list of all participants in the market.
- trade_historylist
A chronological list of all trades executed in the market.
- last_submission_timeint or float
The timestamp of the last order submission.
- completedbool
A flag indicating whether the market session is completed.
Methods:
- submit_order(order)
A method to be implemented by subclasses for submitting orders to the market.
Continuous Markets
Continuous type markets module for financial markets.
- class pymicrostructure.markets.continuous.ContinuousDoubleAuction(initial_fair_price: int = 100)[source]
Bases:
MarketRepresents a continuous order book market, extending the base Market class.
This class implements a market with a continuous order book, where orders are matched continuously as they arrive. It maintains separate bid and ask order books, tracks order book snapshots, and handles order submission, matching, and execution.
Attributes:
- bid_oblist
A list of bid orders in the order book.
- ask_oblist
A list of ask orders in the order book.
- ob_snapshotslist
A list of order book snapshots, capturing the state of the order book over time.
- midpriceslist
A list of tuples containing timestamp and midprice at each snapshot.
- cancellationslist
A list to track cancelled orders.
- durationint or None
The duration of the market simulation in ticks.
- current_tickint
The current tick (time step) of the market simulation.
Methods:
- submit_order(orders)
Submit one or more orders to the market.
- drop_cancelled_orders()
Remove cancelled orders from the order books.
- save_ob_state()
Save the current state of the order book.
- match_orders()
Match and execute orders in the order book.
- get_participant(trader_id)
Retrieve a market participant by their trader ID.
- execute_trade(buyer, seller, price, volume, aggressor_side)
Execute a trade between two participants.
- update_order_status(order)
Update the status of an order after matching.
- run(ticks=10)
Run the market simulation for a specified number of ticks.
- property best_ask: float | None
- property best_bid: float | None
- execute_trade(buyer: Trader, seller: Trader, price: float, volume: int | float, aggressor_side: int) None[source]
Execute a trade between two participants.
This method updates participant positions, records trade information, and adds the trade to the market’s trade history.
Parameters:
- buyerParticipant
The participant buying in this trade.
- sellerParticipant
The participant selling in this trade.
- pricefloat
The price at which the trade is executed.
- volumeint or float
The volume of the asset being traded.
- aggressor_sideint
Indicates which side initiated the trade (1 for buy, -1 for sell).
- get_participant(trader_id)[source]
Retrieve a market participant by their trader ID.
Parameters:
- trader_idstr or int
The unique identifier of the trader.
Returns:
- Participant
The participant object with the matching trader ID.
- static load(filename: str) ContinuousDoubleAuction[source]
- match_orders()[source]
Match and execute orders in the order book.
This method sorts the order books, identifies matching orders, and executes trades when possible.
- property midprice: float | None
- run(ticks: int = 10)[source]
Run the market simulation for a specified number of ticks.
This method simulates the market activity for a given duration, updating participants and processing their actions in each tick.
Parameters:
- ticksint, optional
The number of ticks to run the simulation (default is 10).
- save_ob_state()[source]
Save the current state of the order book.
This method aggregates orders at each price level, creates a snapshot of the current order book state, and updates the midprice.
- property spread: float | None
- submit_order(orders: Order | list[Order])[source]
Submit one or more orders to the market.
This method processes incoming orders, adds them to the appropriate order book, updates order statuses, and triggers order matching.
Parameters:
- ordersUnion[Order, list[Order]]
A single order or a list of orders to be submitted to the market.