@yuants/vendor-okx 0.23.23 → 0.23.25
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 +11 -9
- package/dist/account.js.map +1 -1
- package/dist/api-service.js +29 -0
- package/dist/api-service.js.map +1 -0
- package/dist/api.js +445 -510
- package/dist/api.js.map +1 -1
- package/dist/index.js +5 -3
- package/dist/index.js.map +1 -1
- package/dist/interest_rate.js +4 -4
- package/dist/interest_rate.js.map +1 -1
- package/dist/loan-account.js +3 -2
- package/dist/loan-account.js.map +1 -1
- package/dist/market-order.js +1 -1
- package/dist/market-order.js.map +1 -1
- package/dist/ohlc.js +3 -3
- package/dist/ohlc.js.map +1 -1
- package/dist/order-actions.js +211 -0
- package/dist/order-actions.js.map +1 -0
- package/dist/order.js +3 -3
- package/dist/order.js.map +1 -1
- package/dist/product.js +4 -4
- package/dist/product.js.map +1 -1
- package/dist/public-api.js +146 -0
- package/dist/public-api.js.map +1 -0
- package/dist/quote.js +80 -11
- package/dist/quote.js.map +1 -1
- package/dist/services.js +3 -2
- package/dist/services.js.map +1 -1
- package/dist/strategy-account.js +5 -4
- package/dist/strategy-account.js.map +1 -1
- package/dist/trade.js +6 -5
- package/dist/trade.js.map +1 -1
- package/dist/transfer.js +279 -0
- package/dist/transfer.js.map +1 -0
- package/dist/websocket.js.map +1 -1
- package/lib/account.d.ts +2 -2
- package/lib/account.d.ts.map +1 -1
- package/lib/account.js +10 -8
- package/lib/account.js.map +1 -1
- package/lib/api-service.d.ts +2 -0
- package/lib/api-service.d.ts.map +1 -0
- package/lib/api-service.js +31 -0
- package/lib/api-service.js.map +1 -0
- package/lib/api.d.ts +1251 -1558
- package/lib/api.d.ts.map +1 -1
- package/lib/api.js +477 -514
- package/lib/api.js.map +1 -1
- package/lib/index.d.ts +5 -3
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +5 -3
- package/lib/index.js.map +1 -1
- package/lib/interest_rate.js +4 -4
- package/lib/interest_rate.js.map +1 -1
- package/lib/loan-account.js +2 -1
- package/lib/loan-account.js.map +1 -1
- package/lib/market-order.d.ts.map +1 -1
- package/lib/market-order.js +1 -1
- package/lib/market-order.js.map +1 -1
- package/lib/ohlc.js +3 -3
- package/lib/ohlc.js.map +1 -1
- package/lib/order-actions.d.ts +2 -0
- package/lib/order-actions.d.ts.map +1 -0
- package/lib/order-actions.js +213 -0
- package/lib/order-actions.js.map +1 -0
- package/lib/order.js +2 -2
- package/lib/order.js.map +1 -1
- package/lib/product.js +4 -4
- package/lib/product.js.map +1 -1
- package/lib/public-api.d.ts +359 -0
- package/lib/public-api.d.ts.map +1 -0
- package/lib/public-api.js +161 -0
- package/lib/public-api.js.map +1 -0
- package/lib/quote.d.ts +1 -1
- package/lib/quote.d.ts.map +1 -1
- package/lib/quote.js +78 -9
- package/lib/quote.js.map +1 -1
- package/lib/services.js +2 -1
- package/lib/services.js.map +1 -1
- package/lib/strategy-account.d.ts +2 -2
- package/lib/strategy-account.d.ts.map +1 -1
- package/lib/strategy-account.js +4 -3
- package/lib/strategy-account.js.map +1 -1
- package/lib/trade.js +6 -5
- package/lib/trade.js.map +1 -1
- package/lib/transfer.d.ts +2 -0
- package/lib/transfer.d.ts.map +1 -0
- package/lib/transfer.js +281 -0
- package/lib/transfer.js.map +1 -0
- package/lib/websocket.js.map +1 -1
- package/package.json +13 -12
- package/temp/package-deps.json +34 -31
- package/dist/legacy_index.js +0 -562
- package/dist/legacy_index.js.map +0 -1
- package/dist/logger.js +0 -91
- package/dist/logger.js.map +0 -1
- package/lib/legacy_index.d.ts +0 -2
- package/lib/legacy_index.d.ts.map +0 -1
- package/lib/legacy_index.js +0 -564
- package/lib/legacy_index.js.map +0 -1
- package/lib/logger.d.ts +0 -21
- package/lib/logger.d.ts.map +0 -1
- package/lib/logger.js +0 -98
- package/lib/logger.js.map +0 -1
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const protocol_1 = require("@yuants/protocol");
|
|
4
|
+
const utils_1 = require("@yuants/utils");
|
|
5
|
+
const rxjs_1 = require("rxjs");
|
|
6
|
+
const account_1 = require("./account");
|
|
7
|
+
const api_1 = require("./api");
|
|
8
|
+
const product_1 = require("./product");
|
|
9
|
+
const quote_1 = require("./quote");
|
|
10
|
+
const terminal = protocol_1.Terminal.fromNodeEnv();
|
|
11
|
+
const credential = (0, api_1.getDefaultCredential)();
|
|
12
|
+
(0, rxjs_1.defer)(async () => {
|
|
13
|
+
const tradingAccountId = await (0, rxjs_1.firstValueFrom)(account_1.tradingAccountId$);
|
|
14
|
+
terminal.server.provideService('SubmitOrder', {
|
|
15
|
+
required: ['account_id'],
|
|
16
|
+
properties: {
|
|
17
|
+
account_id: { const: tradingAccountId },
|
|
18
|
+
},
|
|
19
|
+
}, async (msg) => {
|
|
20
|
+
var _a, _b, _c;
|
|
21
|
+
console.info((0, utils_1.formatTime)(Date.now()), 'SubmitOrder', JSON.stringify(msg));
|
|
22
|
+
const order = msg.req;
|
|
23
|
+
const [instType, instId] = (0, utils_1.decodePath)(order.product_id);
|
|
24
|
+
const mapOrderDirectionToSide = (direction) => {
|
|
25
|
+
switch (direction) {
|
|
26
|
+
case 'OPEN_LONG':
|
|
27
|
+
case 'CLOSE_SHORT':
|
|
28
|
+
return 'buy';
|
|
29
|
+
case 'OPEN_SHORT':
|
|
30
|
+
case 'CLOSE_LONG':
|
|
31
|
+
return 'sell';
|
|
32
|
+
}
|
|
33
|
+
throw new Error(`Unknown direction: ${direction}`);
|
|
34
|
+
};
|
|
35
|
+
const mapOrderDirectionToPosSide = (direction) => {
|
|
36
|
+
switch (direction) {
|
|
37
|
+
case 'OPEN_LONG':
|
|
38
|
+
case 'CLOSE_LONG':
|
|
39
|
+
return 'long';
|
|
40
|
+
case 'CLOSE_SHORT':
|
|
41
|
+
case 'OPEN_SHORT':
|
|
42
|
+
return 'short';
|
|
43
|
+
}
|
|
44
|
+
throw new Error(`Unknown direction: ${direction}`);
|
|
45
|
+
};
|
|
46
|
+
const mapOrderTypeToOrdType = (order_type) => {
|
|
47
|
+
switch (order_type) {
|
|
48
|
+
case 'LIMIT':
|
|
49
|
+
return 'limit';
|
|
50
|
+
case 'MARKET':
|
|
51
|
+
return 'market';
|
|
52
|
+
case 'MAKER':
|
|
53
|
+
return 'post_only';
|
|
54
|
+
}
|
|
55
|
+
throw new Error(`Unknown order type: ${order_type}`);
|
|
56
|
+
};
|
|
57
|
+
// 交易数量,表示要购买或者出售的数量。
|
|
58
|
+
// 当币币/币币杠杆以限价买入和卖出时,指交易货币数量。
|
|
59
|
+
// 当币币杠杆以市价买入时,指计价货币的数量。
|
|
60
|
+
// 当币币杠杆以市价卖出时,指交易货币的数量。
|
|
61
|
+
// 对于币币市价单,单位由 tgtCcy 决定
|
|
62
|
+
// 当交割、永续、期权买入和卖出时,指合约张数。
|
|
63
|
+
const mapOrderVolumeToSz = async (order) => {
|
|
64
|
+
if (instType === 'SWAP') {
|
|
65
|
+
return order.volume;
|
|
66
|
+
}
|
|
67
|
+
if (instType === 'MARGIN') {
|
|
68
|
+
if (order.order_type === 'LIMIT') {
|
|
69
|
+
return order.volume;
|
|
70
|
+
}
|
|
71
|
+
if (order.order_type === 'MAKER') {
|
|
72
|
+
return order.volume;
|
|
73
|
+
}
|
|
74
|
+
if (order.order_type === 'MARKET') {
|
|
75
|
+
if (order.order_direction === 'OPEN_SHORT' || order.order_direction === 'CLOSE_LONG') {
|
|
76
|
+
return order.volume;
|
|
77
|
+
}
|
|
78
|
+
//
|
|
79
|
+
const price = await (0, rxjs_1.firstValueFrom)(quote_1.spotMarketTickers$.pipe((0, rxjs_1.map)((x) => mapOrderDirectionToPosSide(order.order_direction) === 'long'
|
|
80
|
+
? +x[instId].askPx
|
|
81
|
+
: +x[instId].bidPx)));
|
|
82
|
+
if (!price) {
|
|
83
|
+
throw new Error(`invalid tick: ${price}`);
|
|
84
|
+
}
|
|
85
|
+
console.info((0, utils_1.formatTime)(Date.now()), 'SubmitOrder', 'price', price);
|
|
86
|
+
const theProduct = await (0, rxjs_1.firstValueFrom)(product_1.productService.mapProductIdToProduct$.pipe((0, rxjs_1.map)((x) => x.get(order.product_id))));
|
|
87
|
+
if (!theProduct) {
|
|
88
|
+
throw new Error(`Unknown product: ${order.position_id}`);
|
|
89
|
+
}
|
|
90
|
+
return (0, utils_1.roundToStep)(order.volume * price, theProduct.volume_step);
|
|
91
|
+
}
|
|
92
|
+
return 0;
|
|
93
|
+
}
|
|
94
|
+
if (instType === 'SPOT') {
|
|
95
|
+
return order.volume;
|
|
96
|
+
}
|
|
97
|
+
throw new Error(`Unknown instType: ${instType}`);
|
|
98
|
+
};
|
|
99
|
+
const params = {
|
|
100
|
+
instId,
|
|
101
|
+
tdMode: instType === 'SPOT' ? 'cash' : 'cross',
|
|
102
|
+
side: mapOrderDirectionToSide(order.order_direction),
|
|
103
|
+
posSide: instType === 'MARGIN' || instType === 'SPOT'
|
|
104
|
+
? 'net'
|
|
105
|
+
: mapOrderDirectionToPosSide(order.order_direction),
|
|
106
|
+
ordType: mapOrderTypeToOrdType(order.order_type),
|
|
107
|
+
sz: (await mapOrderVolumeToSz(order)).toString(),
|
|
108
|
+
tgtCcy: instType === 'SPOT' && order.order_type === 'MARKET' ? 'base_ccy' : undefined,
|
|
109
|
+
reduceOnly: instType === 'MARGIN' && ['CLOSE_LONG', 'CLOSE_SHORT'].includes((_a = order.order_direction) !== null && _a !== void 0 ? _a : '')
|
|
110
|
+
? 'true'
|
|
111
|
+
: undefined,
|
|
112
|
+
px: order.order_type === 'LIMIT' || order.order_type === 'MAKER' ? order.price.toString() : undefined,
|
|
113
|
+
ccy: instType === 'MARGIN' ? 'USDT' : undefined,
|
|
114
|
+
};
|
|
115
|
+
console.info((0, utils_1.formatTime)(Date.now()), 'SubmitOrder', 'params', JSON.stringify(params));
|
|
116
|
+
const res = await (0, api_1.postTradeOrder)(credential, params);
|
|
117
|
+
if (res.code === '0' && ((_c = (_b = res.data) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.ordId)) {
|
|
118
|
+
return {
|
|
119
|
+
res: {
|
|
120
|
+
code: 0,
|
|
121
|
+
message: 'OK',
|
|
122
|
+
data: {
|
|
123
|
+
order_id: res.data[0].ordId,
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
return { res: { code: +res.code, message: res.msg } };
|
|
129
|
+
});
|
|
130
|
+
terminal.server.provideService('ModifyOrder', {
|
|
131
|
+
required: ['account_id'],
|
|
132
|
+
properties: {
|
|
133
|
+
account_id: { const: tradingAccountId },
|
|
134
|
+
},
|
|
135
|
+
}, async (msg) => {
|
|
136
|
+
console.info((0, utils_1.formatTime)(Date.now()), 'ModifyOrder', JSON.stringify(msg));
|
|
137
|
+
const order = msg.req;
|
|
138
|
+
const [instType, instId] = (0, utils_1.decodePath)(order.product_id);
|
|
139
|
+
const params = {
|
|
140
|
+
instId,
|
|
141
|
+
ordId: order.order_id, // 使用现有订单ID
|
|
142
|
+
};
|
|
143
|
+
// 如果需要修改价格
|
|
144
|
+
if (order.price !== undefined) {
|
|
145
|
+
params.newPx = order.price.toString();
|
|
146
|
+
}
|
|
147
|
+
// 如果需要修改数量
|
|
148
|
+
if (order.volume !== undefined) {
|
|
149
|
+
// 处理数量修改,类似于 SubmitOrder 中的逻辑
|
|
150
|
+
if (instType === 'SWAP') {
|
|
151
|
+
params.newSz = order.volume.toString();
|
|
152
|
+
}
|
|
153
|
+
else if (instType === 'SPOT') {
|
|
154
|
+
params.newSz = order.volume.toString();
|
|
155
|
+
}
|
|
156
|
+
else if (instType === 'MARGIN') {
|
|
157
|
+
if (order.order_type === 'LIMIT') {
|
|
158
|
+
params.newSz = order.volume.toString();
|
|
159
|
+
}
|
|
160
|
+
if (order.order_type === 'MAKER') {
|
|
161
|
+
params.newSz = order.volume.toString();
|
|
162
|
+
}
|
|
163
|
+
if (order.order_type === 'MARKET') {
|
|
164
|
+
// 对于市价单,可能需要根据当前价格计算新的数量
|
|
165
|
+
const price = await (0, rxjs_1.firstValueFrom)(quote_1.spotMarketTickers$.pipe((0, rxjs_1.map)((x) => order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_SHORT'
|
|
166
|
+
? +x[instId].askPx
|
|
167
|
+
: +x[instId].bidPx)));
|
|
168
|
+
if (!price) {
|
|
169
|
+
throw new Error(`invalid tick: ${price}`);
|
|
170
|
+
}
|
|
171
|
+
console.info((0, utils_1.formatTime)(Date.now()), 'ModifyOrder', 'price', price);
|
|
172
|
+
const theProduct = await (0, rxjs_1.firstValueFrom)(product_1.productService.mapProductIdToProduct$.pipe((0, rxjs_1.map)((x) => x.get(order.product_id))));
|
|
173
|
+
if (!theProduct) {
|
|
174
|
+
throw new Error(`Unknown product: ${order.position_id}`);
|
|
175
|
+
}
|
|
176
|
+
params.newSz = (0, utils_1.roundToStep)(order.volume * price, theProduct.volume_step).toString();
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
throw new Error(`Unknown instType: ${instType}`);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
console.info((0, utils_1.formatTime)(Date.now()), 'ModifyOrder', 'params', JSON.stringify(params));
|
|
184
|
+
const res = await (0, api_1.postTradeAmendOrder)(credential, params);
|
|
185
|
+
if (res.code !== '0') {
|
|
186
|
+
return {
|
|
187
|
+
res: {
|
|
188
|
+
code: +res.code,
|
|
189
|
+
message: res.msg,
|
|
190
|
+
},
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
return { res: { code: 0, message: 'OK' } };
|
|
194
|
+
});
|
|
195
|
+
terminal.server.provideService('CancelOrder', {
|
|
196
|
+
required: ['account_id'],
|
|
197
|
+
properties: {
|
|
198
|
+
account_id: { const: tradingAccountId },
|
|
199
|
+
},
|
|
200
|
+
}, (msg) => (0, rxjs_1.defer)(async () => {
|
|
201
|
+
const order = msg.req;
|
|
202
|
+
const [instType, instId] = (0, utils_1.decodePath)(order.product_id);
|
|
203
|
+
const res = await (0, api_1.postTradeCancelOrder)(credential, {
|
|
204
|
+
instId,
|
|
205
|
+
ordId: order.order_id,
|
|
206
|
+
});
|
|
207
|
+
if (res.code !== '0') {
|
|
208
|
+
return { res: { code: +res.code, message: res.msg } };
|
|
209
|
+
}
|
|
210
|
+
return { res: { code: 0, message: 'OK' } };
|
|
211
|
+
}));
|
|
212
|
+
}).subscribe();
|
|
213
|
+
//# sourceMappingURL=order-actions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"order-actions.js","sourceRoot":"","sources":["../src/order-actions.ts"],"names":[],"mappings":";;AACA,+CAA4C;AAC5C,yCAAoE;AACpE,+BAAkD;AAClD,uCAA8C;AAC9C,+BAAwG;AACxG,uCAA2C;AAC3C,mCAA6C;AAE7C,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,UAAU,GAAG,IAAA,0BAAoB,GAAE,CAAC;AAE1C,IAAA,YAAK,EAAC,KAAK,IAAI,EAAE;IACf,MAAM,gBAAgB,GAAG,MAAM,IAAA,qBAAc,EAAC,2BAAiB,CAAC,CAAC;IACjE,QAAQ,CAAC,MAAM,CAAC,cAAc,CAC5B,aAAa,EACb;QACE,QAAQ,EAAE,CAAC,YAAY,CAAC;QACxB,UAAU,EAAE;YACV,UAAU,EAAE,EAAE,KAAK,EAAE,gBAAgB,EAAE;SACxC;KACF,EACD,KAAK,EAAE,GAAG,EAAE,EAAE;;QACZ,OAAO,CAAC,IAAI,CAAC,IAAA,kBAAU,EAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,aAAa,EAAE,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC;QACzE,MAAM,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC;QACtB,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,GAAG,IAAA,kBAAU,EAAC,K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,CAAC;SACH;QAED,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,CAAC;IAC7C,CAAC,CACF,CAAC;IAEF,QAAQ,CAAC,MAAM,CAAC,cAAc,CAC5B,aAAa,EACb;QACE,QAAQ,EAAE,CAAC,YAAY,CAAC;QACxB,UAAU,EAAE;YACV,UAAU,EAAE,EAAE,KAAK,EAAE,gBAAgB,EAAE;SACxC;KACF,EACD,CAAC,GAAG,EAAE,EAAE,CACN,IAAA,YAAK,EAAC,KAAK,IAAI,EAAE;QACf,MAAM,KAAK,GAAG,GAAG,CAAC,GAAG,CAAC;QACtB,MAAM,CAAC,QAAQ,EAAE,MAAM,CAAC,GAAG,IAAA,kBAAU,EAAC,KAAK,CAAC,UAAU,CAAC,CAAC;QACxD,MAAM,GAAG,GAAG,MAAM,IAAA,0BAAoB,EAAC,UAAU,EAAE;YACjD,MAAM;YACN,KAAK,EAAE,KAAK,CAAC,QAAS;SACvB,CAAC,CAAC;QACH,IAAI,GAAG,CAAC,IAAI,KAAK,GAAG,EAAE;YACpB,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,GAAG,CAAC,IAAI,EAAE,OAAO,EAAE,GAAG,CAAC,GAAG,EAAE,EAAE,CAAC;SACvD;QACD,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,CAAC;IAC7C,CAAC,CAAC,CACL,CAAC;AACJ,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC","sourcesContent":["import { IOrder } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { decodePath, formatTime, roundToStep } from '@yuants/utils';\nimport { defer, firstValueFrom, map } from 'rxjs';\nimport { tradingAccountId$ } from './account';\nimport { getDefaultCredential, postTradeAmendOrder, postTradeCancelOrder, postTradeOrder } from './api';\nimport { productService } from './product';\nimport { spotMarketTickers$ } from './quote';\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst credential = getDefaultCredential();\n\ndefer(async () => {\n const tradingAccountId = await firstValueFrom(tradingAccountId$);\n terminal.server.provideService<IOrder, { order_id?: string }>(\n 'SubmitOrder',\n {\n required: ['account_id'],\n properties: {\n account_id: { const: tradingAccountId },\n },\n },\n async (msg) => {\n console.info(formatTime(Date.now()), 'SubmitOrder', JSON.stringify(msg));\n const order = msg.req;\n const [instType, instId] = decodePath(order.product_id);\n\n const mapOrderDirectionToSide = (direction?: string) => {\n switch (direction) {\n case 'OPEN_LONG':\n case 'CLOSE_SHORT':\n return 'buy';\n case 'OPEN_SHORT':\n case 'CLOSE_LONG':\n return 'sell';\n }\n throw new Error(`Unknown direction: ${direction}`);\n };\n const mapOrderDirectionToPosSide = (direction?: string) => {\n switch (direction) {\n case 'OPEN_LONG':\n case 'CLOSE_LONG':\n return 'long';\n case 'CLOSE_SHORT':\n case 'OPEN_SHORT':\n return 'short';\n }\n throw new Error(`Unknown direction: ${direction}`);\n };\n const mapOrderTypeToOrdType = (order_type?: string) => {\n switch (order_type) {\n case 'LIMIT':\n return 'limit';\n case 'MARKET':\n return 'market';\n case 'MAKER':\n return 'post_only';\n }\n throw new Error(`Unknown order type: ${order_type}`);\n };\n\n // 交易数量,表示要购买或者出售的数量。\n // 当币币/币币杠杆以限价买入和卖出时,指交易货币数量。\n // 当币币杠杆以市价买入时,指计价货币的数量。\n // 当币币杠杆以市价卖出时,指交易货币的数量。\n // 对于币币市价单,单位由 tgtCcy 决定\n // 当交割、永续、期权买入和卖出时,指合约张数。\n const mapOrderVolumeToSz = async (order: IOrder) => {\n if (instType === 'SWAP') {\n return order.volume;\n }\n if (instType === 'MARGIN') {\n if (order.order_type === 'LIMIT') {\n return order.volume;\n }\n if (order.order_type === 'MAKER') {\n return order.volume;\n }\n if (order.order_type === 'MARKET') {\n if (order.order_direction === 'OPEN_SHORT' || order.order_direction === 'CLOSE_LONG') {\n return order.volume;\n }\n //\n const price = await firstValueFrom(\n spotMarketTickers$.pipe(\n map((x) =>\n mapOrderDirectionToPosSide(order.order_direction) === 'long'\n ? +x[instId].askPx\n : +x[instId].bidPx,\n ),\n ),\n );\n if (!price) {\n throw new Error(`invalid tick: ${price}`);\n }\n console.info(formatTime(Date.now()), 'SubmitOrder', 'price', price);\n const theProduct = await firstValueFrom(\n productService.mapProductIdToProduct$.pipe(map((x) => x.get(order.product_id))),\n );\n if (!theProduct) {\n throw new Error(`Unknown product: ${order.position_id}`);\n }\n return roundToStep(order.volume * price, theProduct.volume_step!);\n }\n\n return 0;\n }\n\n if (instType === 'SPOT') {\n return order.volume;\n }\n\n throw new Error(`Unknown instType: ${instType}`);\n };\n\n const params = {\n instId,\n tdMode: instType === 'SPOT' ? 'cash' : 'cross',\n side: mapOrderDirectionToSide(order.order_direction),\n posSide:\n instType === 'MARGIN' || instType === 'SPOT'\n ? 'net'\n : mapOrderDirectionToPosSide(order.order_direction),\n ordType: mapOrderTypeToOrdType(order.order_type),\n sz: (await mapOrderVolumeToSz(order)).toString(),\n tgtCcy: instType === 'SPOT' && order.order_type === 'MARKET' ? 'base_ccy' : undefined,\n reduceOnly:\n instType === 'MARGIN' && ['CLOSE_LONG', 'CLOSE_SHORT'].includes(order.order_direction ?? '')\n ? 'true'\n : undefined,\n px:\n order.order_type === 'LIMIT' || order.order_type === 'MAKER' ? order.price!.toString() : undefined,\n ccy: instType === 'MARGIN' ? 'USDT' : undefined,\n };\n console.info(formatTime(Date.now()), 'SubmitOrder', 'params', JSON.stringify(params));\n const res = await postTradeOrder(credential, params);\n if (res.code === '0' && res.data?.[0]?.ordId) {\n return {\n res: {\n code: 0,\n message: 'OK',\n data: {\n order_id: res.data[0].ordId,\n },\n },\n };\n }\n return { res: { code: +res.code, message: res.msg } };\n },\n );\n\n terminal.server.provideService<IOrder>(\n 'ModifyOrder',\n {\n required: ['account_id'],\n properties: {\n account_id: { const: tradingAccountId },\n },\n },\n async (msg) => {\n console.info(formatTime(Date.now()), 'ModifyOrder', JSON.stringify(msg));\n const order = msg.req;\n const [instType, instId] = decodePath(order.product_id);\n\n const params: {\n instId: string;\n ordId: string;\n newPx?: string;\n newSz?: string;\n } = {\n instId,\n ordId: order.order_id!, // 使用现有订单ID\n };\n\n // 如果需要修改价格\n if (order.price !== undefined) {\n params.newPx = order.price.toString();\n }\n\n // 如果需要修改数量\n if (order.volume !== undefined) {\n // 处理数量修改,类似于 SubmitOrder 中的逻辑\n if (instType === 'SWAP') {\n params.newSz = order.volume.toString();\n } else if (instType === 'SPOT') {\n params.newSz = order.volume.toString();\n } else if (instType === 'MARGIN') {\n if (order.order_type === 'LIMIT') {\n params.newSz = order.volume.toString();\n }\n if (order.order_type === 'MAKER') {\n params.newSz = order.volume.toString();\n }\n if (order.order_type === 'MARKET') {\n // 对于市价单,可能需要根据当前价格计算新的数量\n const price = await firstValueFrom(\n spotMarketTickers$.pipe(\n map((x) =>\n order.order_direction === 'OPEN_LONG' || order.order_direction === 'CLOSE_SHORT'\n ? +x[instId].askPx\n : +x[instId].bidPx,\n ),\n ),\n );\n if (!price) {\n throw new Error(`invalid tick: ${price}`);\n }\n console.info(formatTime(Date.now()), 'ModifyOrder', 'price', price);\n const theProduct = await firstValueFrom(\n productService.mapProductIdToProduct$.pipe(map((x) => x.get(order.product_id))),\n );\n if (!theProduct) {\n throw new Error(`Unknown product: ${order.position_id}`);\n }\n params.newSz = roundToStep(order.volume * price, theProduct.volume_step!).toString();\n }\n } else {\n throw new Error(`Unknown instType: ${instType}`);\n }\n }\n\n console.info(formatTime(Date.now()), 'ModifyOrder', 'params', JSON.stringify(params));\n\n const res = await postTradeAmendOrder(credential, params);\n if (res.code !== '0') {\n return {\n res: {\n code: +res.code,\n message: res.msg,\n },\n };\n }\n\n return { res: { code: 0, message: 'OK' } };\n },\n );\n\n terminal.server.provideService<IOrder>(\n 'CancelOrder',\n {\n required: ['account_id'],\n properties: {\n account_id: { const: tradingAccountId },\n },\n },\n (msg) =>\n defer(async () => {\n const order = msg.req;\n const [instType, instId] = decodePath(order.product_id);\n const res = await postTradeCancelOrder(credential, {\n instId,\n ordId: order.order_id!,\n });\n if (res.code !== '0') {\n return { res: { code: +res.code, message: res.msg } };\n }\n return { res: { code: 0, message: 'OK' } };\n }),\n );\n}).subscribe();\n"]}
|
package/lib/order.js
CHANGED
|
@@ -75,8 +75,8 @@ const makeOrder = (x, account_id) => {
|
|
|
75
75
|
const TRADING_ACCOUNT_ID = `okx/${uid}/trading`;
|
|
76
76
|
const FUNDING_ACCOUNT_ID = `okx/${uid}/funding/USDT`;
|
|
77
77
|
const EARNING_ACCOUNT_ID = `okx/${uid}/earning/USDT`;
|
|
78
|
-
const swapHistoryOrders = (0, rxjs_1.defer)(() => api_1.
|
|
79
|
-
const swapPendingOrders = (0, rxjs_1.defer)(() => api_1.
|
|
78
|
+
const swapHistoryOrders = (0, rxjs_1.defer)(() => (0, api_1.getTradeOrdersHistory)((0, api_1.getDefaultCredential)(), { instType: 'SWAP' })).pipe((0, rxjs_1.repeat)({ delay: 1000 }), (0, rxjs_1.retry)({ delay: 1000 }), (0, rxjs_1.shareReplay)(1));
|
|
79
|
+
const swapPendingOrders = (0, rxjs_1.defer)(() => (0, api_1.getTradeOrdersPending)((0, api_1.getDefaultCredential)(), { instType: 'SWAP' })).pipe((0, rxjs_1.repeat)({ delay: 1000 }), (0, rxjs_1.retry)({ delay: 1000 }), (0, rxjs_1.shareReplay)(1));
|
|
80
80
|
const ordersFromHistoryOrder$ = swapHistoryOrders.pipe(
|
|
81
81
|
//
|
|
82
82
|
(0, rxjs_1.mergeMap)((x) => (0, rxjs_1.from)(x.data || []).pipe(
|
package/lib/order.js.map
CHANGED
|
@@ -1 +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,+
|
|
1
|
+
{"version":3,"file":"order.js","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,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 { getDefaultCredential, getTradeOrdersHistory, getTradeOrdersPending } 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 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(() =>\n getTradeOrdersHistory(getDefaultCredential(), { instType: 'SWAP' }),\n ).pipe(repeat({ delay: 1000 }), retry({ delay: 1000 }), shareReplay(1));\n\n const swapPendingOrders = defer(() =>\n getTradeOrdersPending(getDefaultCredential(), { instType: 'SWAP' }),\n ).pipe(repeat({ delay: 1000 }), retry({ delay: 1000 }), shareReplay(1));\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.js
CHANGED
|
@@ -4,16 +4,16 @@ exports.productService = void 0;
|
|
|
4
4
|
const data_product_1 = require("@yuants/data-product");
|
|
5
5
|
const protocol_1 = require("@yuants/protocol");
|
|
6
6
|
const utils_1 = require("@yuants/utils");
|
|
7
|
-
const
|
|
7
|
+
const public_api_1 = require("./public-api");
|
|
8
8
|
const terminal = protocol_1.Terminal.fromNodeEnv();
|
|
9
9
|
// Provide QueryProducts service with the new design
|
|
10
10
|
exports.productService = (0, data_product_1.provideQueryProductsService)(terminal, 'OKX', async (req) => {
|
|
11
11
|
const products = [];
|
|
12
12
|
// Get all instrument types in parallel
|
|
13
13
|
const [swapInstruments, marginInstruments, spotInstruments] = await Promise.all([
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
14
|
+
(0, public_api_1.getInstruments)({ instType: 'SWAP' }),
|
|
15
|
+
(0, public_api_1.getInstruments)({ instType: 'MARGIN' }),
|
|
16
|
+
(0, public_api_1.getInstruments)({ instType: 'SPOT' }),
|
|
17
17
|
]);
|
|
18
18
|
// Get USDT swap products
|
|
19
19
|
for (const instrument of swapInstruments.data || []) {
|
package/lib/product.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"product.js","sourceRoot":"","sources":["../src/product.ts"],"names":[],"mappings":";;;AAAA,uDAAoG;AACpG,+CAA4C;AAC5C,yCAA2C;AAC3C
|
|
1
|
+
{"version":3,"file":"product.js","sourceRoot":"","sources":["../src/product.ts"],"names":[],"mappings":";;;AAAA,uDAAoG;AACpG,+CAA4C;AAC5C,yCAA2C;AAC3C,6CAA8C;AAE9C,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,oDAAoD;AACvC,QAAA,cAAc,GAAG,IAAA,0CAA2B,EACvD,QAAQ,EACR,KAAK,EACL,KAAK,EAAE,GAA0B,EAAuB,EAAE;IACxD,MAAM,QAAQ,GAAe,EAAE,CAAC;IAEhC,uCAAuC;IACvC,MAAM,CAAC,eAAe,EAAE,iBAAiB,EAAE,eAAe,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QAC9E,IAAA,2BAAc,EAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC;QACpC,IA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zD,gBAAgB,EAAE,EAAE;YACpB,gBAAgB,EAAE,CAAC;YACnB,iBAAiB,EAAE,CAAC;YACpB,YAAY,EAAE,CAAC;YACf,UAAU,EAAE,CAAC;YACb,UAAU,EAAE,IAAI;YAChB,WAAW,EAAE,IAAI;YACjB,SAAS,EAAE,KAAK;YAChB,gBAAgB,EAAE,IAAI;SACvB,CAAC,CAAC;KACJ;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC,EACD;IACE,qBAAqB,EAAE,OAAQ;CAChC,CACF,CAAC","sourcesContent":["import { IProduct, IQueryProductsRequest, provideQueryProductsService } from '@yuants/data-product';\nimport { Terminal } from '@yuants/protocol';\nimport { encodePath } from '@yuants/utils';\nimport { getInstruments } from './public-api';\n\nconst terminal = Terminal.fromNodeEnv();\n\n// Provide QueryProducts service with the new design\nexport const productService = provideQueryProductsService(\n terminal,\n 'OKX',\n async (req: IQueryProductsRequest): Promise<IProduct[]> => {\n const products: IProduct[] = [];\n\n // Get all instrument types in parallel\n const [swapInstruments, marginInstruments, spotInstruments] = await Promise.all([\n getInstruments({ instType: 'SWAP' }),\n getInstruments({ instType: 'MARGIN' }),\n getInstruments({ instType: 'SPOT' }),\n ]);\n\n // Get USDT swap products\n for (const instrument of swapInstruments.data || []) {\n if (instrument.ctType === 'linear' && instrument.settleCcy === 'USDT' && +instrument.lever > 0) {\n products.push({\n datasource_id: 'OKX',\n product_id: encodePath(instrument.instType, instrument.instId),\n name: `${instrument.ctValCcy}-${instrument.settleCcy}-PERP`,\n base_currency: instrument.ctValCcy,\n quote_currency: instrument.settleCcy,\n value_scale: +instrument.ctVal,\n volume_step: +instrument.lotSz,\n price_step: +instrument.tickSz,\n margin_rate: 1 / +instrument.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 }\n\n // Get margin products\n for (const instrument of marginInstruments.data || []) {\n if (+instrument.lever > 0) {\n products.push({\n datasource_id: 'OKX',\n product_id: encodePath(instrument.instType, instrument.instId),\n base_currency: instrument.baseCcy,\n quote_currency: instrument.quoteCcy,\n value_scale: 1,\n volume_step: +instrument.lotSz,\n price_step: +instrument.tickSz,\n margin_rate: 1 / +instrument.lever,\n name: `${instrument.baseCcy}-${instrument.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 }\n\n // Get spot products\n for (const instrument of spotInstruments.data || []) {\n products.push({\n datasource_id: 'OKX',\n product_id: encodePath(instrument.instType, instrument.instId),\n base_currency: instrument.baseCcy,\n quote_currency: instrument.quoteCcy,\n value_scale: 1,\n volume_step: +instrument.lotSz,\n price_step: +instrument.tickSz,\n margin_rate: 1,\n name: `${instrument.baseCcy}-${instrument.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\n return products;\n },\n {\n auto_refresh_interval: 3600_000,\n },\n);\n"]}
|