@yuants/vendor-okx 0.23.25 → 0.24.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 +35 -144
- package/dist/account.js.map +1 -1
- package/dist/accountInfos/earning.js +15 -0
- package/dist/accountInfos/earning.js.map +1 -0
- package/dist/accountInfos/funding.js +46 -0
- package/dist/accountInfos/funding.js.map +1 -0
- package/dist/accountInfos/index.js +7 -0
- package/dist/accountInfos/index.js.map +1 -0
- package/dist/accountInfos/loan.js +46 -0
- package/dist/accountInfos/loan.js.map +1 -0
- package/dist/accountInfos/strategy.js +56 -0
- package/dist/accountInfos/strategy.js.map +1 -0
- package/dist/accountInfos/trading.js +84 -0
- package/dist/accountInfos/trading.js.map +1 -0
- package/dist/accountInfos/types.js +2 -0
- package/dist/accountInfos/types.js.map +1 -0
- package/dist/{api.js → api/private-api.js} +1 -1
- package/dist/api/private-api.js.map +1 -0
- package/dist/api/public-api.js.map +1 -0
- package/dist/api-service.js +1 -1
- package/dist/api-service.js.map +1 -1
- package/dist/index.js +5 -4
- package/dist/index.js.map +1 -1
- package/dist/loan-account.js +6 -48
- package/dist/loan-account.js.map +1 -1
- package/dist/order-actions-with-credential.js +69 -0
- package/dist/order-actions-with-credential.js.map +1 -0
- package/dist/order-actions.js +12 -184
- package/dist/order-actions.js.map +1 -1
- package/dist/order.js +4 -7
- package/dist/order.js.map +1 -1
- package/dist/orders/cancelOrder.js +13 -0
- package/dist/orders/cancelOrder.js.map +1 -0
- package/dist/orders/modifyOrder.js +58 -0
- package/dist/orders/modifyOrder.js.map +1 -0
- package/dist/orders/submitOrder.js +107 -0
- package/dist/orders/submitOrder.js.map +1 -0
- package/dist/{interest_rate.js → public-data/interest_rate.js} +1 -1
- package/dist/public-data/interest_rate.js.map +1 -0
- package/dist/{market-order.js → public-data/market-order.js} +1 -1
- package/dist/public-data/market-order.js.map +1 -0
- package/dist/{ohlc.js → public-data/ohlc.js} +3 -4
- package/dist/public-data/ohlc.js.map +1 -0
- package/dist/{product.js → public-data/product.js} +1 -1
- package/dist/public-data/product.js.map +1 -0
- package/dist/{quote.js → public-data/quote.js} +2 -3
- package/dist/public-data/quote.js.map +1 -0
- package/dist/services.js +4 -4
- package/dist/services.js.map +1 -1
- package/dist/strategy-account.js +9 -70
- package/dist/strategy-account.js.map +1 -1
- package/dist/trade.js +6 -8
- package/dist/trade.js.map +1 -1
- package/dist/transfer.js +6 -4
- package/dist/transfer.js.map +1 -1
- package/dist/utils/provideOHLCFromTimeBackwardService.js.map +1 -0
- package/dist/utils/provideSeriesFromTimeBackwardService.js.map +1 -0
- package/dist/ws.js.map +1 -1
- package/lib/account.d.ts +6 -3
- package/lib/account.d.ts.map +1 -1
- package/lib/account.js +38 -146
- package/lib/account.js.map +1 -1
- package/lib/accountInfos/earning.d.ts +4 -0
- package/lib/accountInfos/earning.d.ts.map +1 -0
- package/lib/accountInfos/earning.js +19 -0
- package/lib/accountInfos/earning.js.map +1 -0
- package/lib/accountInfos/funding.d.ts +4 -0
- package/lib/accountInfos/funding.d.ts.map +1 -0
- package/lib/accountInfos/funding.js +50 -0
- package/lib/accountInfos/funding.js.map +1 -0
- package/lib/accountInfos/index.d.ts +8 -0
- package/lib/accountInfos/index.d.ts.map +1 -0
- package/lib/accountInfos/index.js +16 -0
- package/lib/accountInfos/index.js.map +1 -0
- package/lib/accountInfos/loan.d.ts +4 -0
- package/lib/accountInfos/loan.d.ts.map +1 -0
- package/lib/accountInfos/loan.js +50 -0
- package/lib/accountInfos/loan.js.map +1 -0
- package/lib/accountInfos/strategy.d.ts +4 -0
- package/lib/accountInfos/strategy.d.ts.map +1 -0
- package/lib/accountInfos/strategy.js +60 -0
- package/lib/accountInfos/strategy.js.map +1 -0
- package/lib/accountInfos/trading.d.ts +5 -0
- package/lib/accountInfos/trading.d.ts.map +1 -0
- package/lib/accountInfos/trading.js +88 -0
- package/lib/accountInfos/trading.js.map +1 -0
- package/lib/accountInfos/types.d.ts +6 -0
- package/lib/accountInfos/types.d.ts.map +1 -0
- package/lib/accountInfos/types.js +3 -0
- package/lib/accountInfos/types.js.map +1 -0
- package/lib/{api.d.ts → api/private-api.d.ts} +1 -1
- package/lib/api/private-api.d.ts.map +1 -0
- package/lib/{api.js → api/private-api.js} +1 -1
- package/lib/api/private-api.js.map +1 -0
- package/lib/api/public-api.d.ts.map +1 -0
- package/lib/api/public-api.js.map +1 -0
- package/lib/api-service.js +1 -1
- package/lib/api-service.js.map +1 -1
- package/lib/index.d.ts +5 -4
- package/lib/index.d.ts.map +1 -1
- package/lib/index.js +5 -4
- package/lib/index.js.map +1 -1
- package/lib/loan-account.js +5 -47
- package/lib/loan-account.js.map +1 -1
- package/lib/order-actions-with-credential.d.ts +2 -0
- package/lib/order-actions-with-credential.d.ts.map +1 -0
- package/lib/order-actions-with-credential.js +71 -0
- package/lib/order-actions-with-credential.js.map +1 -0
- package/lib/order-actions.js +11 -183
- package/lib/order-actions.js.map +1 -1
- package/lib/order.d.ts.map +1 -1
- package/lib/order.js +4 -7
- package/lib/order.js.map +1 -1
- package/lib/orders/cancelOrder.d.ts +4 -0
- package/lib/orders/cancelOrder.d.ts.map +1 -0
- package/lib/orders/cancelOrder.js +17 -0
- package/lib/orders/cancelOrder.js.map +1 -0
- package/lib/orders/modifyOrder.d.ts +4 -0
- package/lib/orders/modifyOrder.d.ts.map +1 -0
- package/lib/orders/modifyOrder.js +62 -0
- package/lib/orders/modifyOrder.js.map +1 -0
- package/lib/orders/submitOrder.d.ts +4 -0
- package/lib/orders/submitOrder.d.ts.map +1 -0
- package/lib/orders/submitOrder.js +111 -0
- package/lib/orders/submitOrder.js.map +1 -0
- package/lib/public-data/interest_rate.d.ts.map +1 -0
- package/lib/{interest_rate.js → public-data/interest_rate.js} +1 -1
- package/lib/public-data/interest_rate.js.map +1 -0
- package/lib/public-data/market-order.d.ts.map +1 -0
- package/lib/{market-order.js → public-data/market-order.js} +1 -1
- package/lib/public-data/market-order.js.map +1 -0
- package/lib/public-data/ohlc.d.ts.map +1 -0
- package/lib/{ohlc.js → public-data/ohlc.js} +3 -4
- package/lib/public-data/ohlc.js.map +1 -0
- package/lib/public-data/product.d.ts.map +1 -0
- package/lib/{product.js → public-data/product.js} +1 -1
- package/{dist → lib/public-data}/product.js.map +1 -1
- package/lib/public-data/quote.d.ts.map +1 -0
- package/lib/{quote.js → public-data/quote.js} +2 -3
- package/lib/public-data/quote.js.map +1 -0
- package/lib/services.js +4 -4
- package/lib/services.js.map +1 -1
- package/lib/strategy-account.d.ts +1 -28
- package/lib/strategy-account.d.ts.map +1 -1
- package/lib/strategy-account.js +10 -72
- package/lib/strategy-account.js.map +1 -1
- package/lib/trade.js +6 -8
- package/lib/trade.js.map +1 -1
- package/lib/transfer.js +17 -15
- package/lib/transfer.js.map +1 -1
- package/lib/utils/provideOHLCFromTimeBackwardService.d.ts.map +1 -0
- package/lib/utils/provideOHLCFromTimeBackwardService.js.map +1 -0
- package/lib/utils/provideSeriesFromTimeBackwardService.d.ts.map +1 -0
- package/lib/utils/provideSeriesFromTimeBackwardService.js.map +1 -0
- package/lib/ws.d.ts +1 -1
- package/lib/ws.d.ts.map +1 -1
- package/lib/ws.js.map +1 -1
- package/package.json +14 -13
- package/temp/package-deps.json +48 -37
- package/dist/api.js.map +0 -1
- package/dist/interest_rate.js.map +0 -1
- package/dist/market-order.js.map +0 -1
- package/dist/ohlc.js.map +0 -1
- package/dist/provideOHLCFromTimeBackwardService.js.map +0 -1
- package/dist/provideSeriesFromTimeBackwardService.js.map +0 -1
- package/dist/public-api.js.map +0 -1
- package/dist/quote.js.map +0 -1
- package/dist/websocket.js +0 -157
- package/dist/websocket.js.map +0 -1
- package/lib/api.d.ts.map +0 -1
- package/lib/api.js.map +0 -1
- package/lib/interest_rate.d.ts.map +0 -1
- package/lib/interest_rate.js.map +0 -1
- package/lib/market-order.d.ts.map +0 -1
- package/lib/market-order.js.map +0 -1
- package/lib/ohlc.d.ts.map +0 -1
- package/lib/ohlc.js.map +0 -1
- package/lib/product.d.ts.map +0 -1
- package/lib/product.js.map +0 -1
- package/lib/provideOHLCFromTimeBackwardService.d.ts.map +0 -1
- package/lib/provideOHLCFromTimeBackwardService.js.map +0 -1
- package/lib/provideSeriesFromTimeBackwardService.d.ts.map +0 -1
- package/lib/provideSeriesFromTimeBackwardService.js.map +0 -1
- package/lib/public-api.d.ts.map +0 -1
- package/lib/public-api.js.map +0 -1
- package/lib/quote.d.ts.map +0 -1
- package/lib/quote.js.map +0 -1
- package/lib/websocket.d.ts +0 -15
- package/lib/websocket.d.ts.map +0 -1
- package/lib/websocket.js +0 -163
- package/lib/websocket.js.map +0 -1
- /package/dist/{public-api.js → api/public-api.js} +0 -0
- /package/dist/{provideOHLCFromTimeBackwardService.js → utils/provideOHLCFromTimeBackwardService.js} +0 -0
- /package/dist/{provideSeriesFromTimeBackwardService.js → utils/provideSeriesFromTimeBackwardService.js} +0 -0
- /package/lib/{public-api.d.ts → api/public-api.d.ts} +0 -0
- /package/lib/{public-api.js → api/public-api.js} +0 -0
- /package/lib/{interest_rate.d.ts → public-data/interest_rate.d.ts} +0 -0
- /package/lib/{market-order.d.ts → public-data/market-order.d.ts} +0 -0
- /package/lib/{ohlc.d.ts → public-data/ohlc.d.ts} +0 -0
- /package/lib/{product.d.ts → public-data/product.d.ts} +0 -0
- /package/lib/{quote.d.ts → public-data/quote.d.ts} +0 -0
- /package/lib/{provideOHLCFromTimeBackwardService.d.ts → utils/provideOHLCFromTimeBackwardService.d.ts} +0 -0
- /package/lib/{provideOHLCFromTimeBackwardService.js → utils/provideOHLCFromTimeBackwardService.js} +0 -0
- /package/lib/{provideSeriesFromTimeBackwardService.d.ts → utils/provideSeriesFromTimeBackwardService.d.ts} +0 -0
- /package/lib/{provideSeriesFromTimeBackwardService.js → utils/provideSeriesFromTimeBackwardService.js} +0 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"order-actions-with-credential.js","sourceRoot":"","sources":["../src/order-actions-with-credential.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nimport { ICredential } from './api/private-api';\nimport { cancelOrder } from './orders/cancelOrder';\nimport { modifyOrder } from './orders/modifyOrder';\nimport { submitOrder } from './orders/submitOrder';\n\nconst terminal = Terminal.fromNodeEnv();\n\nterminal.server.provideService<IOrder & { credential: ICredential }, { order_id?: string }>(\n 'SubmitOrder',\n {\n required: ['account_id', 'credential'],\n properties: {\n account_id: {\n type: 'string',\n pattern: '^okx/',\n },\n credential: {\n type: 'object',\n required: ['access_key', 'secret_key', 'passphrase'],\n properties: {\n access_key: { type: 'string' },\n secret_key: { type: 'string' },\n passphrase: { type: 'string' },\n },\n },\n },\n },\n async (msg) => {\n const order_id = await submitOrder(msg.req.credential, msg.req);\n return { res: { code: 0, message: 'OK', data: { order_id } } };\n },\n);\n\nterminal.server.provideService<IOrder & { credential: ICredential }>(\n 'ModifyOrder',\n {\n required: ['account_id', 'credential'],\n properties: {\n account_id: {\n type: 'string',\n pattern: '^okx/',\n },\n credential: {\n type: 'object',\n required: ['access_key', 'secret_key', 'passphrase'],\n properties: {\n access_key: { type: 'string' },\n secret_key: { type: 'string' },\n passphrase: { type: 'string' },\n },\n },\n },\n },\n async (msg) => {\n await modifyOrder(msg.req.credential, msg.req);\n return { res: { code: 0, message: 'OK' } };\n },\n);\n\nterminal.server.provideService<IOrder & { credential: ICredential }>(\n 'CancelOrder',\n {\n required: ['account_id', 'credential'],\n properties: {\n account_id: {\n type: 'string',\n pattern: '^okx/',\n },\n credential: {\n type: 'object',\n required: ['access_key', 'secret_key', 'passphrase'],\n properties: {\n access_key: { type: 'string' },\n secret_key: { type: 'string' },\n passphrase: { type: 'string' },\n },\n },\n },\n },\n async (msg) => {\n await cancelOrder(msg.req.credential, msg.req);\n return { res: { code: 0, message: 'OK' } };\n },\n);\n"]}
|
package/lib/order-actions.js
CHANGED
|
@@ -1,131 +1,23 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const protocol_1 = require("@yuants/protocol");
|
|
4
|
-
const utils_1 = require("@yuants/utils");
|
|
5
4
|
const rxjs_1 = require("rxjs");
|
|
6
5
|
const account_1 = require("./account");
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
6
|
+
const private_api_1 = require("./api/private-api");
|
|
7
|
+
const cancelOrder_1 = require("./orders/cancelOrder");
|
|
8
|
+
const modifyOrder_1 = require("./orders/modifyOrder");
|
|
9
|
+
const submitOrder_1 = require("./orders/submitOrder");
|
|
10
10
|
const terminal = protocol_1.Terminal.fromNodeEnv();
|
|
11
|
-
const credential = (0,
|
|
11
|
+
const credential = (0, private_api_1.getDefaultCredential)();
|
|
12
12
|
(0, rxjs_1.defer)(async () => {
|
|
13
|
-
const tradingAccountId = await (0,
|
|
13
|
+
const tradingAccountId = await (0, account_1.getTradingAccountId)();
|
|
14
14
|
terminal.server.provideService('SubmitOrder', {
|
|
15
15
|
required: ['account_id'],
|
|
16
16
|
properties: {
|
|
17
17
|
account_id: { const: tradingAccountId },
|
|
18
18
|
},
|
|
19
19
|
}, async (msg) => {
|
|
20
|
-
|
|
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 } };
|
|
20
|
+
return { res: { code: 0, message: 'OK', data: { order_id: await (0, submitOrder_1.submitOrder)(credential, msg.req) } } };
|
|
129
21
|
});
|
|
130
22
|
terminal.server.provideService('ModifyOrder', {
|
|
131
23
|
required: ['account_id'],
|
|
@@ -133,63 +25,7 @@ const credential = (0, api_1.getDefaultCredential)();
|
|
|
133
25
|
account_id: { const: tradingAccountId },
|
|
134
26
|
},
|
|
135
27
|
}, async (msg) => {
|
|
136
|
-
|
|
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
|
-
}
|
|
28
|
+
await (0, modifyOrder_1.modifyOrder)(credential, msg.req);
|
|
193
29
|
return { res: { code: 0, message: 'OK' } };
|
|
194
30
|
});
|
|
195
31
|
terminal.server.provideService('CancelOrder', {
|
|
@@ -197,17 +33,9 @@ const credential = (0, api_1.getDefaultCredential)();
|
|
|
197
33
|
properties: {
|
|
198
34
|
account_id: { const: tradingAccountId },
|
|
199
35
|
},
|
|
200
|
-
},
|
|
201
|
-
|
|
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
|
-
}
|
|
36
|
+
}, async (msg) => {
|
|
37
|
+
await (0, cancelOrder_1.cancelOrder)(credential, msg.req);
|
|
210
38
|
return { res: { code: 0, message: 'OK' } };
|
|
211
|
-
})
|
|
39
|
+
});
|
|
212
40
|
}).subscribe();
|
|
213
41
|
//# sourceMappingURL=order-actions.js.map
|
package/lib/order-actions.js.map
CHANGED
|
@@ -1 +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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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"]}
|
|
1
|
+
{"version":3,"file":"order-actions.js","sourceRoot":"","sources":["../src/order-actions.ts"],"names":[],"mappings":";;AACA,+CAA4C;AAC5C,+BAA6B;AAC7B,uCAAgD;AAChD,mDAAyD;AACzD,sDAAmD;AACnD,sDAAmD;AACnD,sDAAmD;AAEnD,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,UAAU,GAAG,IAAA,kCAAoB,GAAE,CAAC;AAE1C,IAAA,YAAK,EAAC,KAAK,IAAI,EAAE;IACf,MAAM,gBAAgB,GAAG,MAAM,IAAA,6BAAmB,GAAE,CAAC;IACrD,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,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,IAAI,EAAE,EAAE,QAAQ,EAAE,MAAM,IAAA,yBAAW,EAAC,UAAU,EAAE,GAAG,CAAC,GAAG,CAAC,EAAE,EAAE,EAAE,CAAC;IACzG,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,KAAK,EAAE,GAAG,EAAE,EAAE;QACZ,MAAM,IAAA,yBAAW,EAAC,UAAU,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;QACvC,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,KAAK,EAAE,GAAG,EAAE,EAAE;QACZ,MAAM,IAAA,yBAAW,EAAC,UAAU,EAAE,GAAG,CAAC,GAAG,CAAC,CAAC;QACvC,OAAO,EAAE,GAAG,EAAE,EAAE,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,IAAI,EAAE,EAAE,CAAC;IAC7C,CAAC,CACF,CAAC;AACJ,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC","sourcesContent":["import { IOrder } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { defer } from 'rxjs';\nimport { getTradingAccountId } from './account';\nimport { getDefaultCredential } from './api/private-api';\nimport { cancelOrder } from './orders/cancelOrder';\nimport { modifyOrder } from './orders/modifyOrder';\nimport { submitOrder } from './orders/submitOrder';\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst credential = getDefaultCredential();\n\ndefer(async () => {\n const tradingAccountId = await getTradingAccountId();\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 return { res: { code: 0, message: 'OK', data: { order_id: await submitOrder(credential, msg.req) } } };\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 await modifyOrder(credential, msg.req);\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 async (msg) => {\n await cancelOrder(credential, msg.req);\n return { res: { code: 0, message: 'OK' } };\n },\n );\n}).subscribe();\n"]}
|
package/lib/order.d.ts.map
CHANGED
|
@@ -1 +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,
|
|
1
|
+
{"version":3,"file":"order.d.ts","sourceRoot":"","sources":["../src/order.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAI5C,OAAO,EAAiE,OAAO,EAAO,MAAM,MAAM,CAAC;AAInG,eAAO,MAAM,MAAM,iBAAwB,CAAC"}
|
package/lib/order.js
CHANGED
|
@@ -6,7 +6,7 @@ const sql_1 = require("@yuants/sql");
|
|
|
6
6
|
const utils_1 = require("@yuants/utils");
|
|
7
7
|
const rxjs_1 = require("rxjs");
|
|
8
8
|
const account_1 = require("./account");
|
|
9
|
-
const
|
|
9
|
+
const private_api_1 = require("./api/private-api");
|
|
10
10
|
exports.order$ = new rxjs_1.Subject();
|
|
11
11
|
exports.order$
|
|
12
12
|
.pipe(
|
|
@@ -71,12 +71,9 @@ const makeOrder = (x, account_id) => {
|
|
|
71
71
|
};
|
|
72
72
|
};
|
|
73
73
|
(async () => {
|
|
74
|
-
const
|
|
75
|
-
const
|
|
76
|
-
const
|
|
77
|
-
const EARNING_ACCOUNT_ID = `okx/${uid}/earning/USDT`;
|
|
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));
|
|
74
|
+
const TRADING_ACCOUNT_ID = await (0, account_1.getTradingAccountId)();
|
|
75
|
+
const swapHistoryOrders = (0, rxjs_1.defer)(() => (0, private_api_1.getTradeOrdersHistory)((0, private_api_1.getDefaultCredential)(), { instType: 'SWAP' })).pipe((0, rxjs_1.repeat)({ delay: 1000 }), (0, rxjs_1.retry)({ delay: 1000 }), (0, rxjs_1.shareReplay)(1));
|
|
76
|
+
const swapPendingOrders = (0, rxjs_1.defer)(() => (0, private_api_1.getTradeOrdersPending)((0, private_api_1.getDefaultCredential)(), { instType: 'SWAP' })).pipe((0, rxjs_1.repeat)({ delay: 1000 }), (0, rxjs_1.retry)({ delay: 1000 }), (0, rxjs_1.shareReplay)(1));
|
|
80
77
|
const ordersFromHistoryOrder$ = swapHistoryOrders.pipe(
|
|
81
78
|
//
|
|
82
79
|
(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,+
|
|
1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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 { defer, from, map, merge, mergeMap, repeat, retry, shareReplay, Subject, tap } from 'rxjs';\nimport { getTradingAccountId } from './account';\nimport { getDefaultCredential, getTradeOrdersHistory, getTradeOrdersPending } from './api/private-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 TRADING_ACCOUNT_ID = await getTradingAccountId();\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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cancelOrder.d.ts","sourceRoot":"","sources":["../../src/orders/cancelOrder.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAC;AAE5C,OAAO,EAAE,WAAW,EAAwB,MAAM,oBAAoB,CAAC;AAEvE,eAAO,MAAM,WAAW,eAAsB,WAAW,SAAS,MAAM,kBASvE,CAAC"}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cancelOrder = void 0;
|
|
4
|
+
const utils_1 = require("@yuants/utils");
|
|
5
|
+
const private_api_1 = require("../api/private-api");
|
|
6
|
+
const cancelOrder = async (credential, order) => {
|
|
7
|
+
const [, instId] = (0, utils_1.decodePath)(order.product_id);
|
|
8
|
+
const res = await (0, private_api_1.postTradeCancelOrder)(credential, {
|
|
9
|
+
instId,
|
|
10
|
+
ordId: order.order_id,
|
|
11
|
+
});
|
|
12
|
+
if (res.code !== '0') {
|
|
13
|
+
throw new Error(`Cancel order failed: code=${res.code} msg=${res.msg}`);
|
|
14
|
+
}
|
|
15
|
+
};
|
|
16
|
+
exports.cancelOrder = cancelOrder;
|
|
17
|
+
//# sourceMappingURL=cancelOrder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"cancelOrder.js","sourceRoot":"","sources":["../../src/orders/cancelOrder.ts"],"names":[],"mappings":";;;AACA,yCAA2C;AAC3C,oDAAuE;AAEhE,MAAM,WAAW,GAAG,KAAK,EAAE,UAAuB,EAAE,KAAa,EAAE,EAAE;IAC1E,MAAM,CAAC,EAAE,MAAM,CAAC,GAAG,IAAA,kBAAU,EAAC,KAAK,CAAC,UAAU,CAAC,CAAC;IAChD,MAAM,GAAG,GAAG,MAAM,IAAA,kCAAoB,EAAC,UAAU,EAAE;QACjD,MAAM;QACN,KAAK,EAAE,KAAK,CAAC,QAAS;KACvB,CAAC,CAAC;IACH,IAAI,GAAG,CAAC,IAAI,KAAK,GAAG,EAAE;QACpB,MAAM,IAAI,KAAK,CAAC,6BAA6B,GAAG,CAAC,IAAI,QAAQ,GAAG,CAAC,GAAG,EAAE,CAAC,CAAC;KACzE;AACH,CAAC,CAAC;AATW,QAAA,WAAW,eAStB","sourcesContent":["import { IOrder } from '@yuants/data-order';\nimport { decodePath } from '@yuants/utils';\nimport { ICredential, postTradeCancelOrder } from '../api/private-api';\n\nexport const cancelOrder = async (credential: ICredential, order: IOrder) => {\n const [, 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 throw new Error(`Cancel order failed: code=${res.code} msg=${res.msg}`);\n }\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modifyOrder.d.ts","sourceRoot":"","sources":["../../src/orders/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"}
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"modifyOrder.js","sourceRoot":"","sources":["../../src/orders/modifyOrder.ts"],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,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"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"submitOrder.d.ts","sourceRoot":"","sources":["../../src/orders/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,MAAM,CAiFxF,CAAC"}
|
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
};
|
|
103
|
+
console.info((0, utils_1.formatTime)(Date.now()), 'SubmitOrder', 'params', JSON.stringify(params));
|
|
104
|
+
const res = await (0, private_api_1.postTradeOrder)(credential, params);
|
|
105
|
+
if (res.code === '0' && ((_c = (_b = res.data) === null || _b === void 0 ? void 0 : _b[0]) === null || _c === void 0 ? void 0 : _c.ordId)) {
|
|
106
|
+
return res.data[0].ordId;
|
|
107
|
+
}
|
|
108
|
+
throw `Failed to submit order: ${res.code} ${res.msg}`;
|
|
109
|
+
};
|
|
110
|
+
exports.submitOrder = submitOrder;
|
|
111
|
+
//# sourceMappingURL=submitOrder.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"submitOrder.js","sourceRoot":"","sources":["../../src/orders/submitOrder.ts"],"names":[],"mappings":";;;AACA,yCAAoE;AACpE,+BAA2C;AAC3C,oDAAiE;AACjE,oDAAwD;AACxD,gDAA0D;AAE1D,MAAM,uBAAuB,GAAG,CAAC,SAAkB,EAAE,EAAE;IACrD,QAAQ,SAAS,EAAE;QACjB,KAAK,WAAW,CAAC;QACjB,KAAK,aAAa;YAChB,OAAO,KAAK,CAAC;QACf,KAAK,YAAY,CAAC;QAClB,KAAK,YAAY;YACf,OAAO,MAAM,CAAC;KACjB;IACD,MAAM,IAAI,KAAK,CAAC,sBAAsB,SAAS,EAAE,CAAC,CAAC;AACrD,CAAC,CAAC;AAEF,MAAM,0BAA0B,GAAG,CAAC,SAAkB,EAAE,EAAE;IACxD,QAAQ,SAAS,EAAE;QACjB,KAAK,WAAW,CAAC;QACjB,KAAK,YAAY;YACf,OAAO,MAAM,CAAC;QAChB,KAAK,aAAa,CAAC;QACnB,KAAK,YAAY;YACf,OAAO,OAAO,CAAC;KAClB;IACD,MAAM,IAAI,KA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;QAC5C,OAAO,GAAG,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC;KAC1B;IACD,MAAM,2BAA2B,GAAG,CAAC,IAAI,IAAI,GAAG,CAAC,GAAG,EAAE,CAAC;AACzD,CAAC,CAAC;AAjFW,QAAA,WAAW,eAiFtB","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<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 };\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 res.data[0].ordId;\n }\n throw `Failed to submit order: ${res.code} ${res.msg}`;\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"interest_rate.d.ts","sourceRoot":"","sources":["../../src/public-data/interest_rate.ts"],"names":[],"mappings":""}
|