bitmex-orderbook
Advanced tools
Comparing version 1.0.3 to 1.1.0
{ | ||
"name": "bitmex-orderbook", | ||
"version": "1.0.3", | ||
"version": "1.1.0", | ||
"description": "BitMEX WebSocket-driven Orderbook", | ||
@@ -25,2 +25,3 @@ "main": "src/OrderBook.js", | ||
"dependencies": { | ||
"crypto": "1.0.1", | ||
"ws": "3.3.3" | ||
@@ -27,0 +28,0 @@ }, |
@@ -1,5 +0,11 @@ | ||
# BitMEX OrderBook | ||
# BitMEX Order Book | ||
The fastest order book implementation for the BitMEX WebSocket API. | ||
The fastest order book implementation for the [BitMEX WebSocket API](https://www.bitmex.com/app/wsAPI). | ||
* Fast & unthrottled price updates from L2 table. | ||
* Automatically calculates cumulative prices. | ||
* Reuse an existing WebSocket connection. | ||
* Built-in [heartbeats](https://www.bitmex.com/app/wsAPI#Heartbeats) functionality. | ||
* Easy to use. | ||
## Install | ||
@@ -18,9 +24,9 @@ | ||
OrderBook.open("XBTH18", { | ||
onUpdate(orderBookL2) { | ||
await OrderBook.open("XBTH18", { | ||
onUpdate(orderBook) { | ||
// Top 10 ask prices. | ||
const bestAskPrices = orderBookL2.getAskPrices(10); | ||
const bestAskPrices = orderBook.getAskPrices(10); | ||
// Top 5 bid prices, skipping the first 2. | ||
const [thirdBestBid] = orderBookL2.getBidprices(5, 2); | ||
const [thirdBestBid] = orderBook.getBidprices(5, 2); | ||
@@ -40,3 +46,3 @@ thirdBestBid.side; // "Buy" | ||
### `OrderBook.open(symbol, options = {}): Promise` | ||
### `OrderBook.open(symbol, options = {}): Promise<OrderBook>` | ||
@@ -50,3 +56,3 @@ * `symbol : String` - The instrument symbol. Required. | ||
* `options.testmode : Boolean` - Connect to the BitMEX test environment. Only used if `options.socket` is empty. Default: `false`. | ||
* `options.hearbeat : Integer` - Milliseconds between WebSocket connection pings. Default: `15000`. | ||
* `options.heartbeat : Integer` - Milliseconds between WebSocket connection pings. Default: `15000`. | ||
* `options.endpoint : String` - Specifies the wss:// address for a new WebSocket connection. Only used if `options.socket` is empty. Default: `wss://www.bitmex.com/realtime`. | ||
@@ -64,3 +70,3 @@ | ||
Same as `getAskPrices`. Also sorted by best price first. | ||
Same as `orderBook.getAskPrices()`. Sorted by best price first. | ||
@@ -67,0 +73,0 @@ ## License |
@@ -0,1 +1,2 @@ | ||
const crypto = require("crypto"); | ||
const debug = require("debug")("bitmex-orderbook"); | ||
@@ -8,3 +9,4 @@ const WebSocket = require("ws"); | ||
this.connected = this.socket && this.socket.readyState === WebSocket.OPEN; | ||
this.authenticated = false; | ||
this.apiKey = options.apiKey || null; | ||
this.apiSecret = options.apiSecret || null; | ||
this.testmode = options.testmode === true || options.testmode === "true"; | ||
@@ -27,6 +29,16 @@ this.endpoint = | ||
this.socket.on("open", () => { | ||
this.socket.on("open", async () => { | ||
debug("Connection opened"); | ||
this.connected = this.socket.readyState === WebSocket.OPEN; | ||
heartbeatInterval = setInterval(() => this.ping(), this.heartbeat); | ||
if (this.apiKey && this.apiSecret) { | ||
const nonce = Date.now(); | ||
const signature = crypto | ||
.createHmac("sha256", this.apiSecret) | ||
.update(`GET/realtime${nonce}`) | ||
.digest("hex"); | ||
await this.sendMessage({ op: "authKey", args: [this.apiKey, nonce, signature] }); | ||
} | ||
this.subscriptions.forEach(symbol => this.subscribe(symbol)); | ||
@@ -46,9 +58,5 @@ return resolve(true); | ||
this.socket.on("error", err => { | ||
debug("Error:", err); | ||
debug("Connection error:", err); | ||
this.lastError = err; | ||
}); | ||
if (this.connected) { | ||
return resolve(true); | ||
} | ||
}); | ||
@@ -78,2 +86,3 @@ } | ||
async sendMessage(data) { | ||
debug("Sending:", data); | ||
const json = JSON.stringify(data); | ||
@@ -80,0 +89,0 @@ |
Sorry, the diff of this file is not supported yet
77313
289
72
2
+ Addedcrypto@1.0.1
+ Addedcrypto@1.0.1(transitive)