Skip to content

Commit

Permalink
Level2 Datafeed & demos
Browse files Browse the repository at this point in the history
  • Loading branch information
qhxin committed Jul 7, 2020
1 parent 6230f48 commit e0f2887
Show file tree
Hide file tree
Showing 10 changed files with 599 additions and 65 deletions.
43 changes: 15 additions & 28 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -145,35 +145,22 @@ Signature is not required for this part

## Websocket Datafeed

DEMO:
```
// ws demo
const datafeed = new API.websocket.Datafeed();
// close callback
datafeed.onClose(() => {
console.log('ws closed, status ', datafeed.trustConnected);
});
// connect
datafeed.connectSocket();
// subscribe
const topic = `/market/ticker:BTC-USDT`;
const callbackId = datafeed.subscribe(topic, (message) => {
if (message.topic === topic) {
console.log(message.data);
}
});
console.log(`subscribe id: ${callbackId}`);
setTimeout(() => {
// unsubscribe
datafeed.unsubscribe(topic, callbackId);
console.log(`unsubscribed: ${topic} ${callbackId}`);
}, 5000);
### API.websocket.Datafeed

```
Manage websocket connect/private/subscribe/unsubscribe and get realtime datafeed.

DEMO: [demo/ticker_demo.js](demo/ticker_demo.js)

### API.websocket.Level2

Get realtime orderbook in level2 datafeed.

DEMO: [demo/level2_demo.js](demo/level2_demo.js)


### API.websocket.Level3

// TODO

## LICENSE

Expand Down
28 changes: 1 addition & 27 deletions demo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,5 @@ const main = async () => {
console.log(getStatusRl);
};

// run demo
// run rest main
main();

// ws demo
const datafeed = new API.websocket.Datafeed();

// close callback
datafeed.onClose(() => {
console.log('ws closed, status ', datafeed.trustConnected);
});

// connect
datafeed.connectSocket();

// subscribe
const topic = `/market/ticker:BTC-USDT`;
const callbackId = datafeed.subscribe(topic, (message) => {
if (message.topic === topic) {
console.log(message.data);
}
});

console.log(`subscribe id: ${callbackId}`);
setTimeout(() => {
// unsubscribe
datafeed.unsubscribe(topic, callbackId);
console.log(`unsubscribed: ${topic} ${callbackId}`);
}, 5000);
70 changes: 70 additions & 0 deletions demo/level2_demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
const _ = require('lodash');
const logUpdate = require('log-update');
const API = require('../src');

const config = require('./config');
API.init({
...config,
baseUrl: 'https://api.kucoin.io',
});

// ws demo
const datafeed = new API.websocket.Datafeed();

/*
// close callback
datafeed.onClose(() => {
console.log('ws closed, status ', datafeed.trustConnected);
});
// connect
datafeed.connectSocket();
// subscribe
const topic = `/market/level2:BTC-USDT`;
const callbackId = datafeed.subscribe(topic, (message) => {
if (message.topic === topic) {
console.log(JSON.stringify(message.data));
}
});
console.log(`subscribe id: ${callbackId}`);
setTimeout(() => {
// unsubscribe
datafeed.unsubscribe(topic, callbackId);
console.log(`unsubscribed: ${topic} ${callbackId}`);
}, 5000);
*/

const { Level2 } = API.websocket;

Level2.setLogger(() => {});

const l2 = new Level2('BTC-USDT', datafeed);
l2.listen();

const interval = setInterval(async () => {
// read orderbook
const orderbook = l2.getOrderBook(5);

// show Level2
let asksStr = '';
_.eachRight(orderbook.asks, ([price, size]) => {
asksStr += `${price} -> ${size}\n`;
});

let bidsStr = '';
_.each(orderbook.bids, ([price, size]) => {
bidsStr += `${price} -> ${size}\n`;
});

logUpdate.clear();
logUpdate(`------------------------\n` +
`l2 ${orderbook.dirty ? 'Dirty Data' : 'Trust Data'}\n` +
`l2 seq: ${orderbook.sequence}\n` +
`ping: ${orderbook.ping} (ms)\n` +
`------------------------\n` +
`${asksStr}----------sep-----------\n` +
`${bidsStr}------------------------`
);
}, 200);
29 changes: 29 additions & 0 deletions demo/ticker_demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const API = require('../src');

API.init(require('./config'));

// ws demo
const datafeed = new API.websocket.Datafeed();

// close callback
datafeed.onClose(() => {
console.log('ws closed, status ', datafeed.trustConnected);
});

// connect
datafeed.connectSocket();

// subscribe
const topic = `/market/ticker:BTC-USDT`;
const callbackId = datafeed.subscribe(topic, (message) => {
if (message.topic === topic) {
console.log(message.data);
}
});

console.log(`subscribe id: ${callbackId}`);
setTimeout(() => {
// unsubscribe
datafeed.unsubscribe(topic, callbackId);
console.log(`unsubscribed: ${topic} ${callbackId}`);
}, 5000);
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"dependencies": {
"event-emitter": "^0.3.5",
"lodash": "^4.17.15",
"log-update": "^4.0.0",
"md5": "^2.2.1",
"node-fetch": "^2.6.0",
"node-schedule": "^1.3.2",
Expand Down
Loading

0 comments on commit e0f2887

Please sign in to comment.