Class: MarketEngine

MarketEngine(optionsopt)

Market "Engine" providing some minimal housekeeping functions for a trading exchange, but no economic functions.

Constructor

new MarketEngine(optionsopt)

create MarketEngine
Parameters:
Name Type Attributes Default Description
options Object <optional>
{pushArray:1} options copied to this.o
Properties
Name Type Attributes Description
goods string <optional>
sets name of goods to be traded in this market
money string <optional>
sets name of money used in this market
pushArray boolean <optional>
set to 1 if orders are numeric arrays, affects .push handling, accept/reject is in order[0] after prepending 2 elements
pushObject boolean <optional>
set to 1 if orders are objects, affects .push handling, accept/reject is in order.ok
idCol number | string <optional>
order column number or name for id number of agent submitting the order
cancelCol number | string <optional>
order column number or name for indicating cancellation (1) or no cancellation (0) of previous orders by this agent
tCol number | string <optional>
order column number or name for time of order
txCol number | string <optional>
order column number or name for expiration time of order
qCol number | string <optional>
order column number or name for quantity to buy or sell
noBump boolean <optional>
if truthy, no cancel/expire search of old orders is performed when pre-processing new orders
Source:
Listens to Events:
  • event:{trade-cleanup(tradespec)} to reduce order quantity by quantity tradedo

Classes

MarketEngine

Members

a :Array.<(Object|Array.<number>)>

list of active orders orders may be objects or arrays of fixed length
Type:
  • Array.<(Object|Array.<number>)>
Source:

count :number

counter of order number stamp
Type:
  • number
Source:

o :Object

options passed to constructor
Type:
  • Object
Source:

trash :Array.<number>

list of indexes into this.a[] of trashed orders to be removed
Type:
  • Array.<number>
Source:

Methods

bump(neworder)

cancel or expire orders prior to processing new order. new orders can be marked cancelReplace, necessitating a cancel search. new orders also update simulation time, necessitating an expired order search.
Parameters:
Name Type Description
neworder Object | Array.<number>
Source:
Fires:
  • event:{bump} after cancelling or expiring any old orders

cancel(id)

find and cancel previous orders by id. Orders are cancelled by setting quantity to 0 and adding to trash list. Optimistic searching is done: if an order is found which itself has a cancelCol set, the search is complete.
Parameters:
Name Type Description
id number The id number of an agent whose orders will be cancelled.
Source:

clear()

clear active and trash list, reset counter, and emit clear event
Source:
Fires:
  • event:{clear} after clearing active and trash lists and resetting counter

emptyTrash()

delete the orders in the trash list from the active list. Orders are deleted from the active list using Array.splice, so order indexes in this.a[] will also change.
Source:

expire(ts)

find and expire orders. Orders are expired if supplied time ts is greater than order txCol. Orders are expired by setting quantity to 0 and adding to trash list
Parameters:
Name Type Description
ts number The current effective market time
Source:

push(order)

"push" a new order to the market performs housekeeping: 1. pre-adding fields such as processing timestamp and order number 1. emitting before-order(myorder,function reject()) to allow customized order acceptance/rejection rules 1. if rejected, emitting reject(myorder) to allow additional processing or logging of rejected orders, after setting a rejection flag (which can still be unset) 1. assigning the order number in myorder[0] 1. emitting preorder(myorder) for logging or additional processing before order affects books, other orders, or trades 1. procesing any cancellation or expiration triggered in pre-processing of the order with this.bump(myorder) 1. If the this.a active list exists, add the new order to the active list 1. emit order(myorder) to allow for additional customized processing (such as identifying trades or enforcing other rules)
Parameters:
Name Type Description
order Object | Array.<number> A new order to the market for processing
Source:
Fires:
  • {before-order(myorder,event: reject())} to allow customized rejection rules for orders
  • event:{reject(myorder)} to allow for logging or other processing of rejected orders
  • event:{preorder(myorder)} to allow for logging or other processing of acceptable orders
  • event:{order(myorder)} to allow customized rules and trading procedures

reduceQ(ais, qs)

reduce the amounts of orders at active array indexes ais by amounts qs. Calls trash.push(ais[i]), pushing indexes to trash list, for affected orders reduced to zero quantity. Reducing an order to a negative quantity throws Error.
Parameters:
Name Type Description
ais Array.<number> Indexes in the active array this.a[] of the orders to be affected.
qs Array.<number> Amounts for reduction in the quantity of the affected orders.
Source:
Throws:
if order is reduced to a negative quantity
Type
Error

trade(tradeSpec)

Register a trade in this market. Sets tradespec.goods and tradespec.money to market goods and money, if any. Then emits trade(tradeSpec), trade-cleanup(tradeSpec), and after-trade(tradeSpec) for further processing.
Parameters:
Name Type Description
tradeSpec Object
Source:
Fires:
  • event:{trade(tradeSpec)} to allow custom processing of trade
  • event:{trade-cleanup(tradeSpec)} to allow custom cleanup of market structures after processing trade
  • event:{after-trade(tradeSpec)} to allow custom post-processing after trade