-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
599 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.