MarketEngine
Extends:
Market "Engine" providing some minimal housekeeping functions for a trading exchange, but no economic functions.
Constructor Summary
Public Constructor | ||
public |
constructor(options: Object) create MarketEngine |
Member Summary
Public Members | ||
public |
a: Array<Object | number[]> list of active orders orders may be objects or arrays of fixed length |
|
public |
counter of order number stamp |
|
public |
goods: * |
|
public |
options passed to constructor |
|
public |
list of indexes into this.a[] of trashed orders to be removed |
Method Summary
Public Methods | ||
public |
cancel or expire orders prior to processing new order. |
|
public |
find and cancel previous orders by id. |
|
public |
clear() clear active and trash list, reset counter, and emit clear event |
|
public |
emptyTrash(): * delete the orders in the trash list from the active list. |
|
public |
find and expire orders. |
|
public |
"push" a new order to the market performs housekeeping: 1. |
|
public |
reduce the amounts of orders at active array indexes ais by amounts qs. |
|
public |
Register a trade in this market. |
Public Constructors
public constructor(options: Object) source
create MarketEngine
Params:
Name | Type | Attribute | Description |
options | Object |
|
options copied to this.o |
options.goods | string |
|
sets name of goods to be traded in this market |
options.money | string |
|
sets name of money used in this market |
options.pushArray | boolean |
|
set to 1 if orders are numeric arrays, affects .push handling, accept/reject is in order[0] after prepending 2 elements |
options.pushObject | boolean |
|
set to 1 if orders are objects, affects .push handling, accept/reject is in order.ok |
options.idCol | number | string |
|
order column number or name for id number of agent submitting the order |
options.cancelCol | number | string |
|
order column number or name for indicating cancellation (1) or no cancellation (0) of previous orders by this agent |
options.tCol | number | string |
|
order column number or name for time of order |
options.txCol | number | string |
|
order column number or name for expiration time of order |
options.qCol | number | string |
|
order column number or name for quantity to buy or sell |
options.noBump | boolean |
|
if truthy, no cancel/expire search of old orders is performed when pre-processing new orders |
Listen:
trade-cleanup(tradespec) |
to reduce order quantity by quantity tradedo |
Public Members
public a: Array<Object | number[]> source
list of active orders orders may be objects or arrays of fixed length
public goods: * source
Public Methods
public bump(neworder: Object | number[]) source
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.
Emit:
after cancelling or expiring any old orders |
public cancel(id: number): * source
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.
Params:
Name | Type | Attribute | Description |
id | number | The id number of an agent whose orders will be cancelled. |
Return:
* |
public clear() source
clear active and trash list, reset counter, and emit clear event
Emit:
after clearing active and trash lists and resetting counter |
public emptyTrash(): * source
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.
Return:
* |
public expire(ts: number): * source
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
Params:
Name | Type | Attribute | Description |
ts | number | The current effective market time |
Return:
* |
public push(order: Object | number[]) source
"push" a new order to the market performs housekeeping:
- pre-adding fields such as processing timestamp and order number
- emitting before-order(myorder,function reject()) to allow customized order acceptance/rejection rules
- procesing any cancellation or expiration triggered in pre-processing of the order with this.bump(myorder)
- If the this.a activer list exists, add the new order to the active list
- emit order(myorder) to allow for additional customized processing (such as identifying trades or enforcing other rules)
Emit:
before-order(myorder, reject()) |
to allow customized rejection rules for orders |
order(myorder) |
to allow customized rules and trading procedures |
public reduceQ(ais: number[], qs: number[]) source
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.
Throw:
if order is reduced to a negative quantity |
public trade(tradeSpec: Object) source
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.
Params:
Name | Type | Attribute | Description |
tradeSpec | Object |
Emit:
trade(tradeSpec) |
to allow custom processing of trade |
trade-cleanup(tradeSpec) |
to allow custom cleanup of market structures after processing trade |
after-trade(tradeSpec) |
to allow custom post-processing after trade |