npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

market-engine

v3.1.1

Published

event-driven market engine for economic research, simulations, and experiments

Downloads

57

Readme

market-engine

Build Status Known Vulnerabilities Language grade: JavaScript Total alerts

Provides EventEmitter framework for market/auction implementations, order storage and insertion/cancellation/expiration functionality

Breaking Changes for v3

Module changes

v3 is ESM whereas versions 2 and earlier were commonjs.

removing babel dependencies

v3 is not compiled with Babel

minor change to this.reduceQ()

will now throw clearer error if parameters are mismatched

Programmers documentation

jsdoc pages for market-engine

Installation

npm install market-engine --save

Initialization

import MarketEngine from 'market-engine'; // ES Module

Usage

MarketEngine is used as a base class for building classes representing market exchanges with some set of customized rules.

MarketEngine does the housekeeping of maintaining an active list of orders, a trash list, and provides a framework for handline new orders and trades, without specifying the ultimate form of orders or the rules of trade.

Subclasses

For a subclass implementing sequential double auction trading rules, see market-example-contingent

Events

MarketEngine is an EventEmitter. Here is an event reference.

bump -- Fired when one or more orders are cancelled or expired.

clear -- Fired when the market has been reset to an "empty" state with no orders.

before-order -- Fired before each order is processed by the market. Allows for rejection of orders.

order -- Fired when an order is ready for processing, i.e. if not rejected in before-order

trade -- Fired when a trade between orders is ready for processing.

trade-cleanup -- Fired to clean up after trade has been processed.

after-trade -- Fired after trade-cleanup

Setting Event Handlers

In the explanations below, XMarket is a variable containing an instance of MarketEngine.

XMarket.on('clear', function(){...})

specifies function to call when the market data is cleared

XMarket.on('before-order', function(neworder, reject){ ... })

specifies function to call before each order.

If neworder is an Array, a timestamp from Date.now() will be in neworder[0]

If neworder is an Object, the timestamp is in neworder.ts

Any event handler for before-order may reject an order, by using the reject function that is supplied as a 2nd parameter, like this: return reject(neworder)

XMarket.on('order', function(neworder){ ... })

specifies function to call after each order. Array orders will have the ordernum and timestamp prepended. Object orders will have the ordernum in neworder.num and the timestamp in neworder.ts

XMarket.on('trade', function(tradespec){ ... })

specifies function to call after each trade

XMarket.on('trade-cleanup', function(tradespec){ ... })

specifies function to call to clean up after each trade. The first handler attached by default reduces the quantity field of each order in the tradespec and marks the order as trash if the quantity reaches zero.

XMarket.on('after-trade', function(tradespec){ ... })

specifies function to call after the trade event handlers and the trade-cleanup handlers have completed.

##Functions, also see JSdoc documentation.

MarketEngine.prototype.bump(neworder)

cancels and/or expires any orders in the active list this.a depending on the timestamp and id in neworder and the setting of the cancelReplace column in neworder. The active list is always scanned for expiring orders, but will only be scanned for cancellation if the cancelReplace column is truthy in neworder.

MarketEngine.prototype.clear()

clears the active list this.a=[], the trash list this.trash=[] and resets this.count to 0, then emits "clear"

Emits: clear()

MarketEngine.prototype.push(neworder)

processes neworder, as follows:

  1. fires before-order
  2. exits if order rejected by a before-order handler
  3. prepends/sets order number and timestamp as first two fields of neworder array
  4. calls MarketEngine.prototype.bump(neworder), trashing any previously expired or cancelled orders
  5. fires order

Code listening for order may call MarketEngine.prototype.trade(tradespec) to indicate a trade in accordance with some set of market rules, to be defined by the developer.

Emits: before-order(neworder), order(neworder)

MarketEngine.prototype.trade(tradespec)
  1. Copies this.o.goods and this.o.money to tradespec.goods and tradespec.money, if the former are defined.
  2. Fires trade, trade-cleanup and after-trade.

Emits: trade, trade-cleanup, after-trade

MarketEngine.prototype.cancel(id)

Trashes all previous orders by id from the active list this.a .

Trashing an order sets it quantity to 0 and places it in the trash list for deletion by .emptyTrash()

Called by MarketEngine.prototype.bump(neworder) if cancelReplace is indicated.

requires: options.idCol, options.qCol

optional: options.cancelCol (shortens search if a previous cancel/replace found)

MarketEngine.prototype.expire(ts)

Trashes all orders expiring before timestamp ts from the active list this.a.

Trashing an order sets it quantity to 0 and places it in the trash list for deletion by .emptyTrash()

Called by MarketEngine.prototype.bump(neworder) on each neworder

requires: options.txCol, options.qCol

MarketEngine.prototype.reduceQ(ais, qs)

Reduces order quantity of orders locates at indexes in the array ais by the amounts in array qs

requires: options.qCol

MarketEngine.prototype.emptyTrash()

Delete any previously trashed orders from active list this.a

Copyright

Copyright 2016- Paul Brewer, Economic and Financial Technology Consulting LLC

License

MIT