Skip to content
This repository has been archived by the owner on Jan 20, 2020. It is now read-only.

Commit

Permalink
Faster decimal library.
Browse files Browse the repository at this point in the history
  • Loading branch information
lisbakke committed Jan 23, 2018
1 parent c14bdab commit 6b9a14b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 12 deletions.
22 changes: 11 additions & 11 deletions lib/orderbook.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { RBTree } = require('bintrees');
const num = require('num');
const {Decimal} = require('decimal.js');
const assert = require('assert');

class Orderbook {
Expand All @@ -19,17 +19,17 @@ class Orderbook {
.map(order => ({
id: order[2],
side: 'buy',
price: num(order[0]),
size: num(order[1]),
price: Decimal(order[0]),
size: Decimal(order[1]),
}))
.forEach(order => this.add(order));

book.asks
.map(order => ({
id: order[2],
side: 'sell',
price: num(order[0]),
size: num(order[1]),
price: Decimal(order[0]),
size: Decimal(order[1]),
}))
.forEach(order => this.add(order));
} else {
Expand All @@ -50,8 +50,8 @@ class Orderbook {
order = {
id: order.order_id || order.id,
side: order.side,
price: num(order.price),
size: num(order.size || order.remaining_size),
price: Decimal(order.price),
size: Decimal(order.size || order.remaining_size),
};

const tree = this._getTree(order.side);
Expand Down Expand Up @@ -91,8 +91,8 @@ class Orderbook {
}

match(match) {
const size = num(match.size);
const price = num(match.price);
const size = Decimal(match.size);
const price = Decimal(match.price);
const tree = this._getTree(match.side);
const node = tree.find({ price: price });
assert(node);
Expand All @@ -112,8 +112,8 @@ class Orderbook {
}

change(change) {
const size = num(change.new_size);
const price = num(change.price);
const size = Decimal(change.new_size);
const price = Decimal(change.price);
const order = this.get(change.order_id);
const tree = this._getTree(change.side);
const node = tree.find({ price });
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
],
"dependencies": {
"bintrees": "^1.0.1",
"num": "^0.3.0",
"decimal.js": "^9.0.1",
"request": "^2.81.0",
"ws": "^4.0.0"
},
Expand Down

0 comments on commit 6b9a14b

Please sign in to comment.