tardis-dev 17.1.0 → 17.2.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/computable/booksnapshot.d.ts.map +1 -1
- package/dist/computable/booksnapshot.js +92 -36
- package/dist/computable/booksnapshot.js.map +1 -1
- package/dist/computable/computable.d.ts.map +1 -1
- package/dist/computable/computable.js +3 -1
- package/dist/computable/computable.js.map +1 -1
- package/dist/computable/context.d.ts +4 -0
- package/dist/computable/context.d.ts.map +1 -0
- package/dist/computable/context.js +9 -0
- package/dist/computable/context.js.map +1 -0
- package/dist/consts.d.ts +1 -1
- package/dist/consts.d.ts.map +1 -1
- package/dist/consts.js +1 -0
- package/dist/consts.js.map +1 -1
- package/dist/handy.d.ts.map +1 -1
- package/dist/handy.js +4 -2
- package/dist/handy.js.map +1 -1
- package/dist/instrumentinfo.d.ts +4 -1
- package/dist/instrumentinfo.d.ts.map +1 -1
- package/dist/mappers/okex.d.ts.map +1 -1
- package/dist/mappers/okex.js +26 -14
- package/dist/mappers/okex.js.map +1 -1
- package/dist/orderbook.d.ts.map +1 -1
- package/dist/orderbook.js +6 -13
- package/dist/orderbook.js.map +1 -1
- package/dist/priceleveltree.d.ts +27 -0
- package/dist/priceleveltree.d.ts.map +1 -0
- package/dist/priceleveltree.js +165 -0
- package/dist/priceleveltree.js.map +1 -0
- package/example.js +50 -16
- package/package.json +1 -3
- package/src/computable/booksnapshot.ts +109 -39
- package/src/computable/computable.ts +5 -1
- package/src/computable/context.ts +12 -0
- package/src/consts.ts +1 -0
- package/src/handy.ts +4 -2
- package/src/instrumentinfo.ts +5 -0
- package/src/mappers/okex.ts +39 -18
- package/src/orderbook.ts +9 -15
- package/src/priceleveltree.ts +188 -0
package/example.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import { parseArgs } from 'node:util'
|
|
1
2
|
import {
|
|
3
|
+
init,
|
|
2
4
|
normalizeBookChanges,
|
|
3
5
|
normalizeBookTickers,
|
|
4
6
|
normalizeDerivativeTickers,
|
|
@@ -19,8 +21,14 @@ if (optionsError !== undefined) {
|
|
|
19
21
|
Usage:
|
|
20
22
|
node example.js stream <exchange> <symbol> <channel>
|
|
21
23
|
node example.js replay <exchange> <symbol> <channel> <from> <to>
|
|
22
|
-
|
|
23
|
-
|
|
24
|
+
|
|
25
|
+
Options:
|
|
26
|
+
--normalized use normalized <data-type> instead of native <channel>
|
|
27
|
+
--endpoint <url> override API endpoint, default: https://api.tardis.dev/v1
|
|
28
|
+
local API example: http://127.0.0.1:8787/v1
|
|
29
|
+
--api-key <key> override API key, default: TARDIS_DEV_API_KEY env var
|
|
30
|
+
local API example: TD.LOCAL.DEV.API.KEY
|
|
31
|
+
--limit <n> stop after n messages
|
|
24
32
|
|
|
25
33
|
Examples:
|
|
26
34
|
node example.js stream mexc-futures BTC_USDT push.depth
|
|
@@ -30,6 +38,9 @@ Examples:
|
|
|
30
38
|
process.exit(1)
|
|
31
39
|
}
|
|
32
40
|
|
|
41
|
+
init(options.init)
|
|
42
|
+
|
|
43
|
+
let messagesCount = 0
|
|
33
44
|
for await (const message of createMessageStream(options)) {
|
|
34
45
|
if (message === undefined || message?.type === 'disconnect') {
|
|
35
46
|
console.log({ type: 'disconnect' })
|
|
@@ -37,11 +48,26 @@ for await (const message of createMessageStream(options)) {
|
|
|
37
48
|
}
|
|
38
49
|
|
|
39
50
|
console.log(message)
|
|
51
|
+
messagesCount++
|
|
52
|
+
if (options.limit !== undefined && messagesCount >= options.limit) {
|
|
53
|
+
break
|
|
54
|
+
}
|
|
40
55
|
}
|
|
41
56
|
|
|
42
57
|
function getOptions(args) {
|
|
43
|
-
const
|
|
44
|
-
|
|
58
|
+
const { values, positionals } = parseArgs({
|
|
59
|
+
args,
|
|
60
|
+
options: {
|
|
61
|
+
normalized: { type: 'boolean' },
|
|
62
|
+
endpoint: { type: 'string' },
|
|
63
|
+
'api-key': { type: 'string' },
|
|
64
|
+
limit: { type: 'string' }
|
|
65
|
+
},
|
|
66
|
+
allowPositionals: true
|
|
67
|
+
})
|
|
68
|
+
const [mode, exchange, symbol, channelOrDataType, from, to] = positionals
|
|
69
|
+
const normalized = values.normalized === true
|
|
70
|
+
const apiKey = values['api-key'] ?? process.env.TARDIS_DEV_API_KEY
|
|
45
71
|
|
|
46
72
|
const normalizersByDataType = {
|
|
47
73
|
trade: normalizeTrades,
|
|
@@ -53,15 +79,20 @@ function getOptions(args) {
|
|
|
53
79
|
}
|
|
54
80
|
|
|
55
81
|
return {
|
|
56
|
-
mode
|
|
82
|
+
mode, // 'stream' or 'replay'
|
|
57
83
|
normalized,
|
|
58
|
-
exchange
|
|
59
|
-
|
|
60
|
-
channel: normalized ? undefined :
|
|
61
|
-
dataType: normalized ?
|
|
62
|
-
normalizer: normalized ? normalizersByDataType[
|
|
63
|
-
from
|
|
64
|
-
to
|
|
84
|
+
exchange,
|
|
85
|
+
symbol,
|
|
86
|
+
channel: normalized ? undefined : channelOrDataType,
|
|
87
|
+
dataType: normalized ? channelOrDataType : undefined,
|
|
88
|
+
normalizer: normalized ? normalizersByDataType[channelOrDataType] : undefined,
|
|
89
|
+
from,
|
|
90
|
+
to,
|
|
91
|
+
init: {
|
|
92
|
+
...(values.endpoint === undefined ? {} : { endpoint: values.endpoint }),
|
|
93
|
+
...(apiKey === undefined ? {} : { apiKey })
|
|
94
|
+
},
|
|
95
|
+
limit: values.limit === undefined ? undefined : Number(values.limit)
|
|
65
96
|
}
|
|
66
97
|
}
|
|
67
98
|
|
|
@@ -72,7 +103,7 @@ function getOptionsError(options) {
|
|
|
72
103
|
if (options.exchange === undefined) {
|
|
73
104
|
return 'Missing exchange name.'
|
|
74
105
|
}
|
|
75
|
-
if (options.
|
|
106
|
+
if (options.symbol === undefined) {
|
|
76
107
|
return 'Missing symbol.'
|
|
77
108
|
}
|
|
78
109
|
if (options.normalized && options.dataType === undefined) {
|
|
@@ -87,6 +118,9 @@ function getOptionsError(options) {
|
|
|
87
118
|
if (options.mode === 'replay' && (options.from === undefined || options.to === undefined)) {
|
|
88
119
|
return 'Replay mode requires from and to dates.'
|
|
89
120
|
}
|
|
121
|
+
if (options.limit !== undefined && (!Number.isSafeInteger(options.limit) || options.limit <= 0)) {
|
|
122
|
+
return 'Invalid limit. Expected a positive integer.'
|
|
123
|
+
}
|
|
90
124
|
}
|
|
91
125
|
|
|
92
126
|
function createMessageStream(options) {
|
|
@@ -95,7 +129,7 @@ function createMessageStream(options) {
|
|
|
95
129
|
return streamNormalized(
|
|
96
130
|
{
|
|
97
131
|
exchange: options.exchange,
|
|
98
|
-
symbols: options.
|
|
132
|
+
symbols: [options.symbol],
|
|
99
133
|
timeoutIntervalMS: 20_000,
|
|
100
134
|
withDisconnectMessages: true,
|
|
101
135
|
onError: (error) => console.error(`[${options.exchange}] ${error.message}`)
|
|
@@ -107,7 +141,7 @@ function createMessageStream(options) {
|
|
|
107
141
|
return replayNormalized(
|
|
108
142
|
{
|
|
109
143
|
exchange: options.exchange,
|
|
110
|
-
symbols: options.
|
|
144
|
+
symbols: [options.symbol],
|
|
111
145
|
from: options.from,
|
|
112
146
|
to: options.to,
|
|
113
147
|
withDisconnectMessages: true
|
|
@@ -116,7 +150,7 @@ function createMessageStream(options) {
|
|
|
116
150
|
)
|
|
117
151
|
}
|
|
118
152
|
|
|
119
|
-
const nativeFilters = [{ channel: options.channel, symbols: options.
|
|
153
|
+
const nativeFilters = [{ channel: options.channel, symbols: [options.symbol] }]
|
|
120
154
|
if (options.mode === 'stream') {
|
|
121
155
|
return stream({
|
|
122
156
|
exchange: options.exchange,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "tardis-dev",
|
|
3
|
-
"version": "17.
|
|
3
|
+
"version": "17.2.2",
|
|
4
4
|
"engines": {
|
|
5
5
|
"node": ">=25"
|
|
6
6
|
},
|
|
@@ -68,7 +68,6 @@
|
|
|
68
68
|
],
|
|
69
69
|
"license": "MPL-2.0",
|
|
70
70
|
"dependencies": {
|
|
71
|
-
"bintrees": "^1.0.2",
|
|
72
71
|
"debug": "^4.4.3",
|
|
73
72
|
"follow-redirects": "^1.16.0",
|
|
74
73
|
"https-proxy-agent": "^8.0.0",
|
|
@@ -78,7 +77,6 @@
|
|
|
78
77
|
"ws": "^8.21.1"
|
|
79
78
|
},
|
|
80
79
|
"devDependencies": {
|
|
81
|
-
"@types/bintrees": "^1.0.6",
|
|
82
80
|
"@types/debug": "^4.1.13",
|
|
83
81
|
"@types/follow-redirects": "^1.14.4",
|
|
84
82
|
"@types/jest": "^29.5.14",
|
|
@@ -2,6 +2,7 @@ import { decimalPlaces } from '../handy.ts'
|
|
|
2
2
|
import { OrderBook, OnLevelRemovedCB } from '../orderbook.ts'
|
|
3
3
|
import { BookChange, BookPriceLevel, BookSnapshot, Optional } from '../types.ts'
|
|
4
4
|
import { Computable } from './computable.ts'
|
|
5
|
+
import { ComputableContext, registerContextualFactory } from './context.ts'
|
|
5
6
|
|
|
6
7
|
type BookSnapshotComputableOptions = {
|
|
7
8
|
name?: string
|
|
@@ -12,10 +13,18 @@ type BookSnapshotComputableOptions = {
|
|
|
12
13
|
onCrossedLevelRemoved?: OnLevelRemovedCB
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
export const computeBookSnapshots =
|
|
16
|
-
|
|
17
|
-
() =>
|
|
18
|
-
|
|
16
|
+
export const computeBookSnapshots = (options: BookSnapshotComputableOptions): (() => Computable<BookSnapshot>) => {
|
|
17
|
+
const factory = () => new BookSnapshotComputable(options)
|
|
18
|
+
registerContextualFactory(factory, (context) => new BookSnapshotComputable(options, context))
|
|
19
|
+
return factory
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type OrderBookHandle = {
|
|
23
|
+
orderBook: OrderBook
|
|
24
|
+
isOwner: boolean
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const sharedOrderBooksKey = Symbol('bookSnapshotOrderBooks')
|
|
19
28
|
|
|
20
29
|
const emptyBookLevel = {
|
|
21
30
|
price: undefined,
|
|
@@ -41,26 +50,33 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
41
50
|
|
|
42
51
|
private readonly _type = 'book_snapshot'
|
|
43
52
|
private readonly _orderBook: OrderBook
|
|
53
|
+
private readonly _ownsOrderBook: boolean
|
|
44
54
|
private readonly _depth: number
|
|
45
55
|
private readonly _interval: number
|
|
46
56
|
private readonly _name: string
|
|
47
57
|
private readonly _grouping: number | undefined
|
|
48
58
|
private readonly _groupingDecimalPlaces: number | undefined
|
|
59
|
+
private readonly _removeCrossedLevels: boolean
|
|
49
60
|
|
|
50
61
|
private _lastUpdateTimestamp: Date = new Date(-1)
|
|
51
62
|
private _bids: Optional<BookPriceLevel>[] = []
|
|
52
63
|
private _asks: Optional<BookPriceLevel>[] = []
|
|
64
|
+
private _groupedBidBoundary: number | undefined
|
|
65
|
+
private _groupedAskBoundary: number | undefined
|
|
53
66
|
|
|
54
|
-
constructor(
|
|
67
|
+
constructor(
|
|
68
|
+
{ depth, name, interval, removeCrossedLevels, grouping, onCrossedLevelRemoved }: BookSnapshotComputableOptions,
|
|
69
|
+
context?: ComputableContext
|
|
70
|
+
) {
|
|
55
71
|
this._depth = depth
|
|
56
72
|
this._interval = interval
|
|
57
73
|
this._grouping = grouping
|
|
58
74
|
this._groupingDecimalPlaces = this._grouping ? decimalPlaces(this._grouping) : undefined
|
|
75
|
+
this._removeCrossedLevels = Boolean(removeCrossedLevels)
|
|
59
76
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
})
|
|
77
|
+
const orderBookHandle = getOrderBookHandle(context, removeCrossedLevels, onCrossedLevelRemoved)
|
|
78
|
+
this._orderBook = orderBookHandle.orderBook
|
|
79
|
+
this._ownsOrderBook = orderBookHandle.isOwner
|
|
64
80
|
|
|
65
81
|
// initialize all bids/asks levels to empty ones
|
|
66
82
|
for (let i = 0; i < this._depth; i++) {
|
|
@@ -122,11 +138,18 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
122
138
|
}
|
|
123
139
|
|
|
124
140
|
public _update(bookChange: BookChange) {
|
|
125
|
-
this.
|
|
141
|
+
if (this._ownsOrderBook) {
|
|
142
|
+
this._orderBook.update(bookChange)
|
|
143
|
+
}
|
|
126
144
|
|
|
127
145
|
if (this._grouping !== undefined) {
|
|
128
|
-
|
|
129
|
-
this.
|
|
146
|
+
const updateBothGroupedSides = bookChange.isSnapshot || this._removeCrossedLevels
|
|
147
|
+
if (updateBothGroupedSides || this._groupedSideMayHaveChanged(bookChange.bids, this._groupedBidBoundary, false)) {
|
|
148
|
+
this._groupedBidBoundary = this._updateSideGrouped(this._orderBook.bids(), this._bids, false)
|
|
149
|
+
}
|
|
150
|
+
if (updateBothGroupedSides || this._groupedSideMayHaveChanged(bookChange.asks, this._groupedAskBoundary, true)) {
|
|
151
|
+
this._groupedAskBoundary = this._updateSideGrouped(this._orderBook.asks(), this._asks, true)
|
|
152
|
+
}
|
|
130
153
|
} else {
|
|
131
154
|
this._updatedNotGrouped()
|
|
132
155
|
}
|
|
@@ -134,6 +157,20 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
134
157
|
this._lastUpdateTimestamp = bookChange.timestamp
|
|
135
158
|
}
|
|
136
159
|
|
|
160
|
+
private _groupedSideMayHaveChanged(changes: BookPriceLevel[], boundary: number | undefined, roundUp: boolean) {
|
|
161
|
+
if (changes.length === 0) return false
|
|
162
|
+
if (boundary === undefined) return true
|
|
163
|
+
|
|
164
|
+
for (const level of changes) {
|
|
165
|
+
const groupedPrice = this._getGroupedPrice(level.price, roundUp)
|
|
166
|
+
if (roundUp ? groupedPrice <= boundary : groupedPrice >= boundary) {
|
|
167
|
+
return true
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return false
|
|
172
|
+
}
|
|
173
|
+
|
|
137
174
|
private _updatedNotGrouped() {
|
|
138
175
|
const bidsIterable = this._orderBook.bids()
|
|
139
176
|
const asksIterable = this._orderBook.asks()
|
|
@@ -143,7 +180,7 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
143
180
|
const newBid = bidLevelResult.done ? emptyBookLevel : bidLevelResult.value
|
|
144
181
|
|
|
145
182
|
if (levelsChanged(this._bids[i], newBid)) {
|
|
146
|
-
this._bids[i] = {
|
|
183
|
+
this._bids[i] = { price: newBid.price, amount: newBid.amount }
|
|
147
184
|
this._bookChanged = true
|
|
148
185
|
}
|
|
149
186
|
|
|
@@ -151,61 +188,56 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
151
188
|
const newAsk = askLevelResult.done ? emptyBookLevel : askLevelResult.value
|
|
152
189
|
|
|
153
190
|
if (levelsChanged(this._asks[i], newAsk)) {
|
|
154
|
-
this._asks[i] = {
|
|
191
|
+
this._asks[i] = { price: newAsk.price, amount: newAsk.amount }
|
|
155
192
|
this._bookChanged = true
|
|
156
193
|
}
|
|
157
194
|
}
|
|
158
195
|
}
|
|
159
196
|
|
|
160
|
-
private
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return (pricePow - remainder * pow) / pow
|
|
167
|
-
}
|
|
197
|
+
private _getGroupedPrice(price: number, roundUp: boolean) {
|
|
198
|
+
if (this._groupingDecimalPlaces === 0) {
|
|
199
|
+
const remainder = price % this._grouping!
|
|
200
|
+
return price - remainder + (roundUp && remainder > 0 ? this._grouping! : 0)
|
|
201
|
+
}
|
|
168
202
|
|
|
169
|
-
private _getGroupedPriceForAsks = (price: number) => {
|
|
170
203
|
const pow = Math.pow(10, this._groupingDecimalPlaces!)
|
|
171
204
|
const pricePow = price * pow
|
|
172
205
|
const groupPow = this._grouping! * pow
|
|
173
206
|
const remainder = (pricePow % groupPow) / pow
|
|
174
207
|
|
|
175
|
-
return (pricePow - remainder * pow + (remainder > 0 ? groupPow : 0)) / pow
|
|
208
|
+
return (pricePow - remainder * pow + (roundUp && remainder > 0 ? groupPow : 0)) / pow
|
|
176
209
|
}
|
|
177
210
|
|
|
178
211
|
private _updateSideGrouped(
|
|
179
212
|
newLevels: IterableIterator<BookPriceLevel>,
|
|
180
213
|
existingGroupedLevels: Optional<BookPriceLevel>[],
|
|
181
|
-
|
|
214
|
+
roundUp: boolean
|
|
182
215
|
) {
|
|
183
216
|
let currentGroupedPrice: number | undefined = undefined
|
|
184
217
|
let aggAmount = 0
|
|
185
218
|
let currentDepth = 0
|
|
186
219
|
|
|
187
220
|
for (const notGroupedLevel of newLevels) {
|
|
188
|
-
const groupedPrice =
|
|
221
|
+
const groupedPrice = this._getGroupedPrice(notGroupedLevel.price, roundUp)
|
|
189
222
|
|
|
190
223
|
if (currentGroupedPrice == undefined) {
|
|
191
224
|
currentGroupedPrice = groupedPrice
|
|
192
225
|
}
|
|
193
226
|
|
|
194
227
|
if (currentGroupedPrice != groupedPrice) {
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
existingGroupedLevels[currentDepth] = groupedLevel
|
|
228
|
+
const existingGroupedLevel = existingGroupedLevels[currentDepth]
|
|
229
|
+
if (existingGroupedLevel.amount !== aggAmount || existingGroupedLevel.price !== currentGroupedPrice) {
|
|
230
|
+
existingGroupedLevels[currentDepth] = {
|
|
231
|
+
price: currentGroupedPrice,
|
|
232
|
+
amount: aggAmount
|
|
233
|
+
}
|
|
202
234
|
this._bookChanged = true
|
|
203
235
|
}
|
|
204
236
|
|
|
205
237
|
currentDepth++
|
|
206
238
|
|
|
207
239
|
if (currentDepth === this._depth) {
|
|
208
|
-
|
|
240
|
+
return currentGroupedPrice
|
|
209
241
|
}
|
|
210
242
|
|
|
211
243
|
currentGroupedPrice = groupedPrice
|
|
@@ -216,16 +248,25 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
216
248
|
}
|
|
217
249
|
|
|
218
250
|
if (currentDepth < this._depth && aggAmount > 0) {
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
251
|
+
const existingGroupedLevel = existingGroupedLevels[currentDepth]
|
|
252
|
+
if (existingGroupedLevel.amount !== aggAmount || existingGroupedLevel.price !== currentGroupedPrice) {
|
|
253
|
+
existingGroupedLevels[currentDepth] = {
|
|
254
|
+
price: currentGroupedPrice,
|
|
255
|
+
amount: aggAmount
|
|
256
|
+
}
|
|
257
|
+
this._bookChanged = true
|
|
222
258
|
}
|
|
259
|
+
currentDepth++
|
|
260
|
+
}
|
|
223
261
|
|
|
224
|
-
|
|
225
|
-
|
|
262
|
+
for (let i = currentDepth; i < this._depth; i++) {
|
|
263
|
+
if (levelsChanged(existingGroupedLevels[i], emptyBookLevel)) {
|
|
264
|
+
existingGroupedLevels[i] = emptyBookLevel
|
|
226
265
|
this._bookChanged = true
|
|
227
266
|
}
|
|
228
267
|
}
|
|
268
|
+
|
|
269
|
+
return currentDepth === this._depth ? currentGroupedPrice : undefined
|
|
229
270
|
}
|
|
230
271
|
|
|
231
272
|
public _getSnapshot(bookChange: BookChange) {
|
|
@@ -252,3 +293,32 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
252
293
|
return Math.floor(timestamp.valueOf() / this._interval)
|
|
253
294
|
}
|
|
254
295
|
}
|
|
296
|
+
|
|
297
|
+
function getOrderBookHandle(
|
|
298
|
+
context: ComputableContext | undefined,
|
|
299
|
+
removeCrossedLevels: boolean | undefined,
|
|
300
|
+
onCrossedLevelRemoved: OnLevelRemovedCB | undefined
|
|
301
|
+
): OrderBookHandle {
|
|
302
|
+
if (context === undefined || onCrossedLevelRemoved !== undefined) {
|
|
303
|
+
return {
|
|
304
|
+
orderBook: new OrderBook({ removeCrossedLevels, onCrossedLevelRemoved }),
|
|
305
|
+
isOwner: true
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
let sharedOrderBooks = context.get(sharedOrderBooksKey) as Map<boolean, OrderBook> | undefined
|
|
310
|
+
if (sharedOrderBooks === undefined) {
|
|
311
|
+
sharedOrderBooks = new Map()
|
|
312
|
+
context.set(sharedOrderBooksKey, sharedOrderBooks)
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
const key = removeCrossedLevels === true
|
|
316
|
+
const sharedOrderBook = sharedOrderBooks.get(key)
|
|
317
|
+
if (sharedOrderBook !== undefined) {
|
|
318
|
+
return { orderBook: sharedOrderBook, isOwner: false }
|
|
319
|
+
}
|
|
320
|
+
|
|
321
|
+
const orderBook = new OrderBook({ removeCrossedLevels })
|
|
322
|
+
sharedOrderBooks.set(key, orderBook)
|
|
323
|
+
return { orderBook, isOwner: true }
|
|
324
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Disconnect, Exchange, NormalizedData } from '../types.ts'
|
|
2
|
+
import { ComputableContext, createWithContext } from './context.ts'
|
|
2
3
|
|
|
3
4
|
export type Computable<T extends NormalizedData> = {
|
|
4
5
|
readonly sourceDataTypes: string[]
|
|
@@ -70,7 +71,10 @@ class Computables {
|
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
if (this._computables[exchange]![id] === undefined) {
|
|
73
|
-
|
|
74
|
+
const context: ComputableContext = new Map()
|
|
75
|
+
this._computables[exchange]![id] = createComputablesMap(
|
|
76
|
+
this._computablesFactories.map((factory) => createWithContext(factory, context))
|
|
77
|
+
)
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
return this._computables[exchange]![id]!
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type ComputableContext = Map<symbol, unknown>
|
|
2
|
+
|
|
3
|
+
const contextualFactories = new WeakMap<Function, (context: ComputableContext) => unknown>()
|
|
4
|
+
|
|
5
|
+
export function registerContextualFactory<T>(factory: () => T, contextualFactory: (context: ComputableContext) => T) {
|
|
6
|
+
contextualFactories.set(factory, contextualFactory)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function createWithContext<T>(factory: () => T, context: ComputableContext): T {
|
|
10
|
+
const contextualFactory = contextualFactories.get(factory)
|
|
11
|
+
return contextualFactory === undefined ? factory() : (contextualFactory(context) as T)
|
|
12
|
+
}
|
package/src/consts.ts
CHANGED
package/src/handy.ts
CHANGED
|
@@ -640,8 +640,10 @@ async function _downloadFile(requestOptions: RequestOptions, url: string, downlo
|
|
|
640
640
|
// based on https://github.com/nodejs/node/issues/28172 - only reliable way to consume response stream and avoiding all the 'gotchas'
|
|
641
641
|
let responseHeaders: Record<string, string> = {}
|
|
642
642
|
await new Promise<void>((resolve, reject) => {
|
|
643
|
-
const
|
|
644
|
-
|
|
643
|
+
const protocol = new URL(url).protocol
|
|
644
|
+
const requestClient = protocol === 'http:' ? http : https
|
|
645
|
+
const req = requestClient
|
|
646
|
+
.get(url, { ...requestOptions, ...(protocol === 'http:' ? { agent: undefined } : {}) }, (res) => {
|
|
645
647
|
const { statusCode } = res
|
|
646
648
|
if (statusCode !== 200) {
|
|
647
649
|
// read the error response text and throw it as an HttpError
|
package/src/instrumentinfo.ts
CHANGED
|
@@ -109,6 +109,8 @@ export type ContractType =
|
|
|
109
109
|
| 'interest_rate_swap'
|
|
110
110
|
| 'repo'
|
|
111
111
|
| 'index'
|
|
112
|
+
| 'linear_xperp'
|
|
113
|
+
| 'inverse_xperp'
|
|
112
114
|
|
|
113
115
|
export type UnderlyingType = 'native' | 'equity' | 'commodity' | 'fixed_income' | 'fx' | 'index' | 'pre_market'
|
|
114
116
|
|
|
@@ -179,13 +181,16 @@ export interface InstrumentInfo {
|
|
|
179
181
|
amountIncrement?: number
|
|
180
182
|
contractMultiplier?: number
|
|
181
183
|
minTradeAmount?: number
|
|
184
|
+
minNotional?: number
|
|
182
185
|
makerFee?: number
|
|
183
186
|
takerFee?: number
|
|
184
187
|
quanto?: boolean
|
|
185
188
|
inverse?: boolean
|
|
186
189
|
settlementCurrency?: string
|
|
187
190
|
underlyingIndex?: string
|
|
191
|
+
underlyingType?: UnderlyingType
|
|
188
192
|
contractType?: ContractType
|
|
193
|
+
expiry?: string
|
|
189
194
|
quoteCurrency?: string
|
|
190
195
|
type?: string
|
|
191
196
|
}[]
|
package/src/mappers/okex.ts
CHANGED
|
@@ -9,6 +9,7 @@ const OKEX_V5_API_SWITCH_DATE = new Date('2021-12-23T00:00:00.000Z')
|
|
|
9
9
|
const OKEX_V5_TBT_BOOK_TICKER_RELEASE_DATE = new Date('2022-05-06T00:00:00.000Z')
|
|
10
10
|
const OKX_PUBLIC_BOOKS_CHANNEL_START_DATE = new Date('2023-02-25T00:00:00.000Z')
|
|
11
11
|
const OKX_PUBLIC_BOOKS_CHANNEL_END_DATE = new Date('2023-03-09T00:00:00.000Z')
|
|
12
|
+
const OKEX_USDC_INDEX_SWITCH_DATE = new Date('2023-04-10T08:40:00.000Z')
|
|
12
13
|
const OKEX_PUBLIC_BOOKS_SWITCH_DATE = new Date('2026-05-21T00:00:00.000Z')
|
|
13
14
|
const OKEX_SPOT_DEPTH_L2_TBT_SWITCH_DATE = new Date('2020-04-10T00:00:00.000Z')
|
|
14
15
|
const OKEX_FUTURES_DEPTH_L2_TBT_SWITCH_DATE = new Date('2019-12-05T00:00:00.000Z')
|
|
@@ -33,7 +34,11 @@ export const okexMappers = exchangeMappers({
|
|
|
33
34
|
}),
|
|
34
35
|
derivativeTickers: mapper([
|
|
35
36
|
{ until: OKEX_V5_API_SWITCH_DATE, use: () => new OkexDerivativeTickerMapper('okex-futures') },
|
|
36
|
-
{
|
|
37
|
+
{
|
|
38
|
+
until: OKEX_USDC_INDEX_SWITCH_DATE,
|
|
39
|
+
use: () => new OkexV5DerivativeTickerMapper('okex-futures', { useLegacyUSDCIndex: true })
|
|
40
|
+
},
|
|
41
|
+
{ use: () => new OkexV5DerivativeTickerMapper('okex-futures', { useLegacyUSDCIndex: false }) }
|
|
37
42
|
]),
|
|
38
43
|
liquidations: mapper([
|
|
39
44
|
{ until: OKEX_V5_API_SWITCH_DATE, use: () => new OkexLiquidationsMapper('okex-futures', { market: 'futures' }) },
|
|
@@ -46,7 +51,11 @@ export const okexMappers = exchangeMappers({
|
|
|
46
51
|
bookChanges: okexBookChangesMapper('okex-swap', { market: 'swap', depthL2TbtSwitchDate: OKEX_SWAP_DEPTH_L2_TBT_SWITCH_DATE }),
|
|
47
52
|
derivativeTickers: mapper([
|
|
48
53
|
{ until: OKEX_V5_API_SWITCH_DATE, use: () => new OkexDerivativeTickerMapper('okex-swap') },
|
|
49
|
-
{
|
|
54
|
+
{
|
|
55
|
+
until: OKEX_USDC_INDEX_SWITCH_DATE,
|
|
56
|
+
use: () => new OkexV5DerivativeTickerMapper('okex-swap', { useLegacyUSDCIndex: true })
|
|
57
|
+
},
|
|
58
|
+
{ use: () => new OkexV5DerivativeTickerMapper('okex-swap', { useLegacyUSDCIndex: false }) }
|
|
50
59
|
]),
|
|
51
60
|
liquidations: mapper([
|
|
52
61
|
{ until: OKEX_V5_API_SWITCH_DATE, use: () => new OkexLiquidationsMapper('okex-swap', { market: 'swap' }) },
|
|
@@ -366,11 +375,14 @@ class OkexV5DerivativeTickerMapper implements Mapper<'okex-futures' | 'okex-swap
|
|
|
366
375
|
private readonly pendingTickerInfoHelper = new PendingTickerInfoHelper()
|
|
367
376
|
private readonly _indexPrices = new Map<string, number>()
|
|
368
377
|
|
|
369
|
-
private _futuresChannels = ['tickers', 'open-interest', 'mark-price', 'index-tickers'] as const
|
|
378
|
+
private _futuresChannels = ['tickers', 'open-interest', 'mark-price', 'index-tickers', 'funding-rate'] as const
|
|
370
379
|
|
|
371
380
|
private _swapChannels = ['tickers', 'open-interest', 'mark-price', 'index-tickers', 'funding-rate'] as const
|
|
372
381
|
|
|
373
|
-
constructor(
|
|
382
|
+
constructor(
|
|
383
|
+
private readonly _exchange: Exchange,
|
|
384
|
+
private readonly _options: { useLegacyUSDCIndex: boolean }
|
|
385
|
+
) {}
|
|
374
386
|
|
|
375
387
|
canHandle(message: any) {
|
|
376
388
|
const channels = this._exchange === 'okex-futures' ? this._futuresChannels : this._swapChannels
|
|
@@ -388,14 +400,7 @@ class OkexV5DerivativeTickerMapper implements Mapper<'okex-futures' | 'okex-swap
|
|
|
388
400
|
const channels = this._exchange === 'okex-futures' ? this._futuresChannels : this._swapChannels
|
|
389
401
|
return channels.map((channel) => {
|
|
390
402
|
if (channel === 'index-tickers') {
|
|
391
|
-
const indexes =
|
|
392
|
-
symbols !== undefined
|
|
393
|
-
? symbols.map((s) => {
|
|
394
|
-
const symbolParts = s.split('-')
|
|
395
|
-
const quotePart = symbolParts[1] === 'USDC' ? 'USD' : symbolParts[1]
|
|
396
|
-
return `${symbolParts[0]}-${quotePart}`
|
|
397
|
-
})
|
|
398
|
-
: undefined
|
|
403
|
+
const indexes = symbols !== undefined ? symbols.map((symbol) => this._getIndexSymbol(symbol)) : undefined
|
|
399
404
|
|
|
400
405
|
return {
|
|
401
406
|
channel,
|
|
@@ -429,11 +434,7 @@ class OkexV5DerivativeTickerMapper implements Mapper<'okex-futures' | 'okex-swap
|
|
|
429
434
|
|
|
430
435
|
for (const dataMessage of message.data) {
|
|
431
436
|
const pendingTickerInfo = this.pendingTickerInfoHelper.getPendingTickerInfo(dataMessage.instId, this._exchange)
|
|
432
|
-
const
|
|
433
|
-
const quotePart = symbolParts[1] === 'USDC' ? 'USD' : symbolParts[1]
|
|
434
|
-
|
|
435
|
-
const indexSymbol = `${symbolParts[0]}-${quotePart}`
|
|
436
|
-
|
|
437
|
+
const indexSymbol = this._getIndexSymbol(dataMessage.instId)
|
|
437
438
|
const indexPrice = this._indexPrices.get(indexSymbol)
|
|
438
439
|
|
|
439
440
|
if (indexPrice !== undefined) {
|
|
@@ -473,6 +474,10 @@ class OkexV5DerivativeTickerMapper implements Mapper<'okex-futures' | 'okex-swap
|
|
|
473
474
|
if (fundingRateMessage.nextFundingRate !== undefined && fundingRateMessage.nextFundingRate !== '') {
|
|
474
475
|
pendingTickerInfo.updatePredictedFundingRate(Number(fundingRateMessage.nextFundingRate))
|
|
475
476
|
}
|
|
477
|
+
|
|
478
|
+
if (fundingRateMessage.ts !== undefined) {
|
|
479
|
+
pendingTickerInfo.updateTimestamp(new Date(Number(fundingRateMessage.ts)))
|
|
480
|
+
}
|
|
476
481
|
}
|
|
477
482
|
|
|
478
483
|
if (message.arg.channel === 'tickers') {
|
|
@@ -491,6 +496,13 @@ class OkexV5DerivativeTickerMapper implements Mapper<'okex-futures' | 'okex-swap
|
|
|
491
496
|
}
|
|
492
497
|
}
|
|
493
498
|
}
|
|
499
|
+
|
|
500
|
+
private _getIndexSymbol(instrumentId: string) {
|
|
501
|
+
const symbolParts = instrumentId.split('-')
|
|
502
|
+
const quoteCurrency = symbolParts[1].split('_')[0]
|
|
503
|
+
const indexQuoteCurrency = quoteCurrency === 'USDC' && this._options.useLegacyUSDCIndex ? 'USD' : quoteCurrency
|
|
504
|
+
return `${symbolParts[0]}-${indexQuoteCurrency}`
|
|
505
|
+
}
|
|
494
506
|
}
|
|
495
507
|
|
|
496
508
|
class OkexV5LiquidationsMapper implements Mapper<OKEX_EXCHANGES, Liquidation> {
|
|
@@ -808,7 +820,16 @@ type OkexV5IndexTickerMessage = {
|
|
|
808
820
|
|
|
809
821
|
type OkexV5FundingRateMessage = {
|
|
810
822
|
arg: { channel: 'funding-rate'; instId: string }
|
|
811
|
-
data: [
|
|
823
|
+
data: [
|
|
824
|
+
{
|
|
825
|
+
fundingRate: '0.00048105' | undefined
|
|
826
|
+
fundingTime: '1640131200000'
|
|
827
|
+
instId: string
|
|
828
|
+
instType: 'SWAP' | 'FUTURES'
|
|
829
|
+
nextFundingRate: ''
|
|
830
|
+
ts?: '1640131200000'
|
|
831
|
+
}
|
|
832
|
+
]
|
|
812
833
|
}
|
|
813
834
|
|
|
814
835
|
type OkexV5LiquidationMessage = {
|
package/src/orderbook.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import { BookChange, BookPriceLevel, Writeable } from './types.ts'
|
|
1
|
+
import { PriceLevelTree } from './priceleveltree.ts'
|
|
2
|
+
import { BookChange, BookPriceLevel } from './types.ts'
|
|
4
3
|
|
|
5
4
|
export type OnLevelRemovedCB = (
|
|
6
5
|
bookChange: BookChange,
|
|
@@ -11,8 +10,8 @@ export type OnLevelRemovedCB = (
|
|
|
11
10
|
) => void
|
|
12
11
|
|
|
13
12
|
export class OrderBook {
|
|
14
|
-
private readonly _bids = new
|
|
15
|
-
private readonly _asks = new
|
|
13
|
+
private readonly _bids = new PriceLevelTree(-1)
|
|
14
|
+
private readonly _asks = new PriceLevelTree(1)
|
|
16
15
|
private readonly _removeCrossedLevels: boolean | undefined
|
|
17
16
|
private readonly _onCrossedLevelRemoved: OnLevelRemovedCB | undefined
|
|
18
17
|
|
|
@@ -147,18 +146,13 @@ export class OrderBook {
|
|
|
147
146
|
}
|
|
148
147
|
}
|
|
149
148
|
|
|
150
|
-
function applyPriceLevelChanges(tree:
|
|
149
|
+
function applyPriceLevelChanges(tree: PriceLevelTree, priceLevelChanges: BookPriceLevel[]) {
|
|
151
150
|
for (const priceLevel of priceLevelChanges) {
|
|
152
|
-
|
|
153
|
-
const nodeExists = node !== null
|
|
154
|
-
const levelShouldBeRemoved = priceLevel.amount === 0
|
|
155
|
-
|
|
156
|
-
if (nodeExists && levelShouldBeRemoved) {
|
|
151
|
+
if (priceLevel.amount === 0) {
|
|
157
152
|
tree.remove(priceLevel)
|
|
158
|
-
|
|
159
|
-
node.amount = priceLevel.amount
|
|
160
|
-
} else if (levelShouldBeRemoved === false) {
|
|
161
|
-
tree.insert({ ...priceLevel })
|
|
153
|
+
continue
|
|
162
154
|
}
|
|
155
|
+
|
|
156
|
+
tree.upsert(priceLevel)
|
|
163
157
|
}
|
|
164
158
|
}
|