@yuants/vendor-okx 0.23.25 → 0.23.26
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 +13 -136
- 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/loan-account.js +3 -45
- package/dist/loan-account.js.map +1 -1
- package/dist/order-actions.js +10 -182
- package/dist/order-actions.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/strategy-account.js +5 -59
- package/dist/strategy-account.js.map +1 -1
- package/lib/account.d.ts +2 -0
- package/lib/account.d.ts.map +1 -1
- package/lib/account.js +12 -136
- 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/loan-account.js +2 -44
- package/lib/loan-account.js.map +1 -1
- package/lib/order-actions.js +8 -180
- package/lib/order-actions.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/strategy-account.js +4 -58
- package/lib/strategy-account.js.map +1 -1
- package/package.json +13 -13
- package/temp/package-deps.json +29 -19
package/dist/account.js
CHANGED
|
@@ -3,13 +3,11 @@ import { providePendingOrdersService } from '@yuants/data-order';
|
|
|
3
3
|
import { Terminal } from '@yuants/protocol';
|
|
4
4
|
import { encodePath } from '@yuants/utils';
|
|
5
5
|
import { defer, filter, firstValueFrom, map, repeat, retry, shareReplay } from 'rxjs';
|
|
6
|
-
import { getAccountConfig,
|
|
7
|
-
import {
|
|
8
|
-
import { getMarketIndexTicker } from './public-api';
|
|
6
|
+
import { getAccountConfig, getDefaultCredential, getTradeOrdersPending } from './api';
|
|
7
|
+
import { getTradingAccountInfo, getFundingAccountInfo, getEarningAccountInfo, marketIndexTickerUSDT$, } from './accountInfos';
|
|
9
8
|
const terminal = Terminal.fromNodeEnv();
|
|
10
9
|
const credential = getDefaultCredential();
|
|
11
10
|
export const accountConfig$ = defer(() => getAccountConfig(credential)).pipe(repeat({ delay: 10000 }), retry({ delay: 10000 }), shareReplay(1));
|
|
12
|
-
const subAccountUids$ = defer(() => getSubAccountList(credential)).pipe(repeat({ delay: 10000 }), retry({ delay: 10000 }), shareReplay(1));
|
|
13
11
|
export const accountUid$ = accountConfig$.pipe(map((x) => x.data[0].uid), filter((x) => !!x), shareReplay(1));
|
|
14
12
|
defer(async () => {
|
|
15
13
|
const uid = await firstValueFrom(accountUid$);
|
|
@@ -41,149 +39,28 @@ defer(async () => {
|
|
|
41
39
|
});
|
|
42
40
|
}, { auto_refresh_interval: 5000 });
|
|
43
41
|
}).subscribe();
|
|
44
|
-
const marketIndexTickerUSDT$ = defer(() => getMarketIndexTicker({ quoteCcy: 'USDT' })).pipe(map((x) => {
|
|
45
|
-
const mapInstIdToPrice = new Map();
|
|
46
|
-
x.data.forEach((inst) => mapInstIdToPrice.set(inst.instId, Number(inst.idxPx)));
|
|
47
|
-
return mapInstIdToPrice;
|
|
48
|
-
}), repeat({ delay: 1000 }), retry({ delay: 5000 }), shareReplay(1));
|
|
49
42
|
export const tradingAccountId$ = accountUid$.pipe(map((uid) => `okx/${uid}/trading`), shareReplay(1));
|
|
50
43
|
defer(async () => {
|
|
51
44
|
const tradingAccountId = await firstValueFrom(tradingAccountId$);
|
|
52
45
|
addAccountMarket(terminal, { account_id: tradingAccountId, market_id: 'OKX' });
|
|
53
|
-
provideAccountInfoService(terminal, tradingAccountId,
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
getAccountPositions(credential, {}),
|
|
57
|
-
getAccountBalance(credential, {}),
|
|
58
|
-
firstValueFrom(productService.mapProductIdToProduct$),
|
|
59
|
-
firstValueFrom(marketIndexTickerUSDT$),
|
|
60
|
-
]);
|
|
61
|
-
let totalEquity = 0;
|
|
62
|
-
let totalFree = 0;
|
|
63
|
-
const positions = [];
|
|
64
|
-
(_a = balanceApi.data[0]) === null || _a === void 0 ? void 0 : _a.details.forEach((detail) => {
|
|
65
|
-
var _a, _b, _c, _d, _e, _f;
|
|
66
|
-
if (detail.ccy === 'USDT') {
|
|
67
|
-
const balance = +((_a = detail.cashBal) !== null && _a !== void 0 ? _a : 0);
|
|
68
|
-
const free = Math.min(balance, // free should no more than balance if there is much profits
|
|
69
|
-
+((_b = detail.availEq) !== null && _b !== void 0 ? _b : 0));
|
|
70
|
-
const equity = +((_c = detail.eq) !== null && _c !== void 0 ? _c : 0) - +((_d = detail.stgyEq) !== null && _d !== void 0 ? _d : 0);
|
|
71
|
-
totalEquity += equity;
|
|
72
|
-
totalFree += free;
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
const volume = +((_e = detail.cashBal) !== null && _e !== void 0 ? _e : 0);
|
|
76
|
-
const free_volume = Math.min(volume, // free should no more than balance if there is much profits
|
|
77
|
-
+((_f = detail.availEq) !== null && _f !== void 0 ? _f : 0));
|
|
78
|
-
const closable_price = marketIndexTickerUSDT.get(detail.ccy + '-USDT') || 0;
|
|
79
|
-
const delta_equity = volume * closable_price || 0;
|
|
80
|
-
const delta_profit = +detail.totalPnl || 0;
|
|
81
|
-
const product_id = encodePath('SPOT', `${detail.ccy}-USDT`);
|
|
82
|
-
positions.push({
|
|
83
|
-
position_id: product_id,
|
|
84
|
-
datasource_id: 'OKX',
|
|
85
|
-
product_id: product_id,
|
|
86
|
-
direction: 'LONG',
|
|
87
|
-
volume: volume,
|
|
88
|
-
free_volume: free_volume,
|
|
89
|
-
position_price: +detail.accAvgPx,
|
|
90
|
-
floating_profit: delta_profit,
|
|
91
|
-
closable_price: closable_price,
|
|
92
|
-
valuation: delta_equity,
|
|
93
|
-
});
|
|
94
|
-
totalEquity += delta_equity;
|
|
95
|
-
}
|
|
96
|
-
});
|
|
97
|
-
positionsApi.data.forEach((x) => {
|
|
98
|
-
var _a, _b;
|
|
99
|
-
const direction = x.posSide === 'long' ? 'LONG' : x.posSide === 'short' ? 'SHORT' : +x.pos > 0 ? 'LONG' : 'SHORT';
|
|
100
|
-
const volume = Math.abs(+x.pos);
|
|
101
|
-
const product_id = encodePath(x.instType, x.instId);
|
|
102
|
-
const closable_price = +x.last;
|
|
103
|
-
const valuation = ((_b = (_a = mapProductIdToProduct.get(product_id)) === null || _a === void 0 ? void 0 : _a.value_scale) !== null && _b !== void 0 ? _b : 1) * volume * closable_price || 0;
|
|
104
|
-
positions.push({
|
|
105
|
-
position_id: x.posId,
|
|
106
|
-
datasource_id: 'OKX',
|
|
107
|
-
product_id,
|
|
108
|
-
direction,
|
|
109
|
-
volume: volume,
|
|
110
|
-
free_volume: +x.availPos,
|
|
111
|
-
closable_price,
|
|
112
|
-
position_price: +x.avgPx,
|
|
113
|
-
floating_profit: +x.upl,
|
|
114
|
-
valuation,
|
|
115
|
-
});
|
|
116
|
-
});
|
|
117
|
-
return {
|
|
118
|
-
money: {
|
|
119
|
-
currency: 'USDT',
|
|
120
|
-
equity: totalEquity,
|
|
121
|
-
free: totalFree,
|
|
122
|
-
},
|
|
123
|
-
positions: positions,
|
|
124
|
-
};
|
|
125
|
-
}, { auto_refresh_interval: 1000 });
|
|
46
|
+
provideAccountInfoService(terminal, tradingAccountId, () => getTradingAccountInfo(credential), {
|
|
47
|
+
auto_refresh_interval: 1000,
|
|
48
|
+
});
|
|
126
49
|
}).subscribe();
|
|
127
50
|
defer(async () => {
|
|
128
51
|
const uid = await firstValueFrom(accountUid$);
|
|
129
52
|
const fundingAccountId = `okx/${uid}/funding/USDT`;
|
|
130
|
-
provideAccountInfoService(terminal, fundingAccountId,
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
firstValueFrom(marketIndexTickerUSDT$),
|
|
134
|
-
]);
|
|
135
|
-
const positions = [];
|
|
136
|
-
let equity = 0;
|
|
137
|
-
let free = 0;
|
|
138
|
-
assetBalances.data.forEach((x) => {
|
|
139
|
-
if (x.ccy === 'USDT') {
|
|
140
|
-
const balance = +x.bal;
|
|
141
|
-
equity += balance;
|
|
142
|
-
free += balance;
|
|
143
|
-
return;
|
|
144
|
-
}
|
|
145
|
-
const price = marketIndexTickerUSDT.get(`${x.ccy}-USDT`) || 0;
|
|
146
|
-
const valuation = price * +x.bal || 0;
|
|
147
|
-
const productId = encodePath('SPOT', `${x.ccy}-USDT`);
|
|
148
|
-
positions.push({
|
|
149
|
-
datasource_id: 'OKX',
|
|
150
|
-
position_id: productId,
|
|
151
|
-
product_id: productId,
|
|
152
|
-
direction: 'LONG',
|
|
153
|
-
volume: +x.bal,
|
|
154
|
-
free_volume: +x.bal,
|
|
155
|
-
position_price: price,
|
|
156
|
-
floating_profit: 0,
|
|
157
|
-
closable_price: price,
|
|
158
|
-
valuation,
|
|
159
|
-
});
|
|
160
|
-
equity += valuation;
|
|
161
|
-
});
|
|
162
|
-
return {
|
|
163
|
-
money: {
|
|
164
|
-
currency: 'USDT',
|
|
165
|
-
equity,
|
|
166
|
-
free,
|
|
167
|
-
},
|
|
168
|
-
positions,
|
|
169
|
-
};
|
|
170
|
-
}, { auto_refresh_interval: 1000 });
|
|
53
|
+
provideAccountInfoService(terminal, fundingAccountId, () => getFundingAccountInfo(credential), {
|
|
54
|
+
auto_refresh_interval: 1000,
|
|
55
|
+
});
|
|
171
56
|
}).subscribe();
|
|
172
57
|
defer(async () => {
|
|
173
58
|
const uid = await firstValueFrom(accountUid$);
|
|
174
59
|
const earningAccountId = `okx/${uid}/earning/USDT`;
|
|
175
|
-
provideAccountInfoService(terminal, earningAccountId,
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
const free = equity;
|
|
179
|
-
return {
|
|
180
|
-
money: {
|
|
181
|
-
currency: 'USDT',
|
|
182
|
-
equity,
|
|
183
|
-
free,
|
|
184
|
-
},
|
|
185
|
-
positions: [],
|
|
186
|
-
};
|
|
187
|
-
}, { auto_refresh_interval: 5000 });
|
|
60
|
+
provideAccountInfoService(terminal, earningAccountId, () => getEarningAccountInfo(credential), {
|
|
61
|
+
auto_refresh_interval: 5000,
|
|
62
|
+
});
|
|
188
63
|
}).subscribe();
|
|
64
|
+
// 导出 marketIndexTickerUSDT$ 供其他模块使用
|
|
65
|
+
export { marketIndexTickerUSDT$ };
|
|
189
66
|
//# sourceMappingURL=account.js.map
|
package/dist/account.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"account.js","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAa,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AAC9F,OAAO,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,MAAM,CAAC;AACtF,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,mBAAmB,EACnB,gBAAgB,EAChB,wBAAwB,EACxB,iBAAiB,EACjB,oBAAoB,EACpB,qBAAqB,GACtB,MAAM,OAAO,CAAC;AACf,OAAO,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAC3C,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAEpD,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,UAAU,GAAG,oBAAoB,EAAE,CAAC;AAE1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,QAAQ,EAAE,MAAM;gBAChB,MAAM;gBACN,IAAI;aACL;YACD,SAAS,EAAE,EAAE;SACd,CAAC;IACJ,CAAC,EACD,EAAE,qBAAqB,EAAE,IAAI,EAAE,CAChC,CAAC;AACJ,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC","sourcesContent":["import { addAccountMarket, IPosition, provideAccountInfoService } from '@yuants/data-account';\nimport { providePendingOrdersService } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { encodePath } from '@yuants/utils';\nimport { defer, filter, firstValueFrom, map, repeat, retry, shareReplay } from 'rxjs';\nimport {\n getAccountConfig,\n getAccountBalance,\n getAccountPositions,\n getAssetBalances,\n getFinanceSavingsBalance,\n getSubAccountList,\n getDefaultCredential,\n getTradeOrdersPending,\n} from './api';\nimport { productService } from './product';\nimport { getMarketIndexTicker } from './public-api';\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst credential = getDefaultCredential();\n\nexport const accountConfig$ = defer(() => getAccountConfig(credential)).pipe(\n repeat({ delay: 10_000 }),\n retry({ delay: 10_000 }),\n shareReplay(1),\n);\n\nconst subAccountUids$ = defer(() => getSubAccountList(credential)).pipe(\n repeat({ delay: 10_000 }),\n retry({ delay: 10_000 }),\n shareReplay(1),\n);\n\nexport const accountUid$ = accountConfig$.pipe(\n map((x) => x.data[0].uid),\n filter((x) => !!x),\n shareReplay(1),\n);\n\ndefer(async () => {\n const uid = await firstValueFrom(accountUid$);\n const account_id = `okx/${uid}/trading`;\n providePendingOrdersService(\n terminal,\n account_id,\n async () => {\n const orders = await getTradeOrdersPending(credential, {});\n return orders.data.map((x) => {\n const order_type = x.ordType === 'market' ? 'MARKET' : x.ordType === 'limit' ? 'LIMIT' : 'UNKNOWN';\n\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 return {\n order_id: x.ordId,\n account_id,\n product_id: encodePath(x.instType, x.instId),\n submit_at: +x.cTime,\n filled_at: +x.fillTime,\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 };\n });\n },\n { auto_refresh_interval: 5000 },\n );\n}).subscribe();\n\nconst marketIndexTickerUSDT$ = defer(() => getMarketIndexTicker({ quoteCcy: 'USDT' })).pipe(\n map((x) => {\n const mapInstIdToPrice = new Map<string, number>();\n x.data.forEach((inst) => mapInstIdToPrice.set(inst.instId, Number(inst.idxPx)));\n return mapInstIdToPrice;\n }),\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nexport const tradingAccountId$ = accountUid$.pipe(\n map((uid) => `okx/${uid}/trading`),\n shareReplay(1),\n);\n\ndefer(async () => {\n const tradingAccountId = await firstValueFrom(tradingAccountId$);\n addAccountMarket(terminal, { account_id: tradingAccountId, market_id: 'OKX' });\n\n provideAccountInfoService(\n terminal,\n tradingAccountId,\n async () => {\n const [positionsApi, balanceApi, mapProductIdToProduct, marketIndexTickerUSDT] = await Promise.all([\n getAccountPositions(credential, {}),\n getAccountBalance(credential, {}),\n firstValueFrom(productService.mapProductIdToProduct$),\n firstValueFrom(marketIndexTickerUSDT$),\n ]);\n\n let totalEquity = 0;\n let totalFree = 0;\n const positions: IPosition[] = [];\n\n balanceApi.data[0]?.details.forEach((detail) => {\n if (detail.ccy === 'USDT') {\n const balance = +(detail.cashBal ?? 0);\n const free = Math.min(\n balance, // free should no more than balance if there is much profits\n +(detail.availEq ?? 0),\n );\n const equity = +(detail.eq ?? 0) - +(detail.stgyEq ?? 0);\n totalEquity += equity;\n totalFree += free;\n } else {\n const volume = +(detail.cashBal ?? 0);\n const free_volume = Math.min(\n volume, // free should no more than balance if there is much profits\n +(detail.availEq ?? 0),\n );\n const closable_price = marketIndexTickerUSDT.get(detail.ccy + '-USDT') || 0;\n const delta_equity = volume * closable_price || 0;\n const delta_profit = +detail.totalPnl || 0;\n\n const product_id = encodePath('SPOT', `${detail.ccy}-USDT`);\n positions.push({\n position_id: product_id,\n datasource_id: 'OKX',\n product_id: product_id,\n direction: 'LONG',\n volume: volume,\n free_volume: free_volume,\n position_price: +detail.accAvgPx,\n floating_profit: delta_profit,\n closable_price: closable_price,\n valuation: delta_equity,\n });\n\n totalEquity += delta_equity;\n }\n });\n positionsApi.data.forEach((x) => {\n const direction =\n x.posSide === 'long' ? 'LONG' : x.posSide === 'short' ? 'SHORT' : +x.pos > 0 ? 'LONG' : 'SHORT';\n const volume = Math.abs(+x.pos);\n const product_id = encodePath(x.instType, x.instId);\n const closable_price = +x.last;\n const valuation =\n (mapProductIdToProduct.get(product_id)?.value_scale ?? 1) * volume * closable_price || 0;\n\n positions.push({\n position_id: x.posId,\n datasource_id: 'OKX',\n product_id,\n direction,\n volume: volume,\n free_volume: +x.availPos,\n closable_price,\n position_price: +x.avgPx,\n floating_profit: +x.upl,\n valuation,\n });\n });\n return {\n money: {\n currency: 'USDT',\n equity: totalEquity,\n free: totalFree,\n },\n positions: positions,\n };\n },\n { auto_refresh_interval: 1000 },\n );\n}).subscribe();\n\ndefer(async () => {\n const uid = await firstValueFrom(accountUid$);\n\n const fundingAccountId = `okx/${uid}/funding/USDT`;\n\n provideAccountInfoService(\n terminal,\n fundingAccountId,\n async () => {\n const [assetBalances, marketIndexTickerUSDT] = await Promise.all([\n getAssetBalances(credential, {}),\n firstValueFrom(marketIndexTickerUSDT$),\n ]);\n\n const positions: IPosition[] = [];\n let equity = 0;\n let free = 0;\n\n assetBalances.data.forEach((x) => {\n if (x.ccy === 'USDT') {\n const balance = +x.bal;\n equity += balance;\n free += balance;\n return;\n }\n\n const price = marketIndexTickerUSDT.get(`${x.ccy}-USDT`) || 0;\n const valuation = price * +x.bal || 0;\n const productId = encodePath('SPOT', `${x.ccy}-USDT`);\n\n positions.push({\n datasource_id: 'OKX',\n position_id: productId,\n product_id: productId,\n direction: 'LONG',\n volume: +x.bal,\n free_volume: +x.bal,\n position_price: price,\n floating_profit: 0,\n closable_price: price,\n valuation,\n });\n\n equity += valuation;\n });\n\n return {\n money: {\n currency: 'USDT',\n equity,\n free,\n },\n positions,\n };\n },\n { auto_refresh_interval: 1000 },\n );\n}).subscribe();\n\ndefer(async () => {\n const uid = await firstValueFrom(accountUid$);\n const earningAccountId = `okx/${uid}/earning/USDT`;\n provideAccountInfoService(\n terminal,\n earningAccountId,\n async () => {\n const offers = await getFinanceSavingsBalance(credential, {});\n const equity = offers.data.filter((x) => x.ccy === 'USDT').reduce((acc, x) => acc + +x.amt, 0);\n const free = equity;\n return {\n money: {\n currency: 'USDT',\n equity,\n free,\n },\n positions: [],\n };\n },\n { auto_refresh_interval: 5000 },\n );\n}).subscribe();\n"]}
|
|
1
|
+
{"version":3,"file":"account.js","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,gBAAgB,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AACnF,OAAO,EAAE,2BAA2B,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,MAAM,CAAC;AACtF,OAAO,EAAE,gBAAgB,EAAE,oBAAoB,EAAE,qBAAqB,EAAE,MAAM,OAAO,CAAC;AACtF,OAAO,EACL,qBAAqB,EACrB,qBAAqB,EACrB,qBAAqB,EACrB,sBAAsB,GACvB,MAAM,gBAAgB,CAAC;AAExB,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,UAAU,GAAG,oBAAoB,EAAE,CAAC;AAE1C,MAAM,CAAC,MAAM,cAAc,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,gBAAg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CAAC,CAAC;IAE9C,MAAM,gBAAgB,GAAG,OAAO,GAAG,eAAe,CAAC;IAEnD,yBAAyB,CAAC,QAAQ,EAAE,gBAAgB,EAAE,GAAG,EAAE,CAAC,qBAAqB,CAAC,UAAU,CAAC,EAAE;QAC7F,qBAAqB,EAAE,IAAI;KAC5B,CAAC,CAAC;AACL,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;AAEf,KAAK,CAAC,KAAK,IAAI,EAAE;IACf,MAAM,GAAG,GAAG,MAAM,cAAc,CAAC,WAAW,CAAC,CAAC;IAC9C,MAAM,gBAAgB,GAAG,OAAO,GAAG,eAAe,CAAC;IACnD,yBAAyB,CAAC,QAAQ,EAAE,gBAAgB,EAAE,GAAG,EAAE,CAAC,qBAAqB,CAAC,UAAU,CAAC,EAAE;QAC7F,qBAAqB,EAAE,IAAI;KAC5B,CAAC,CAAC;AACL,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC;AAEf,oCAAoC;AACpC,OAAO,EAAE,sBAAsB,EAAE,CAAC","sourcesContent":["import { addAccountMarket, provideAccountInfoService } from '@yuants/data-account';\nimport { providePendingOrdersService } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { encodePath } from '@yuants/utils';\nimport { defer, filter, firstValueFrom, map, repeat, retry, shareReplay } from 'rxjs';\nimport { getAccountConfig, getDefaultCredential, getTradeOrdersPending } from './api';\nimport {\n getTradingAccountInfo,\n getFundingAccountInfo,\n getEarningAccountInfo,\n marketIndexTickerUSDT$,\n} from './accountInfos';\n\nconst terminal = Terminal.fromNodeEnv();\n\nconst credential = getDefaultCredential();\n\nexport const accountConfig$ = defer(() => getAccountConfig(credential)).pipe(\n repeat({ delay: 10_000 }),\n retry({ delay: 10_000 }),\n shareReplay(1),\n);\n\nexport const accountUid$ = accountConfig$.pipe(\n map((x) => x.data[0].uid),\n filter((x) => !!x),\n shareReplay(1),\n);\n\ndefer(async () => {\n const uid = await firstValueFrom(accountUid$);\n const account_id = `okx/${uid}/trading`;\n providePendingOrdersService(\n terminal,\n account_id,\n async () => {\n const orders = await getTradeOrdersPending(credential, {});\n return orders.data.map((x) => {\n const order_type = x.ordType === 'market' ? 'MARKET' : x.ordType === 'limit' ? 'LIMIT' : 'UNKNOWN';\n\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 return {\n order_id: x.ordId,\n account_id,\n product_id: encodePath(x.instType, x.instId),\n submit_at: +x.cTime,\n filled_at: +x.fillTime,\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 };\n });\n },\n { auto_refresh_interval: 5000 },\n );\n}).subscribe();\n\nexport const tradingAccountId$ = accountUid$.pipe(\n map((uid) => `okx/${uid}/trading`),\n shareReplay(1),\n);\n\ndefer(async () => {\n const tradingAccountId = await firstValueFrom(tradingAccountId$);\n addAccountMarket(terminal, { account_id: tradingAccountId, market_id: 'OKX' });\n\n provideAccountInfoService(terminal, tradingAccountId, () => getTradingAccountInfo(credential), {\n auto_refresh_interval: 1000,\n });\n}).subscribe();\n\ndefer(async () => {\n const uid = await firstValueFrom(accountUid$);\n\n const fundingAccountId = `okx/${uid}/funding/USDT`;\n\n provideAccountInfoService(terminal, fundingAccountId, () => getFundingAccountInfo(credential), {\n auto_refresh_interval: 1000,\n });\n}).subscribe();\n\ndefer(async () => {\n const uid = await firstValueFrom(accountUid$);\n const earningAccountId = `okx/${uid}/earning/USDT`;\n provideAccountInfoService(terminal, earningAccountId, () => getEarningAccountInfo(credential), {\n auto_refresh_interval: 5000,\n });\n}).subscribe();\n\n// 导出 marketIndexTickerUSDT$ 供其他模块使用\nexport { marketIndexTickerUSDT$ };\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { getFinanceSavingsBalance } from '../api';
|
|
2
|
+
export const getEarningAccountInfo = async (credential) => {
|
|
3
|
+
const offers = await getFinanceSavingsBalance(credential, {});
|
|
4
|
+
const equity = offers.data.filter((x) => x.ccy === 'USDT').reduce((acc, x) => acc + +x.amt, 0);
|
|
5
|
+
const free = equity;
|
|
6
|
+
return {
|
|
7
|
+
money: {
|
|
8
|
+
currency: 'USDT',
|
|
9
|
+
equity,
|
|
10
|
+
free,
|
|
11
|
+
},
|
|
12
|
+
positions: [],
|
|
13
|
+
};
|
|
14
|
+
};
|
|
15
|
+
//# sourceMappingURL=earning.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"earning.js","sourceRoot":"","sources":["../../src/accountInfos/earning.ts"],"names":[],"mappings":"AAAA,OAAO,EAAe,wBAAwB,EAAE,MAAM,QAAQ,CAAC;AAG/D,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,EAAE,UAAuB,EAA6B,EAAE;IAChG,MAAM,MAAM,GAAG,MAAM,wBAAwB,CAAC,UAAU,EAAE,EAAE,CAAC,CAAC;IAC9D,MAAM,MAAM,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,GAAG,KAAK,MAAM,CAAC,CAAC,MAAM,CAAC,CAAC,GAAG,EAAE,CAAC,EAAE,EAAE,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;IAC/F,MAAM,IAAI,GAAG,MAAM,CAAC;IAEpB,OAAO;QACL,KAAK,EAAE;YACL,QAAQ,EAAE,MAAM;YAChB,MAAM;YACN,IAAI;SACL;QACD,SAAS,EAAE,EAAE;KACd,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { ICredential, getFinanceSavingsBalance } from '../api';\nimport { IAccountInfoCore } from './types';\n\nexport const getEarningAccountInfo = async (credential: ICredential): Promise<IAccountInfoCore> => {\n const offers = await getFinanceSavingsBalance(credential, {});\n const equity = offers.data.filter((x) => x.ccy === 'USDT').reduce((acc, x) => acc + +x.amt, 0);\n const free = equity;\n\n return {\n money: {\n currency: 'USDT',\n equity,\n free,\n },\n positions: [],\n };\n};\n"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { encodePath } from '@yuants/utils';
|
|
2
|
+
import { firstValueFrom } from 'rxjs';
|
|
3
|
+
import { getAssetBalances } from '../api';
|
|
4
|
+
import { marketIndexTickerUSDT$ } from './trading';
|
|
5
|
+
export const getFundingAccountInfo = async (credential) => {
|
|
6
|
+
const [assetBalances, marketIndexTickerUSDT] = await Promise.all([
|
|
7
|
+
getAssetBalances(credential, {}),
|
|
8
|
+
firstValueFrom(marketIndexTickerUSDT$),
|
|
9
|
+
]);
|
|
10
|
+
const positions = [];
|
|
11
|
+
let equity = 0;
|
|
12
|
+
let free = 0;
|
|
13
|
+
assetBalances.data.forEach((x) => {
|
|
14
|
+
if (x.ccy === 'USDT') {
|
|
15
|
+
const balance = +x.bal;
|
|
16
|
+
equity += balance;
|
|
17
|
+
free += balance;
|
|
18
|
+
return;
|
|
19
|
+
}
|
|
20
|
+
const price = marketIndexTickerUSDT.get(`${x.ccy}-USDT`) || 0;
|
|
21
|
+
const valuation = price * +x.bal || 0;
|
|
22
|
+
const productId = encodePath('SPOT', `${x.ccy}-USDT`);
|
|
23
|
+
positions.push({
|
|
24
|
+
datasource_id: 'OKX',
|
|
25
|
+
position_id: productId,
|
|
26
|
+
product_id: productId,
|
|
27
|
+
direction: 'LONG',
|
|
28
|
+
volume: +x.bal,
|
|
29
|
+
free_volume: +x.bal,
|
|
30
|
+
position_price: price,
|
|
31
|
+
floating_profit: 0,
|
|
32
|
+
closable_price: price,
|
|
33
|
+
valuation,
|
|
34
|
+
});
|
|
35
|
+
equity += valuation;
|
|
36
|
+
});
|
|
37
|
+
return {
|
|
38
|
+
money: {
|
|
39
|
+
currency: 'USDT',
|
|
40
|
+
equity,
|
|
41
|
+
free,
|
|
42
|
+
},
|
|
43
|
+
positions,
|
|
44
|
+
};
|
|
45
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,CAAC,IAAI,CAAC;YACb,aAAa,EAAE,KAAK;YACpB,WAAW,EAAE,SAAS;YACtB,UAAU,EAAE,SAAS;YACrB,SAAS,EAAE,MAAM;YACjB,MAAM,EAAE,CAAC,CAAC,CAAC,GAAG;YACd,WAAW,EAAE,CAAC,CAAC,CAAC,GAAG;YACnB,cAAc,EAAE,KAAK;YACrB,eAAe,EAAE,CAAC;YAClB,cAAc,EAAE,KAAK;YACrB,SAAS;SACV,CAAC,CAAC;QAEH,MAAM,IAAI,SAAS,CAAC;IACtB,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,KAAK,EAAE;YACL,QAAQ,EAAE,MAAM;YAChB,MAAM;YACN,IAAI;SACL;QACD,SAAS;KACV,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { encodePath } from '@yuants/utils';\nimport { firstValueFrom } from 'rxjs';\nimport { ICredential, getAssetBalances } from '../api';\nimport { marketIndexTickerUSDT$ } from './trading';\nimport { IAccountInfoCore } from './types';\n\nexport const getFundingAccountInfo = async (credential: ICredential): Promise<IAccountInfoCore> => {\n const [assetBalances, marketIndexTickerUSDT] = await Promise.all([\n getAssetBalances(credential, {}),\n firstValueFrom(marketIndexTickerUSDT$),\n ]);\n\n const positions: IPosition[] = [];\n let equity = 0;\n let free = 0;\n\n assetBalances.data.forEach((x) => {\n if (x.ccy === 'USDT') {\n const balance = +x.bal;\n equity += balance;\n free += balance;\n return;\n }\n\n const price = marketIndexTickerUSDT.get(`${x.ccy}-USDT`) || 0;\n const valuation = price * +x.bal || 0;\n const productId = encodePath('SPOT', `${x.ccy}-USDT`);\n\n positions.push({\n datasource_id: 'OKX',\n position_id: productId,\n product_id: productId,\n direction: 'LONG',\n volume: +x.bal,\n free_volume: +x.bal,\n position_price: price,\n floating_profit: 0,\n closable_price: price,\n valuation,\n });\n\n equity += valuation;\n });\n\n return {\n money: {\n currency: 'USDT',\n equity,\n free,\n },\n positions,\n };\n};\n"]}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export { getTradingAccountInfo } from './trading';
|
|
2
|
+
export { getFundingAccountInfo } from './funding';
|
|
3
|
+
export { getLoanAccountInfo } from './loan';
|
|
4
|
+
export { getEarningAccountInfo } from './earning';
|
|
5
|
+
export { getStrategyAccountInfo } from './strategy';
|
|
6
|
+
export { marketIndexTickerUSDT$ } from './trading';
|
|
7
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/accountInfos/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAClD,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAClD,OAAO,EAAE,kBAAkB,EAAE,MAAM,QAAQ,CAAC;AAC5C,OAAO,EAAE,qBAAqB,EAAE,MAAM,WAAW,CAAC;AAClD,OAAO,EAAE,sBAAsB,EAAE,MAAM,YAAY,CAAC;AACpD,OAAO,EAAE,sBAAsB,EAAE,MAAM,WAAW,CAAC","sourcesContent":["export { getTradingAccountInfo } from './trading';\nexport { getFundingAccountInfo } from './funding';\nexport { getLoanAccountInfo } from './loan';\nexport { getEarningAccountInfo } from './earning';\nexport { getStrategyAccountInfo } from './strategy';\nexport { marketIndexTickerUSDT$ } from './trading';\nexport { IAccountInfoCore } from './types';\n"]}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { encodePath } from '@yuants/utils';
|
|
2
|
+
import { getFlexibleLoanInfo } from '../api';
|
|
3
|
+
export const getLoanAccountInfo = async (credential) => {
|
|
4
|
+
const res = await getFlexibleLoanInfo(credential);
|
|
5
|
+
const data = res.data[0];
|
|
6
|
+
const positions = [];
|
|
7
|
+
for (const loan of data.loanData) {
|
|
8
|
+
positions.push({
|
|
9
|
+
datasource_id: 'OKX',
|
|
10
|
+
product_id: `SPOT/${loan.ccy}-USDT`,
|
|
11
|
+
volume: +loan.amt,
|
|
12
|
+
free_volume: +loan.amt,
|
|
13
|
+
position_id: encodePath('loan', loan.ccy),
|
|
14
|
+
direction: 'SHORT',
|
|
15
|
+
position_price: 0,
|
|
16
|
+
closable_price: 0,
|
|
17
|
+
floating_profit: 0,
|
|
18
|
+
valuation: 0,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
for (const collateral of data.collateralData) {
|
|
22
|
+
positions.push({
|
|
23
|
+
datasource_id: 'OKX',
|
|
24
|
+
product_id: `SPOT/${collateral.ccy}-USDT`,
|
|
25
|
+
volume: +collateral.amt,
|
|
26
|
+
free_volume: +collateral.amt,
|
|
27
|
+
position_id: encodePath('collateral', collateral.ccy),
|
|
28
|
+
direction: 'LONG',
|
|
29
|
+
position_price: 0,
|
|
30
|
+
closable_price: 0,
|
|
31
|
+
floating_profit: 0,
|
|
32
|
+
valuation: 0,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
const equity = +data.collateralNotionalUsd - +data.loanNotionalUsd;
|
|
36
|
+
const free = equity - +data.collateralNotionalUsd;
|
|
37
|
+
return {
|
|
38
|
+
money: {
|
|
39
|
+
currency: 'USDT',
|
|
40
|
+
equity,
|
|
41
|
+
free,
|
|
42
|
+
},
|
|
43
|
+
positions,
|
|
44
|
+
};
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=loan.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"loan.js","sourceRoot":"","sources":["../../src/accountInfos/loan.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAe,mBAAmB,EAAE,MAAM,QAAQ,CAAC;AAG1D,MAAM,CAAC,MAAM,kBAAkB,GAAG,KAAK,EAAE,UAAuB,EAA6B,EAAE;IAC7F,MAAM,GAAG,GAAG,MAAM,mBAAmB,CAAC,UAAU,CAAC,CAAC;IAClD,MAAM,IAAI,GAAG,GAAG,CAAC,IAAI,CAAC,CAAC,CAA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["import { IPosition } from '@yuants/data-account';\nimport { encodePath } from '@yuants/utils';\nimport { ICredential, getFlexibleLoanInfo } from '../api';\nimport { IAccountInfoCore } from './types';\n\nexport const getLoanAccountInfo = async (credential: ICredential): Promise<IAccountInfoCore> => {\n const res = await getFlexibleLoanInfo(credential);\n const data = res.data[0];\n\n const positions: IPosition[] = [];\n for (const loan of data.loanData) {\n positions.push({\n datasource_id: 'OKX',\n product_id: `SPOT/${loan.ccy}-USDT`,\n volume: +loan.amt,\n free_volume: +loan.amt,\n position_id: encodePath('loan', loan.ccy),\n direction: 'SHORT',\n position_price: 0,\n closable_price: 0,\n floating_profit: 0,\n valuation: 0,\n });\n }\n for (const collateral of data.collateralData) {\n positions.push({\n datasource_id: 'OKX',\n product_id: `SPOT/${collateral.ccy}-USDT`,\n volume: +collateral.amt,\n free_volume: +collateral.amt,\n position_id: encodePath('collateral', collateral.ccy),\n direction: 'LONG',\n position_price: 0,\n closable_price: 0,\n floating_profit: 0,\n valuation: 0,\n });\n }\n const equity = +data.collateralNotionalUsd - +data.loanNotionalUsd;\n const free = equity - +data.collateralNotionalUsd;\n\n return {\n money: {\n currency: 'USDT',\n equity,\n free,\n },\n positions,\n };\n};\n"]}
|
|
@@ -0,0 +1,56 @@
|
|
|
1
|
+
import { encodePath } from '@yuants/utils';
|
|
2
|
+
import { getGridOrdersAlgoPending, getGridPositions } from '../api';
|
|
3
|
+
export const getStrategyAccountInfo = async (credential) => {
|
|
4
|
+
// TODO: 需要分页获取所有的网格订单 (每页 100 条)
|
|
5
|
+
const [gridAlgoOrders] = await Promise.all([
|
|
6
|
+
getGridOrdersAlgoPending(credential, {
|
|
7
|
+
algoOrdType: 'contract_grid',
|
|
8
|
+
}),
|
|
9
|
+
]);
|
|
10
|
+
let totalEquity = 0;
|
|
11
|
+
const positions = [];
|
|
12
|
+
const gridPositionsRes = await Promise.all(gridAlgoOrders.data.map((item) => getGridPositions(credential, { algoOrdType: 'contract_grid', algoId: item.algoId })));
|
|
13
|
+
gridPositionsRes.forEach((gridPositions, index) => {
|
|
14
|
+
var _a, _b, _c, _d;
|
|
15
|
+
let positionValuation = 0;
|
|
16
|
+
const leverage = +((_a = gridAlgoOrders.data) === null || _a === void 0 ? void 0 : _a[index].actualLever);
|
|
17
|
+
(_b = gridPositions === null || gridPositions === void 0 ? void 0 : gridPositions.data) === null || _b === void 0 ? void 0 : _b.forEach((position) => {
|
|
18
|
+
var _a, _b, _c;
|
|
19
|
+
if (+position.pos !== 0) {
|
|
20
|
+
const directionRaw = (_c = (_b = (_a = gridAlgoOrders.data) === null || _a === void 0 ? void 0 : _a[index]) === null || _b === void 0 ? void 0 : _b.direction) !== null && _c !== void 0 ? _c : '';
|
|
21
|
+
const direction = directionRaw ? directionRaw.toUpperCase() : 'LONG';
|
|
22
|
+
positions.push({
|
|
23
|
+
position_id: encodePath(position.algoId, position.instId),
|
|
24
|
+
datasource_id: 'OKX',
|
|
25
|
+
product_id: encodePath(position.instType, position.instId),
|
|
26
|
+
direction,
|
|
27
|
+
volume: Math.abs(+position.pos),
|
|
28
|
+
free_volume: +position.pos,
|
|
29
|
+
position_price: +position.avgPx,
|
|
30
|
+
floating_profit: +position.upl,
|
|
31
|
+
closable_price: +position.last,
|
|
32
|
+
valuation: +position.notionalUsd,
|
|
33
|
+
});
|
|
34
|
+
positionValuation += +position.notionalUsd;
|
|
35
|
+
}
|
|
36
|
+
});
|
|
37
|
+
if (leverage === 0) {
|
|
38
|
+
// 实际杠杆为 0,说明没有持仓,直接把投资金额和累计盈亏算到净值里
|
|
39
|
+
totalEquity += +((_c = gridAlgoOrders.data) === null || _c === void 0 ? void 0 : _c[index].investment) + +((_d = gridAlgoOrders.data) === null || _d === void 0 ? void 0 : _d[index].totalPnl);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
// 历史提取金额不会从 investment, totalPnl 扣减
|
|
43
|
+
// 计算净值需要通过仓位的名义价值和实际杠杆计算
|
|
44
|
+
totalEquity += positionValuation / leverage;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
return {
|
|
48
|
+
money: {
|
|
49
|
+
currency: 'USDT',
|
|
50
|
+
equity: totalEquity,
|
|
51
|
+
free: 0, // TODO: 累计策略的可提取资金作为 free
|
|
52
|
+
},
|
|
53
|
+
positions,
|
|
54
|
+
};
|
|
55
|
+
};
|
|
56
|
+
//# sourceMappingURL=strategy.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"strategy.js","sourceRoot":"","sources":["../../src/accountInfos/strategy.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAe,wBAAwB,EAAE,gBAAgB,EAAE,MAAM,QAAQ,CAAC;AAGjF,MAAM,CAAC,MAAM,sBAAsB,GAAG,KAAK,EAAE,UAAuB,EAA6B,EAAE;IACjG,iCAAiC;IACjC,MAAM,CAAC,cAAc,CAAC,GAAG,MAAM,OAAO,CAAC,GAAG,CAAC;QACzC,wBAAwB,CAAC,UAAU,EAAE;YACnC,WAAW,EAAE,eAAe;SAC7B,CAAC;KACH,CAAC,CAAC;IAEH,IAAI,WAAW,GAAG,CAAC,CAAC;IACpB,MAAM,SAAS,GAAgB,EAAE,CAAC;IAElC,MAAM,gBAAgB,GAAG,MAAM,OAAO,CAAC,GAAG,CACxC,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAC/B,gBAAgB,CAAC,UAAU,EAAE,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,EAAE,IAAI,CAAC,MAAM,EAAE,CAAC,CACpF,CACF,CAAC;IAEF,gBAAgB,CAAC,OAAO,CAAC,CAAC,aAAa,EAAE,KAAK,EAAE,EAAE;;QAChD,IAAI,iBAAiB,GAAG,CAAC,CAAC;QAC1B,MAAM,QAAQ,GAAG,CAAC,CAAA,MAAA,cAAc,CAAC,IAAI,0CAAG,KAAK,EAAE,WAAW,CAAA,CAAC;QAC3D,MA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ACL,oCAAoC;YACpC,yBAAyB;YACzB,WAAW,IAAI,iBAAiB,GAAG,QAAQ,CAAC;SAC7C;IACH,CAAC,CAAC,CAAC;IAEH,OAAO;QACL,KAAK,EAAE;YACL,QAAQ,EAAE,MAAM;YAChB,MAAM,EAAE,WAAW;YACnB,IAAI,EAAE,CAAC,EAAE,0BAA0B;SACpC;QACD,SAAS;KACV,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { IPosition } from '@yuants/data-account';\nimport { encodePath } from '@yuants/utils';\nimport { ICredential, getGridOrdersAlgoPending, getGridPositions } from '../api';\nimport { IAccountInfoCore } from './types';\n\nexport const getStrategyAccountInfo = async (credential: ICredential): Promise<IAccountInfoCore> => {\n // TODO: 需要分页获取所有的网格订单 (每页 100 条)\n const [gridAlgoOrders] = await Promise.all([\n getGridOrdersAlgoPending(credential, {\n algoOrdType: 'contract_grid',\n }),\n ]);\n\n let totalEquity = 0;\n const positions: IPosition[] = [];\n\n const gridPositionsRes = await Promise.all(\n gridAlgoOrders.data.map((item) =>\n getGridPositions(credential, { algoOrdType: 'contract_grid', algoId: item.algoId }),\n ),\n );\n\n gridPositionsRes.forEach((gridPositions, index) => {\n let positionValuation = 0;\n const leverage = +gridAlgoOrders.data?.[index].actualLever;\n gridPositions?.data?.forEach((position) => {\n if (+position.pos !== 0) {\n const directionRaw = gridAlgoOrders.data?.[index]?.direction ?? '';\n const direction = directionRaw ? directionRaw.toUpperCase() : 'LONG';\n positions.push({\n position_id: encodePath(position.algoId, position.instId),\n datasource_id: 'OKX',\n product_id: encodePath(position.instType, position.instId),\n direction,\n volume: Math.abs(+position.pos),\n free_volume: +position.pos,\n position_price: +position.avgPx,\n floating_profit: +position.upl,\n closable_price: +position.last,\n valuation: +position.notionalUsd,\n });\n positionValuation += +position.notionalUsd;\n }\n });\n if (leverage === 0) {\n // 实际杠杆为 0,说明没有持仓,直接把投资金额和累计盈亏算到净值里\n totalEquity += +gridAlgoOrders.data?.[index].investment + +gridAlgoOrders.data?.[index].totalPnl;\n } else {\n // 历史提取金额不会从 investment, totalPnl 扣减\n // 计算净值需要通过仓位的名义价值和实际杠杆计算\n totalEquity += positionValuation / leverage;\n }\n });\n\n return {\n money: {\n currency: 'USDT',\n equity: totalEquity,\n free: 0, // TODO: 累计策略的可提取资金作为 free\n },\n positions,\n };\n};\n"]}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { encodePath } from '@yuants/utils';
|
|
2
|
+
import { defer, firstValueFrom, map, repeat, retry, shareReplay } from 'rxjs';
|
|
3
|
+
import { getAccountBalance, getAccountPositions } from '../api';
|
|
4
|
+
import { productService } from '../product';
|
|
5
|
+
import { getMarketIndexTicker } from '../public-api';
|
|
6
|
+
export const marketIndexTickerUSDT$ = defer(() => getMarketIndexTicker({ quoteCcy: 'USDT' })).pipe(map((x) => {
|
|
7
|
+
const mapInstIdToPrice = new Map();
|
|
8
|
+
x.data.forEach((inst) => mapInstIdToPrice.set(inst.instId, Number(inst.idxPx)));
|
|
9
|
+
return mapInstIdToPrice;
|
|
10
|
+
}), repeat({ delay: 1000 }), retry({ delay: 5000 }), shareReplay(1));
|
|
11
|
+
export const getTradingAccountInfo = async (credential) => {
|
|
12
|
+
var _a;
|
|
13
|
+
const [positionsApi, balanceApi, mapProductIdToProduct, marketIndexTickerUSDT] = await Promise.all([
|
|
14
|
+
getAccountPositions(credential, {}),
|
|
15
|
+
getAccountBalance(credential, {}),
|
|
16
|
+
firstValueFrom(productService.mapProductIdToProduct$),
|
|
17
|
+
firstValueFrom(marketIndexTickerUSDT$),
|
|
18
|
+
]);
|
|
19
|
+
let totalEquity = 0;
|
|
20
|
+
let totalFree = 0;
|
|
21
|
+
const positions = [];
|
|
22
|
+
(_a = balanceApi.data[0]) === null || _a === void 0 ? void 0 : _a.details.forEach((detail) => {
|
|
23
|
+
var _a, _b, _c, _d, _e, _f;
|
|
24
|
+
if (detail.ccy === 'USDT') {
|
|
25
|
+
const balance = +((_a = detail.cashBal) !== null && _a !== void 0 ? _a : 0);
|
|
26
|
+
const free = Math.min(balance, // free should no more than balance if there is much profits
|
|
27
|
+
+((_b = detail.availEq) !== null && _b !== void 0 ? _b : 0));
|
|
28
|
+
const equity = +((_c = detail.eq) !== null && _c !== void 0 ? _c : 0) - +((_d = detail.stgyEq) !== null && _d !== void 0 ? _d : 0);
|
|
29
|
+
totalEquity += equity;
|
|
30
|
+
totalFree += free;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
const volume = +((_e = detail.cashBal) !== null && _e !== void 0 ? _e : 0);
|
|
34
|
+
const free_volume = Math.min(volume, // free should no more than balance if there is much profits
|
|
35
|
+
+((_f = detail.availEq) !== null && _f !== void 0 ? _f : 0));
|
|
36
|
+
const closable_price = marketIndexTickerUSDT.get(detail.ccy + '-USDT') || 0;
|
|
37
|
+
const delta_equity = volume * closable_price || 0;
|
|
38
|
+
const delta_profit = +detail.totalPnl || 0;
|
|
39
|
+
const product_id = encodePath('SPOT', `${detail.ccy}-USDT`);
|
|
40
|
+
positions.push({
|
|
41
|
+
position_id: product_id,
|
|
42
|
+
datasource_id: 'OKX',
|
|
43
|
+
product_id: product_id,
|
|
44
|
+
direction: 'LONG',
|
|
45
|
+
volume: volume,
|
|
46
|
+
free_volume: free_volume,
|
|
47
|
+
position_price: +detail.accAvgPx,
|
|
48
|
+
floating_profit: delta_profit,
|
|
49
|
+
closable_price: closable_price,
|
|
50
|
+
valuation: delta_equity,
|
|
51
|
+
});
|
|
52
|
+
totalEquity += delta_equity;
|
|
53
|
+
}
|
|
54
|
+
});
|
|
55
|
+
positionsApi.data.forEach((x) => {
|
|
56
|
+
var _a, _b;
|
|
57
|
+
const direction = x.posSide === 'long' ? 'LONG' : x.posSide === 'short' ? 'SHORT' : +x.pos > 0 ? 'LONG' : 'SHORT';
|
|
58
|
+
const volume = Math.abs(+x.pos);
|
|
59
|
+
const product_id = encodePath(x.instType, x.instId);
|
|
60
|
+
const closable_price = +x.last;
|
|
61
|
+
const valuation = ((_b = (_a = mapProductIdToProduct.get(product_id)) === null || _a === void 0 ? void 0 : _a.value_scale) !== null && _b !== void 0 ? _b : 1) * volume * closable_price || 0;
|
|
62
|
+
positions.push({
|
|
63
|
+
position_id: x.posId,
|
|
64
|
+
datasource_id: 'OKX',
|
|
65
|
+
product_id,
|
|
66
|
+
direction,
|
|
67
|
+
volume: volume,
|
|
68
|
+
free_volume: +x.availPos,
|
|
69
|
+
closable_price,
|
|
70
|
+
position_price: +x.avgPx,
|
|
71
|
+
floating_profit: +x.upl,
|
|
72
|
+
valuation,
|
|
73
|
+
});
|
|
74
|
+
});
|
|
75
|
+
return {
|
|
76
|
+
money: {
|
|
77
|
+
currency: 'USDT',
|
|
78
|
+
equity: totalEquity,
|
|
79
|
+
free: totalFree,
|
|
80
|
+
},
|
|
81
|
+
positions: positions,
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
//# sourceMappingURL=trading.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trading.js","sourceRoot":"","sources":["../../src/accountInfos/trading.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,MAAM,MAAM,CAAC;AAC9E,OAAO,EAAe,iBAAiB,EAAE,mBAAmB,EAAE,MAAM,QAAQ,CAAC;AAC7E,OAAO,EAAE,cAAc,EAAE,MAAM,YAAY,CAAC;AAC5C,OAAO,EAAE,oBAAoB,EAAE,MAAM,eAAe,CAAC;AAGrD,MAAM,CAAC,MAAM,sBAAsB,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,oBAAoB,CAAC,EAAE,QAAQ,EAAE,MAAM,EAAE,CAAC,CAAC,CAAC,IAAI,CAChG,GAAG,CAAC,CAAC,CAAC,EAAE,EAAE;IACR,MAAM,gBAAgB,GAAG,IAAI,GAAG,EAAkB,CAAC;IACn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AC,CAAC,OAAO,KAAK,MAAM,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,OAAO,KAAK,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC;QAClG,MAAM,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC;QAChC,MAAM,UAAU,GAAG,UAAU,CAAC,CAAC,CAAC,QAAQ,EAAE,CAAC,CAAC,MAAM,CAAC,CAAC;QACpD,MAAM,cAAc,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC;QAC/B,MAAM,SAAS,GACb,CAAC,MAAA,MAAA,qBAAqB,CAAC,GAAG,CAAC,UAAU,CAAC,0CAAE,WAAW,mCAAI,CAAC,CAAC,GAAG,MAAM,GAAG,cAAc,IAAI,CAAC,CAAC;QAE3F,SAAS,CAAC,IAAI,CAAC;YACb,WAAW,EAAE,CAAC,CAAC,KAAK;YACpB,aAAa,EAAE,KAAK;YACpB,UAAU;YACV,SAAS;YACT,MAAM,EAAE,MAAM;YACd,WAAW,EAAE,CAAC,CAAC,CAAC,QAAQ;YACxB,cAAc;YACd,cAAc,EAAE,CAAC,CAAC,CAAC,KAAK;YACxB,eAAe,EAAE,CAAC,CAAC,CAAC,GAAG;YACvB,SAAS;SACV,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;IACH,OAAO;QACL,KAAK,EAAE;YACL,QAAQ,EAAE,MAAM;YAChB,MAAM,EAAE,WAAW;YACnB,IAAI,EAAE,SAAS;SAChB;QACD,SAAS,EAAE,SAAS;KACrB,CAAC;AACJ,CAAC,CAAC","sourcesContent":["import { IAccountMoney, IPosition } from '@yuants/data-account';\nimport { encodePath } from '@yuants/utils';\nimport { defer, firstValueFrom, map, repeat, retry, shareReplay } from 'rxjs';\nimport { ICredential, getAccountBalance, getAccountPositions } from '../api';\nimport { productService } from '../product';\nimport { getMarketIndexTicker } from '../public-api';\nimport { IAccountInfoCore } from './types';\n\nexport const marketIndexTickerUSDT$ = defer(() => getMarketIndexTicker({ quoteCcy: 'USDT' })).pipe(\n map((x) => {\n const mapInstIdToPrice = new Map<string, number>();\n x.data.forEach((inst: any) => mapInstIdToPrice.set(inst.instId, Number(inst.idxPx)));\n return mapInstIdToPrice;\n }),\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nexport const getTradingAccountInfo = async (credential: ICredential): Promise<IAccountInfoCore> => {\n const [positionsApi, balanceApi, mapProductIdToProduct, marketIndexTickerUSDT] = await Promise.all([\n getAccountPositions(credential, {}),\n getAccountBalance(credential, {}),\n firstValueFrom(productService.mapProductIdToProduct$),\n firstValueFrom(marketIndexTickerUSDT$),\n ]);\n\n let totalEquity = 0;\n let totalFree = 0;\n const positions: IPosition[] = [];\n\n balanceApi.data[0]?.details.forEach((detail) => {\n if (detail.ccy === 'USDT') {\n const balance = +(detail.cashBal ?? 0);\n const free = Math.min(\n balance, // free should no more than balance if there is much profits\n +(detail.availEq ?? 0),\n );\n const equity = +(detail.eq ?? 0) - +(detail.stgyEq ?? 0);\n totalEquity += equity;\n totalFree += free;\n } else {\n const volume = +(detail.cashBal ?? 0);\n const free_volume = Math.min(\n volume, // free should no more than balance if there is much profits\n +(detail.availEq ?? 0),\n );\n const closable_price = marketIndexTickerUSDT.get(detail.ccy + '-USDT') || 0;\n const delta_equity = volume * closable_price || 0;\n const delta_profit = +detail.totalPnl || 0;\n\n const product_id = encodePath('SPOT', `${detail.ccy}-USDT`);\n positions.push({\n position_id: product_id,\n datasource_id: 'OKX',\n product_id: product_id,\n direction: 'LONG',\n volume: volume,\n free_volume: free_volume,\n position_price: +detail.accAvgPx,\n floating_profit: delta_profit,\n closable_price: closable_price,\n valuation: delta_equity,\n });\n\n totalEquity += delta_equity;\n }\n });\n positionsApi.data.forEach((x) => {\n const direction =\n x.posSide === 'long' ? 'LONG' : x.posSide === 'short' ? 'SHORT' : +x.pos > 0 ? 'LONG' : 'SHORT';\n const volume = Math.abs(+x.pos);\n const product_id = encodePath(x.instType, x.instId);\n const closable_price = +x.last;\n const valuation =\n (mapProductIdToProduct.get(product_id)?.value_scale ?? 1) * volume * closable_price || 0;\n\n positions.push({\n position_id: x.posId,\n datasource_id: 'OKX',\n product_id,\n direction,\n volume: volume,\n free_volume: +x.availPos,\n closable_price,\n position_price: +x.avgPx,\n floating_profit: +x.upl,\n valuation,\n });\n });\n return {\n money: {\n currency: 'USDT',\n equity: totalEquity,\n free: totalFree,\n },\n positions: positions,\n };\n};\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/accountInfos/types.ts"],"names":[],"mappings":"","sourcesContent":["import { IAccountMoney, IPosition } from '@yuants/data-account';\n\nexport interface IAccountInfoCore {\n money: Pick<IAccountMoney, 'currency' | 'equity' | 'free'>;\n positions: IPosition[];\n}\n"]}
|
package/dist/loan-account.js
CHANGED
|
@@ -1,56 +1,14 @@
|
|
|
1
1
|
import { provideAccountInfoService } from '@yuants/data-account';
|
|
2
2
|
import { Terminal } from '@yuants/protocol';
|
|
3
|
-
import { encodePath } from '@yuants/utils';
|
|
4
3
|
import { defer, firstValueFrom } from 'rxjs';
|
|
5
4
|
import { accountUid$ } from './account';
|
|
6
|
-
import { getDefaultCredential
|
|
5
|
+
import { getDefaultCredential } from './api';
|
|
6
|
+
import { getLoanAccountInfo } from './accountInfos';
|
|
7
7
|
defer(async () => {
|
|
8
8
|
const uid = await firstValueFrom(accountUid$);
|
|
9
9
|
const loanAccountId = `okx/${uid}/loan/USDT`;
|
|
10
10
|
const credential = getDefaultCredential();
|
|
11
|
-
provideAccountInfoService(Terminal.fromNodeEnv(), loanAccountId,
|
|
12
|
-
const res = await getFlexibleLoanInfo(credential);
|
|
13
|
-
const data = res.data[0];
|
|
14
|
-
const positions = [];
|
|
15
|
-
for (const loan of data.loanData) {
|
|
16
|
-
positions.push({
|
|
17
|
-
datasource_id: 'OKX',
|
|
18
|
-
product_id: `SPOT/${loan.ccy}-USDT`,
|
|
19
|
-
volume: +loan.amt,
|
|
20
|
-
free_volume: +loan.amt,
|
|
21
|
-
position_id: encodePath('loan', loan.ccy),
|
|
22
|
-
direction: 'SHORT',
|
|
23
|
-
position_price: 0,
|
|
24
|
-
closable_price: 0,
|
|
25
|
-
floating_profit: 0,
|
|
26
|
-
valuation: 0,
|
|
27
|
-
});
|
|
28
|
-
}
|
|
29
|
-
for (const collateral of data.collateralData) {
|
|
30
|
-
positions.push({
|
|
31
|
-
datasource_id: 'OKX',
|
|
32
|
-
product_id: `SPOT/${collateral.ccy}-USDT`,
|
|
33
|
-
volume: +collateral.amt,
|
|
34
|
-
free_volume: +collateral.amt,
|
|
35
|
-
position_id: encodePath('collateral', collateral.ccy),
|
|
36
|
-
direction: 'LONG',
|
|
37
|
-
position_price: 0,
|
|
38
|
-
closable_price: 0,
|
|
39
|
-
floating_profit: 0,
|
|
40
|
-
valuation: 0,
|
|
41
|
-
});
|
|
42
|
-
}
|
|
43
|
-
const equity = +data.collateralNotionalUsd - +data.loanNotionalUsd;
|
|
44
|
-
const free = equity - +data.collateralNotionalUsd;
|
|
45
|
-
return {
|
|
46
|
-
money: {
|
|
47
|
-
currency: 'USDT',
|
|
48
|
-
equity,
|
|
49
|
-
free,
|
|
50
|
-
},
|
|
51
|
-
positions,
|
|
52
|
-
};
|
|
53
|
-
}, {
|
|
11
|
+
provideAccountInfoService(Terminal.fromNodeEnv(), loanAccountId, () => getLoanAccountInfo(credential), {
|
|
54
12
|
auto_refresh_interval: 1000,
|
|
55
13
|
});
|
|
56
14
|
}).subscribe();
|
package/dist/loan-account.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"loan-account.js","sourceRoot":"","sources":["../src/loan-account.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"loan-account.js","sourceRoot":"","sources":["../src/loan-account.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,yBAAyB,EAAE,MAAM,sBAAsB,CAAC;AACjE,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,KAAK,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAC7C,OAAO,EAAE,WAAW,EAAE,MAAM,WAAW,CAAC;AACxC,OAAO,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAC7C,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AAEpD,KAAK,CAAC,KAAK,IAAI,EAAE;IACf,MAAM,GAAG,GAAG,MAAM,cAAc,CAAC,WAAW,CAAC,CAAC;IAC9C,MAAM,aAAa,GAAG,OAAO,GAAG,YAAY,CAAC;IAC7C,MAAM,UAAU,GAAG,oBAAoB,EAAE,CAAC;IAE1C,yBAAyB,CAAC,QAAQ,CAAC,WAAW,EAAE,EAAE,aAAa,EAAE,GAAG,EAAE,CAAC,kBAAkB,CAAC,UAAU,CAAC,EAAE;QACrG,qBAAqB,EAAE,IAAI;KAC5B,CAAC,CAAC;AACL,CAAC,CAAC,CAAC,SAAS,EAAE,CAAC","sourcesContent":["import { provideAccountInfoService } from '@yuants/data-account';\nimport { Terminal } from '@yuants/protocol';\nimport { defer, firstValueFrom } from 'rxjs';\nimport { accountUid$ } from './account';\nimport { getDefaultCredential } from './api';\nimport { getLoanAccountInfo } from './accountInfos';\n\ndefer(async () => {\n const uid = await firstValueFrom(accountUid$);\n const loanAccountId = `okx/${uid}/loan/USDT`;\n const credential = getDefaultCredential();\n\n provideAccountInfoService(Terminal.fromNodeEnv(), loanAccountId, () => getLoanAccountInfo(credential), {\n auto_refresh_interval: 1000,\n });\n}).subscribe();\n"]}
|