Skip to content
kebetsi edited this page Feb 16, 2015 · 7 revisions

The Market Simulator is composed of the MarketSimulator which receives orders, looks for a match in the order books using the MarketRules’ matching function and executes them or stores them depending on whether a match exists. The MarketSimulator outputs Transactions records and delta orders to the BackLoop which distributes them to the trading agents and indicators. The RevenueCompute component can be plugged on the Exchange’s output to display each trader’s revenue.

###MarketSimulator The exchange simulator is defined in the MarketSimulator class. It takes a marketId and a MarketRules as constructor parameters. The marketId is used to identify the generated Transaction when orders are executed. It is useful when multiple exchanges are simulated at the same time, for example when back-testing an arbitrage strategy. It maintains an ask and a bid orders book which are defined in the OrderBook class. It also stores the price of the last executed order in the tradingPrice variable. It currently supports market and limit orders, the matching algorithm is defined in the matchingFunction() method from the MarketRules parameter.

###MarketRules The market configuration has been abstracted from the MarketSimulator in order to conveniently simulate exchanges with different orders matching strategies and order books orderings. A default configuration is defined by the MarketRules class, to customize it, the user must override its components in a new class. The books priorities are defined by an Ordering object which implements a compare function. The default ordering classifies the limit orders first by price then by timestamp.

The matching function is executed when the exchange receives a limit or market order:

def matchingFunction(marketId: Long, newOrder: Order,
  newOrdersBook: TreeSet[Order], bestMatchsBook: TreeSet[Order],
  send: Streamable => Unit, matchExists: (Double, Double) => Boolean,
  oldTradingPrice: Double,
  enqueueOrElse: (Order, TreeSet[Order]) => Unit): Double

The marketId is the same as the MarketSimulator’s, the newOrder parameter is the incoming order for which we will look for a match in the order books. If the newOrder is a bid, the newOrdersBook is the bid orders book and the bestMatchsBook is the ask orders book, otherwise the newOrdersBook is the ask orders book and the bestMatchsBook is the bid orders book. the send() method is the MarketSimulator’s send() method inherited from the Component superclass. the matchExists() method takes as parameters the newOrder’s price and the price of the best offer from the other book and compares them to decide if an order execution is possible. It returns always true when the newOrder is a market order. the enqueueOrElse() method is called in the matching function when no match is found for the newOrder. In the case of a limit order, it must be enqueued. When the newOrder is a market order, the default implementation is set to discard it, but there are exchanges where it is converted in a limit order. The return value of the matching function is the trading price, it returns the price of the transaction when an order execution occurs and the oldTradingPrice otherwise.

The MarketSimulator allows partial filling of orders, if an incoming order’s volume is superior to the volume of the best offer, a volume equal to the best offer’s volume is executed at the best offer’s price and the new order is reinput into the exchange with an updated volume recursively until its volume is depleted or there are no more matching orders in the books. If the best offer’s volume is superior to the incoming order’s volume, the new order is executed at the best offer’s price and the best offer order is inserted back in the books with the updated volume.

When an order is executed, the send() method is used to output Transactions and delta orders when orders are executed. Delta orders are used by trading agents whose strategy relies on the order books content. The agents listening to the delta orders can maintain their own version of the order books without having to execute the relatively complex matching function.

We have thought of implementing a commission parameter to define the price for entering various orders. This function is not yet supported by the MarketSimulator.

###BackLoop and RevenueCompute The Market Simulator contains utility components such as a BackLoop, a RevenueCompute. The BackLoop receives the Exchange’s output, it saves the Transactions in a Persistor for future analysis and distributes the Transactions and delta orders to various traders and Indicators. The RevenueCompute receives transactions from the BackLoop and computes each trader’s revenue by using the volume, price, buyerId, sellerId contained in each Transaction.

Clone this wiki locally