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-example-contingent

v3.2.1

Published

provides exchange Market with time-based and cancellable limit orders, and contingent orders such as stop, OSO (one-sends-others) and OCO (one-cancels-others)

Downloads

18

Readme

market-example-contingent

Build Status Known Vulnerabilities Language grade: JavaScript Total alerts

Provides exchange Market with time-based and cancellable limit orders, and contingent orders such as stop, OSO (one-sends-others) and OCO (one-cancels-others)

Built using market-engine, market-pricing, and partial-index packages also posted on npm

Breaking Changes in v3

Module changes

v3 is ESM whereas versions 2 and earlier were commonjs.

removing babel dependencies

v3 is not compiled with Babel

Installation

npm install market-example-contingent --save

Initialization

import * as MEC from 'market-example-contingent'; // ES6

const MEC = require('market-example-contingent'); // CJS

const XMarket = new MEC.Market({money:'coins', goods:'X', ...other options...});

Provides

  • new MEC.Market(config) constructor function for Market.
  • MEC.orderHeader 19 element array of strings, giving column headers for internal order array format
  • MEC.oa(order_object) function for converting orders from object format to array format
  • MEC.ao(order_array) function for converting orders from array format to object format

Inheritance Diagram

MEC.Market inherits linearly from market-engine and EventEmitter and constructors are chained.

EventEmitter
     |
market-engine (npm)
     |
MEC.Market

This means MEC.Market understands .push(some_order) and .trade(tradeSpec) from market-engine as well as .on('someEvent', function(params){ ... }) and .emit('someEvent') from EventEmitter.

Usage

Documentation on ESDoc

The primary documentation for market-example-contingent is on ESDoc

Creating Event Handlers

XMarket.on('trade', function(tradespec){
      // react to trade, do logging, etc.
});

XMarket.on('stops', function(t, matches){
       // t is the official time of the order
       // matches is a two element array [nbuystops,nsellstops]
});

XMarket.on('order', function(myorder){
       // something to do on every order
});

XMarket.on('before-order', function(myorder){
       // something to do on every order before it is processed
});

The use of arrow functions for event handlers is discouraged. The market's base class is EventEmitter and sets this to point at the market instance when a standard function is passed.

See also the MarketEngine documentation in package market-engine, as the market object inherits from MarketEngine.

Order Format

Order Lifecycle

An order begins life as 17 element numeric arrays, consisting of elements 2-18 above.

Generally, new orders should be pushed to an array XMarket.inbox used as a holding area and processsed in a loop similar to the following:

while(XMarket.inbox.length)
    XMarket.push(XMarket.inbox.shift());

This is because the execution of orders can trigger other orders, which are pushed to the .inbox internally in order to avoid issues of re-entrancy. The market procedues are not designed to be reentrant.

When Xmarket.push(order) is called, the order is checked in event before-order for following various configurable market rules. For example, a configuration setting could require an order with a buy price to exceed the highest active buy price. Orders that are not rejected are appended to the active list, the array Xmarket.a.

If the order is not rejected, the order number and local insertion timestamp are prepended as array elements 0 and 1, and the order is inserted into the active array XMarket.a and indexed in relevant order books. There are four order books which are partial indexes of active buy, sell, buyStop and sellStop orders.

When a matching order arrives from the other side of the market, the matched orders have their order quantities decremented by the traded quantity and a trade(tradespec) event is fired. Other pieces of software that perform accounting, log trades, or update displays may wish to set a .on('trade', function(){...}) listener for event trade.

After processing and emitting a trade, if any order in the trade has trigger fields, a new order is pushed to the inbox, transforming the trigger fields to regular order fields and using the old order's quantity traded (not quantity ordered) as the new quantity. After creating orders from triggers, stop/stop-limit orders are checked and, if the stop loss criterion is met, converted into new limit orders.