@yuants/vendor-okx 0.16.9 → 0.17.1

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.
Files changed (90) hide show
  1. package/dist/account.js +198 -0
  2. package/dist/account.js.map +1 -0
  3. package/dist/api.js +463 -0
  4. package/dist/api.js.map +1 -0
  5. package/dist/cli.js +3 -0
  6. package/dist/cli.js.map +1 -0
  7. package/dist/cluster.js +80 -0
  8. package/dist/cluster.js.map +1 -0
  9. package/dist/extension.js +89 -0
  10. package/dist/extension.js.map +1 -0
  11. package/dist/index.js +7 -0
  12. package/dist/index.js.map +1 -0
  13. package/dist/interest_rate.js +133 -0
  14. package/dist/interest_rate.js.map +1 -0
  15. package/dist/legacy_index.js +554 -0
  16. package/dist/legacy_index.js.map +1 -0
  17. package/dist/logger.js +91 -0
  18. package/dist/logger.js.map +1 -0
  19. package/dist/ohlc.js +171 -0
  20. package/dist/ohlc.js.map +1 -0
  21. package/dist/order.js +96 -0
  22. package/dist/order.js.map +1 -0
  23. package/dist/product.js +85 -0
  24. package/dist/product.js.map +1 -0
  25. package/dist/quote.js +58 -0
  26. package/dist/quote.js.map +1 -0
  27. package/dist/vendor-okx.d.ts +1 -0
  28. package/dist/websocket.js +80 -0
  29. package/dist/websocket.js.map +1 -0
  30. package/lib/account.d.ts +102 -0
  31. package/lib/account.d.ts.map +1 -0
  32. package/lib/account.js +201 -0
  33. package/lib/account.js.map +1 -0
  34. package/lib/api.d.ts +1401 -0
  35. package/lib/api.d.ts.map +1 -0
  36. package/lib/api.js +470 -0
  37. package/lib/api.js.map +1 -0
  38. package/lib/cli.d.ts +3 -0
  39. package/lib/cli.d.ts.map +1 -0
  40. package/lib/cli.js +5 -0
  41. package/lib/cli.js.map +1 -0
  42. package/lib/cluster.d.ts +2 -0
  43. package/lib/cluster.d.ts.map +1 -0
  44. package/lib/cluster.js +108 -0
  45. package/lib/cluster.js.map +1 -0
  46. package/lib/extension.d.ts +4 -0
  47. package/lib/extension.d.ts.map +1 -0
  48. package/lib/extension.js +91 -0
  49. package/lib/extension.js.map +1 -0
  50. package/lib/index.d.ts +7 -0
  51. package/lib/index.d.ts.map +1 -0
  52. package/lib/index.js +9 -0
  53. package/lib/index.js.map +1 -0
  54. package/lib/interest_rate.d.ts +2 -0
  55. package/lib/interest_rate.d.ts.map +1 -0
  56. package/lib/interest_rate.js +135 -0
  57. package/lib/interest_rate.js.map +1 -0
  58. package/lib/legacy_index.d.ts +2 -0
  59. package/lib/legacy_index.d.ts.map +1 -0
  60. package/lib/legacy_index.js +556 -0
  61. package/lib/legacy_index.js.map +1 -0
  62. package/lib/logger.d.ts +21 -0
  63. package/lib/logger.d.ts.map +1 -0
  64. package/lib/logger.js +98 -0
  65. package/lib/logger.js.map +1 -0
  66. package/lib/ohlc.d.ts +2 -0
  67. package/lib/ohlc.d.ts.map +1 -0
  68. package/lib/ohlc.js +173 -0
  69. package/lib/ohlc.js.map +1 -0
  70. package/lib/order.d.ts +4 -0
  71. package/lib/order.d.ts.map +1 -0
  72. package/lib/order.js +99 -0
  73. package/lib/order.js.map +1 -0
  74. package/lib/product.d.ts +6 -0
  75. package/lib/product.d.ts.map +1 -0
  76. package/lib/product.js +88 -0
  77. package/lib/product.js.map +1 -0
  78. package/lib/quote.d.ts +42 -0
  79. package/lib/quote.d.ts.map +1 -0
  80. package/lib/quote.js +61 -0
  81. package/lib/quote.js.map +1 -0
  82. package/lib/websocket.d.ts +14 -0
  83. package/lib/websocket.d.ts.map +1 -0
  84. package/lib/websocket.js +83 -0
  85. package/lib/websocket.js.map +1 -0
  86. package/package.json +9 -4
  87. package/temp/image-tag +1 -0
  88. package/temp/package-deps.json +42 -0
  89. package/temp/vendor-okx.api.json +177 -0
  90. package/temp/vendor-okx.api.md +9 -0
