forked from coinbase/coinbase-pro-trading-toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
t002_liveOrderbook.ts
75 lines (70 loc) · 4.31 KB
/
t002_liveOrderbook.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/***************************************************************************************************************************
* @license *
* Copyright 2017 Coinbase, Inc. *
* *
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance *
* with the License. You may obtain a copy of the License at *
* *
* http://www.apache.org/licenses/LICENSE-2.0 *
* *
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on *
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the *
* License for the specific language governing permissions and limitations under the License. *
***************************************************************************************************************************/
import * as CBPTT from 'coinbase-pro-trading-toolkit';
import { CoinbaseProFeed } from "coinbase-pro-trading-toolkit/build/src/exchanges";
import { LiveBookConfig, LiveOrderbook, SkippedMessageEvent, TradeMessage } from "coinbase-pro-trading-toolkit/build/src/core";
import { Ticker } from "coinbase-pro-trading-toolkit/build/src/exchanges/PublicExchangeAPI";
import { CumulativePriceLevel } from "coinbase-pro-trading-toolkit/build/src/lib";
const product = 'LTC-USD';
const logger = CBPTT.utils.ConsoleLoggerFactory({ level: 'debug' });
const printOrderbook = CBPTT.utils.printOrderbook;
const printTicker = CBPTT.utils.printTicker;
/*
Simple demo that sets up a live order book and then periodically prints some stats to the console.
*/
let tradeVolume: number = 0;
CBPTT.Factories.CoinbasePro.FeedFactory(logger, [product]).then((feed: CoinbaseProFeed) => {
// Configure the live book object
const config: LiveBookConfig = {
product: product,
logger: logger
};
const book = new LiveOrderbook(config);
book.on('LiveOrderbook.snapshot', () => {
logger.log('info', 'Snapshot received by LiveOrderbook Demo');
setInterval(() => {
console.log(printOrderbook(book, 10));
printOrderbookStats(book);
logger.log('info', `Cumulative trade volume: ${tradeVolume.toFixed(4)}`);
}, 5000);
});
book.on('LiveOrderbook.ticker', (ticker: Ticker) => {
console.log(printTicker(ticker));
});
book.on('LiveOrderbook.trade', (trade: TradeMessage) => {
tradeVolume += +(trade.size);
});
book.on('LiveOrderbook.skippedMessage', (details: SkippedMessageEvent) => {
// On Coinbase Pro, this event should never be emitted, but we put it here for completeness
console.log('SKIPPED MESSAGE', details);
console.log('Reconnecting to feed');
feed.reconnect(0);
});
book.on('end', () => {
console.log('Orderbook closed');
});
book.on('error', (err) => {
console.log('Livebook errored: ', err);
feed.pipe(book);
});
feed.pipe(book);
});
function printOrderbookStats(book: LiveOrderbook) {
console.log(`Number of bids: \t${book.numBids}\tasks: ${book.numAsks}`);
console.log(`Total ${book.baseCurrency} liquidity: \t${book.bidsTotal.toFixed(3)}\tasks: ${book.asksTotal.toFixed(3)}`);
let orders: CumulativePriceLevel[] = book.ordersForValue('buy', 100, false);
console.log(`Cost of buying 100 ${book.baseCurrency}: ${orders[orders.length - 1].cumValue.toFixed(2)} ${book.quoteCurrency}`);
orders = book.ordersForValue('sell', 1000, true);
console.log(`Need to sell ${orders[orders.length - 1].cumSize.toFixed(3)} ${book.baseCurrency} to get 1000 ${book.quoteCurrency}`);
}