Home Reference Source Repository
import {MarketEngine} from 'market-engine/src/index.js'
public class | source

MarketEngine

Extends:

events~EventEmitter → MarketEngine

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

bump(neworder: Object | number[])

cancel or expire orders prior to processing new order.

public

cancel(id: number): *

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

expire(ts: number): *

find and expire orders.

public

push(order: Object | number[])

"push" a new order to the market performs housekeeping: 1.

public

reduceQ(ais: number[], qs: number[])

reduce the amounts of orders at active array indexes ais by amounts qs.

public

trade(tradeSpec: Object)

Register a trade in this market.

Public Constructors

public constructor(options: Object) source

create MarketEngine

Params:

NameTypeAttributeDescription
options Object
  • optional
  • default: {pushArray:1}

options copied to this.o

options.goods string
  • optional

sets name of goods to be traded in this market

options.money string
  • optional

sets name of money used in this market

options.pushArray boolean
  • optional

set to 1 if orders are numeric arrays, affects .push handling, accept/reject is in order[0] after prepending 2 elements

options.pushObject boolean
  • optional

set to 1 if orders are objects, affects .push handling, accept/reject is in order.ok

options.idCol number | string
  • optional

order column number or name for id number of agent submitting the order

options.cancelCol number | string
  • optional

order column number or name for indicating cancellation (1) or no cancellation (0) of previous orders by this agent

options.tCol number | string
  • optional

order column number or name for time of order

options.txCol number | string
  • optional

order column number or name for expiration time of order

options.qCol number | string
  • optional

order column number or name for quantity to buy or sell

options.noBump boolean
  • optional

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 count: number source

counter of order number stamp

public goods: * source

public o: Object source

options passed to constructor

public trash: number[] source

list of indexes into this.a[] of trashed orders to be removed

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.

Params:

NameTypeAttributeDescription
neworder Object | number[]

Emit:

bump

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:

NameTypeAttributeDescription
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:

clear

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:

NameTypeAttributeDescription
ts number

The current effective market time

Return:

*

public push(order: Object | number[]) source

"push" a new order to the market performs housekeeping:

  1. pre-adding fields such as processing timestamp and order number
  2. emitting before-order(myorder,function reject()) to allow customized order acceptance/rejection rules
  3. procesing any cancellation or expiration triggered in pre-processing of the order with this.bump(myorder)
  4. If the this.a activer list exists, add the new order to the active list
  5. emit order(myorder) to allow for additional customized processing (such as identifying trades or enforcing other rules)

Params:

NameTypeAttributeDescription
order Object | number[]

A new order to the market for processing

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.

Params:

NameTypeAttributeDescription
ais number[]

Indexes in the active array this.a[] of the orders to be affected.

qs number[]

Amounts for reduction in the quantity of the affected orders.

Throw:

Error

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:

NameTypeAttributeDescription
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