package/dist/logger.js ADDED
@@ -0,0 +1,91 @@
1
+ import cluster from 'cluster';
2
+ /**
3
+ * 集群模式下的日志管理器
4
+ * 在worker进程中将日志发送到主进程,在主进程中直接使用console
5
+ */
6
+ class ClusterLogger {
7
+ constructor() {
8
+ this.isWorker = !cluster.isPrimary;
9
+ }
10
+ sendToMaster(level, message, ...args) {
11
+ if (this.isWorker && process.send) {
12
+ const formattedMessage = args.length > 0
13
+ ? `${message} ${args
14
+ .map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg)))
15
+ .join(' ')}`
16
+ : message;
17
+ process.send({
18
+ type: 'log',
19
+ level,
20
+ message: `[PID:${process.pid}] ${formattedMessage}`,
21
+ pid: process.pid,
22
+ label: process.env.WORKER_LABEL || 'unknown',
23
+ timestamp: Date.now(),
24
+ });
25
+ }
26
+ else {
27
+ // 在主进程中直接输出
28
+ const formattedMessage = args.length > 0
29
+ ? `${message} ${args
30
+ .map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg)))
31
+ .join(' ')}`
32
+ : message;
33
+ const logMessage = `[PID:${process.pid}] ${formattedMessage}`;
34
+ // 控制台输出
35
+ switch (level) {
36
+ case 'error':
37
+ console.error(logMessage);
38
+ break;
39
+ case 'warn':
40
+ console.warn(logMessage);
41
+ break;
42
+ case 'debug':
43
+ console.debug(logMessage);
44
+ break;
45
+ default:
46
+ console.log(logMessage);
47
+ }
48
+ }
49
+ }
50
+ info(message, ...args) {
51
+ this.sendToMaster('info', message, ...args);
52
+ }
53
+ warn(message, ...args) {
54
+ this.sendToMaster('warn', message, ...args);
55
+ }
56
+ error(message, ...args) {
57
+ this.sendToMaster('error', message, ...args);
58
+ }
59
+ debug(message, ...args) {
60
+ this.sendToMaster('debug', message, ...args);
61
+ }
62
+ log(message, ...args) {
63
+ this.info(message, ...args);
64
+ }
65
+ }
66
+ // 默认配置的logger实例
67
+ export const logger = new ClusterLogger();
68
+ // 猛踩油门
69
+ export const overrideConsole = () => {
70
+ if (!cluster.isPrimary) {
71
+ const originalConsole = {
72
+ log: console.log,
73
+ info: console.info,
74
+ warn: console.warn,
75
+ error: console.error,
76
+ };
77
+ console.log = (...args) => logger.info(args.join(' '));
78
+ console.info = (...args) => logger.info(args.join(' '));
79
+ console.warn = (...args) => logger.warn(args.join(' '));
80
+ console.error = (...args) => logger.error(args.join(' '));
81
+ // 提供恢复方法
82
+ return () => {
83
+ console.log = originalConsole.log;
84
+ console.info = originalConsole.info;
85
+ console.warn = originalConsole.warn;
86
+ console.error = originalConsole.error;
87
+ };
88
+ }
89
+ return () => { }; // 主进程中返回空函数
90
+ };
91
+ //# sourceMappingURL=logger.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAAA,OAAO,OAAO,MAAM,SAAS,CAAC;AAQ9B;;;GAGG;AACH,MAAM,aAAa;IAAnB;QACU,aAAQ,GAAG,CAAC,OAAO,CAAC,SAAS,CAAC;IAkExC,CAAC;IAhES,YAAY,CAAC,KAAe,EAAE,OAAe,EAAE,GAAG,IAAW;QACnE,IAAI,IAAI,CAAC,QAAQ,IAAI,OAAO,CAAC,IAAI,EAAE;YACjC,MAAM,gBAAgB,GACpB,IAAI,CAAC,MAAM,GAAG,CAAC;gBACb,CAAC,CAAC,GAAG,OAAO,IAAI,IAAI;qBACf,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,CAAC,OAAO,GAAG,KAAK,QAAQ,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;qBAC3E,IAAI,CAAC,GAAG,CAAC,EAAE;gBAChB,CAAC,CAAC,OAAO,CAAC;YAEd,OAAO,CAAC,IAAI,CAAC;gBACX,IAAI,EAAE,KAAK;gBACX,KAAK;gBACL,OAAO,EAAE,QAAQ,OAAO,CAAC,GAAG,KAAK,gBAAgB,EAAE;gBACnD,GAAG,EAAE,OAAO,CAAC,GAAG;gBAChB,KAAK,EAAE,OAAO,CAAC,GAAG,CAAC,YAAY,IAAI,SAAS;gBAC5C,SAAS,EAAE,IAAI,CAAC,GA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,GAAG,eAAe,CAAC,GAAG,CAAC;YAClC,OAAO,CAAC,IAAI,GAAG,eAAe,CAAC,IAAI,CAAC;YACpC,OAAO,CAAC,IAAI,GAAG,eAAe,CAAC,IAAI,CAAC;YACpC,OAAO,CAAC,KAAK,GAAG,eAAe,CAAC,KAAK,CAAC;QACxC,CAAC,CAAC;KACH;IACD,OAAO,GAAG,EAAE,GAAE,CAAC,CAAC,CAAC,YAAY;AAC/B,CAAC,CAAC","sourcesContent":["import cluster from 'cluster';\nimport { formatTime } from '@yuants/utils';\n\n/**\n * 日志级别\n */\nexport type LogLevel = 'info' | 'warn' | 'error' | 'debug';\n\n/**\n * 集群模式下的日志管理器\n * 在worker进程中将日志发送到主进程,在主进程中直接使用console\n */\nclass ClusterLogger {\n private isWorker = !cluster.isPrimary;\n\n private sendToMaster(level: LogLevel, message: string, ...args: any[]) {\n if (this.isWorker && process.send) {\n const formattedMessage =\n args.length > 0\n ? `${message} ${args\n .map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg)))\n .join(' ')}`\n : message;\n\n process.send({\n type: 'log',\n level,\n message: `[PID:${process.pid}] ${formattedMessage}`,\n pid: process.pid,\n label: process.env.WORKER_LABEL || 'unknown',\n timestamp: Date.now(),\n });\n } else {\n // 在主进程中直接输出\n const formattedMessage =\n args.length > 0\n ? `${message} ${args\n .map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg)))\n .join(' ')}`\n : message;\n\n const logMessage = `[PID:${process.pid}] ${formattedMessage}`;\n\n // 控制台输出\n switch (level) {\n case 'error':\n console.error(logMessage);\n break;\n case 'warn':\n console.warn(logMessage);\n break;\n case 'debug':\n console.debug(logMessage);\n break;\n default:\n console.log(logMessage);\n }\n }\n }\n\n info(message: string, ...args: any[]) {\n this.sendToMaster('info', message, ...args);\n }\n\n warn(message: string, ...args: any[]) {\n this.sendToMaster('warn', message, ...args);\n }\n\n error(message: string, ...args: any[]) {\n this.sendToMaster('error', message, ...args);\n }\n\n debug(message: string, ...args: any[]) {\n this.sendToMaster('debug', message, ...args);\n }\n\n log(message: string, ...args: any[]) {\n this.info(message, ...args);\n }\n}\n\n// 默认配置的logger实例\nexport const logger = new ClusterLogger();\n\n// 猛踩油门\nexport const overrideConsole = () => {\n if (!cluster.isPrimary) {\n const originalConsole = {\n log: console.log,\n info: console.info,\n warn: console.warn,\n error: console.error,\n };\n\n console.log = (...args: any[]) => logger.info(args.join(' '));\n console.info = (...args: any[]) => logger.info(args.join(' '));\n console.warn = (...args: any[]) => logger.warn(args.join(' '));\n console.error = (...args: any[]) => logger.error(args.join(' '));\n\n // 提供恢复方法\n return () => {\n console.log = originalConsole.log;\n console.info = originalConsole.info;\n console.warn = originalConsole.warn;\n console.error = originalConsole.error;\n };\n }\n return () => {}; // 主进程中返回空函数\n};\n"]}
package/dist/ohlc.js ADDED
@@ -0,0 +1,171 @@
1
+ var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
2
+ var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
3
+ if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
4
+ var g = generator.apply(thisArg, _arguments || []), i, q = [];
5
+ return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
6
+ function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
7
+ function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
8
+ function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
9
+ function fulfill(value) { resume("next", value); }
10
+ function reject(value) { resume("throw", value); }
11
+ function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
12
+ };
13
+ import { createSeriesProvider } from '@yuants/data-series';
14
+ import { Terminal } from '@yuants/protocol';
15
+ import { convertDurationToOffset, decodePath, encodePath, formatTime } from '@yuants/utils';
16
+ import { firstValueFrom, Observable, timer } from 'rxjs';
17
+ import { client } from './api';
18
+ import { okxBusinessWsClient } from './websocket';
19
+ import { writeToSQL } from '@yuants/sql';
20
+ // 时间粒度,默认值1m
21
+ // 如 [1m/3m/5m/15m/30m/1H/2H/4H]
22
+ // 香港时间开盘价k线:[6H/12H/1D/1W/1M]
23
+ // UTC时间开盘价k线:[6Hutc/12Hutc/1Dutc/1Wutc/1Mutc]
24
+ const DURATION_TO_OKX_BAR_TYPE = {
25
+ PT1M: '1m',
26
+ PT3M: '3m',
27
+ PT5M: '5m',
28
+ PT15M: '15m',
29
+ PT30M: '30m',
30
+ PT1H: '1H',
31
+ PT2H: '2H',
32
+ PT4H: '4H',
33
+ PT6H: '6H',
34
+ PT12H: '12H',
35
+ P1D: '1D',
36
+ P1W: '1W',
37
+ P1M: '1M',
38
+ };
39
+ const DURATION_TO_OKX_CANDLE_TYPE = {
40
+ PT1M: 'mark-price-candle1m',
41
+ PT3M: 'mark-price-candle3m',
42
+ PT5M: 'mark-price-candle5m',
43
+ PT15M: 'mark-price-candle15m',
44
+ PT30M: 'mark-price-candle30m',
45
+ PT1H: 'mark-price-candle1H',
46
+ PT2H: 'mark-price-candle2H',
47
+ PT4H: 'mark-price-candle4H',
48
+ PT6H: 'mark-price-candle6H',
49
+ PT12H: 'mark-price-candle12H',
50
+ P1D: 'mark-price-candle1D',
51
+ P1W: 'mark-price-candle1W',
52
+ P1M: 'mark-price-candle1M',
53
+ };
54
+ createSeriesProvider(Terminal.fromNodeEnv(), {
55
+ tableName: 'ohlc',
56
+ series_id_prefix_parts: ['OKX'],
57
+ reversed: true,
58
+ serviceOptions: { concurrent: 1 },
59
+ queryFn: function ({ series_id, ended_at }) {
60
+ return __asyncGenerator(this, arguments, function* () {
61
+ const [datasource_id, product_id, duration] = decodePath(series_id);
62
+ const offset = convertDurationToOffset(duration);
63
+ if (!datasource_id) {
64
+ throw 'datasource_id is required';
65
+ }
66
+ if (!product_id) {
67
+ throw 'product_id is required';
68
+ }
69
+ if (!offset) {
70
+ throw 'duration is invalid';
71
+ }
72
+ const [instType, instId] = decodePath(product_id);
73
+ if (!instId) {
74
+ throw `invalid product_id: ${product_id}`;
75
+ }
76
+ const bar = DURATION_TO_OKX_BAR_TYPE[duration];
77
+ if (!bar) {
78
+ throw `unsupported duration: ${duration}`;
79
+ }
80
+ let currentStartTime = ended_at;
81
+ while (true) {
82
+ // 向前翻页,时间降序,不含 after 时间点
83
+ const res = yield __await(client.getHistoryCandles({
84
+ instId,
85
+ bar,
86
+ after: `${currentStartTime}`,
87
+ limit: '100',
88
+ }));
89
+ if (res.code !== '0') {
90
+ throw `API failed: ${res.code} ${res.msg}`;
91
+ }
92
+ if (res.data.length === 0)
93
+ break;
94
+ currentStartTime = +res.data[res.data.length - 1][0];
95
+ const data = res.data.map((x) => ({
96
+ series_id,
97
+ datasource_id,
98
+ product_id,
99
+ duration,
100
+ created_at: formatTime(+x[0]),
101
+ closed_at: formatTime(+x[0] + offset),
102
+ open: x[1],
103
+ high: x[2],
104
+ low: x[3],
105
+ close: x[4],
106
+ volume: x[5],
107
+ open_interest: '0',
108
+ }));
109
+ yield yield __await(data);
110
+ yield __await(firstValueFrom(timer(1000)));
111
+ }
112
+ });
113
+ },
114
+ });
115
+ Terminal.fromNodeEnv().channel.publishChannel('ohlc', { pattern: `^OKX/` }, (series_id) => {
116
+ // const [] = decodePath(args)
117
+ const [datasource_id, product_id, duration] = decodePath(series_id);
118
+ const [, instId] = decodePath(product_id);
119
+ const offset = convertDurationToOffset(duration);
120
+ if (!datasource_id) {
121
+ throw 'datasource_id is required';
122
+ }
123
+ if (!product_id) {
124
+ throw 'product_id is required';
125
+ }
126
+ if (!offset) {
127
+ throw 'duration is invalid';
128
+ }
129
+ const candleType = DURATION_TO_OKX_CANDLE_TYPE[duration];
130
+ console.info(formatTime(Date.now()), `subscribe`, series_id);
131
+ return new Observable((subscriber) => {
132
+ console.info(formatTime(Date.now()), `subscribe`, candleType, instId);
133
+ okxBusinessWsClient.subscribe(candleType, instId, async (data) => {
134
+ const created_at = Number(data[0]);
135
+ const closed_at = created_at + offset;
136
+ const open = data[1];
137
+ const high = data[2];
138
+ const low = data[3];
139
+ const close = data[4];
140
+ if (isNaN(closed_at)) {
141
+ return;
142
+ }
143
+ console.info(formatTime(Date.now()), `insertData`, data);
144
+ const cancelData = {
145
+ closed_at: formatTime(closed_at),
146
+ created_at: formatTime(created_at),
147
+ open,
148
+ high,
149
+ low,
150
+ close,
151
+ series_id,
152
+ datasource_id,
153
+ duration,
154
+ product_id,
155
+ volume: '0',
156
+ open_interest: '0',
157
+ };
158
+ subscriber.next(cancelData);
159
+ });
160
+ return () => {
161
+ okxBusinessWsClient.unsubscribe(candleType, instId);
162
+ };
163
+ }).pipe(writeToSQL({
164
+ tableName: 'ohlc',
165
+ conflictKeys: ['created_at', 'series_id'],
166
+ writeInterval: 1000,
167
+ terminal: Terminal.fromNodeEnv(),
168
+ keyFn: (x) => encodePath(x.created_at, x.series_id),
169
+ }));
170
+ });
171
+ //# sourceMappingURL=ohlc.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ohlc.js","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,uBAAuB,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC5F,OAAO,EAAE,cAAc,EAAE,UAAU,EAAM,KAAK,EAAE,MAAM,MAAM,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,mBAAmB,EAAE,MAAM,aAAa,CAAC;AAClD,OAAO,EAA2C,UAAU,EAAE,MAAM,aAAa,CAAC;AAElF,aAAa;AACb,gCAAgC;AAChC,8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CAAC,YAAY,EAAE,WAAW,CAAC;QACzC,aAAa,EAAE,IAAI;QACnB,QAAQ,EAAE,QAAQ,CAAC,WAAW,EAAE;QAChC,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,SAAS,CAAC;KACpD,CAAC,CACH,CAAC;AACJ,CAAC,CAAC,CAAC","sourcesContent":["import { IOHLC } from '@yuants/data-ohlc';\nimport { createSeriesProvider } from '@yuants/data-series';\nimport { Terminal } from '@yuants/protocol';\nimport { convertDurationToOffset, decodePath, encodePath, formatTime } from '@yuants/utils';\nimport { firstValueFrom, Observable, of, timer } from 'rxjs';\nimport { client } from './api';\nimport { okxBusinessWsClient } from './websocket';\nimport { buildInsertManyIntoTableSQL, requestSQL, writeToSQL } from '@yuants/sql';\n\n// 时间粒度,默认值1m\n// 如 [1m/3m/5m/15m/30m/1H/2H/4H]\n// 香港时间开盘价k线:[6H/12H/1D/1W/1M]\n// UTC时间开盘价k线:[6Hutc/12Hutc/1Dutc/1Wutc/1Mutc]\n\nconst DURATION_TO_OKX_BAR_TYPE: Record<string, string> = {\n PT1M: '1m',\n PT3M: '3m',\n PT5M: '5m',\n PT15M: '15m',\n PT30M: '30m',\n\n PT1H: '1H',\n PT2H: '2H',\n PT4H: '4H',\n PT6H: '6H',\n PT12H: '12H',\n\n P1D: '1D',\n P1W: '1W',\n P1M: '1M',\n};\n\nconst DURATION_TO_OKX_CANDLE_TYPE: Record<string, string> = {\n PT1M: 'mark-price-candle1m',\n PT3M: 'mark-price-candle3m',\n PT5M: 'mark-price-candle5m',\n PT15M: 'mark-price-candle15m',\n PT30M: 'mark-price-candle30m',\n\n PT1H: 'mark-price-candle1H',\n PT2H: 'mark-price-candle2H',\n PT4H: 'mark-price-candle4H',\n PT6H: 'mark-price-candle6H',\n PT12H: 'mark-price-candle12H',\n\n P1D: 'mark-price-candle1D',\n P1W: 'mark-price-candle1W',\n P1M: 'mark-price-candle1M',\n};\n\ncreateSeriesProvider<IOHLC>(Terminal.fromNodeEnv(), {\n tableName: 'ohlc',\n series_id_prefix_parts: ['OKX'],\n reversed: true,\n serviceOptions: { concurrent: 1 },\n queryFn: async function* ({ series_id, ended_at }) {\n const [datasource_id, product_id, duration] = decodePath(series_id);\n const offset = convertDurationToOffset(duration);\n if (!datasource_id) {\n throw 'datasource_id is required';\n }\n if (!product_id) {\n throw 'product_id is required';\n }\n if (!offset) {\n throw 'duration is invalid';\n }\n const [instType, instId] = decodePath(product_id);\n if (!instId) {\n throw `invalid product_id: ${product_id}`;\n }\n\n const bar = DURATION_TO_OKX_BAR_TYPE[duration];\n if (!bar) {\n throw `unsupported duration: ${duration}`;\n }\n\n let currentStartTime = ended_at;\n\n while (true) {\n // 向前翻页,时间降序,不含 after 时间点\n const res = await client.getHistoryCandles({\n instId,\n bar,\n after: `${currentStartTime}`,\n limit: '100',\n });\n if (res.code !== '0') {\n throw `API failed: ${res.code} ${res.msg}`;\n }\n if (res.data.length === 0) break;\n currentStartTime = +res.data[res.data.length - 1][0];\n const data = res.data.map(\n (x): IOHLC => ({\n series_id,\n datasource_id,\n product_id,\n duration,\n created_at: formatTime(+x[0]),\n closed_at: formatTime(+x[0] + offset),\n open: x[1],\n high: x[2],\n low: x[3],\n close: x[4],\n volume: x[5],\n open_interest: '0',\n }),\n );\n yield data;\n await firstValueFrom(timer(1000));\n }\n },\n});\n\nTerminal.fromNodeEnv().channel.publishChannel('ohlc', { pattern: `^OKX/` }, (series_id) => {\n // const [] = decodePath(args)\n const [datasource_id, product_id, duration] = decodePath(series_id);\n const [, instId] = decodePath(product_id);\n const offset = convertDurationToOffset(duration);\n if (!datasource_id) {\n throw 'datasource_id is required';\n }\n if (!product_id) {\n throw 'product_id is required';\n }\n if (!offset) {\n throw 'duration is invalid';\n }\n const candleType = DURATION_TO_OKX_CANDLE_TYPE[duration];\n console.info(formatTime(Date.now()), `subscribe`, series_id);\n return new Observable<IOHLC>((subscriber) => {\n console.info(formatTime(Date.now()), `subscribe`, candleType, instId);\n okxBusinessWsClient.subscribe(candleType, instId, async (data: string[]) => {\n const created_at = Number(data[0]);\n const closed_at = created_at + offset;\n const open = data[1];\n const high = data[2];\n const low = data[3];\n const close = data[4];\n if (isNaN(closed_at)) {\n return;\n }\n console.info(formatTime(Date.now()), `insertData`, data);\n const cancelData: IOHLC = {\n closed_at: formatTime(closed_at),\n created_at: formatTime(created_at),\n open,\n high,\n low,\n close,\n series_id,\n datasource_id,\n duration,\n product_id,\n volume: '0',\n open_interest: '0',\n };\n subscriber.next(cancelData);\n });\n return () => {\n okxBusinessWsClient.unsubscribe(candleType, instId);\n };\n }).pipe(\n writeToSQL({\n tableName: 'ohlc',\n conflictKeys: ['created_at', 'series_id'],\n writeInterval: 1000,\n terminal: Terminal.fromNodeEnv(),\n keyFn: (x) => encodePath(x.created_at, x.series_id),\n }),\n );\n});\n"]}
package/dist/order.js ADDED
@@ -0,0 +1,96 @@
1
+ import { Terminal } from '@yuants/protocol';
2
+ import { writeToSQL } from '@yuants/sql';
3
+ import { encodePath, formatTime } from '@yuants/utils';
4
+ import { defer, firstValueFrom, from, map, merge, mergeMap, repeat, retry, shareReplay, Subject, tap, } from 'rxjs';
5
+ import { accountUid$ } from './account';
6
+ import { client } from './api';
7
+ export const order$ = new Subject();
8
+ order$
9
+ .pipe(
10
+ //
11
+ // mergeMap((x) => x),
12
+ writeToSQL({
13
+ terminal: Terminal.fromNodeEnv(),
14
+ tableName: '"order"',
15
+ writeInterval: 1000,
16
+ keyFn: (order) => encodePath(order.account_id, order.order_id),
17
+ columns: [
18
+ 'order_id',
19
+ 'account_id',
20
+ 'product_id',
21
+ 'position_id',
22
+ 'order_type',
23
+ 'order_direction',
24
+ 'volume',
25
+ 'submit_at',
26
+ 'updated_at',
27
+ 'filled_at',
28
+ 'price',
29
+ 'traded_volume',
30
+ 'traded_price',
31
+ 'order_status',
32
+ 'comment',
33
+ 'profit_correction',
34
+ 'real_profit',
35
+ 'inferred_base_currency_price',
36
+ ],
37
+ conflictKeys: ['account_id', 'order_id'],
38
+ }))
39
+ .subscribe();
40
+ const makeOrder = (x, account_id) => {
41
+ const order_type = x.ordType === 'market' ? 'MARKET' : x.ordType === 'limit' ? 'LIMIT' : 'UNKNOWN';
42
+ const order_direction = x.side === 'buy'
43
+ ? x.posSide === 'long'
44
+ ? 'OPEN_LONG'
45
+ : 'CLOSE_SHORT'
46
+ : x.posSide === 'short'
47
+ ? 'OPEN_SHORT'
48
+ : 'CLOSE_LONG';
49
+ const order_status = ['live', 'partially_filled'].includes(x.state)
50
+ ? 'ACCEPTED'
51
+ : x.state === 'filled'
52
+ ? 'TRADED'
53
+ : 'CANCELLED';
54
+ return {
55
+ order_id: x.clOrdId !== '' ? x.clOrdId : x.ordId,
56
+ account_id,
57
+ product_id: encodePath(x.instType, x.instId),
58
+ submit_at: +x.cTime,
59
+ filled_at: +x.fillTime,
60
+ created_at: formatTime(+x.cTime),
61
+ updated_at: formatTime(+x.uTime),
62
+ order_type,
63
+ order_direction,
64
+ volume: +x.sz,
65
+ traded_volume: +x.accFillSz,
66
+ price: +x.px,
67
+ traded_price: +x.avgPx,
68
+ order_status,
69
+ };
70
+ };
71
+ (async () => {
72
+ const uid = await firstValueFrom(accountUid$);
73
+ const TRADING_ACCOUNT_ID = `okx/${uid}/trading`;
74
+ const FUNDING_ACCOUNT_ID = `okx/${uid}/funding/USDT`;
75
+ const EARNING_ACCOUNT_ID = `okx/${uid}/earning/USDT`;
76
+ const swapHistoryOrders = defer(() => client.getTradeOrdersHistory({ instType: 'SWAP' })).pipe(repeat({ delay: 1000 }), retry({ delay: 1000 }), shareReplay(1));
77
+ const swapPendingOrders = defer(() => client.getTradeOrdersPending({ instType: 'SWAP' })).pipe(repeat({ delay: 1000 }), retry({ delay: 1000 }), shareReplay(1));
78
+ const ordersFromHistoryOrder$ = swapHistoryOrders.pipe(
79
+ //
80
+ mergeMap((x) => from(x.data || []).pipe(
81
+ //
82
+ map((x) => makeOrder(x, TRADING_ACCOUNT_ID)))));
83
+ const ordersFromPendingOrder$ = swapPendingOrders.pipe(
84
+ //
85
+ mergeMap((x) => from(x.data || []).pipe(
86
+ //
87
+ map((x) => makeOrder(x, TRADING_ACCOUNT_ID)))));
88
+ merge(ordersFromHistoryOrder$, ordersFromPendingOrder$)
89
+ .pipe(
90
+ //
91
+ tap((x) => {
92
+ order$.next(x);
93
+ }))
94
+ .subscribe();
95
+ })();
96
+ //# sourceMappingURL=order.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"order.js","sourceRoot":"","sources":["../src/order.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EACL,KAAK,EACL,cAAc,EACd,IAAI,EACJ,GAAG,EACH,KAAK,EACL,QAAQ,EACR,MAAM,EACN,KAAK,EACL,WAAW,EACX,OAAO,EACP,GAAG,GACJ,MAAM,MAAM,CAAC;AACd,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE/B,MAAM,CAAC,MAAM,MAAM,GAAG,IAAI,OAAO,EAAU,CAAC;AAE5C,MAAM;KACH,IAAI;AACH,EAAE;AACF,sBAAsB;AACtB,UAAU,CAAC;IACT,QAAQ,EAAE,QAAQ,CAAC,WAAW,EAAE;IAChC,SAAS,EAAE,SAAS;IACpB,aAAa,EAAE,IAAI;IACnB,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,QAAQ,CAAC;IAC9D,OAAO,EAAE;QACP,UAAU;QACV,YAAY;QACZ,YAAY;QACZ,aAAa;QACb,YAAY;QACZ,iBAAiB;QACjB,QAAQ;QACR,W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,EAAE;IACF,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CACb,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,IAAI;IACrB,EAAE;IACF,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,SAAS,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAC7C,CACF,CACF,CAAC;IAEF,MAAM,uBAAuB,GAAG,iBAAiB,CAAC,IAAI;IACpD,EAAE;IACF,QAAQ,CAAC,CAAC,CAAC,EAAE,EAAE,CACb,IAAI,CAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,IAAI;IACrB,EAAE;IACF,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,SAAS,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAC7C,CACF,CACF,CAAC;IAEF,KAAK,CAAC,uBAAuB,EAAE,uBAAuB,CAAC;SACpD,IAAI;IACH,EAAE;IACF,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;QACR,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC;IACjB,CAAC,CAAC,CACH;SACA,SAAS,EAAE,CAAC;AACjB,CAAC,CAAC,EAAE,CAAC","sourcesContent":["import { IOrder } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { writeToSQL } from '@yuants/sql';\nimport { encodePath, formatTime } from '@yuants/utils';\nimport {\n defer,\n firstValueFrom,\n from,\n map,\n merge,\n mergeMap,\n repeat,\n retry,\n shareReplay,\n Subject,\n tap,\n} from 'rxjs';\nimport { accountUid$ } from './account';\nimport { client } from './api';\n\nexport const order$ = new Subject<IOrder>();\n\norder$\n .pipe(\n //\n // mergeMap((x) => x),\n writeToSQL({\n terminal: Terminal.fromNodeEnv(),\n tableName: '\"order\"',\n writeInterval: 1000,\n keyFn: (order) => encodePath(order.account_id, order.order_id),\n columns: [\n 'order_id',\n 'account_id',\n 'product_id',\n 'position_id',\n 'order_type',\n 'order_direction',\n 'volume',\n 'submit_at',\n 'updated_at',\n 'filled_at',\n 'price',\n 'traded_volume',\n 'traded_price',\n 'order_status',\n 'comment',\n 'profit_correction',\n 'real_profit',\n 'inferred_base_currency_price',\n ],\n conflictKeys: ['account_id', 'order_id'],\n }),\n )\n .subscribe();\n\nconst makeOrder = (\n x: {\n ordType: string;\n side: string;\n posSide: string;\n instType: string;\n instId: string;\n cTime: string;\n uTime: string;\n fillTime: string;\n sz: string;\n accFillSz: string;\n px: string;\n avgPx: string;\n state: string;\n clOrdId: string;\n ordId: string;\n },\n account_id: string,\n): IOrder => {\n const order_type = x.ordType === 'market' ? 'MARKET' : x.ordType === 'limit' ? 'LIMIT' : 'UNKNOWN';\n const order_direction =\n x.side === 'buy'\n ? x.posSide === 'long'\n ? 'OPEN_LONG'\n : 'CLOSE_SHORT'\n : x.posSide === 'short'\n ? 'OPEN_SHORT'\n : 'CLOSE_LONG';\n const order_status = ['live', 'partially_filled'].includes(x.state)\n ? 'ACCEPTED'\n : x.state === 'filled'\n ? 'TRADED'\n : 'CANCELLED';\n return {\n order_id: x.clOrdId !== '' ? x.clOrdId : x.ordId,\n account_id,\n product_id: encodePath(x.instType, x.instId),\n submit_at: +x.cTime,\n filled_at: +x.fillTime,\n created_at: formatTime(+x.cTime),\n updated_at: formatTime(+x.uTime),\n order_type,\n order_direction,\n volume: +x.sz,\n traded_volume: +x.accFillSz,\n price: +x.px,\n traded_price: +x.avgPx,\n order_status,\n };\n};\n\n(async () => {\n const uid = await firstValueFrom(accountUid$);\n\n const TRADING_ACCOUNT_ID = `okx/${uid}/trading`;\n const FUNDING_ACCOUNT_ID = `okx/${uid}/funding/USDT`;\n const EARNING_ACCOUNT_ID = `okx/${uid}/earning/USDT`;\n\n const swapHistoryOrders = defer(() => client.getTradeOrdersHistory({ instType: 'SWAP' })).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n );\n\n const swapPendingOrders = defer(() => client.getTradeOrdersPending({ instType: 'SWAP' })).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 1000 }),\n shareReplay(1),\n );\n\n const ordersFromHistoryOrder$ = swapHistoryOrders.pipe(\n //\n mergeMap((x) =>\n from(x.data || []).pipe(\n //\n map((x) => makeOrder(x, TRADING_ACCOUNT_ID)),\n ),\n ),\n );\n\n const ordersFromPendingOrder$ = swapPendingOrders.pipe(\n //\n mergeMap((x) =>\n from(x.data || []).pipe(\n //\n map((x) => makeOrder(x, TRADING_ACCOUNT_ID)),\n ),\n ),\n );\n\n merge(ordersFromHistoryOrder$, ordersFromPendingOrder$)\n .pipe(\n //\n tap((x) => {\n order$.next(x);\n }),\n )\n .subscribe();\n})();\n"]}
@@ -0,0 +1,85 @@
1
+ import { encodePath } from '@yuants/utils';
2
+ import { Terminal } from '@yuants/protocol';
3
+ import { createSQLWriter } from '@yuants/sql';
4
+ import { defer, filter, from, map, mergeMap, repeat, retry, shareReplay, Subject, tap, toArray } from 'rxjs';
5
+ import { client } from './api';
6
+ const product$ = new Subject();
7
+ const swapInstruments$ = defer(() => client.getInstruments({ instType: 'SWAP' })).pipe(repeat({ delay: 3600000 }), retry({ delay: 10000 }), shareReplay(1));
8
+ export const usdtSwapProducts$ = swapInstruments$.pipe(mergeMap((x) => from(x.data || []).pipe(filter((x) => x.ctType === 'linear' && x.settleCcy === 'USDT'),
9
+ // ISSUE: 可能有 level = '' 的错误情况 (SPK-USDT and RESOLV-USDT at 2025-06-19 08 UTC)
10
+ filter((x) => +x.lever > 0), map((x) => ({
11
+ datasource_id: 'OKX',
12
+ product_id: encodePath(x.instType, x.instId),
13
+ name: `${x.ctValCcy}-${x.settleCcy}-PERP`,
14
+ base_currency: x.ctValCcy,
15
+ quote_currency: x.settleCcy,
16
+ value_scale: +x.ctVal,
17
+ volume_step: +x.lotSz,
18
+ price_step: +x.tickSz,
19
+ margin_rate: 1 / +x.lever,
20
+ value_scale_unit: '',
21
+ value_based_cost: 0,
22
+ volume_based_cost: 0,
23
+ max_position: 0,
24
+ max_volume: 0,
25
+ allow_long: true,
26
+ allow_short: true,
27
+ market_id: 'OKX',
28
+ no_interest_rate: false,
29
+ })), tap((x) => product$.next(x)), toArray())), shareReplay(1));
30
+ usdtSwapProducts$.subscribe();
31
+ const marginInstruments$ = defer(() => client.getInstruments({ instType: 'MARGIN' })).pipe(repeat({ delay: 3600000 }), retry({ delay: 10000 }), shareReplay(1));
32
+ export const marginProducts$ = marginInstruments$.pipe(mergeMap((x) => from(x.data || []).pipe(
33
+ // ISSUE: 可能有 level = '' 的错误情况 (SPK-USDT and RESOLV-USDT at 2025-06-19 08 UTC)
34
+ filter((x) => +x.lever > 0), map((x) => ({
35
+ datasource_id: 'OKX',
36
+ product_id: encodePath(x.instType, x.instId),
37
+ base_currency: x.baseCcy,
38
+ quote_currency: x.quoteCcy,
39
+ value_scale: 1,
40
+ volume_step: +x.lotSz,
41
+ price_step: +x.tickSz,
42
+ margin_rate: 1 / +x.lever,
43
+ name: `${x.baseCcy}-${x.quoteCcy}-MARGIN`,
44
+ value_scale_unit: '',
45
+ value_based_cost: 0,
46
+ volume_based_cost: 0,
47
+ max_position: 0,
48
+ max_volume: 0,
49
+ allow_long: true,
50
+ allow_short: true,
51
+ market_id: 'OKX',
52
+ no_interest_rate: false,
53
+ })), tap((x) => product$.next(x)), toArray())), shareReplay(1));
54
+ marginProducts$.subscribe();
55
+ export const mapProductIdToMarginProduct$ = marginProducts$.pipe(map((x) => new Map(x.map((x) => [x.product_id, x])), shareReplay(1)));
56
+ defer(() => client.getInstruments({ instType: 'SPOT' }))
57
+ .pipe(repeat({ delay: 3600000 }), retry({ delay: 10000 }), mergeMap((x) => from(x.data || []).pipe(map((x) => ({
58
+ datasource_id: 'OKX',
59
+ product_id: encodePath(x.instType, x.instId),
60
+ base_currency: x.baseCcy,
61
+ quote_currency: x.quoteCcy,
62
+ value_scale: 1,
63
+ volume_step: +x.lotSz,
64
+ price_step: +x.tickSz,
65
+ margin_rate: 1,
66
+ name: `${x.baseCcy}-${x.quoteCcy}-SPOT`,
67
+ value_scale_unit: '',
68
+ value_based_cost: 0,
69
+ volume_based_cost: 0,
70
+ max_position: 0,
71
+ max_volume: 0,
72
+ allow_long: true,
73
+ allow_short: true,
74
+ market_id: 'OKX',
75
+ no_interest_rate: true,
76
+ })), tap((x) => product$.next(x)), toArray())))
77
+ .subscribe();
78
+ createSQLWriter(Terminal.fromNodeEnv(), {
79
+ data$: product$,
80
+ tableName: 'product',
81
+ writeInterval: 1000,
82
+ conflictKeys: ['datasource_id', 'product_id'],
83
+ });
84
+ export const mapProductIdToUsdtSwapProduct$ = usdtSwapProducts$.pipe(map((x) => new Map(x.map((x) => [x.product_id, x]))), shareReplay(1));
85
+ //# sourceMappingURL=product.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"product.js","sourceRoot":"","sources":["../src/product.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAC9C,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC7G,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE/B,MAAM,QAAQ,GAAG,IAAI,OAAO,EAAY,CAAC;AAEzC,MAAM,gBAAgB,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,cAAc,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,IAAI,CACpF,MAAM,CAAC,EAAE,KAAK,EAAE,OAAQ,EAAE,CAAC,EAC3B,KAAK,CAAC,EAAE,KAAK,EAAE,KAAM,EAAE,CAAC,EACxB,WAAW,CAAC,C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AC,CAAC,CAAC,KAAK;IACrB,UAAU,EAAE,CAAC,CAAC,CAAC,MAAM;IACrB,WAAW,EAAE,CAAC;IACd,IAAI,EAAE,GAAG,CAAC,CAAC,OAAO,IAAI,CAAC,CAAC,QAAQ,OAAO;IACvC,gBAAgB,EAAE,EAAE;IACpB,gBAAgB,EAAE,CAAC;IACnB,iBAAiB,EAAE,CAAC;IACpB,YAAY,EAAE,CAAC;IACf,UAAU,EAAE,CAAC;IACb,UAAU,EAAE,IAAI;IAChB,WAAW,EAAE,IAAI;IACjB,SAAS,EAAE,KAAK;IAChB,gBAAgB,EAAE,IAAI;CACvB,CAAC,CACH,EACD,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,EAC5B,OAAO,EAAE,CACV,CACF,CACF;KACA,SAAS,EAAE,CAAC;AAEf,eAAe,CAAW,QAAQ,CAAC,WAAW,EAAE,EAAE;IAChD,KAAK,EAAE,QAAQ;IACf,SAAS,EAAE,SAAS;IACpB,aAAa,EAAE,IAAK;IACpB,YAAY,EAAE,CAAC,eAAe,EAAE,YAAY,CAAC;CAC9C,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,8BAA8B,GAAG,iBAAiB,CAAC,IAAI,CAClE,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EACpD,WAAW,CAAC,CAAC,CAAC,CACf,CAAC","sourcesContent":["import { encodePath } from '@yuants/utils';\nimport { IProduct } from '@yuants/data-product';\nimport { Terminal } from '@yuants/protocol';\nimport { createSQLWriter } from '@yuants/sql';\nimport { defer, filter, from, map, mergeMap, repeat, retry, shareReplay, Subject, tap, toArray } from 'rxjs';\nimport { client } from './api';\n\nconst product$ = new Subject<IProduct>();\n\nconst swapInstruments$ = defer(() => client.getInstruments({ instType: 'SWAP' })).pipe(\n repeat({ delay: 3600_000 }),\n retry({ delay: 10_000 }),\n shareReplay(1),\n);\n\nexport const usdtSwapProducts$ = swapInstruments$.pipe(\n mergeMap((x) =>\n from(x.data || []).pipe(\n filter((x) => x.ctType === 'linear' && x.settleCcy === 'USDT'),\n // ISSUE: 可能有 level = '' 的错误情况 (SPK-USDT and RESOLV-USDT at 2025-06-19 08 UTC)\n filter((x) => +x.lever > 0),\n map(\n (x): IProduct => ({\n datasource_id: 'OKX',\n product_id: encodePath(x.instType, x.instId),\n name: `${x.ctValCcy}-${x.settleCcy}-PERP`,\n base_currency: x.ctValCcy,\n quote_currency: x.settleCcy,\n value_scale: +x.ctVal,\n volume_step: +x.lotSz,\n price_step: +x.tickSz,\n margin_rate: 1 / +x.lever,\n value_scale_unit: '',\n value_based_cost: 0,\n volume_based_cost: 0,\n max_position: 0,\n max_volume: 0,\n allow_long: true,\n allow_short: true,\n market_id: 'OKX',\n no_interest_rate: false,\n }),\n ),\n tap((x) => product$.next(x)),\n toArray(),\n ),\n ),\n shareReplay(1),\n);\n\nusdtSwapProducts$.subscribe();\nconst marginInstruments$ = defer(() => client.getInstruments({ instType: 'MARGIN' })).pipe(\n repeat({ delay: 3600_000 }),\n retry({ delay: 10_000 }),\n shareReplay(1),\n);\nexport const marginProducts$ = marginInstruments$.pipe(\n mergeMap((x) =>\n from(x.data || []).pipe(\n // ISSUE: 可能有 level = '' 的错误情况 (SPK-USDT and RESOLV-USDT at 2025-06-19 08 UTC)\n filter((x) => +x.lever > 0),\n map(\n (x): IProduct => ({\n datasource_id: 'OKX',\n product_id: encodePath(x.instType, x.instId),\n base_currency: x.baseCcy,\n quote_currency: x.quoteCcy,\n value_scale: 1,\n volume_step: +x.lotSz,\n price_step: +x.tickSz,\n margin_rate: 1 / +x.lever,\n name: `${x.baseCcy}-${x.quoteCcy}-MARGIN`,\n value_scale_unit: '',\n value_based_cost: 0,\n volume_based_cost: 0,\n max_position: 0,\n max_volume: 0,\n allow_long: true,\n allow_short: true,\n market_id: 'OKX',\n no_interest_rate: false,\n }),\n ),\n tap((x) => product$.next(x)),\n toArray(),\n ),\n ),\n shareReplay(1),\n);\n\nmarginProducts$.subscribe();\n\nexport const mapProductIdToMarginProduct$ = marginProducts$.pipe(\n map((x) => new Map(x.map((x) => [x.product_id, x])), shareReplay(1)),\n);\n\ndefer(() => client.getInstruments({ instType: 'SPOT' }))\n .pipe(\n repeat({ delay: 3600_000 }),\n retry({ delay: 10_000 }),\n mergeMap((x) =>\n from(x.data || []).pipe(\n map(\n (x): IProduct => ({\n datasource_id: 'OKX',\n product_id: encodePath(x.instType, x.instId),\n base_currency: x.baseCcy,\n quote_currency: x.quoteCcy,\n value_scale: 1,\n volume_step: +x.lotSz,\n price_step: +x.tickSz,\n margin_rate: 1,\n name: `${x.baseCcy}-${x.quoteCcy}-SPOT`,\n value_scale_unit: '',\n value_based_cost: 0,\n volume_based_cost: 0,\n max_position: 0,\n max_volume: 0,\n allow_long: true,\n allow_short: true,\n market_id: 'OKX',\n no_interest_rate: true,\n }),\n ),\n tap((x) => product$.next(x)),\n toArray(),\n ),\n ),\n )\n .subscribe();\n\ncreateSQLWriter<IProduct>(Terminal.fromNodeEnv(), {\n data$: product$,\n tableName: 'product',\n writeInterval: 1_000,\n conflictKeys: ['datasource_id', 'product_id'],\n});\n\nexport const mapProductIdToUsdtSwapProduct$ = usdtSwapProducts$.pipe(\n map((x) => new Map(x.map((x) => [x.product_id, x]))),\n shareReplay(1),\n);\n"]}
package/dist/quote.js ADDED
@@ -0,0 +1,58 @@
1
+ import { encodePath } from '@yuants/utils';
2
+ import { Terminal } from '@yuants/protocol';
3
+ import { writeToSQL } from '@yuants/sql';
4
+ import { defer, from, groupBy, map, merge, mergeMap, repeat, retry, scan, shareReplay, toArray } from 'rxjs';
5
+ import { client } from './api';
6
+ const swapTickers$ = defer(() => client.getMarketTickers({ instType: 'SWAP' })).pipe(repeat({ delay: 5000 }), retry({ delay: 5000 }), shareReplay(1));
7
+ const spotTickers$ = defer(() => client.getMarketTickers({ instType: 'SPOT' })).pipe(repeat({ delay: 5000 }), retry({ delay: 5000 }), shareReplay(1));
8
+ export const swapMarketTickers$ = defer(() => swapTickers$).pipe(mergeMap((x) => from(x.data).pipe(map((x) => [x.instId, x]), toArray(), map((x) => Object.fromEntries(x)))), repeat({ delay: 5000 }), retry({ delay: 5000 }), shareReplay(1));
9
+ export const spotMarketTickers$ = defer(() => spotTickers$).pipe(mergeMap((x) => from(x.data).pipe(map((x) => [x.instId, x]), toArray(), map((x) => Object.fromEntries(x)))), repeat({ delay: 5000 }), retry({ delay: 5000 }), shareReplay(1));
10
+ const quote1$ = swapTickers$.pipe(mergeMap((x) => x.data || []), map((ticker) => ({
11
+ datasource_id: 'OKX',
12
+ product_id: encodePath('SWAP', ticker.instId),
13
+ last_price: ticker.last,
14
+ ask_price: ticker.askPx,
15
+ bid_price: ticker.bidPx,
16
+ ask_volume: ticker.askSz,
17
+ bid_volume: ticker.bidSz,
18
+ })));
19
+ const quote2$ = spotTickers$.pipe(mergeMap((x) => x.data || []), map((ticker) => ({
20
+ datasource_id: 'OKX',
21
+ product_id: encodePath('SPOT', ticker.instId),
22
+ last_price: ticker.last,
23
+ ask_price: ticker.askPx,
24
+ bid_price: ticker.bidPx,
25
+ ask_volume: ticker.askSz,
26
+ bid_volume: ticker.bidSz,
27
+ })));
28
+ const quote4$ = spotTickers$.pipe(mergeMap((x) => x.data || []), map((ticker) => ({
29
+ datasource_id: 'OKX',
30
+ product_id: encodePath('MARGIN', ticker.instId),
31
+ last_price: ticker.last,
32
+ ask_price: ticker.askPx,
33
+ bid_price: ticker.bidPx,
34
+ ask_volume: ticker.askSz,
35
+ bid_volume: ticker.bidSz,
36
+ })));
37
+ const swapOpenInterests$ = defer(() => client.getOpenInterest({ instType: 'SWAP' })).pipe(repeat({ delay: 10000 }), retry({ delay: 10000 }), shareReplay(1));
38
+ const quote3$ = swapOpenInterests$.pipe(mergeMap((x) => x.data || []), map((x) => ({
39
+ datasource_id: 'OKX',
40
+ product_id: encodePath('SWAP', x.instId),
41
+ open_interest: x.oi,
42
+ })));
43
+ // 合并不同来源的数据并进行合并,避免死锁
44
+ if (process.env.WRITE_QUOTE_TO_SQL === 'true') {
45
+ merge(quote1$, quote2$, quote3$, quote4$)
46
+ .pipe(groupBy((x) => encodePath(x.datasource_id, x.product_id)), mergeMap((group$) => {
47
+ return group$.pipe(scan((acc, cur) => Object.assign(acc, cur), {}));
48
+ }), writeToSQL({
49
+ terminal: Terminal.fromNodeEnv(),
50
+ writeInterval: 1000,
51
+ tableName: 'quote',
52
+ keyFn: (quote) => encodePath(quote.datasource_id, quote.product_id),
53
+ conflictKeys: ['datasource_id', 'product_id'],
54
+ }))
55
+ .subscribe();
56
+ }
57
+ export const swapOpenInterest$ = defer(() => swapOpenInterests$).pipe(map((x) => new Map(x.data.map((x) => [x.instId, +x.oi]))), shareReplay(1));
58
+ //# sourceMappingURL=quote.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"quote.js","sourceRoot":"","sources":["../src/quote.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAE3C,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AACzC,OAAO,EAAE,KAAK,EAAE,IAAI,EAAE,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,IAAI,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC7G,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAE/B,MAAM,YAAY,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,gBAAgB,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,IAAI,CAClF,MAAM,CA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C,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC;SACtC,IAAI,CACH,OAAO,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,aAAa,EAAE,CAAC,CAAC,UAAU,CAAC,CAAC,EACzD,QAAQ,CAAC,CAAC,MAAM,EAAE,EAAE;QAClB,OAAO,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,EAAqB,CAAC,CAAC,CAAC;IACzF,CAAC,CAAC,EACF,UAAU,CAAC;QACT,QAAQ,EAAE,QAAQ,CAAC,WAAW,EAAE;QAChC,aAAa,EAAE,IAAI;QACnB,SAAS,EAAE,OAAO;QAClB,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,aAAa,EAAE,KAAK,CAAC,UAAU,CAAC;QACnE,YAAY,EAAE,CAAC,eAAe,EAAE,YAAY,CAAC;KAC9C,CAAC,CACH;SACA,SAAS,EAAE,CAAC;CAChB;AAED,MAAM,CAAC,MAAM,iBAAiB,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,kBAAkB,CAAC,CAAC,IAAI,CACnE,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAI,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,EAAE,CAAU,CAAC,CAAC,CAAC,EAClE,WAAW,CAAC,CAAC,CAAC,CACf,CAAC","sourcesContent":["import { encodePath } from '@yuants/utils';\nimport { IQuote } from '@yuants/data-quote';\nimport { Terminal } from '@yuants/protocol';\nimport { writeToSQL } from '@yuants/sql';\nimport { defer, from, groupBy, map, merge, mergeMap, repeat, retry, scan, shareReplay, toArray } from 'rxjs';\nimport { client } from './api';\n\nconst swapTickers$ = defer(() => client.getMarketTickers({ instType: 'SWAP' })).pipe(\n repeat({ delay: 5000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst spotTickers$ = defer(() => client.getMarketTickers({ instType: 'SPOT' })).pipe(\n repeat({ delay: 5000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nexport const swapMarketTickers$ = defer(() => swapTickers$).pipe(\n mergeMap((x) =>\n from(x.data).pipe(\n map((x) => [x.instId, x] as const),\n toArray(),\n map((x) => Object.fromEntries(x)),\n ),\n ),\n repeat({ delay: 5000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nexport const spotMarketTickers$ = defer(() => spotTickers$).pipe(\n mergeMap((x) =>\n from(x.data).pipe(\n map((x) => [x.instId, x] as const),\n toArray(),\n map((x) => Object.fromEntries(x)),\n ),\n ),\n repeat({ delay: 5000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst quote1$ = swapTickers$.pipe(\n mergeMap((x) => x.data || []),\n map(\n (ticker): Partial<IQuote> => ({\n datasource_id: 'OKX',\n product_id: encodePath('SWAP', ticker.instId),\n last_price: ticker.last,\n ask_price: ticker.askPx,\n bid_price: ticker.bidPx,\n ask_volume: ticker.askSz,\n bid_volume: ticker.bidSz,\n }),\n ),\n);\n\nconst quote2$ = spotTickers$.pipe(\n mergeMap((x) => x.data || []),\n map(\n (ticker): Partial<IQuote> => ({\n datasource_id: 'OKX',\n product_id: encodePath('SPOT', ticker.instId),\n last_price: ticker.last,\n ask_price: ticker.askPx,\n bid_price: ticker.bidPx,\n ask_volume: ticker.askSz,\n bid_volume: ticker.bidSz,\n }),\n ),\n);\n\nconst quote4$ = spotTickers$.pipe(\n mergeMap((x) => x.data || []),\n map(\n (ticker): Partial<IQuote> => ({\n datasource_id: 'OKX',\n product_id: encodePath('MARGIN', ticker.instId),\n last_price: ticker.last,\n ask_price: ticker.askPx,\n bid_price: ticker.bidPx,\n ask_volume: ticker.askSz,\n bid_volume: ticker.bidSz,\n }),\n ),\n);\n\nconst swapOpenInterests$ = defer(() => client.getOpenInterest({ instType: 'SWAP' })).pipe(\n repeat({ delay: 10_000 }),\n retry({ delay: 10_000 }),\n shareReplay(1),\n);\n\nconst quote3$ = swapOpenInterests$.pipe(\n mergeMap((x) => x.data || []),\n map(\n (x): Partial<IQuote> => ({\n datasource_id: 'OKX',\n product_id: encodePath('SWAP', x.instId),\n open_interest: x.oi,\n }),\n ),\n);\n\n// 合并不同来源的数据并进行合并,避免死锁\nif (process.env.WRITE_QUOTE_TO_SQL === 'true') {\n merge(quote1$, quote2$, quote3$, quote4$)\n .pipe(\n groupBy((x) => encodePath(x.datasource_id, x.product_id)),\n mergeMap((group$) => {\n return group$.pipe(scan((acc, cur) => Object.assign(acc, cur), {} as Partial<IQuote>));\n }),\n writeToSQL({\n terminal: Terminal.fromNodeEnv(),\n writeInterval: 1000,\n tableName: 'quote',\n keyFn: (quote) => encodePath(quote.datasource_id, quote.product_id),\n conflictKeys: ['datasource_id', 'product_id'],\n }),\n )\n .subscribe();\n}\n\nexport const swapOpenInterest$ = defer(() => swapOpenInterests$).pipe(\n map((x) => new Map(x.data.map((x) => [x.instId, +x.oi] as const))),\n shareReplay(1),\n);\n"]}
@@ -0,0 +1 @@
1
+ export { }