Constructor
new MarketEngine(optionsopt)
create MarketEngine
Parameters:
Name | Type | Attributes | Default | Description | ||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
options |
Object |
<optional> |
{pushArray:1} | options copied to this.o
Properties
|
Listens to Events:
- event:{trade-cleanup(tradespec)} to reduce order quantity by quantity tradedo
Classes
Members
a :Array.<(Object|Array.<number>)>
list of active orders
orders may be objects or arrays of fixed length
Type:
- Array.<(Object|Array.<number>)>
count :number
counter of order number stamp
Type:
- number
o :Object
options passed to constructor
Type:
- Object
trash :Array.<number>
list of indexes into this.a[] of trashed orders to be removed
Type:
- Array.<number>
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> |
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. |
clear()
clear active and trash list, reset counter, and emit clear event
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.
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 |
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 |
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. |
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 |
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