@icgio/icg-utils 1.9.63 → 1.9.65
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/index.js +1 -0
- package/lib/influxdb.js +4 -4
- package/lib/market-history-buffer.js +72 -0
- package/package.json +1 -1
package/index.js
CHANGED
|
@@ -9,6 +9,7 @@ module.exports = {
|
|
|
9
9
|
logger: require('./lib/logger.js'),
|
|
10
10
|
observable: require('./lib/observable.js'),
|
|
11
11
|
ohlcv: require('./lib/ohlcv'),
|
|
12
|
+
market_history_buffer: require('./lib/market-history-buffer.js'),
|
|
12
13
|
rate_limiter: require('./lib/rate-limiter.js'),
|
|
13
14
|
ordered_dict: require('./lib/ordered-dict.js').ordered_dict,
|
|
14
15
|
sorted_book: require('./lib/sorted-book.js'),
|
package/lib/influxdb.js
CHANGED
|
@@ -28,10 +28,6 @@ class Influx {
|
|
|
28
28
|
}
|
|
29
29
|
async query(query_string, cb) {
|
|
30
30
|
const query_api = this.influx_db.getQueryApi(this.org)
|
|
31
|
-
let result = await query_api.queryRaw(query_string)
|
|
32
|
-
// Split by empty lines into table blocks — each InfluxDB table may have
|
|
33
|
-
// different columns (different tag sets), so parse each with its own headers
|
|
34
|
-
const tables = result.split(/\r?\n\s*\r?\n/).filter((b) => b.trim())
|
|
35
31
|
const parse_table = (csv) =>
|
|
36
32
|
new Promise((resolve, reject) => {
|
|
37
33
|
parse(csv, { columns: true, comment: '#', skip_empty_lines: true, relax_column_count: true }, (err, res) => {
|
|
@@ -40,6 +36,10 @@ class Influx {
|
|
|
40
36
|
})
|
|
41
37
|
})
|
|
42
38
|
try {
|
|
39
|
+
let result = await query_api.queryRaw(query_string)
|
|
40
|
+
// Split by empty lines into table blocks — each InfluxDB table may have
|
|
41
|
+
// different columns (different tag sets), so parse each with its own headers
|
|
42
|
+
const tables = result.split(/\r?\n\s*\r?\n/).filter((b) => b.trim())
|
|
43
43
|
const results = await Promise.all(tables.map((t) => parse_table(t)))
|
|
44
44
|
cb({ success: true, body: results.flat() })
|
|
45
45
|
} catch (err) {
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Market history buffer helpers.
|
|
3
|
+
*
|
|
4
|
+
* Replaces the per-trade `unshift + filter` pattern used by exchange
|
|
5
|
+
* adapters (coinbase, bitstamp, hashkeyglobal, bitmart, blofin, biconomy,
|
|
6
|
+
* upbit, coinstore, ...). The old pattern is O(N) per trade because:
|
|
7
|
+
* 1. Array.prototype.unshift shifts every existing element.
|
|
8
|
+
* 2. Array.prototype.filter walks all N elements and allocates a fresh
|
|
9
|
+
* array, plus the per-element `new Date(trade.close_time)` wrapping
|
|
10
|
+
* adds an allocation per element.
|
|
11
|
+
* At hundreds of trades/sec on active pairs (e.g., Coinbase BTCUSD via
|
|
12
|
+
* FIX MD) this dominates CPU and pegs the event loop.
|
|
13
|
+
*
|
|
14
|
+
* Buffer shape expected:
|
|
15
|
+
* market.market_history_dict[pair] // Array<trade>
|
|
16
|
+
* market.market_history_store_time // retention in ms
|
|
17
|
+
* Each trade is `{ ..., close_time: Date|number }`.
|
|
18
|
+
*
|
|
19
|
+
* Storage is chronological (oldest at index 0, newest at the tail) so we
|
|
20
|
+
* can push in O(1) and prune from the head in one splice. Readers get
|
|
21
|
+
* newest-first results to match existing consumer expectations.
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
const PRUNE_INTERVAL_MS = 30 * 1000
|
|
25
|
+
|
|
26
|
+
function record_market_history_trade(market, pair, trade) {
|
|
27
|
+
const arr = market.market_history_dict[pair]
|
|
28
|
+
if (!arr) return
|
|
29
|
+
arr.push(trade)
|
|
30
|
+
maybe_prune(market, pair, arr)
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function record_market_history_trades(market, pair, trades) {
|
|
34
|
+
const arr = market.market_history_dict[pair]
|
|
35
|
+
if (!arr || !trades || !trades.length) return
|
|
36
|
+
for (const trade of trades) arr.push(trade)
|
|
37
|
+
maybe_prune(market, pair, arr)
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
function maybe_prune(market, pair, arr) {
|
|
41
|
+
if (!market._last_history_prune_at) market._last_history_prune_at = {}
|
|
42
|
+
const now = Date.now()
|
|
43
|
+
if (now - (market._last_history_prune_at[pair] || 0) < PRUNE_INTERVAL_MS) return
|
|
44
|
+
market._last_history_prune_at[pair] = now
|
|
45
|
+
const cutoff = now - market.market_history_store_time
|
|
46
|
+
let i = 0
|
|
47
|
+
while (i < arr.length && to_ms(arr[i].close_time) <= cutoff) i++
|
|
48
|
+
if (i > 0) arr.splice(0, i)
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
function read_market_history_since(market, pair, timeframe_in_ms) {
|
|
52
|
+
const arr = market.market_history_dict[pair]
|
|
53
|
+
if (!Array.isArray(arr)) return []
|
|
54
|
+
const cutoff = Date.now() - timeframe_in_ms
|
|
55
|
+
const result = []
|
|
56
|
+
// Walk newest -> oldest. Exchange WS feeds emit trades in venue order per
|
|
57
|
+
// pair, so close_time is near-monotonic and we can break early once we
|
|
58
|
+
// cross the cutoff. Drops a read from O(N=full_buffer) to O(K=window).
|
|
59
|
+
for (let i = arr.length - 1; i >= 0; i--) {
|
|
60
|
+
if (to_ms(arr[i].close_time) <= cutoff) break
|
|
61
|
+
result.push(arr[i])
|
|
62
|
+
}
|
|
63
|
+
return result
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
function to_ms(value) {
|
|
67
|
+
if (typeof value === 'number') return value
|
|
68
|
+
if (value && typeof value.valueOf === 'function') return value.valueOf()
|
|
69
|
+
return Number(value)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
module.exports = { record_market_history_trade, record_market_history_trades, read_market_history_since }
|