@mcb/matching-simulator
v0.0.13
Published
An API providing an order book, matching engine and pricing engine for JavaScript.
Downloads
3
Readme
Matching simulator
An API providing an order book, matching engine and pricing engine for JavaScript.
Installation
npm install @mcb/matching-simulator --save
Usage
CommonJs
const matchingSimulator = require('@mcb/matching-simulator');
ES6
import the necessary API's directly. For example:
import { createBook } from '@mcb/matching-simulator';
Quick start examples
Creating an order book, matching and pricing engine
The following example establishes a new book and matching engine. The consumer then assigns a listener to the matching engine's onTrade event. This event will fire when two orders match as with the two subsequent lines.
import { createBook, Side } from '@mcb/matching-simulator';
const book = createBook();
const matchingEngine = book.createMatchingEngine();
const pricingEngine = book.createPricingEngine();
matchingEngine.onTrade = (trade) =>
console.log(`${trade.user.userId} ${trade.side} ${trade.volume} ${trade.instrumentId} @ ${trade.price}`);
pricingEngine.onPrice = (price) =>
console.log(`${price.instrumentId} ${price.price}`);
const user = { userId: 'user1', firmId: 'test-bank' };
const user2 = { userId: 'user2', firmId: 'test-bank2' };
book.addOrder(user, 'tx1', Side.Buy, 50, 5);
book.addOrder(user2, 'tx1', Side.Sell, 50, 5);
/*
Output
------
> user1 buy 5 tx1 @ 50
> user2 sell 5 tx1 @ 50
*/