@yuants/vendor-okx 0.16.9 → 0.17.0
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/dist/account.js +198 -0
- package/dist/account.js.map +1 -0
- package/dist/api.js +463 -0
- package/dist/api.js.map +1 -0
- package/dist/cli.js +3 -0
- package/dist/cli.js.map +1 -0
- package/dist/cluster.js +80 -0
- package/dist/cluster.js.map +1 -0
- package/dist/extension.js +89 -0
- package/dist/extension.js.map +1 -0
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -0
- package/dist/interest_rate.js +133 -0
- package/dist/interest_rate.js.map +1 -0
- package/dist/legacy_index.js +554 -0
- package/dist/legacy_index.js.map +1 -0
- package/dist/logger.js +91 -0
- package/dist/logger.js.map +1 -0
- package/dist/ohlc.js +98 -0
- package/dist/ohlc.js.map +1 -0
- package/dist/order.js +96 -0
- package/dist/order.js.map +1 -0
- package/dist/product.js +85 -0
- package/dist/product.js.map +1 -0
- package/dist/quote.js +58 -0
- package/dist/quote.js.map +1 -0
- package/dist/vendor-okx.d.ts +1 -0
- package/lib/account.d.ts +102 -0
- package/lib/account.d.ts.map +1 -0
- package/lib/account.js +201 -0
- package/lib/account.js.map +1 -0
- package/lib/api.d.ts +1401 -0
- package/lib/api.d.ts.map +1 -0
- package/lib/api.js +470 -0
- package/lib/api.js.map +1 -0
- package/lib/cli.d.ts +3 -0
- package/lib/cli.d.ts.map +1 -0
- package/lib/cli.js +5 -0
- package/lib/cli.js.map +1 -0
- package/lib/cluster.d.ts +2 -0
- package/lib/cluster.d.ts.map +1 -0
- package/lib/cluster.js +108 -0
- package/lib/cluster.js.map +1 -0
- package/lib/extension.d.ts +4 -0
- package/lib/extension.d.ts.map +1 -0
- package/lib/extension.js +91 -0
- package/lib/extension.js.map +1 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.d.ts.map +1 -0
- package/lib/index.js +9 -0
- package/lib/index.js.map +1 -0
- package/lib/interest_rate.d.ts +2 -0
- package/lib/interest_rate.d.ts.map +1 -0
- package/lib/interest_rate.js +135 -0
- package/lib/interest_rate.js.map +1 -0
- package/lib/legacy_index.d.ts +2 -0
- package/lib/legacy_index.d.ts.map +1 -0
- package/lib/legacy_index.js +556 -0
- package/lib/legacy_index.js.map +1 -0
- package/lib/logger.d.ts +21 -0
- package/lib/logger.d.ts.map +1 -0
- package/lib/logger.js +98 -0
- package/lib/logger.js.map +1 -0
- package/lib/ohlc.d.ts +2 -0
- package/lib/ohlc.d.ts.map +1 -0
- package/lib/ohlc.js +100 -0
- package/lib/ohlc.js.map +1 -0
- package/lib/order.d.ts +4 -0
- package/lib/order.d.ts.map +1 -0
- package/lib/order.js +99 -0
- package/lib/order.js.map +1 -0
- package/lib/product.d.ts +6 -0
- package/lib/product.d.ts.map +1 -0
- package/lib/product.js +88 -0
- package/lib/product.js.map +1 -0
- package/lib/quote.d.ts +42 -0
- package/lib/quote.d.ts.map +1 -0
- package/lib/quote.js +61 -0
- package/lib/quote.js.map +1 -0
- package/package.json +5 -2
- package/temp/image-tag +1 -0
- package/temp/package-deps.json +41 -0
- package/temp/vendor-okx.api.json +177 -0
- package/temp/vendor-okx.api.md +9 -0
package/lib/logger.d.ts
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
/**
|
2
|
+
* 日志级别
|
3
|
+
*/
|
4
|
+
export declare type LogLevel = 'info' | 'warn' | 'error' | 'debug';
|
5
|
+
/**
|
6
|
+
* 集群模式下的日志管理器
|
7
|
+
* 在worker进程中将日志发送到主进程,在主进程中直接使用console
|
8
|
+
*/
|
9
|
+
declare class ClusterLogger {
|
10
|
+
private isWorker;
|
11
|
+
private sendToMaster;
|
12
|
+
info(message: string, ...args: any[]): void;
|
13
|
+
warn(message: string, ...args: any[]): void;
|
14
|
+
error(message: string, ...args: any[]): void;
|
15
|
+
debug(message: string, ...args: any[]): void;
|
16
|
+
log(message: string, ...args: any[]): void;
|
17
|
+
}
|
18
|
+
export declare const logger: ClusterLogger;
|
19
|
+
export declare const overrideConsole: () => () => void;
|
20
|
+
export {};
|
21
|
+
//# sourceMappingURL=logger.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"logger.d.ts","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":"AAGA;;GAEG;AACH,oBAAY,QAAQ,GAAG,MAAM,GAAG,MAAM,GAAG,OAAO,GAAG,OAAO,CAAC;AAE3D;;;GAGG;AACH,cAAM,aAAa;IACjB,OAAO,CAAC,QAAQ,CAAsB;IAEtC,OAAO,CAAC,YAAY;IA6CpB,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE;IAIpC,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE;IAIpC,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE;IAIrC,KAAK,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE;IAIrC,GAAG,CAAC,OAAO,EAAE,MAAM,EAAE,GAAG,IAAI,EAAE,GAAG,EAAE;CAGpC;AAGD,eAAO,MAAM,MAAM,eAAsB,CAAC;AAG1C,eAAO,MAAM,eAAe,kBAuB3B,CAAC"}
|
package/lib/logger.js
ADDED
@@ -0,0 +1,98 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
4
|
+
};
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
6
|
+
exports.overrideConsole = exports.logger = void 0;
|
7
|
+
const cluster_1 = __importDefault(require("cluster"));
|
8
|
+
/**
|
9
|
+
* 集群模式下的日志管理器
|
10
|
+
* 在worker进程中将日志发送到主进程,在主进程中直接使用console
|
11
|
+
*/
|
12
|
+
class ClusterLogger {
|
13
|
+
constructor() {
|
14
|
+
this.isWorker = !cluster_1.default.isPrimary;
|
15
|
+
}
|
16
|
+
sendToMaster(level, message, ...args) {
|
17
|
+
if (this.isWorker && process.send) {
|
18
|
+
const formattedMessage = args.length > 0
|
19
|
+
? `${message} ${args
|
20
|
+
.map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg)))
|
21
|
+
.join(' ')}`
|
22
|
+
: message;
|
23
|
+
process.send({
|
24
|
+
type: 'log',
|
25
|
+
level,
|
26
|
+
message: `[PID:${process.pid}] ${formattedMessage}`,
|
27
|
+
pid: process.pid,
|
28
|
+
label: process.env.WORKER_LABEL || 'unknown',
|
29
|
+
timestamp: Date.now(),
|
30
|
+
});
|
31
|
+
}
|
32
|
+
else {
|
33
|
+
// 在主进程中直接输出
|
34
|
+
const formattedMessage = args.length > 0
|
35
|
+
? `${message} ${args
|
36
|
+
.map((arg) => (typeof arg === 'object' ? JSON.stringify(arg) : String(arg)))
|
37
|
+
.join(' ')}`
|
38
|
+
: message;
|
39
|
+
const logMessage = `[PID:${process.pid}] ${formattedMessage}`;
|
40
|
+
// 控制台输出
|
41
|
+
switch (level) {
|
42
|
+
case 'error':
|
43
|
+
console.error(logMessage);
|
44
|
+
break;
|
45
|
+
case 'warn':
|
46
|
+
console.warn(logMessage);
|
47
|
+
break;
|
48
|
+
case 'debug':
|
49
|
+
console.debug(logMessage);
|
50
|
+
break;
|
51
|
+
default:
|
52
|
+
console.log(logMessage);
|
53
|
+
}
|
54
|
+
}
|
55
|
+
}
|
56
|
+
info(message, ...args) {
|
57
|
+
this.sendToMaster('info', message, ...args);
|
58
|
+
}
|
59
|
+
warn(message, ...args) {
|
60
|
+
this.sendToMaster('warn', message, ...args);
|
61
|
+
}
|
62
|
+
error(message, ...args) {
|
63
|
+
this.sendToMaster('error', message, ...args);
|
64
|
+
}
|
65
|
+
debug(message, ...args) {
|
66
|
+
this.sendToMaster('debug', message, ...args);
|
67
|
+
}
|
68
|
+
log(message, ...args) {
|
69
|
+
this.info(message, ...args);
|
70
|
+
}
|
71
|
+
}
|
72
|
+
// 默认配置的logger实例
|
73
|
+
exports.logger = new ClusterLogger();
|
74
|
+
// 猛踩油门
|
75
|
+
const overrideConsole = () => {
|
76
|
+
if (!cluster_1.default.isPrimary) {
|
77
|
+
const originalConsole = {
|
78
|
+
log: console.log,
|
79
|
+
info: console.info,
|
80
|
+
warn: console.warn,
|
81
|
+
error: console.error,
|
82
|
+
};
|
83
|
+
console.log = (...args) => exports.logger.info(args.join(' '));
|
84
|
+
console.info = (...args) => exports.logger.info(args.join(' '));
|
85
|
+
console.warn = (...args) => exports.logger.warn(args.join(' '));
|
86
|
+
console.error = (...args) => exports.logger.error(args.join(' '));
|
87
|
+
// 提供恢复方法
|
88
|
+
return () => {
|
89
|
+
console.log = originalConsole.log;
|
90
|
+
console.info = originalConsole.info;
|
91
|
+
console.warn = originalConsole.warn;
|
92
|
+
console.error = originalConsole.error;
|
93
|
+
};
|
94
|
+
}
|
95
|
+
return () => { }; // 主进程中返回空函数
|
96
|
+
};
|
97
|
+
exports.overrideConsole = overrideConsole;
|
98
|
+
//# sourceMappingURL=logger.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"logger.js","sourceRoot":"","sources":["../src/logger.ts"],"names":[],"mappings":";;;;;;AAAA,sDAA8B;AAQ9B;;;GAGG;AACH,MAAM,aAAa;IAAnB;QACU,aAAQ,GAAG,CAAC,iBAAO,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,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C,GAAG,CAAC,CAAC,CAAC;QAC/D,OAAO,CAAC,IAAI,GAAG,CAAC,GAAG,IAAW,EAAE,EAAE,CAAC,cAAM,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;QAC/D,OAAO,CAAC,KAAK,GAAG,CAAC,GAAG,IAAW,EAAE,EAAE,CAAC,cAAM,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC;QAEjE,SAAS;QACT,OAAO,GAAG,EAAE;YACV,OAAO,CAAC,GAAG,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;AAvBW,QAAA,eAAe,mBAuB1B","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/lib/ohlc.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"ohlc.d.ts","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":""}
|
package/lib/ohlc.js
ADDED
@@ -0,0 +1,100 @@
|
|
1
|
+
"use strict";
|
2
|
+
var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
|
3
|
+
var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
|
4
|
+
if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
|
5
|
+
var g = generator.apply(thisArg, _arguments || []), i, q = [];
|
6
|
+
return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
|
7
|
+
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); }); }; }
|
8
|
+
function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
|
9
|
+
function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
|
10
|
+
function fulfill(value) { resume("next", value); }
|
11
|
+
function reject(value) { resume("throw", value); }
|
12
|
+
function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
|
13
|
+
};
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
15
|
+
const data_series_1 = require("@yuants/data-series");
|
16
|
+
const protocol_1 = require("@yuants/protocol");
|
17
|
+
const utils_1 = require("@yuants/utils");
|
18
|
+
const rxjs_1 = require("rxjs");
|
19
|
+
const api_1 = require("./api");
|
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
|
+
(0, data_series_1.createSeriesProvider)(protocol_1.Terminal.fromNodeEnv(), {
|
40
|
+
tableName: 'ohlc',
|
41
|
+
series_id_prefix_parts: ['OKX'],
|
42
|
+
reversed: true,
|
43
|
+
serviceOptions: { concurrent: 1 },
|
44
|
+
queryFn: function ({ series_id, ended_at }) {
|
45
|
+
return __asyncGenerator(this, arguments, function* () {
|
46
|
+
const [datasource_id, product_id, duration] = (0, utils_1.decodePath)(series_id);
|
47
|
+
const offset = (0, utils_1.convertDurationToOffset)(duration);
|
48
|
+
if (!datasource_id) {
|
49
|
+
throw 'datasource_id is required';
|
50
|
+
}
|
51
|
+
if (!product_id) {
|
52
|
+
throw 'product_id is required';
|
53
|
+
}
|
54
|
+
if (!offset) {
|
55
|
+
throw 'duration is invalid';
|
56
|
+
}
|
57
|
+
const [instType, instId] = (0, utils_1.decodePath)(product_id);
|
58
|
+
if (!instId) {
|
59
|
+
throw `invalid product_id: ${product_id}`;
|
60
|
+
}
|
61
|
+
const bar = DURATION_TO_OKX_BAR_TYPE[duration];
|
62
|
+
if (!bar) {
|
63
|
+
throw `unsupported duration: ${duration}`;
|
64
|
+
}
|
65
|
+
let currentStartTime = ended_at;
|
66
|
+
while (true) {
|
67
|
+
// 向前翻页,时间降序,不含 after 时间点
|
68
|
+
const res = yield __await(api_1.client.getHistoryCandles({
|
69
|
+
instId,
|
70
|
+
bar,
|
71
|
+
after: `${currentStartTime}`,
|
72
|
+
limit: '100',
|
73
|
+
}));
|
74
|
+
if (res.code !== '0') {
|
75
|
+
throw `API failed: ${res.code} ${res.msg}`;
|
76
|
+
}
|
77
|
+
if (res.data.length === 0)
|
78
|
+
break;
|
79
|
+
currentStartTime = +res.data[res.data.length - 1][0];
|
80
|
+
const data = res.data.map((x) => ({
|
81
|
+
series_id,
|
82
|
+
datasource_id,
|
83
|
+
product_id,
|
84
|
+
duration,
|
85
|
+
created_at: (0, utils_1.formatTime)(+x[0]),
|
86
|
+
closed_at: (0, utils_1.formatTime)(+x[0] + offset),
|
87
|
+
open: x[1],
|
88
|
+
high: x[2],
|
89
|
+
low: x[3],
|
90
|
+
close: x[4],
|
91
|
+
volume: x[5],
|
92
|
+
open_interest: '0',
|
93
|
+
}));
|
94
|
+
yield yield __await(data);
|
95
|
+
yield __await((0, rxjs_1.firstValueFrom)((0, rxjs_1.timer)(1000)));
|
96
|
+
}
|
97
|
+
});
|
98
|
+
},
|
99
|
+
});
|
100
|
+
//# sourceMappingURL=ohlc.js.map
|
package/lib/ohlc.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"ohlc.js","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AACA,qDAA2D;AAC3D,+CAA4C;AAC5C,yCAAgF;AAChF,+BAA6C;AAC7C,+BAA+B;AAE/B,aAAa;AACb,gCAAgC;AAChC,8BAA8B;AAC9B,8CAA8C;AAE9C,MAAM,wBAAwB,GAA2B;IACvD,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,KAAK,EAAE,KAAK;IACZ,KAAK,EAAE,KAAK;IAEZ,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,KAAK,EAAE,KAAK;IAEZ,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;CACV,CAAC;AAEF,IAAA,kCAAoB,EAAQ,mBAAQ,CAAC,WAAW,EAAE,EAAE;IAClD,SAAS,EAAE,MAAM;IACjB,sBAAsB,EAAE,CAAC,KAAK,CAAC;IAC/B,QAAQ,EAAE,IAAI;IACd,cAAc,EAAE,EAAE,UAAU,EAAE,CAAC,EAAE;IACjC,OAAO,EAAE,UAAiB,EAAE,SAAS,EAAE,QAAQ,EAAE;;YAC/C,MAAM,CAAC,aAAa,EAAE,UAAU,EAAE,QAAQ,CAAC,GAAG,IAAA,kBAAU,EAAC,SAAS,CAAC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AS;oBACT,aAAa;oBACb,UAAU;oBACV,QAAQ;oBACR,UAAU,EAAE,IAAA,kBAAU,EAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;oBAC7B,SAAS,EAAE,IAAA,kBAAU,EAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,MAAM,CAAC;oBACrC,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;oBACV,IAAI,EAAE,CAAC,CAAC,CAAC,CAAC;oBACV,GAAG,EAAE,CAAC,CAAC,CAAC,CAAC;oBACT,KAAK,EAAE,CAAC,CAAC,CAAC,CAAC;oBACX,MAAM,EAAE,CAAC,CAAC,CAAC,CAAC;oBACZ,aAAa,EAAE,GAAG;iBACnB,CAAC,CACH,CAAC;gBACF,oBAAM,IAAI,CAAA,CAAC;gBACX,cAAM,IAAA,qBAAc,EAAC,IAAA,YAAK,EAAC,IAAI,CAAC,CAAC,CAAA,CAAC;aACnC;QACH,CAAC;KAAA;CACF,CAAC,CAAC","sourcesContent":["import { IOHLC } from '@yuants/data-ohlc';\nimport { createSeriesProvider } from '@yuants/data-series';\nimport { Terminal } from '@yuants/protocol';\nimport { convertDurationToOffset, decodePath, formatTime } from '@yuants/utils';\nimport { firstValueFrom, timer } from 'rxjs';\nimport { client } from './api';\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\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"]}
|
package/lib/order.d.ts
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"order.d.ts","sourceRoot":"","sources":["../src/order.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAI5C,OAAO,EAUL,OAAO,EAER,MAAM,MAAM,CAAC;AAId,eAAO,MAAM,MAAM,iBAAwB,CAAC"}
|
package/lib/order.js
ADDED
@@ -0,0 +1,99 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.order$ = void 0;
|
4
|
+
const protocol_1 = require("@yuants/protocol");
|
5
|
+
const sql_1 = require("@yuants/sql");
|
6
|
+
const utils_1 = require("@yuants/utils");
|
7
|
+
const rxjs_1 = require("rxjs");
|
8
|
+
const account_1 = require("./account");
|
9
|
+
const api_1 = require("./api");
|
10
|
+
exports.order$ = new rxjs_1.Subject();
|
11
|
+
exports.order$
|
12
|
+
.pipe(
|
13
|
+
//
|
14
|
+
// mergeMap((x) => x),
|
15
|
+
(0, sql_1.writeToSQL)({
|
16
|
+
terminal: protocol_1.Terminal.fromNodeEnv(),
|
17
|
+
tableName: '"order"',
|
18
|
+
writeInterval: 1000,
|
19
|
+
keyFn: (order) => (0, utils_1.encodePath)(order.account_id, order.order_id),
|
20
|
+
columns: [
|
21
|
+
'order_id',
|
22
|
+
'account_id',
|
23
|
+
'product_id',
|
24
|
+
'position_id',
|
25
|
+
'order_type',
|
26
|
+
'order_direction',
|
27
|
+
'volume',
|
28
|
+
'submit_at',
|
29
|
+
'updated_at',
|
30
|
+
'filled_at',
|
31
|
+
'price',
|
32
|
+
'traded_volume',
|
33
|
+
'traded_price',
|
34
|
+
'order_status',
|
35
|
+
'comment',
|
36
|
+
'profit_correction',
|
37
|
+
'real_profit',
|
38
|
+
'inferred_base_currency_price',
|
39
|
+
],
|
40
|
+
conflictKeys: ['account_id', 'order_id'],
|
41
|
+
}))
|
42
|
+
.subscribe();
|
43
|
+
const makeOrder = (x, account_id) => {
|
44
|
+
const order_type = x.ordType === 'market' ? 'MARKET' : x.ordType === 'limit' ? 'LIMIT' : 'UNKNOWN';
|
45
|
+
const order_direction = x.side === 'buy'
|
46
|
+
? x.posSide === 'long'
|
47
|
+
? 'OPEN_LONG'
|
48
|
+
: 'CLOSE_SHORT'
|
49
|
+
: x.posSide === 'short'
|
50
|
+
? 'OPEN_SHORT'
|
51
|
+
: 'CLOSE_LONG';
|
52
|
+
const order_status = ['live', 'partially_filled'].includes(x.state)
|
53
|
+
? 'ACCEPTED'
|
54
|
+
: x.state === 'filled'
|
55
|
+
? 'TRADED'
|
56
|
+
: 'CANCELLED';
|
57
|
+
return {
|
58
|
+
order_id: x.clOrdId !== '' ? x.clOrdId : x.ordId,
|
59
|
+
account_id,
|
60
|
+
product_id: (0, utils_1.encodePath)(x.instType, x.instId),
|
61
|
+
submit_at: +x.cTime,
|
62
|
+
filled_at: +x.fillTime,
|
63
|
+
created_at: (0, utils_1.formatTime)(+x.cTime),
|
64
|
+
updated_at: (0, utils_1.formatTime)(+x.uTime),
|
65
|
+
order_type,
|
66
|
+
order_direction,
|
67
|
+
volume: +x.sz,
|
68
|
+
traded_volume: +x.accFillSz,
|
69
|
+
price: +x.px,
|
70
|
+
traded_price: +x.avgPx,
|
71
|
+
order_status,
|
72
|
+
};
|
73
|
+
};
|
74
|
+
(async () => {
|
75
|
+
const uid = await (0, rxjs_1.firstValueFrom)(account_1.accountUid$);
|
76
|
+
const TRADING_ACCOUNT_ID = `okx/${uid}/trading`;
|
77
|
+
const FUNDING_ACCOUNT_ID = `okx/${uid}/funding/USDT`;
|
78
|
+
const EARNING_ACCOUNT_ID = `okx/${uid}/earning/USDT`;
|
79
|
+
const swapHistoryOrders = (0, rxjs_1.defer)(() => api_1.client.getTradeOrdersHistory({ instType: 'SWAP' })).pipe((0, rxjs_1.repeat)({ delay: 1000 }), (0, rxjs_1.retry)({ delay: 1000 }), (0, rxjs_1.shareReplay)(1));
|
80
|
+
const swapPendingOrders = (0, rxjs_1.defer)(() => api_1.client.getTradeOrdersPending({ instType: 'SWAP' })).pipe((0, rxjs_1.repeat)({ delay: 1000 }), (0, rxjs_1.retry)({ delay: 1000 }), (0, rxjs_1.shareReplay)(1));
|
81
|
+
const ordersFromHistoryOrder$ = swapHistoryOrders.pipe(
|
82
|
+
//
|
83
|
+
(0, rxjs_1.mergeMap)((x) => (0, rxjs_1.from)(x.data || []).pipe(
|
84
|
+
//
|
85
|
+
(0, rxjs_1.map)((x) => makeOrder(x, TRADING_ACCOUNT_ID)))));
|
86
|
+
const ordersFromPendingOrder$ = swapPendingOrders.pipe(
|
87
|
+
//
|
88
|
+
(0, rxjs_1.mergeMap)((x) => (0, rxjs_1.from)(x.data || []).pipe(
|
89
|
+
//
|
90
|
+
(0, rxjs_1.map)((x) => makeOrder(x, TRADING_ACCOUNT_ID)))));
|
91
|
+
(0, rxjs_1.merge)(ordersFromHistoryOrder$, ordersFromPendingOrder$)
|
92
|
+
.pipe(
|
93
|
+
//
|
94
|
+
(0, rxjs_1.tap)((x) => {
|
95
|
+
exports.order$.next(x);
|
96
|
+
}))
|
97
|
+
.subscribe();
|
98
|
+
})();
|
99
|
+
//# sourceMappingURL=order.js.map
|
package/lib/order.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"order.js","sourceRoot":"","sources":["../src/order.ts"],"names":[],"mappings":";;;AACA,+CAA4C;AAC5C,qCAAyC;AACzC,yCAAuD;AACvD,+BAYc;AACd,uCAAwC;AACxC,+BAA+B;AAElB,QAAA,MAAM,GAAG,IAAI,cAAO,EAAU,CAAC;AAE5C,cAAM;KACH,IAAI;AACH,EAAE;AACF,sBAAsB;AACtB,IAAA,gBAAU,EAAC;IACT,QAAQ,EAAE,mBAAQ,CAAC,WAAW,EAAE;IAChC,SAAS,EAAE,SAAS;IACpB,aAAa,EAAE,IAAI;IACnB,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAA,kBAAU,EAAC,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,WAAW;QACX,YAAY;QACZ,WAAW;QACX,OAAO;QACP,eAAe;QACf,cAAc;QACd,cAAc;QACd,SAAS;QACT,mBAAmB;QACnB,aAAa;QACb,8BAA8B;KAC/B;IACD,YAAY,EAAE,CAAC,YAAY,EAAE,UAAU,CAAC;CACzC,CAAC,CACH;KACA,SAAS,EAAE,CAAC;AAEf,MAAM,SAAS,GAAG,CAChB,CAgBC,EACD,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AE,EAAE,CAAC,SAAS,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAC7C,CACF,CACF,CAAC;IAEF,MAAM,uBAAuB,GAAG,iBAAiB,CAAC,IAAI;IACpD,EAAE;IACF,IAAA,eAAQ,EAAC,CAAC,CAAC,EAAE,EAAE,CACb,IAAA,WAAI,EAAC,CAAC,CAAC,IAAI,IAAI,EAAE,CAAC,CAAC,IAAI;IACrB,EAAE;IACF,IAAA,UAAG,EAAC,CAAC,CAAC,EAAE,EAAE,CAAC,SAAS,CAAC,CAAC,EAAE,kBAAkB,CAAC,CAAC,CAC7C,CACF,CACF,CAAC;IAEF,IAAA,YAAK,EAAC,uBAAuB,EAAE,uBAAuB,CAAC;SACpD,IAAI;IACH,EAAE;IACF,IAAA,UAAG,EAAC,CAAC,CAAC,EAAE,EAAE;QACR,cAAM,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"]}
|
package/lib/product.d.ts
ADDED
@@ -0,0 +1,6 @@
|
|
1
|
+
import { IProduct } from '@yuants/data-product';
|
2
|
+
export declare const usdtSwapProducts$: import("rxjs").Observable<IProduct[]>;
|
3
|
+
export declare const marginProducts$: import("rxjs").Observable<IProduct[]>;
|
4
|
+
export declare const mapProductIdToMarginProduct$: import("rxjs").Observable<Map<string, IProduct>>;
|
5
|
+
export declare const mapProductIdToUsdtSwapProduct$: import("rxjs").Observable<Map<string, IProduct>>;
|
6
|
+
//# sourceMappingURL=product.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"product.d.ts","sourceRoot":"","sources":["../src/product.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAchD,eAAO,MAAM,iBAAiB,uCAiC7B,CAAC;AAQF,eAAO,MAAM,eAAe,uCAgC3B,CAAC;AAIF,eAAO,MAAM,4BAA4B,kDAExC,CAAC;AA4CF,eAAO,MAAM,8BAA8B,kDAG1C,CAAC"}
|
package/lib/product.js
ADDED
@@ -0,0 +1,88 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.mapProductIdToUsdtSwapProduct$ = exports.mapProductIdToMarginProduct$ = exports.marginProducts$ = exports.usdtSwapProducts$ = void 0;
|
4
|
+
const utils_1 = require("@yuants/utils");
|
5
|
+
const protocol_1 = require("@yuants/protocol");
|
6
|
+
const sql_1 = require("@yuants/sql");
|
7
|
+
const rxjs_1 = require("rxjs");
|
8
|
+
const api_1 = require("./api");
|
9
|
+
const product$ = new rxjs_1.Subject();
|
10
|
+
const swapInstruments$ = (0, rxjs_1.defer)(() => api_1.client.getInstruments({ instType: 'SWAP' })).pipe((0, rxjs_1.repeat)({ delay: 3600000 }), (0, rxjs_1.retry)({ delay: 10000 }), (0, rxjs_1.shareReplay)(1));
|
11
|
+
exports.usdtSwapProducts$ = swapInstruments$.pipe((0, rxjs_1.mergeMap)((x) => (0, rxjs_1.from)(x.data || []).pipe((0, rxjs_1.filter)((x) => x.ctType === 'linear' && x.settleCcy === 'USDT'),
|
12
|
+
// ISSUE: 可能有 level = '' 的错误情况 (SPK-USDT and RESOLV-USDT at 2025-06-19 08 UTC)
|
13
|
+
(0, rxjs_1.filter)((x) => +x.lever > 0), (0, rxjs_1.map)((x) => ({
|
14
|
+
datasource_id: 'OKX',
|
15
|
+
product_id: (0, utils_1.encodePath)(x.instType, x.instId),
|
16
|
+
name: `${x.ctValCcy}-${x.settleCcy}-PERP`,
|
17
|
+
base_currency: x.ctValCcy,
|
18
|
+
quote_currency: x.settleCcy,
|
19
|
+
value_scale: +x.ctVal,
|
20
|
+
volume_step: +x.lotSz,
|
21
|
+
price_step: +x.tickSz,
|
22
|
+
margin_rate: 1 / +x.lever,
|
23
|
+
value_scale_unit: '',
|
24
|
+
value_based_cost: 0,
|
25
|
+
volume_based_cost: 0,
|
26
|
+
max_position: 0,
|
27
|
+
max_volume: 0,
|
28
|
+
allow_long: true,
|
29
|
+
allow_short: true,
|
30
|
+
market_id: 'OKX',
|
31
|
+
no_interest_rate: false,
|
32
|
+
})), (0, rxjs_1.tap)((x) => product$.next(x)), (0, rxjs_1.toArray)())), (0, rxjs_1.shareReplay)(1));
|
33
|
+
exports.usdtSwapProducts$.subscribe();
|
34
|
+
const marginInstruments$ = (0, rxjs_1.defer)(() => api_1.client.getInstruments({ instType: 'MARGIN' })).pipe((0, rxjs_1.repeat)({ delay: 3600000 }), (0, rxjs_1.retry)({ delay: 10000 }), (0, rxjs_1.shareReplay)(1));
|
35
|
+
exports.marginProducts$ = marginInstruments$.pipe((0, rxjs_1.mergeMap)((x) => (0, rxjs_1.from)(x.data || []).pipe(
|
36
|
+
// ISSUE: 可能有 level = '' 的错误情况 (SPK-USDT and RESOLV-USDT at 2025-06-19 08 UTC)
|
37
|
+
(0, rxjs_1.filter)((x) => +x.lever > 0), (0, rxjs_1.map)((x) => ({
|
38
|
+
datasource_id: 'OKX',
|
39
|
+
product_id: (0, utils_1.encodePath)(x.instType, x.instId),
|
40
|
+
base_currency: x.baseCcy,
|
41
|
+
quote_currency: x.quoteCcy,
|
42
|
+
value_scale: 1,
|
43
|
+
volume_step: +x.lotSz,
|
44
|
+
price_step: +x.tickSz,
|
45
|
+
margin_rate: 1 / +x.lever,
|
46
|
+
name: `${x.baseCcy}-${x.quoteCcy}-MARGIN`,
|
47
|
+
value_scale_unit: '',
|
48
|
+
value_based_cost: 0,
|
49
|
+
volume_based_cost: 0,
|
50
|
+
max_position: 0,
|
51
|
+
max_volume: 0,
|
52
|
+
allow_long: true,
|
53
|
+
allow_short: true,
|
54
|
+
market_id: 'OKX',
|
55
|
+
no_interest_rate: false,
|
56
|
+
})), (0, rxjs_1.tap)((x) => product$.next(x)), (0, rxjs_1.toArray)())), (0, rxjs_1.shareReplay)(1));
|
57
|
+
exports.marginProducts$.subscribe();
|
58
|
+
exports.mapProductIdToMarginProduct$ = exports.marginProducts$.pipe((0, rxjs_1.map)((x) => new Map(x.map((x) => [x.product_id, x])), (0, rxjs_1.shareReplay)(1)));
|
59
|
+
(0, rxjs_1.defer)(() => api_1.client.getInstruments({ instType: 'SPOT' }))
|
60
|
+
.pipe((0, rxjs_1.repeat)({ delay: 3600000 }), (0, rxjs_1.retry)({ delay: 10000 }), (0, rxjs_1.mergeMap)((x) => (0, rxjs_1.from)(x.data || []).pipe((0, rxjs_1.map)((x) => ({
|
61
|
+
datasource_id: 'OKX',
|
62
|
+
product_id: (0, utils_1.encodePath)(x.instType, x.instId),
|
63
|
+
base_currency: x.baseCcy,
|
64
|
+
quote_currency: x.quoteCcy,
|
65
|
+
value_scale: 1,
|
66
|
+
volume_step: +x.lotSz,
|
67
|
+
price_step: +x.tickSz,
|
68
|
+
margin_rate: 1,
|
69
|
+
name: `${x.baseCcy}-${x.quoteCcy}-SPOT`,
|
70
|
+
value_scale_unit: '',
|
71
|
+
value_based_cost: 0,
|
72
|
+
volume_based_cost: 0,
|
73
|
+
max_position: 0,
|
74
|
+
max_volume: 0,
|
75
|
+
allow_long: true,
|
76
|
+
allow_short: true,
|
77
|
+
market_id: 'OKX',
|
78
|
+
no_interest_rate: true,
|
79
|
+
})), (0, rxjs_1.tap)((x) => product$.next(x)), (0, rxjs_1.toArray)())))
|
80
|
+
.subscribe();
|
81
|
+
(0, sql_1.createSQLWriter)(protocol_1.Terminal.fromNodeEnv(), {
|
82
|
+
data$: product$,
|
83
|
+
tableName: 'product',
|
84
|
+
writeInterval: 1000,
|
85
|
+
conflictKeys: ['datasource_id', 'product_id'],
|
86
|
+
});
|
87
|
+
exports.mapProductIdToUsdtSwapProduct$ = exports.usdtSwapProducts$.pipe((0, rxjs_1.map)((x) => new Map(x.map((x) => [x.product_id, x]))), (0, rxjs_1.shareReplay)(1));
|
88
|
+
//# sourceMappingURL=product.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"product.js","sourceRoot":"","sources":["../src/product.ts"],"names":[],"mappings":";;;AAAA,yCAA2C;AAE3C,+CAA4C;AAC5C,qCAA8C;AAC9C,+BAA6G;AAC7G,+BAA+B;AAE/B,MAAM,QAAQ,GAAG,IAAI,cAAO,EAAY,CAAC;AAEzC,MAAM,gBAAgB,GAAG,IAAA,YAAK,EAAC,GAAG,EAAE,CAAC,YAAM,CAAC,cAAc,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,IAAI,CACpF,IAAA,aAAM,EAAC,EAAE,K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CAAC,CAAC,CAAC,CAAC,CAAC,EACpD,IAAA,kBAAW,EAAC,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/lib/quote.d.ts
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
export declare const swapMarketTickers$: import("rxjs").Observable<{
|
2
|
+
[k: string]: {
|
3
|
+
instType: string;
|
4
|
+
instId: string;
|
5
|
+
last: string;
|
6
|
+
lastSz: string;
|
7
|
+
askPx: string;
|
8
|
+
askSz: string;
|
9
|
+
bidPx: string;
|
10
|
+
bidSz: string;
|
11
|
+
open24h: string;
|
12
|
+
high24h: string;
|
13
|
+
low24h: string;
|
14
|
+
volCcy24h: string;
|
15
|
+
vol24h: string;
|
16
|
+
sodUtc0: string;
|
17
|
+
sodUtc8: string;
|
18
|
+
ts: string;
|
19
|
+
};
|
20
|
+
}>;
|
21
|
+
export declare const spotMarketTickers$: import("rxjs").Observable<{
|
22
|
+
[k: string]: {
|
23
|
+
instType: string;
|
24
|
+
instId: string;
|
25
|
+
last: string;
|
26
|
+
lastSz: string;
|
27
|
+
askPx: string;
|
28
|
+
askSz: string;
|
29
|
+
bidPx: string;
|
30
|
+
bidSz: string;
|
31
|
+
open24h: string;
|
32
|
+
high24h: string;
|
33
|
+
low24h: string;
|
34
|
+
volCcy24h: string;
|
35
|
+
vol24h: string;
|
36
|
+
sodUtc0: string;
|
37
|
+
sodUtc8: string;
|
38
|
+
ts: string;
|
39
|
+
};
|
40
|
+
}>;
|
41
|
+
export declare const swapOpenInterest$: import("rxjs").Observable<Map<string, number>>;
|
42
|
+
//# sourceMappingURL=quote.d.ts.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"quote.d.ts","sourceRoot":"","sources":["../src/quote.ts"],"names":[],"mappings":"AAmBA,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;;;;;EAW9B,CAAC;AAEF,eAAO,MAAM,kBAAkB;;;;;;;;;;;;;;;;;;;EAW9B,CAAC;AAmFF,eAAO,MAAM,iBAAiB,gDAG7B,CAAC"}
|
package/lib/quote.js
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
"use strict";
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
+
exports.swapOpenInterest$ = exports.spotMarketTickers$ = exports.swapMarketTickers$ = void 0;
|
4
|
+
const utils_1 = require("@yuants/utils");
|
5
|
+
const protocol_1 = require("@yuants/protocol");
|
6
|
+
const sql_1 = require("@yuants/sql");
|
7
|
+
const rxjs_1 = require("rxjs");
|
8
|
+
const api_1 = require("./api");
|
9
|
+
const swapTickers$ = (0, rxjs_1.defer)(() => api_1.client.getMarketTickers({ instType: 'SWAP' })).pipe((0, rxjs_1.repeat)({ delay: 5000 }), (0, rxjs_1.retry)({ delay: 5000 }), (0, rxjs_1.shareReplay)(1));
|
10
|
+
const spotTickers$ = (0, rxjs_1.defer)(() => api_1.client.getMarketTickers({ instType: 'SPOT' })).pipe((0, rxjs_1.repeat)({ delay: 5000 }), (0, rxjs_1.retry)({ delay: 5000 }), (0, rxjs_1.shareReplay)(1));
|
11
|
+
exports.swapMarketTickers$ = (0, rxjs_1.defer)(() => swapTickers$).pipe((0, rxjs_1.mergeMap)((x) => (0, rxjs_1.from)(x.data).pipe((0, rxjs_1.map)((x) => [x.instId, x]), (0, rxjs_1.toArray)(), (0, rxjs_1.map)((x) => Object.fromEntries(x)))), (0, rxjs_1.repeat)({ delay: 5000 }), (0, rxjs_1.retry)({ delay: 5000 }), (0, rxjs_1.shareReplay)(1));
|
12
|
+
exports.spotMarketTickers$ = (0, rxjs_1.defer)(() => spotTickers$).pipe((0, rxjs_1.mergeMap)((x) => (0, rxjs_1.from)(x.data).pipe((0, rxjs_1.map)((x) => [x.instId, x]), (0, rxjs_1.toArray)(), (0, rxjs_1.map)((x) => Object.fromEntries(x)))), (0, rxjs_1.repeat)({ delay: 5000 }), (0, rxjs_1.retry)({ delay: 5000 }), (0, rxjs_1.shareReplay)(1));
|
13
|
+
const quote1$ = swapTickers$.pipe((0, rxjs_1.mergeMap)((x) => x.data || []), (0, rxjs_1.map)((ticker) => ({
|
14
|
+
datasource_id: 'OKX',
|
15
|
+
product_id: (0, utils_1.encodePath)('SWAP', ticker.instId),
|
16
|
+
last_price: ticker.last,
|
17
|
+
ask_price: ticker.askPx,
|
18
|
+
bid_price: ticker.bidPx,
|
19
|
+
ask_volume: ticker.askSz,
|
20
|
+
bid_volume: ticker.bidSz,
|
21
|
+
})));
|
22
|
+
const quote2$ = spotTickers$.pipe((0, rxjs_1.mergeMap)((x) => x.data || []), (0, rxjs_1.map)((ticker) => ({
|
23
|
+
datasource_id: 'OKX',
|
24
|
+
product_id: (0, utils_1.encodePath)('SPOT', ticker.instId),
|
25
|
+
last_price: ticker.last,
|
26
|
+
ask_price: ticker.askPx,
|
27
|
+
bid_price: ticker.bidPx,
|
28
|
+
ask_volume: ticker.askSz,
|
29
|
+
bid_volume: ticker.bidSz,
|
30
|
+
})));
|
31
|
+
const quote4$ = spotTickers$.pipe((0, rxjs_1.mergeMap)((x) => x.data || []), (0, rxjs_1.map)((ticker) => ({
|
32
|
+
datasource_id: 'OKX',
|
33
|
+
product_id: (0, utils_1.encodePath)('MARGIN', ticker.instId),
|
34
|
+
last_price: ticker.last,
|
35
|
+
ask_price: ticker.askPx,
|
36
|
+
bid_price: ticker.bidPx,
|
37
|
+
ask_volume: ticker.askSz,
|
38
|
+
bid_volume: ticker.bidSz,
|
39
|
+
})));
|
40
|
+
const swapOpenInterests$ = (0, rxjs_1.defer)(() => api_1.client.getOpenInterest({ instType: 'SWAP' })).pipe((0, rxjs_1.repeat)({ delay: 10000 }), (0, rxjs_1.retry)({ delay: 10000 }), (0, rxjs_1.shareReplay)(1));
|
41
|
+
const quote3$ = swapOpenInterests$.pipe((0, rxjs_1.mergeMap)((x) => x.data || []), (0, rxjs_1.map)((x) => ({
|
42
|
+
datasource_id: 'OKX',
|
43
|
+
product_id: (0, utils_1.encodePath)('SWAP', x.instId),
|
44
|
+
open_interest: x.oi,
|
45
|
+
})));
|
46
|
+
// 合并不同来源的数据并进行合并,避免死锁
|
47
|
+
if (process.env.WRITE_QUOTE_TO_SQL === 'true') {
|
48
|
+
(0, rxjs_1.merge)(quote1$, quote2$, quote3$, quote4$)
|
49
|
+
.pipe((0, rxjs_1.groupBy)((x) => (0, utils_1.encodePath)(x.datasource_id, x.product_id)), (0, rxjs_1.mergeMap)((group$) => {
|
50
|
+
return group$.pipe((0, rxjs_1.scan)((acc, cur) => Object.assign(acc, cur), {}));
|
51
|
+
}), (0, sql_1.writeToSQL)({
|
52
|
+
terminal: protocol_1.Terminal.fromNodeEnv(),
|
53
|
+
writeInterval: 1000,
|
54
|
+
tableName: 'quote',
|
55
|
+
keyFn: (quote) => (0, utils_1.encodePath)(quote.datasource_id, quote.product_id),
|
56
|
+
conflictKeys: ['datasource_id', 'product_id'],
|
57
|
+
}))
|
58
|
+
.subscribe();
|
59
|
+
}
|
60
|
+
exports.swapOpenInterest$ = (0, rxjs_1.defer)(() => swapOpenInterests$).pipe((0, rxjs_1.map)((x) => new Map(x.data.map((x) => [x.instId, +x.oi]))), (0, rxjs_1.shareReplay)(1));
|
61
|
+
//# sourceMappingURL=quote.js.map
|
package/lib/quote.js.map
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"quote.js","sourceRoot":"","sources":["../src/quote.ts"],"names":[],"mappings":";;;AAAA,yCAA2C;AAE3C,+CAA4C;AAC5C,qCAAyC;AACzC,+BAA6G;AAC7G,+BAA+B;AAE/B,MAAM,YAAY,GAAG,IAAA,YAAK,EAAC,GAAG,EAAE,CAAC,YAAM,CAAC,gBAAgB,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,IAAI,CAClF,IAAA,aAAM,EAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACvB,IAAA,YAAK,EAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACtB,IAAA,kBAAW,EAAC,CAAC,CAAC,CACf,CAAC;AAEF,MAAM,YAAY,GAAG,IAAA,YAAK,EAAC,GAAG,EAAE,CAA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AAE;IAC7C,IAAA,YAAK,EAAC,OAAO,EAAE,OAAO,EAAE,OAAO,EAAE,OAAO,CAAC;SACtC,IAAI,CACH,IAAA,cAAO,EAAC,CAAC,CAAC,EAAE,EAAE,CAAC,IAAA,kBAAU,EAAC,CAAC,CAAC,aAAa,EAAE,CAAC,CAAC,UAAU,CAAC,CAAC,EACzD,IAAA,eAAQ,EAAC,CAAC,MAAM,EAAE,EAAE;QAClB,OAAO,MAAM,CAAC,IAAI,CAAC,IAAA,WAAI,EAAC,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,CAAC,GAAG,EAAE,GAAG,CAAC,EAAE,EAAqB,CAAC,CAAC,CAAC;IACzF,CAAC,CAAC,EACF,IAAA,gBAAU,EAAC;QACT,QAAQ,EAAE,mBAAQ,CAAC,WAAW,EAAE;QAChC,aAAa,EAAE,IAAI;QACnB,SAAS,EAAE,OAAO;QAClB,KAAK,EAAE,CAAC,KAAK,EAAE,EAAE,CAAC,IAAA,kBAAU,EAAC,KAAK,CAAC,aAAa,EAAE,KAAK,CAAC,UAAU,CAAC;QACnE,YAAY,EAAE,CAAC,eAAe,EAAE,YAAY,CAAC;KAC9C,CAAC,CACH;SACA,SAAS,EAAE,CAAC;CAChB;AAEY,QAAA,iBAAiB,GAAG,IAAA,YAAK,EAAC,GAAG,EAAE,CAAC,kBAAkB,CAAC,CAAC,IAAI,CACnE,IAAA,UAAG,EAAC,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,IAAA,kBAAW,EAAC,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"]}
|