@yuants/vendor-okx 0.29.0 → 0.29.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/experimental/exchange.js +3 -3
- package/dist/experimental/exchange.js.map +1 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/orders/cancelOrder.js +1 -1
- package/dist/orders/cancelOrder.js.map +1 -1
- package/dist/orders/modifyOrder.js +1 -1
- package/dist/orders/modifyOrder.js.map +1 -1
- package/dist/orders/submitOrder.js +2 -2
- package/dist/orders/submitOrder.js.map +1 -1
- package/dist/public-data/new-quote.js +192 -0
- package/dist/public-data/new-quote.js.map +1 -0
- package/dist/public-data/quote.js +11 -3
- package/dist/public-data/quote.js.map +1 -1
- package/dist/services.js +1 -1
- package/dist/services.js.map +1 -1
- package/dist/ws.js +3 -0
- package/dist/ws.js.map +1 -1
- package/lib/experimental/exchange.js +3 -3
- package/lib/experimental/exchange.js.map +1 -1
- package/lib/index.d.ts +1 -0
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/orders/cancelOrder.js +1 -1
- package/lib/orders/cancelOrder.js.map +1 -1
- package/lib/orders/modifyOrder.js +1 -1
- package/lib/orders/modifyOrder.js.map +1 -1
- package/lib/orders/submitOrder.js +2 -2
- package/lib/orders/submitOrder.js.map +1 -1
- package/lib/public-data/new-quote.d.ts +22 -0
- package/lib/public-data/new-quote.d.ts.map +1 -0
- package/lib/public-data/new-quote.js +195 -0
- package/lib/public-data/new-quote.js.map +1 -0
- package/lib/public-data/quote.d.ts.map +1 -1
- package/lib/public-data/quote.js +10 -2
- package/lib/public-data/quote.js.map +1 -1
- package/lib/services.js +1 -1
- package/lib/services.js.map +1 -1
- package/lib/ws.d.ts +15 -0
- package/lib/ws.d.ts.map +1 -1
- package/lib/ws.js +5 -1
- package/lib/ws.js.map +1 -1
- package/package.json +1 -1
- package/temp/package-deps.json +12 -14
- package/dist/experimental/cancelOrder.js +0 -13
- package/dist/experimental/cancelOrder.js.map +0 -1
- package/dist/experimental/modifyOrder.js +0 -58
- package/dist/experimental/modifyOrder.js.map +0 -1
- package/dist/experimental/submitOrder.js +0 -110
- package/dist/experimental/submitOrder.js.map +0 -1
- package/lib/experimental/cancelOrder.d.ts +0 -4
- package/lib/experimental/cancelOrder.d.ts.map +0 -1
- package/lib/experimental/cancelOrder.js +0 -17
- package/lib/experimental/cancelOrder.js.map +0 -1
- package/lib/experimental/modifyOrder.d.ts +0 -4
- package/lib/experimental/modifyOrder.d.ts.map +0 -1
- package/lib/experimental/modifyOrder.js +0 -62
- package/lib/experimental/modifyOrder.js.map +0 -1
- package/lib/experimental/submitOrder.d.ts +0 -6
- package/lib/experimental/submitOrder.d.ts.map +0 -1
- package/lib/experimental/submitOrder.js +0 -114
- package/lib/experimental/submitOrder.js.map +0 -1
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"modifyOrder.d.ts","sourceRoot":"","sources":["../../src/experimental/modifyOrder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAG5C,OAAO,EAAE,WAAW,EAAuB,MAAM,oBAAoB,CAAC;AAItE,eAAO,MAAM,WAAW,eAAsB,WAAW,SAAS,MAAM,kBAiEvE,CAAC"}
|
|
@@ -1,62 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.modifyOrder = void 0;
|
|
4
|
-
const utils_1 = require("@yuants/utils");
|
|
5
|
-
const rxjs_1 = require("rxjs");
|
|
6
|
-
const private_api_1 = require("../api/private-api");
|
|
7
|
-
const product_1 = require("../public-data/product");
|
|
8
|
-
const quote_1 = require("../public-data/quote");
|
|
9
|
-
const modifyOrder = async (credential, order) => {
|
|
10
|
-
const [, instType, instId] = (0, utils_1.decodePath)(order.product_id);
|
|
11
|
-
const params = {
|
|
12
|
-
instId,
|
|
13
|
-
ordId: order.order_id, // 使用现有订单ID
|
|
14
|
-
};
|
|
15
|
-
// 如果需要修改价格
|
|
16
|
-
if (order.price !== undefined) {
|
|
17
|
-
params.newPx = order.price.toString();
|
|
18
|
-
}
|
|
19
|
-
// 如果需要修改数量
|
|
20
|
-
if (order.volume !== undefined) {
|
|
21
|
-
// 处理数量修改,类似于 SubmitOrder 中的逻辑
|
|
22
|
-
if (instType === 'SWAP') {
|
|
23
|
-
params.newSz = order.volume.toString();
|
|
24
|
-
}
|
|
25
|
-
else if (instType === 'SPOT') {
|
|
26
|
-
params.newSz = order.volume.toString();
|
|
27
|
-
}
|
|
28
|
-
else if (instType === 'MARGIN') {
|
|
29
|
-
if (order.order_type === 'LIMIT') {
|
|
30
|
-
params.newSz = order.volume.toString();
|
|
31
|
-
}
|
|
32
|
-
if (order.order_type === 'MAKER') {
|
|
33
|
-
params.newSz = order.volume.toString();
|
|
34
|
-
}
|
|
35
|
-
if (order.order_type === 'MARKET') {
|
|
36
|
-
// 对于市价单,可能需要根据当前价格计算新的数量
|
|
37
|
-
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'
|
|
38
|
-
? +x[instId].askPx
|
|
39
|
-
: +x[instId].bidPx)));
|
|
40
|
-
if (!price) {
|
|
41
|
-
throw new Error(`invalid tick: ${price}`);
|
|
42
|
-
}
|
|
43
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'ModifyOrder', 'price', price);
|
|
44
|
-
const theProduct = await (0, rxjs_1.firstValueFrom)(product_1.productService.mapProductIdToProduct$.pipe((0, rxjs_1.map)((x) => x.get(order.product_id))));
|
|
45
|
-
if (!theProduct) {
|
|
46
|
-
throw new Error(`Unknown product: ${order.position_id}`);
|
|
47
|
-
}
|
|
48
|
-
params.newSz = (0, utils_1.roundToStep)(order.volume * price, theProduct.volume_step).toString();
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
else {
|
|
52
|
-
throw new Error(`Unknown instType: ${instType}`);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'ModifyOrder', 'params', JSON.stringify(params));
|
|
56
|
-
const res = await (0, private_api_1.postTradeAmendOrder)(credential, params);
|
|
57
|
-
if (res.code !== '0') {
|
|
58
|
-
throw new Error(`ModifyOrder failed: code=${res.code}, msg=${res.msg}`);
|
|
59
|
-
}
|
|
60
|
-
};
|
|
61
|
-
exports.modifyOrder = modifyOrder;
|
|
62
|
-
//# sourceMappingURL=modifyOrder.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"modifyOrder.js","sourceRoot":"","sources":["../../src/experimental/modifyOrder.ts"],"names":[],"mappings":";;;AACA,yCAAoE;AACpE,+BAA2C;AAC3C,oDAAsE;AACtE,oDAAwD;AACxD,gDAA0D;AAEnD,MAAM,WAAW,GAAG,KAAK,EAAE,UAAuB,EAAE,KAAa,EAAE,EAAE;IAC1E,MAAM,CAAC,EAAE,QAAQ,EAAE,MAAM,CAAC,GAAG,IAAA,kBAAU,EAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IAC1D,MAAM,MAAM,GAKR;QACF,MAAM;QACN,KAAK,EAAE,KAAK,CAAC,QAAS,EAAE,WAAW;KACpC,CAAC;IAEF,WAAW;IACX,IAAI,KAAK,CAAC,KAAK,KAAK,SAAS,EAAE;QAC7B,MAAM,CAAC,KAAK,GAAG,KAAK,CAAC,KAAK,CAAC,QAAQ,EAAE,CAAC;KACvC;IAED,WAAW;IACX,IAAI,KAAK,CAAC,MAAM,KAAK,SAAS,EAAE;QAC9B,8BAA8B;QAC9B,IAAI,QAAQ,KAAK,MAAM,EAAE;YACvB,MAAM,CAAC,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,QAAQ,EAAE,CAAC;SACxC;aAAM,IAAI,QAAQ,KAAK,MAAM,EAAE;YAC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U,EAAE;oBACf,MAAM,IAAI,KAAK,CAAC,oBAAoB,KAAK,CAAC,WAAW,EAAE,CAAC,CAAC;iBAC1D;gBACD,MAAM,CAAC,KAAK,GAAG,IAAA,mBAAW,EAAC,KAAK,CAAC,MAAM,GAAG,KAAK,EAAE,UAAU,CAAC,WAAY,CAAC,CAAC,QAAQ,EAAE,CAAC;aACtF;SACF;aAAM;YACL,MAAM,IAAI,KAAK,CAAC,qBAAqB,QAAQ,EAAE,CAAC,CAAC;SAClD;KACF;IAED,OAAO,CAAC,IAAI,CAAC,IAAA,kBAAU,EAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,aAAa,EAAE,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;IAEtF,MAAM,GAAG,GAAG,MAAM,IAAA,iCAAmB,EAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IAC1D,IAAI,GAAG,CAAC,IAAI,KAAK,GAAG,EAAE;QACpB,MAAM,IAAI,KAAK,CAAC,4BAA4B,GAAG,CAAC,IAAI,SAAS,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC;KACzE;AACH,CAAC,CAAC;AAjEW,QAAA,WAAW,eAiEtB","sourcesContent":["import { IOrder } from '@yuants/data-order';\nimport { formatTime, decodePath, roundToStep } from '@yuants/utils';\nimport { firstValueFrom, map } from 'rxjs';\nimport { ICredential, postTradeAmendOrder } from '../api/private-api';\nimport { productService } from '../public-data/product';\nimport { spotMarketTickers$ } from '../public-data/quote';\n\nexport const modifyOrder = async (credential: ICredential, order: IOrder) => {\n const [, instType, instId] = decodePath(order.product_id);\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 throw new Error(`ModifyOrder failed: code=${res.code}, msg=${res.msg}`);\n }\n};\n"]}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"submitOrder.d.ts","sourceRoot":"","sources":["../../src/experimental/submitOrder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAG5C,OAAO,EAAE,WAAW,EAAkB,MAAM,oBAAoB,CAAC;AAwCjE,eAAO,MAAM,WAAW,eAAsB,WAAW,SAAS,MAAM,KAAG,QAAQ;IAAE,QAAQ,EAAE,MAAM,CAAA;CAAE,CAoFtG,CAAC"}
|
|
@@ -1,114 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.submitOrder = void 0;
|
|
4
|
-
const utils_1 = require("@yuants/utils");
|
|
5
|
-
const rxjs_1 = require("rxjs");
|
|
6
|
-
const private_api_1 = require("../api/private-api");
|
|
7
|
-
const product_1 = require("../public-data/product");
|
|
8
|
-
const quote_1 = require("../public-data/quote");
|
|
9
|
-
const mapOrderDirectionToSide = (direction) => {
|
|
10
|
-
switch (direction) {
|
|
11
|
-
case 'OPEN_LONG':
|
|
12
|
-
case 'CLOSE_SHORT':
|
|
13
|
-
return 'buy';
|
|
14
|
-
case 'OPEN_SHORT':
|
|
15
|
-
case 'CLOSE_LONG':
|
|
16
|
-
return 'sell';
|
|
17
|
-
}
|
|
18
|
-
throw new Error(`Unknown direction: ${direction}`);
|
|
19
|
-
};
|
|
20
|
-
const mapOrderDirectionToPosSide = (direction) => {
|
|
21
|
-
switch (direction) {
|
|
22
|
-
case 'OPEN_LONG':
|
|
23
|
-
case 'CLOSE_LONG':
|
|
24
|
-
return 'long';
|
|
25
|
-
case 'CLOSE_SHORT':
|
|
26
|
-
case 'OPEN_SHORT':
|
|
27
|
-
return 'short';
|
|
28
|
-
}
|
|
29
|
-
throw new Error(`Unknown direction: ${direction}`);
|
|
30
|
-
};
|
|
31
|
-
const mapOrderTypeToOrdType = (order_type) => {
|
|
32
|
-
switch (order_type) {
|
|
33
|
-
case 'LIMIT':
|
|
34
|
-
return 'limit';
|
|
35
|
-
case 'MARKET':
|
|
36
|
-
return 'market';
|
|
37
|
-
case 'MAKER':
|
|
38
|
-
return 'post_only';
|
|
39
|
-
}
|
|
40
|
-
throw new Error(`Unknown order type: ${order_type}`);
|
|
41
|
-
};
|
|
42
|
-
const submitOrder = async (credential, order) => {
|
|
43
|
-
var _a, _b, _c;
|
|
44
|
-
const [, instType, instId] = (0, utils_1.decodePath)(order.product_id);
|
|
45
|
-
// 交易数量,表示要购买或者出售的数量。
|
|
46
|
-
// 当币币/币币杠杆以限价买入和卖出时,指交易货币数量。
|
|
47
|
-
// 当币币杠杆以市价买入时,指计价货币的数量。
|
|
48
|
-
// 当币币杠杆以市价卖出时,指交易货币的数量。
|
|
49
|
-
// 对于币币市价单,单位由 tgtCcy 决定
|
|
50
|
-
// 当交割、永续、期权买入和卖出时,指合约张数。
|
|
51
|
-
const mapOrderVolumeToSz = async (order) => {
|
|
52
|
-
if (instType === 'SWAP') {
|
|
53
|
-
return order.volume;
|
|
54
|
-
}
|
|
55
|
-
if (instType === 'MARGIN') {
|
|
56
|
-
if (order.order_type === 'LIMIT') {
|
|
57
|
-
return order.volume;
|
|
58
|
-
}
|
|
59
|
-
if (order.order_type === 'MAKER') {
|
|
60
|
-
return order.volume;
|
|
61
|
-
}
|
|
62
|
-
if (order.order_type === 'MARKET') {
|
|
63
|
-
if (order.order_direction === 'OPEN_SHORT' || order.order_direction === 'CLOSE_LONG') {
|
|
64
|
-
return order.volume;
|
|
65
|
-
}
|
|
66
|
-
//
|
|
67
|
-
const price = await (0, rxjs_1.firstValueFrom)(quote_1.spotMarketTickers$.pipe((0, rxjs_1.map)((x) => mapOrderDirectionToPosSide(order.order_direction) === 'long'
|
|
68
|
-
? +x[instId].askPx
|
|
69
|
-
: +x[instId].bidPx)));
|
|
70
|
-
if (!price) {
|
|
71
|
-
throw new Error(`invalid tick: ${price}`);
|
|
72
|
-
}
|
|
73
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'SubmitOrder', 'price', price);
|
|
74
|
-
const theProduct = await (0, rxjs_1.firstValueFrom)(product_1.productService.mapProductIdToProduct$.pipe((0, rxjs_1.map)((x) => x.get(order.product_id))));
|
|
75
|
-
if (!theProduct) {
|
|
76
|
-
throw new Error(`Unknown product: ${order.position_id}`);
|
|
77
|
-
}
|
|
78
|
-
return (0, utils_1.roundToStep)(order.volume * price, theProduct.volume_step);
|
|
79
|
-
}
|
|
80
|
-
return 0;
|
|
81
|
-
}
|
|
82
|
-
if (instType === 'SPOT') {
|
|
83
|
-
return order.volume;
|
|
84
|
-
}
|
|
85
|
-
throw new Error(`Unknown instType: ${instType}`);
|
|
86
|
-
};
|
|
87
|
-
const params = {
|
|
88
|
-
instId,
|
|
89
|
-
tdMode: instType === 'SPOT' ? 'cash' : 'cross',
|
|
90
|
-
side: mapOrderDirectionToSide(order.order_direction),
|
|
91
|
-
posSide: instType === 'MARGIN' || instType === 'SPOT'
|
|
92
|
-
? 'net'
|
|
93
|
-
: mapOrderDirectionToPosSide(order.order_direction),
|
|
94
|
-
ordType: mapOrderTypeToOrdType(order.order_type),
|
|
95
|
-
sz: (await mapOrderVolumeToSz(order)).toString(),
|
|
96
|
-
tgtCcy: instType === 'SPOT' && order.order_type === 'MARKET' ? 'base_ccy' : undefined,
|
|
97
|
-
reduceOnly: instType === 'MARGIN' && ['CLOSE_LONG', 'CLOSE_SHORT'].includes((_a = order.order_direction) !== null && _a !== void 0 ? _a : '')
|
|
98
|
-
? 'true'
|
|
99
|
-
: undefined,
|
|
100
|
-
px: order.order_type === 'LIMIT' || order.order_type === 'MAKER' ? order.price.toString() : undefined,
|
|
101
|
-
ccy: instType === 'MARGIN' ? 'USDT' : undefined,
|
|
102
|
-
tag: process.env.BROKER_TAG,
|
|
103
|
-
};
|
|
104
|
-
console.info((0, utils_1.formatTime)(Date.now()), 'SubmitOrder', 'params', JSON.stringify(params));
|
|
105
|
-
const res = await (0, private_api_1.postTradeOrder)(credential, params);
|
|
106
|
-
if (res.code === '0' && ((_c = (_b = res.data) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.ordId)) {
|
|
107
|
-
return { order_id: res.data[0].ordId };
|
|
108
|
-
}
|
|
109
|
-
throw `Failed to submit order: ${res.code} ${res.msg}: ${res.data
|
|
110
|
-
.map((x) => `${x.sCode} ${x.sMsg}`)
|
|
111
|
-
.join('; ')}`;
|
|
112
|
-
};
|
|
113
|
-
exports.submitOrder = submitOrder;
|
|
114
|
-
//# sourceMappingURL=submitOrder.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"submitO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F,CAAC,CAAC,MAAM;YACR,CAAC,CAAC,SAAS;QACf,EAAE,EAAE,KAAK,CAAC,UAAU,KAAK,OAAO,IAAI,KAAK,CAAC,UAAU,KAAK,OAAO,CAAC,CAAC,CAAC,KAAK,CAAC,KAAM,CAAC,QAAQ,EAAE,CAAC,CAAC,CAAC,SAAS;QACtG,GAAG,EAAE,QAAQ,KAAK,QAAQ,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS;QAC/C,GAAG,EAAE,OAAO,CAAC,GAAG,CAAC,UAAU;KAC5B,CAAC;IACF,OAAO,CAAC,IAAI,CAAC,IAAA,kBAAU,EAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,aAAa,EAAE,QAAQ,EAAE,IAAI,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAC;IACtF,MAAM,GAAG,GAAG,MAAM,IAAA,4BAAc,EAAC,UAAU,EAAE,MAAM,CAAC,CAAC;IACrD,IAAI,GAAG,CAAC,IAAI,KAAK,GAAG,KAAI,MAAA,MAAA,GAAG,CAAC,IAAI,0CAAG,CAAC,CAAC,0CAAE,KAAK,CAAA,EAAE;QAC5C,OAAO,EAAE,QAAQ,EAAE,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,EAAE,CAAC;KACxC;IACD,MAAM,2BAA2B,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,GAAG,KAAK,GAAG,CAAC,IAAI;SAC9D,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,IAAI,EAAE,CAAC;SAClC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC;AAClB,CAAC,CAAC;AApFW,QAAA,WAAW,eAoFtB","sourcesContent":["import { IOrder } from '@yuants/data-order';\nimport { decodePath, formatTime, roundToStep } from '@yuants/utils';\nimport { firstValueFrom, map } from 'rxjs';\nimport { ICredential, postTradeOrder } from '../api/private-api';\nimport { productService } from '../public-data/product';\nimport { spotMarketTickers$ } from '../public-data/quote';\n\nconst 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\nconst 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\nconst 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\nexport const submitOrder = async (credential: ICredential, order: IOrder): Promise<{ order_id: string }> => {\n const [, instType, instId] = decodePath(order.product_id);\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: order.order_type === 'LIMIT' || order.order_type === 'MAKER' ? order.price!.toString() : undefined,\n ccy: instType === 'MARGIN' ? 'USDT' : undefined,\n tag: process.env.BROKER_TAG,\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 { order_id: res.data[0].ordId };\n }\n throw `Failed to submit order: ${res.code} ${res.msg}: ${res.data\n .map((x) => `${x.sCode} ${x.sMsg}`)\n .join('; ')}`;\n};\n"]}
|