@yuants/vendor-okx 0.17.1 → 0.17.3

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 CHANGED
@@ -95,31 +95,31 @@ export const tradingAccountInfo$ = accountPosition$.pipe(withLatestFrom(accountU
95
95
  account_id: account_id,
96
96
  updated_at: Date.now(),
97
97
  money: money,
98
- currencies: [money],
99
98
  positions: positions,
100
- orders: orders.data.map((x) => {
101
- const order_type = x.ordType === 'market' ? 'MARKET' : x.ordType === 'limit' ? 'LIMIT' : 'UNKNOWN';
102
- const order_direction = x.side === 'buy'
103
- ? x.posSide === 'long'
104
- ? 'OPEN_LONG'
105
- : 'CLOSE_SHORT'
106
- : x.posSide === 'short'
107
- ? 'OPEN_SHORT'
108
- : 'CLOSE_LONG';
109
- return {
110
- order_id: x.ordId,
111
- account_id,
112
- product_id: encodePath(x.instType, x.instId),
113
- submit_at: +x.cTime,
114
- filled_at: +x.fillTime,
115
- order_type,
116
- order_direction,
117
- volume: +x.sz,
118
- traded_volume: +x.accFillSz,
119
- price: +x.px,
120
- traded_price: +x.avgPx,
121
- };
122
- }),
99
+ // orders: orders.data.map((x): IOrder => {
100
+ // const order_type = x.ordType === 'market' ? 'MARKET' : x.ordType === 'limit' ? 'LIMIT' : 'UNKNOWN';
101
+ // const order_direction =
102
+ // x.side === 'buy'
103
+ // ? x.posSide === 'long'
104
+ // ? 'OPEN_LONG'
105
+ // : 'CLOSE_SHORT'
106
+ // : x.posSide === 'short'
107
+ // ? 'OPEN_SHORT'
108
+ // : 'CLOSE_LONG';
109
+ // return {
110
+ // order_id: x.ordId,
111
+ // account_id,
112
+ // product_id: encodePath(x.instType, x.instId),
113
+ // submit_at: +x.cTime,
114
+ // filled_at: +x.fillTime,
115
+ // order_type,
116
+ // order_direction,
117
+ // volume: +x.sz,
118
+ // traded_volume: +x.accFillSz,
119
+ // price: +x.px,
120
+ // traded_price: +x.avgPx,
121
+ // };
122
+ // }),
123
123
  };
124
124
  }), shareReplay(1));
125
125
  const sub = defer(() => accountUid$)
@@ -166,9 +166,7 @@ const fundingAccountInfo$ = combineLatest([accountUid$, assetBalance$, marketInd
166
166
  account_id: `okx/${uid}/funding/USDT`,
167
167
  updated_at: Date.now(),
168
168
  money: money,
169
- currencies: [money],
170
169
  positions: positions,
171
- orders: [],
172
170
  };
173
171
  }), shareReplay(1));
174
172
  const savingBalance$ = defer(() => client.getFinanceSavingsBalance({})).pipe(repeat({ delay: 5000 }), retry({ delay: 5000 }), shareReplay(1));
@@ -190,9 +188,7 @@ const earningAccountInfo$ = combineLatest([accountUid$, savingBalance$]).pipe(ma
190
188
  account_id: `okx/${uid}/earning/USDT`,
191
189
  updated_at: Date.now(),
192
190
  money: money,
193
- currencies: [money],
194
191
  positions: [],
195
- orders: [],
196
192
  };
197
193
  }), shareReplay(1));
198
194
  //# sourceMappingURL=account.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"account.js","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAIhB,kBAAkB,GACnB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAC5G,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,4BAA4B,EAAE,8BAA8B,EAAE,MAAM,WAAW,CAAC;AAEzF,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,mBAAmB,CAAC,EAAE,CAAC,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P,CAAC;IACF,OAAO;QACL,UAAU,EAAE,OAAO,GAAG,eAAe;QACrC,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE;QACtB,KAAK,EAAE,KAAK;QACZ,UAAU,EAAE,CAAC,KAAK,CAAC;QACnB,SAAS,EAAE,EAAE;QACb,MAAM,EAAE,EAAE;KACX,CAAC;AACJ,CAAC,CAAC,EACF,WAAW,CAAC,CAAC,CAAC,CACf,CAAC","sourcesContent":["import {\n addAccountMarket,\n IAccountInfo,\n IAccountMoney,\n IPosition,\n publishAccountInfo,\n} from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { encodePath } from '@yuants/utils';\nimport { combineLatest, defer, filter, first, map, repeat, retry, shareReplay, withLatestFrom } from 'rxjs';\nimport { client } from './api';\nimport { mapProductIdToMarginProduct$, mapProductIdToUsdtSwapProduct$ } from './product';\n\nconst terminal = Terminal.fromNodeEnv();\n\nexport const accountPosition$ = defer(() => client.getAccountPositions({})).pipe(\n repeat({ delay: 5000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nexport const accountConfig$ = defer(() => client.getAccountConfig()).pipe(\n repeat({ delay: 10_000 }),\n retry({ delay: 10_000 }),\n shareReplay(1),\n);\n\nconst subAccountUids$ = defer(() => client.getSubAccountList()).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\nconst accountBalance$ = defer(() => client.getAccountBalance({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst accountUsdtBalance$ = accountBalance$.pipe(\n map((x) => x.data[0]?.details.find((x) => x.ccy === 'USDT')),\n filter((x): x is Exclude<typeof x, undefined> => !!x),\n shareReplay(1),\n);\n\nconst pendingOrders$ = defer(() => client.getTradeOrdersPending({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst marketIndexTickerUSDT$ = defer(() => client.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 tradingAccountInfo$ = accountPosition$.pipe(\n withLatestFrom(\n accountUid$,\n accountBalance$,\n pendingOrders$,\n mapProductIdToUsdtSwapProduct$,\n mapProductIdToMarginProduct$,\n marketIndexTickerUSDT$,\n ),\n map(\n ([\n positionsApi,\n uid,\n balanceApi,\n orders,\n mapProductIdToUsdtSwapProduct,\n mapProductIdToMarginProduct,\n marketIndexTickerUSDT,\n ]): IAccountInfo => {\n const account_id = `okx/${uid}/trading`;\n const money: IAccountMoney = { currency: 'USDT', equity: 0, balance: 0, used: 0, free: 0, profit: 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);\n const used = equity - free;\n const profit = equity - balance;\n money.equity += equity;\n money.balance += balance;\n money.used += used;\n money.free += free;\n money.profit += profit;\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 const delta_balance = delta_equity - delta_profit;\n const delta_used = delta_equity; // all used\n const delta_free = 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 money.equity += delta_equity;\n money.profit += delta_profit;\n money.balance += delta_balance;\n money.used += delta_used;\n money.free += delta_free;\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 x.instType === 'SWAP'\n ? (mapProductIdToUsdtSwapProduct.get(product_id)?.value_scale ?? 1) * volume * closable_price\n : x.instType === 'MARGIN'\n ? (mapProductIdToMarginProduct.get(product_id)?.value_scale ?? 1) * volume * closable_price\n : 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 account_id: account_id,\n updated_at: Date.now(),\n money: money,\n currencies: [money],\n positions: positions,\n orders: orders.data.map((x): IOrder => {\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 },\n ),\n shareReplay(1),\n);\n\nconst sub = defer(() => accountUid$)\n .pipe(first())\n .subscribe((uid) => {\n publishAccountInfo(terminal, `okx/${uid}/trading`, tradingAccountInfo$);\n addAccountMarket(terminal, { account_id: `okx/${uid}/trading`, market_id: 'OKX' });\n publishAccountInfo(terminal, `okx/${uid}/funding/USDT`, fundingAccountInfo$);\n publishAccountInfo(terminal, `okx/${uid}/earning/USDT`, earningAccountInfo$);\n });\ndefer(() => terminal.dispose$).subscribe(() => sub.unsubscribe());\n\nconst assetBalance$ = defer(() => client.getAssetBalances({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst fundingAccountInfo$ = combineLatest([accountUid$, assetBalance$, marketIndexTickerUSDT$]).pipe(\n map(([uid, assetBalances, marketIndexTickerUSDT]): IAccountInfo => {\n const money: IAccountMoney = { currency: 'USDT', equity: 0, balance: 0, used: 0, free: 0, profit: 0 };\n const positions: IPosition[] = [];\n\n assetBalances.data.forEach((x) => {\n if (x.ccy === 'USDT') {\n money.equity += +x.bal;\n money.balance += +x.bal;\n money.free += +x.bal;\n } else {\n const price = marketIndexTickerUSDT.get(x.ccy + '-USDT') || 0;\n const productId = encodePath('SPOT', `${x.ccy}-USDT`);\n const valuation = price * +x.bal || 0;\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: valuation,\n });\n\n money.equity += valuation;\n money.balance += valuation;\n money.used += valuation;\n }\n });\n\n return {\n account_id: `okx/${uid}/funding/USDT`,\n updated_at: Date.now(),\n money: money,\n currencies: [money],\n positions: positions,\n orders: [],\n };\n }),\n shareReplay(1),\n);\n\nconst savingBalance$ = defer(() => client.getFinanceSavingsBalance({})).pipe(\n repeat({ delay: 5000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst earningAccountInfo$ = combineLatest([accountUid$, savingBalance$]).pipe(\n map(([uid, offers]): IAccountInfo => {\n const equity = offers.data.filter((x) => x.ccy === 'USDT').reduce((acc, x) => acc + +x.amt, 0);\n const balance = equity;\n const free = equity;\n const used = 0;\n const profit = 0;\n\n const money: IAccountMoney = {\n currency: 'USDT',\n equity,\n balance,\n used,\n free,\n profit,\n };\n return {\n account_id: `okx/${uid}/earning/USDT`,\n updated_at: Date.now(),\n money: money,\n currencies: [money],\n positions: [],\n orders: [],\n };\n }),\n shareReplay(1),\n);\n"]}
1
+ {"version":3,"file":"account.js","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,gBAAgB,EAIhB,kBAAkB,GACnB,MAAM,sBAAsB,CAAC;AAE9B,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC3C,OAAO,EAAE,aAAa,EAAE,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,WAAW,EAAE,cAAc,EAAE,MAAM,MAAM,CAAC;AAC5G,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAC/B,OAAO,EAAE,4BAA4B,EAAE,8BAA8B,EAAE,MAAM,WAAW,CAAC;AAEzF,MAAM,QAAQ,GAAG,QAAQ,CAAC,WAAW,EAAE,CAAC;AAExC,MAAM,CAAC,MAAM,gBAAgB,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,mBAAmB,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAC9E,MAAM,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACvB,KAAK,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACtB,WAAW,CAAC,CAAC,CAAC,CACf,CAAC;AAEF,MAAM,CAAC,MAAM,cAAc,GAAG,KAAK,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,gBAAgB,EAAE,CAAC,CAAC,IAAI,CACvE,MAAM,CAAC,EAAE,KAAK,EAAE,KAAM,EAAE,CAAC,EACzB,KAA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G,CAAC,CAAC;IACf,MAAM,MAAM,GAAG,CAAC,CAAC;IAEjB,MAAM,KAAK,GAAkB;QAC3B,QAAQ,EAAE,MAAM;QAChB,MAAM;QACN,OAAO;QACP,IAAI;QACJ,IAAI;QACJ,MAAM;KACP,CAAC;IACF,OAAO;QACL,UAAU,EAAE,OAAO,GAAG,eAAe;QACrC,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE;QACtB,KAAK,EAAE,KAAK;QACZ,SAAS,EAAE,EAAE;KACd,CAAC;AACJ,CAAC,CAAC,EACF,WAAW,CAAC,CAAC,CAAC,CACf,CAAC","sourcesContent":["import {\n addAccountMarket,\n IAccountInfo,\n IAccountMoney,\n IPosition,\n publishAccountInfo,\n} from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { encodePath } from '@yuants/utils';\nimport { combineLatest, defer, filter, first, map, repeat, retry, shareReplay, withLatestFrom } from 'rxjs';\nimport { client } from './api';\nimport { mapProductIdToMarginProduct$, mapProductIdToUsdtSwapProduct$ } from './product';\n\nconst terminal = Terminal.fromNodeEnv();\n\nexport const accountPosition$ = defer(() => client.getAccountPositions({})).pipe(\n repeat({ delay: 5000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nexport const accountConfig$ = defer(() => client.getAccountConfig()).pipe(\n repeat({ delay: 10_000 }),\n retry({ delay: 10_000 }),\n shareReplay(1),\n);\n\nconst subAccountUids$ = defer(() => client.getSubAccountList()).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\nconst accountBalance$ = defer(() => client.getAccountBalance({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst accountUsdtBalance$ = accountBalance$.pipe(\n map((x) => x.data[0]?.details.find((x) => x.ccy === 'USDT')),\n filter((x): x is Exclude<typeof x, undefined> => !!x),\n shareReplay(1),\n);\n\nconst pendingOrders$ = defer(() => client.getTradeOrdersPending({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst marketIndexTickerUSDT$ = defer(() => client.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 tradingAccountInfo$ = accountPosition$.pipe(\n withLatestFrom(\n accountUid$,\n accountBalance$,\n pendingOrders$,\n mapProductIdToUsdtSwapProduct$,\n mapProductIdToMarginProduct$,\n marketIndexTickerUSDT$,\n ),\n map(\n ([\n positionsApi,\n uid,\n balanceApi,\n orders,\n mapProductIdToUsdtSwapProduct,\n mapProductIdToMarginProduct,\n marketIndexTickerUSDT,\n ]): IAccountInfo => {\n const account_id = `okx/${uid}/trading`;\n const money: IAccountMoney = { currency: 'USDT', equity: 0, balance: 0, used: 0, free: 0, profit: 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);\n const used = equity - free;\n const profit = equity - balance;\n money.equity += equity;\n money.balance += balance;\n money.used += used;\n money.free += free;\n money.profit += profit;\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 const delta_balance = delta_equity - delta_profit;\n const delta_used = delta_equity; // all used\n const delta_free = 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 money.equity += delta_equity;\n money.profit += delta_profit;\n money.balance += delta_balance;\n money.used += delta_used;\n money.free += delta_free;\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 x.instType === 'SWAP'\n ? (mapProductIdToUsdtSwapProduct.get(product_id)?.value_scale ?? 1) * volume * closable_price\n : x.instType === 'MARGIN'\n ? (mapProductIdToMarginProduct.get(product_id)?.value_scale ?? 1) * volume * closable_price\n : 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 account_id: account_id,\n updated_at: Date.now(),\n money: money,\n positions: positions,\n // orders: orders.data.map((x): IOrder => {\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 },\n ),\n shareReplay(1),\n);\n\nconst sub = defer(() => accountUid$)\n .pipe(first())\n .subscribe((uid) => {\n publishAccountInfo(terminal, `okx/${uid}/trading`, tradingAccountInfo$);\n addAccountMarket(terminal, { account_id: `okx/${uid}/trading`, market_id: 'OKX' });\n publishAccountInfo(terminal, `okx/${uid}/funding/USDT`, fundingAccountInfo$);\n publishAccountInfo(terminal, `okx/${uid}/earning/USDT`, earningAccountInfo$);\n });\ndefer(() => terminal.dispose$).subscribe(() => sub.unsubscribe());\n\nconst assetBalance$ = defer(() => client.getAssetBalances({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst fundingAccountInfo$ = combineLatest([accountUid$, assetBalance$, marketIndexTickerUSDT$]).pipe(\n map(([uid, assetBalances, marketIndexTickerUSDT]): IAccountInfo => {\n const money: IAccountMoney = { currency: 'USDT', equity: 0, balance: 0, used: 0, free: 0, profit: 0 };\n const positions: IPosition[] = [];\n\n assetBalances.data.forEach((x) => {\n if (x.ccy === 'USDT') {\n money.equity += +x.bal;\n money.balance += +x.bal;\n money.free += +x.bal;\n } else {\n const price = marketIndexTickerUSDT.get(x.ccy + '-USDT') || 0;\n const productId = encodePath('SPOT', `${x.ccy}-USDT`);\n const valuation = price * +x.bal || 0;\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: valuation,\n });\n\n money.equity += valuation;\n money.balance += valuation;\n money.used += valuation;\n }\n });\n\n return {\n account_id: `okx/${uid}/funding/USDT`,\n updated_at: Date.now(),\n money: money,\n positions: positions,\n };\n }),\n shareReplay(1),\n);\n\nconst savingBalance$ = defer(() => client.getFinanceSavingsBalance({})).pipe(\n repeat({ delay: 5000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst earningAccountInfo$ = combineLatest([accountUid$, savingBalance$]).pipe(\n map(([uid, offers]): IAccountInfo => {\n const equity = offers.data.filter((x) => x.ccy === 'USDT').reduce((acc, x) => acc + +x.amt, 0);\n const balance = equity;\n const free = equity;\n const used = 0;\n const profit = 0;\n\n const money: IAccountMoney = {\n currency: 'USDT',\n equity,\n balance,\n used,\n free,\n profit,\n };\n return {\n account_id: `okx/${uid}/earning/USDT`,\n updated_at: Date.now(),\n money: money,\n positions: [],\n };\n }),\n shareReplay(1),\n);\n"]}
package/dist/ohlc.js CHANGED
@@ -15,7 +15,7 @@ import { Terminal } from '@yuants/protocol';
15
15
  import { convertDurationToOffset, decodePath, encodePath, formatTime } from '@yuants/utils';
16
16
  import { firstValueFrom, Observable, timer } from 'rxjs';
17
17
  import { client } from './api';
18
- import { okxBusinessWsClient } from './websocket';
18
+ import { OKXWsClient } from './websocket';
19
19
  import { writeToSQL } from '@yuants/sql';
20
20
  // 时间粒度,默认值1m
21
21
  // 如 [1m/3m/5m/15m/30m/1H/2H/4H]
@@ -130,6 +130,7 @@ Terminal.fromNodeEnv().channel.publishChannel('ohlc', { pattern: `^OKX/` }, (ser
130
130
  console.info(formatTime(Date.now()), `subscribe`, series_id);
131
131
  return new Observable((subscriber) => {
132
132
  console.info(formatTime(Date.now()), `subscribe`, candleType, instId);
133
+ const okxBusinessWsClient = new OKXWsClient('wss://wspap.okx.com:8443/ws/v5/business');
133
134
  okxBusinessWsClient.subscribe(candleType, instId, async (data) => {
134
135
  const created_at = Number(data[0]);
135
136
  const closed_at = created_at + offset;
package/dist/ohlc.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"ohlc.js","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,uBAAuB,EAAE,UAAU,EAAE,UAAU,E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AAa,EAAE,GAAG;aACnB,CAAC;YACF,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAC9B,CAAC,CAAC,CAAC;QACH,OAAO,GAAG,EAAE;YACV,mBAAmB,CAAC,WAAW,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;QACtD,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC,IAAI,CACL,UAAU,CAAC;QACT,SAAS,EAAE,MAAM;QACjB,YAAY,EAAE,CAAC,YAAY,EAAE,WAAW,CAAC;QACzC,aAAa,EAAE,IAAI;QACnB,QAAQ,EAAE,QAAQ,CAAC,WAAW,EAAE;QAChC,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,SAAS,CAAC;KACpD,CAAC,CACH,CAAC;AACJ,CAAC,CAAC,CAAC","sourcesContent":["import { IOHLC } from '@yuants/data-ohlc';\nimport { createSeriesProvider } from '@yuants/data-series';\nimport { Terminal } from '@yuants/protocol';\nimport { convertDurationToOffset, decodePath, encodePath, formatTime } from '@yuants/utils';\nimport { firstValueFrom, Observable, of, timer } from 'rxjs';\nimport { client } from './api';\nimport { okxBusinessWsClient } from './websocket';\nimport { buildInsertManyIntoTableSQL, requestSQL, writeToSQL } from '@yuants/sql';\n\n// 时间粒度,默认值1m\n// 如 [1m/3m/5m/15m/30m/1H/2H/4H]\n// 香港时间开盘价k线:[6H/12H/1D/1W/1M]\n// UTC时间开盘价k线:[6Hutc/12Hutc/1Dutc/1Wutc/1Mutc]\n\nconst DURATION_TO_OKX_BAR_TYPE: Record<string, string> = {\n PT1M: '1m',\n PT3M: '3m',\n PT5M: '5m',\n PT15M: '15m',\n PT30M: '30m',\n\n PT1H: '1H',\n PT2H: '2H',\n PT4H: '4H',\n PT6H: '6H',\n PT12H: '12H',\n\n P1D: '1D',\n P1W: '1W',\n P1M: '1M',\n};\n\nconst DURATION_TO_OKX_CANDLE_TYPE: Record<string, string> = {\n PT1M: 'mark-price-candle1m',\n PT3M: 'mark-price-candle3m',\n PT5M: 'mark-price-candle5m',\n PT15M: 'mark-price-candle15m',\n PT30M: 'mark-price-candle30m',\n\n PT1H: 'mark-price-candle1H',\n PT2H: 'mark-price-candle2H',\n PT4H: 'mark-price-candle4H',\n PT6H: 'mark-price-candle6H',\n PT12H: 'mark-price-candle12H',\n\n P1D: 'mark-price-candle1D',\n P1W: 'mark-price-candle1W',\n P1M: 'mark-price-candle1M',\n};\n\ncreateSeriesProvider<IOHLC>(Terminal.fromNodeEnv(), {\n tableName: 'ohlc',\n series_id_prefix_parts: ['OKX'],\n reversed: true,\n serviceOptions: { concurrent: 1 },\n queryFn: async function* ({ series_id, ended_at }) {\n const [datasource_id, product_id, duration] = decodePath(series_id);\n const offset = convertDurationToOffset(duration);\n if (!datasource_id) {\n throw 'datasource_id is required';\n }\n if (!product_id) {\n throw 'product_id is required';\n }\n if (!offset) {\n throw 'duration is invalid';\n }\n const [instType, instId] = decodePath(product_id);\n if (!instId) {\n throw `invalid product_id: ${product_id}`;\n }\n\n const bar = DURATION_TO_OKX_BAR_TYPE[duration];\n if (!bar) {\n throw `unsupported duration: ${duration}`;\n }\n\n let currentStartTime = ended_at;\n\n while (true) {\n // 向前翻页,时间降序,不含 after 时间点\n const res = await client.getHistoryCandles({\n instId,\n bar,\n after: `${currentStartTime}`,\n limit: '100',\n });\n if (res.code !== '0') {\n throw `API failed: ${res.code} ${res.msg}`;\n }\n if (res.data.length === 0) break;\n currentStartTime = +res.data[res.data.length - 1][0];\n const data = res.data.map(\n (x): IOHLC => ({\n series_id,\n datasource_id,\n product_id,\n duration,\n created_at: formatTime(+x[0]),\n closed_at: formatTime(+x[0] + offset),\n open: x[1],\n high: x[2],\n low: x[3],\n close: x[4],\n volume: x[5],\n open_interest: '0',\n }),\n );\n yield data;\n await firstValueFrom(timer(1000));\n }\n },\n});\n\nTerminal.fromNodeEnv().channel.publishChannel('ohlc', { pattern: `^OKX/` }, (series_id) => {\n // const [] = decodePath(args)\n const [datasource_id, product_id, duration] = decodePath(series_id);\n const [, instId] = decodePath(product_id);\n const offset = convertDurationToOffset(duration);\n if (!datasource_id) {\n throw 'datasource_id is required';\n }\n if (!product_id) {\n throw 'product_id is required';\n }\n if (!offset) {\n throw 'duration is invalid';\n }\n const candleType = DURATION_TO_OKX_CANDLE_TYPE[duration];\n console.info(formatTime(Date.now()), `subscribe`, series_id);\n return new Observable<IOHLC>((subscriber) => {\n console.info(formatTime(Date.now()), `subscribe`, candleType, instId);\n okxBusinessWsClient.subscribe(candleType, instId, async (data: string[]) => {\n const created_at = Number(data[0]);\n const closed_at = created_at + offset;\n const open = data[1];\n const high = data[2];\n const low = data[3];\n const close = data[4];\n if (isNaN(closed_at)) {\n return;\n }\n console.info(formatTime(Date.now()), `insertData`, data);\n const cancelData: IOHLC = {\n closed_at: formatTime(closed_at),\n created_at: formatTime(created_at),\n open,\n high,\n low,\n close,\n series_id,\n datasource_id,\n duration,\n product_id,\n volume: '0',\n open_interest: '0',\n };\n subscriber.next(cancelData);\n });\n return () => {\n okxBusinessWsClient.unsubscribe(candleType, instId);\n };\n }).pipe(\n writeToSQL({\n tableName: 'ohlc',\n conflictKeys: ['created_at', 'series_id'],\n writeInterval: 1000,\n terminal: Terminal.fromNodeEnv(),\n keyFn: (x) => encodePath(x.created_at, x.series_id),\n }),\n );\n});\n"]}
1
+ {"version":3,"file":"ohlc.js","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":";;;;;;;;;;;;AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,qBAAqB,CAAC;AAC3D,OAAO,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAC5C,OAAO,EAAE,uBAAuB,EAAE,UAAU,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AAC5F,OAAO,EAAE,cAAc,EAAE,UAAU,EAAM,KAAK,EAAE,MAAM,MAAM,CAAC;AAC7D,OAAO,EAAE,MAAM,EAAE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;QAC9B,CAAC,CAAC,CAAC;QACH,OAAO,GAAG,EAAE;YACV,mBAAmB,CAAC,WAAW,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;QACtD,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC,IAAI,CACL,UAAU,CAAC;QACT,SAAS,EAAE,MAAM;QACjB,YAAY,EAAE,CAAC,YAAY,EAAE,WAAW,CAAC;QACzC,aAAa,EAAE,IAAI;QACnB,QAAQ,EAAE,QAAQ,CAAC,WAAW,EAAE;QAChC,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,SAAS,CAAC;KACpD,CAAC,CACH,CAAC;AACJ,CAAC,CAAC,CAAC","sourcesContent":["import { IOHLC } from '@yuants/data-ohlc';\nimport { createSeriesProvider } from '@yuants/data-series';\nimport { Terminal } from '@yuants/protocol';\nimport { convertDurationToOffset, decodePath, encodePath, formatTime } from '@yuants/utils';\nimport { firstValueFrom, Observable, of, timer } from 'rxjs';\nimport { client } from './api';\nimport { OKXWsClient } from './websocket';\nimport { writeToSQL } from '@yuants/sql';\n\n// 时间粒度,默认值1m\n// 如 [1m/3m/5m/15m/30m/1H/2H/4H]\n// 香港时间开盘价k线:[6H/12H/1D/1W/1M]\n// UTC时间开盘价k线:[6Hutc/12Hutc/1Dutc/1Wutc/1Mutc]\n\nconst DURATION_TO_OKX_BAR_TYPE: Record<string, string> = {\n PT1M: '1m',\n PT3M: '3m',\n PT5M: '5m',\n PT15M: '15m',\n PT30M: '30m',\n\n PT1H: '1H',\n PT2H: '2H',\n PT4H: '4H',\n PT6H: '6H',\n PT12H: '12H',\n\n P1D: '1D',\n P1W: '1W',\n P1M: '1M',\n};\n\nconst DURATION_TO_OKX_CANDLE_TYPE: Record<string, string> = {\n PT1M: 'mark-price-candle1m',\n PT3M: 'mark-price-candle3m',\n PT5M: 'mark-price-candle5m',\n PT15M: 'mark-price-candle15m',\n PT30M: 'mark-price-candle30m',\n\n PT1H: 'mark-price-candle1H',\n PT2H: 'mark-price-candle2H',\n PT4H: 'mark-price-candle4H',\n PT6H: 'mark-price-candle6H',\n PT12H: 'mark-price-candle12H',\n\n P1D: 'mark-price-candle1D',\n P1W: 'mark-price-candle1W',\n P1M: 'mark-price-candle1M',\n};\n\ncreateSeriesProvider<IOHLC>(Terminal.fromNodeEnv(), {\n tableName: 'ohlc',\n series_id_prefix_parts: ['OKX'],\n reversed: true,\n serviceOptions: { concurrent: 1 },\n queryFn: async function* ({ series_id, ended_at }) {\n const [datasource_id, product_id, duration] = decodePath(series_id);\n const offset = convertDurationToOffset(duration);\n if (!datasource_id) {\n throw 'datasource_id is required';\n }\n if (!product_id) {\n throw 'product_id is required';\n }\n if (!offset) {\n throw 'duration is invalid';\n }\n const [instType, instId] = decodePath(product_id);\n if (!instId) {\n throw `invalid product_id: ${product_id}`;\n }\n\n const bar = DURATION_TO_OKX_BAR_TYPE[duration];\n if (!bar) {\n throw `unsupported duration: ${duration}`;\n }\n\n let currentStartTime = ended_at;\n\n while (true) {\n // 向前翻页,时间降序,不含 after 时间点\n const res = await client.getHistoryCandles({\n instId,\n bar,\n after: `${currentStartTime}`,\n limit: '100',\n });\n if (res.code !== '0') {\n throw `API failed: ${res.code} ${res.msg}`;\n }\n if (res.data.length === 0) break;\n currentStartTime = +res.data[res.data.length - 1][0];\n const data = res.data.map(\n (x): IOHLC => ({\n series_id,\n datasource_id,\n product_id,\n duration,\n created_at: formatTime(+x[0]),\n closed_at: formatTime(+x[0] + offset),\n open: x[1],\n high: x[2],\n low: x[3],\n close: x[4],\n volume: x[5],\n open_interest: '0',\n }),\n );\n yield data;\n await firstValueFrom(timer(1000));\n }\n },\n});\n\nTerminal.fromNodeEnv().channel.publishChannel('ohlc', { pattern: `^OKX/` }, (series_id) => {\n // const [] = decodePath(args)\n const [datasource_id, product_id, duration] = decodePath(series_id);\n const [, instId] = decodePath(product_id);\n const offset = convertDurationToOffset(duration);\n if (!datasource_id) {\n throw 'datasource_id is required';\n }\n if (!product_id) {\n throw 'product_id is required';\n }\n if (!offset) {\n throw 'duration is invalid';\n }\n const candleType = DURATION_TO_OKX_CANDLE_TYPE[duration];\n console.info(formatTime(Date.now()), `subscribe`, series_id);\n return new Observable<IOHLC>((subscriber) => {\n console.info(formatTime(Date.now()), `subscribe`, candleType, instId);\n const okxBusinessWsClient = new OKXWsClient('wss://wspap.okx.com:8443/ws/v5/business');\n okxBusinessWsClient.subscribe(candleType, instId, async (data: string[]) => {\n const created_at = Number(data[0]);\n const closed_at = created_at + offset;\n const open = data[1];\n const high = data[2];\n const low = data[3];\n const close = data[4];\n if (isNaN(closed_at)) {\n return;\n }\n console.info(formatTime(Date.now()), `insertData`, data);\n const cancelData: IOHLC = {\n closed_at: formatTime(closed_at),\n created_at: formatTime(created_at),\n open,\n high,\n low,\n close,\n series_id,\n datasource_id,\n duration,\n product_id,\n volume: '0',\n open_interest: '0',\n };\n subscriber.next(cancelData);\n });\n return () => {\n okxBusinessWsClient.unsubscribe(candleType, instId);\n };\n }).pipe(\n writeToSQL({\n tableName: 'ohlc',\n conflictKeys: ['created_at', 'series_id'],\n writeInterval: 1000,\n terminal: Terminal.fromNodeEnv(),\n keyFn: (x) => encodePath(x.created_at, x.series_id),\n }),\n );\n});\n"]}
package/dist/websocket.js CHANGED
@@ -1,11 +1,19 @@
1
1
  import { encodePath, formatTime } from '@yuants/utils';
2
+ import { interval, tap } from 'rxjs';
2
3
  // import WebSocket from 'ws';
3
- class OKXWsClient {
4
+ export class OKXWsClient {
4
5
  constructor(url) {
5
6
  this.connected = false;
6
7
  // this.instId = instId;
7
8
  this.ws = new WebSocket(url);
8
9
  this.pendingSub = [];
10
+ this.keepAlive = interval(25000)
11
+ .pipe(tap(() => {
12
+ if (this.connected) {
13
+ this.ws.send('ping');
14
+ }
15
+ }))
16
+ .subscribe();
9
17
  // this.ws.
10
18
  this.ws.addEventListener('open', () => {
11
19
  this.connected = true;
@@ -25,6 +33,9 @@ class OKXWsClient {
25
33
  // 处理消息
26
34
  handleMessage(raw) {
27
35
  var _a, _b;
36
+ if (raw.data === 'pong') {
37
+ return;
38
+ }
28
39
  const msg = JSON.parse(raw.data);
29
40
  if ((_a = msg.arg) === null || _a === void 0 ? void 0 : _a.channel) {
30
41
  const channelId = encodePath(msg.arg.channel, msg.arg.instId);
@@ -72,9 +83,11 @@ class OKXWsClient {
72
83
  };
73
84
  this.ws.send(JSON.stringify(unSubMsg));
74
85
  this.subscriptions.delete(channelId);
86
+ if (this.subscriptions.size === 0) {
87
+ this.keepAlive.unsubscribe();
88
+ }
75
89
  delete this.handlers[channelId];
76
90
  console.info(`📩 Sent unsubscribe for ${channelId}`);
77
91
  }
78
92
  }
79
- export const okxBusinessWsClient = new OKXWsClient('wss://wspap.okx.com:8443/ws/v5/business');
80
93
  //# sourceMappingURL=websocket.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"websocket.js","sourceRoot":"","sources":["../src/websocket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACvD,8BAA8B;AAE9B,MAAM,WAAW;IAMf,YAAY,GAAW;QAJvB,cAAS,GAAY,KAAK,CAAC;QAKzB,wBAAwB;QACxB,IAAI,CAAC,EAAE,GAAG,IAAI,SAAS,CAAC,GAAG,CAAC,CAAC;QAC7B,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;QACrB,WAAW;QACX,IAAI,CAAC,EAAE,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;YACpC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;YACtB,OAAO,CAAC,IAAI,CAAC,UAAU,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,gBAAgB,CAAC,CAAC;YACvD,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;gBACjC,MAAM,GAAG,GAAG,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;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EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;SAC5B,CAAC;QAEF,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;QACvC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QACrC,OAAO,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;QAChC,OAAO,CAAC,IAAI,CAAC,2BAA2B,SAAS,EAAE,CAAC,CAAC;IACvD,CAAC;CACF;AAED,MAAM,CAAC,MAAM,mBAAmB,GAAG,IAAI,WAAW,CAAC,yCAAyC,CAAC,CAAC","sourcesContent":["import { encodePath, formatTime } from '@yuants/utils';\n// import WebSocket from 'ws';\n\nclass OKXWsClient {\n ws: WebSocket;\n connected: boolean = false;\n subscriptions: Set<string>;\n handlers: Record<string, Function>;\n pendingSub: string[];\n constructor(url: string) {\n // this.instId = instId;\n this.ws = new WebSocket(url);\n this.pendingSub = [];\n // this.ws.\n this.ws.addEventListener('open', () => {\n this.connected = true;\n console.info(formatTime(Date.now()), '✅ WS connected');\n while (this.pendingSub.length > 0) {\n const msg = this.pendingSub.shift();\n if (msg) {\n this.ws.send(msg);\n console.info(formatTime(Date.now()), `📩 Sent subscribe for ${msg}`);\n }\n }\n });\n this.ws.addEventListener('message', (raw) => this.handleMessage(raw));\n this.subscriptions = new Set();\n this.handlers = {}; // key: channel, value: callback\n }\n\n // 处理消息\n handleMessage(raw: any) {\n const msg = JSON.parse(raw.data);\n if (msg.arg?.channel) {\n const channelId = encodePath(msg.arg.channel, msg.arg.instId);\n const data = msg.data?.[0];\n if (data && this.handlers[channelId]) {\n this.handlers[channelId](data, msg.arg);\n }\n } else if (msg.event) {\n console.info('Event:', msg);\n }\n }\n\n // 调用订阅\n subscribe(channel: string, instId?: string, handler?: Function) {\n const channelId = encodePath(channel, instId);\n if (this.subscriptions.has(channelId)) {\n console.info(formatTime(Date.now()), `⚠️ Already subscribed: ${channelId}`);\n return;\n }\n\n const subMsg = {\n op: 'subscribe',\n args: [{ channel, instId }],\n };\n\n if (this.connected) {\n this.ws.send(JSON.stringify(subMsg));\n console.info(formatTime(Date.now()), `📩 Sent subscribe for ${channelId}`);\n } else {\n this.pendingSub.push(JSON.stringify(subMsg));\n console.info(formatTime(Date.now()), `📩 add subscribe for ${channelId} to pending list`);\n }\n this.subscriptions.add(channelId);\n if (handler) {\n this.handlers[channelId] = handler;\n }\n }\n\n // 取消订阅\n unsubscribe(channel: string, instId?: string) {\n const channelId = encodePath(channel, instId);\n if (!this.subscriptions.has(channelId)) return;\n\n const unSubMsg = {\n op: 'unsubscribe',\n args: [{ channel, instId }],\n };\n\n this.ws.send(JSON.stringify(unSubMsg));\n this.subscriptions.delete(channelId);\n delete this.handlers[channelId];\n console.info(`📩 Sent unsubscribe for ${channelId}`);\n }\n}\n\nexport const okxBusinessWsClient = new OKXWsClient('wss://wspap.okx.com:8443/ws/v5/business');\n"]}
1
+ {"version":3,"file":"websocket.js","sourceRoot":"","sources":["../src/websocket.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,UAAU,EAAE,MAAM,eAAe,CAAC;AACvD,OAAO,EAAE,QAAQ,EAAgB,GAAG,EAAE,MAAM,MAAM,CAAC;AACnD,8BAA8B;AAE9B,MAAM,OAAO,WAAW;IAOtB,YAAY,GAAW;QALvB,cAAS,GAAY,KAAK,CAAC;QAMzB,wBAAwB;QACxB,IAAI,CAAC,EAAE,GAAG,IAAI,SAAS,CAAC,GAAG,CAAC,CAAC;QAC7B,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;QACrB,IAAI,CAAC,SAAS,GAAG,QAAQ,CAAC,KAAK,CAAC;aAC7B,IAAI,CACH,GAAG,CAAC,GAAG,EAAE;YACP,IAAI,IAAI,CAAC,SAAS,EAAE;gBAClB,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;aACtB;QACH,CAAC,CAAC,CACH;aACA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C3F;QACD,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,SAAS,CAAC,CAAC;QAClC,IAAI,OAAO,EAAE;YACX,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,GAAG,OAAO,CAAC;SACpC;IACH,CAAC;IAED,OAAO;IACP,WAAW,CAAC,OAAe,EAAE,MAAe;QAC1C,MAAM,SAAS,GAAG,UAAU,CAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC9C,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,SAAS,CAAC;YAAE,OAAO;QAE/C,MAAM,QAAQ,GAAG;YACf,EAAE,EAAE,aAAa;YACjB,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;SAC5B,CAAC;QAEF,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;QACvC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QACrC,IAAI,IAAI,CAAC,aAAa,CAAC,IAAI,KAAK,CAAC,EAAE;YACjC,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC;SAC9B;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;QAChC,OAAO,CAAC,IAAI,CAAC,2BAA2B,SAAS,EAAE,CAAC,CAAC;IACvD,CAAC;CACF","sourcesContent":["import { encodePath, formatTime } from '@yuants/utils';\nimport { interval, Subscription, tap } from 'rxjs';\n// import WebSocket from 'ws';\n\nexport class OKXWsClient {\n ws: WebSocket;\n connected: boolean = false;\n subscriptions: Set<string>;\n handlers: Record<string, Function>;\n pendingSub: string[];\n keepAlive: Subscription;\n constructor(url: string) {\n // this.instId = instId;\n this.ws = new WebSocket(url);\n this.pendingSub = [];\n this.keepAlive = interval(25000)\n .pipe(\n tap(() => {\n if (this.connected) {\n this.ws.send('ping');\n }\n }),\n )\n .subscribe();\n // this.ws.\n this.ws.addEventListener('open', () => {\n this.connected = true;\n console.info(formatTime(Date.now()), '✅ WS connected');\n while (this.pendingSub.length > 0) {\n const msg = this.pendingSub.shift();\n if (msg) {\n this.ws.send(msg);\n console.info(formatTime(Date.now()), `📩 Sent subscribe for ${msg}`);\n }\n }\n });\n\n this.ws.addEventListener('message', (raw) => this.handleMessage(raw));\n this.subscriptions = new Set();\n this.handlers = {}; // key: channel, value: callback\n }\n\n // 处理消息\n handleMessage(raw: any) {\n if (raw.data === 'pong') {\n return;\n }\n const msg = JSON.parse(raw.data);\n if (msg.arg?.channel) {\n const channelId = encodePath(msg.arg.channel, msg.arg.instId);\n const data = msg.data?.[0];\n if (data && this.handlers[channelId]) {\n this.handlers[channelId](data, msg.arg);\n }\n } else if (msg.event) {\n console.info('Event:', msg);\n }\n }\n\n // 调用订阅\n subscribe(channel: string, instId?: string, handler?: Function) {\n const channelId = encodePath(channel, instId);\n if (this.subscriptions.has(channelId)) {\n console.info(formatTime(Date.now()), `⚠️ Already subscribed: ${channelId}`);\n return;\n }\n\n const subMsg = {\n op: 'subscribe',\n args: [{ channel, instId }],\n };\n\n if (this.connected) {\n this.ws.send(JSON.stringify(subMsg));\n console.info(formatTime(Date.now()), `📩 Sent subscribe for ${channelId}`);\n } else {\n this.pendingSub.push(JSON.stringify(subMsg));\n console.info(formatTime(Date.now()), `📩 add subscribe for ${channelId} to pending list`);\n }\n this.subscriptions.add(channelId);\n if (handler) {\n this.handlers[channelId] = handler;\n }\n }\n\n // 取消订阅\n unsubscribe(channel: string, instId?: string) {\n const channelId = encodePath(channel, instId);\n if (!this.subscriptions.has(channelId)) return;\n\n const unSubMsg = {\n op: 'unsubscribe',\n args: [{ channel, instId }],\n };\n\n this.ws.send(JSON.stringify(unSubMsg));\n this.subscriptions.delete(channelId);\n if (this.subscriptions.size === 0) {\n this.keepAlive.unsubscribe();\n }\n delete this.handlers[channelId];\n console.info(`📩 Sent unsubscribe for ${channelId}`);\n }\n}\n"]}
@@ -1 +1 @@
1
- {"version":3,"file":"account.d.ts","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,EAIb,MAAM,sBAAsB,CAAC;AAU9B,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAI5B,CAAC;AAEF,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;EAI1B,CAAC;AAQF,eAAO,MAAM,WAAW,mCAIvB,CAAC;AA+BF,eAAO,MAAM,mBAAmB,yCAqI/B,CAAC"}
1
+ {"version":3,"file":"account.d.ts","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":"AAAA,OAAO,EAEL,YAAY,EAIb,MAAM,sBAAsB,CAAC;AAU9B,eAAO,MAAM,gBAAgB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EAI5B,CAAC;AAEF,eAAO,MAAM,cAAc;;;;;;;;;;;;;;;;;;;;;;;;;EAI1B,CAAC;AAQF,eAAO,MAAM,WAAW,mCAIvB,CAAC;AA+BF,eAAO,MAAM,mBAAmB,yCAoI/B,CAAC"}
package/lib/account.js CHANGED
@@ -98,31 +98,31 @@ exports.tradingAccountInfo$ = exports.accountPosition$.pipe((0, rxjs_1.withLates
98
98
  account_id: account_id,
99
99
  updated_at: Date.now(),
100
100
  money: money,
101
- currencies: [money],
102
101
  positions: positions,
103
- orders: orders.data.map((x) => {
104
- const order_type = x.ordType === 'market' ? 'MARKET' : x.ordType === 'limit' ? 'LIMIT' : 'UNKNOWN';
105
- const order_direction = x.side === 'buy'
106
- ? x.posSide === 'long'
107
- ? 'OPEN_LONG'
108
- : 'CLOSE_SHORT'
109
- : x.posSide === 'short'
110
- ? 'OPEN_SHORT'
111
- : 'CLOSE_LONG';
112
- return {
113
- order_id: x.ordId,
114
- account_id,
115
- product_id: (0, utils_1.encodePath)(x.instType, x.instId),
116
- submit_at: +x.cTime,
117
- filled_at: +x.fillTime,
118
- order_type,
119
- order_direction,
120
- volume: +x.sz,
121
- traded_volume: +x.accFillSz,
122
- price: +x.px,
123
- traded_price: +x.avgPx,
124
- };
125
- }),
102
+ // orders: orders.data.map((x): IOrder => {
103
+ // const order_type = x.ordType === 'market' ? 'MARKET' : x.ordType === 'limit' ? 'LIMIT' : 'UNKNOWN';
104
+ // const order_direction =
105
+ // x.side === 'buy'
106
+ // ? x.posSide === 'long'
107
+ // ? 'OPEN_LONG'
108
+ // : 'CLOSE_SHORT'
109
+ // : x.posSide === 'short'
110
+ // ? 'OPEN_SHORT'
111
+ // : 'CLOSE_LONG';
112
+ // return {
113
+ // order_id: x.ordId,
114
+ // account_id,
115
+ // product_id: encodePath(x.instType, x.instId),
116
+ // submit_at: +x.cTime,
117
+ // filled_at: +x.fillTime,
118
+ // order_type,
119
+ // order_direction,
120
+ // volume: +x.sz,
121
+ // traded_volume: +x.accFillSz,
122
+ // price: +x.px,
123
+ // traded_price: +x.avgPx,
124
+ // };
125
+ // }),
126
126
  };
127
127
  }), (0, rxjs_1.shareReplay)(1));
128
128
  const sub = (0, rxjs_1.defer)(() => exports.accountUid$)
@@ -169,9 +169,7 @@ const fundingAccountInfo$ = (0, rxjs_1.combineLatest)([exports.accountUid$, asse
169
169
  account_id: `okx/${uid}/funding/USDT`,
170
170
  updated_at: Date.now(),
171
171
  money: money,
172
- currencies: [money],
173
172
  positions: positions,
174
- orders: [],
175
173
  };
176
174
  }), (0, rxjs_1.shareReplay)(1));
177
175
  const savingBalance$ = (0, rxjs_1.defer)(() => api_1.client.getFinanceSavingsBalance({})).pipe((0, rxjs_1.repeat)({ delay: 5000 }), (0, rxjs_1.retry)({ delay: 5000 }), (0, rxjs_1.shareReplay)(1));
@@ -193,9 +191,7 @@ const earningAccountInfo$ = (0, rxjs_1.combineLatest)([exports.accountUid$, savi
193
191
  account_id: `okx/${uid}/earning/USDT`,
194
192
  updated_at: Date.now(),
195
193
  money: money,
196
- currencies: [money],
197
194
  positions: [],
198
- orders: [],
199
195
  };
200
196
  }), (0, rxjs_1.shareReplay)(1));
201
197
  //# sourceMappingURL=account.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"account.js","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":";;;AAAA,uDAM8B;AAE9B,+CAA4C;AAC5C,yCAA2C;AAC3C,+BAA4G;AAC5G,+BAA+B;AAC/B,uCAAyF;AAEzF,MAAM,QAAQ,GAAG,mBAAQ,CAAC,WAAW,EAAE,CAAC;AAE3B,QAAA,gBAAgB,GAAG,IAAA,YAAK,EAAC,GAAG,EAAE,CAAC,YAAM,CAAC,mBAAmB,CAAC,EAAE,CAAC,CAAC,CAAC,IAAI,CAC9E,IAAA,aAAM,EAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACvB,IAAA,YAAK,EAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACtB,IAAA,kBAAW,EAAC,CAAC,CAAC,CACf,CAAC;AAEW,QAAA,cAAc,GAAG,IAAA,YAAK,EAAC,GAAG,EAAE,CAAC,YAAM,CAAC,gBAAgB,EAAE,CAAC,CAAC,IAAI,CACvE,IAAA,aAAM,EAAC,EAAE,KAAK,EAAE,KAAM,EAAE,CAAC,EACzB,IAAA,YAAK,EAAC,EAAE,KAAK,EAAE,KAAM,EAAE,CAAC,EACxB,IAAA,kBAAW,EAAC,CAAC,CAAC,CACf,CAAC;AAEF,MAAM,eAAe,GAAG,IAAA,YAAK,EAAC,GAAG,EAAE,CAAC,YAAM,CAAC,iBAAiB,EAAE,CAAC,CAAC,IAAI,CAClE,IAAA,aAAM,EAAC,EAAE,KAAK,EAAE,KAAM,EAAE,CAAC,EACzB,IAAA,YAAK,EAAC,EAAE,KAAK,EAAE,KAAM,EAAE,CAAC,EACxB,IAAA,kBAAW,EAAC,CAAC,CAAC,CACf,CAAC;AAEW,QAAA,WAAW,GAAG,sBAAc,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E,IAAI,EAAE,CAAC,EACvB,IAAA,YAAK,EAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC,EACtB,IAAA,kBAAW,EAAC,CAAC,CAAC,CACf,CAAC;AAEF,MAAM,mBAAmB,GAAG,IAAA,oBAAa,EAAC,CAAC,mBAAW,EAAE,cAAc,CAAC,CAAC,CAAC,IAAI,CAC3E,IAAA,UAAG,EAAC,CAAC,CAAC,GAAG,EAAE,MAAM,CAAC,EAAgB,EAAE;IAClC,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,OAAO,GAAG,MAAM,CAAC;IACvB,MAAM,IAAI,GAAG,MAAM,CAAC;IACpB,MAAM,IAAI,GAAG,CAAC,CAAC;IACf,MAAM,MAAM,GAAG,CAAC,CAAC;IAEjB,MAAM,KAAK,GAAkB;QAC3B,QAAQ,EAAE,MAAM;QAChB,MAAM;QACN,OAAO;QACP,IAAI;QACJ,IAAI;QACJ,MAAM;KACP,CAAC;IACF,OAAO;QACL,UAAU,EAAE,OAAO,GAAG,eAAe;QACrC,UAAU,EAAE,IAAI,CAAC,GAAG,EAAE;QACtB,KAAK,EAAE,KAAK;QACZ,UAAU,EAAE,CAAC,KAAK,CAAC;QACnB,SAAS,EAAE,EAAE;QACb,MAAM,EAAE,EAAE;KACX,CAAC;AACJ,CAAC,CAAC,EACF,IAAA,kBAAW,EAAC,CAAC,CAAC,CACf,CAAC","sourcesContent":["import {\n addAccountMarket,\n IAccountInfo,\n IAccountMoney,\n IPosition,\n publishAccountInfo,\n} from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { encodePath } from '@yuants/utils';\nimport { combineLatest, defer, filter, first, map, repeat, retry, shareReplay, withLatestFrom } from 'rxjs';\nimport { client } from './api';\nimport { mapProductIdToMarginProduct$, mapProductIdToUsdtSwapProduct$ } from './product';\n\nconst terminal = Terminal.fromNodeEnv();\n\nexport const accountPosition$ = defer(() => client.getAccountPositions({})).pipe(\n repeat({ delay: 5000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nexport const accountConfig$ = defer(() => client.getAccountConfig()).pipe(\n repeat({ delay: 10_000 }),\n retry({ delay: 10_000 }),\n shareReplay(1),\n);\n\nconst subAccountUids$ = defer(() => client.getSubAccountList()).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\nconst accountBalance$ = defer(() => client.getAccountBalance({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst accountUsdtBalance$ = accountBalance$.pipe(\n map((x) => x.data[0]?.details.find((x) => x.ccy === 'USDT')),\n filter((x): x is Exclude<typeof x, undefined> => !!x),\n shareReplay(1),\n);\n\nconst pendingOrders$ = defer(() => client.getTradeOrdersPending({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst marketIndexTickerUSDT$ = defer(() => client.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 tradingAccountInfo$ = accountPosition$.pipe(\n withLatestFrom(\n accountUid$,\n accountBalance$,\n pendingOrders$,\n mapProductIdToUsdtSwapProduct$,\n mapProductIdToMarginProduct$,\n marketIndexTickerUSDT$,\n ),\n map(\n ([\n positionsApi,\n uid,\n balanceApi,\n orders,\n mapProductIdToUsdtSwapProduct,\n mapProductIdToMarginProduct,\n marketIndexTickerUSDT,\n ]): IAccountInfo => {\n const account_id = `okx/${uid}/trading`;\n const money: IAccountMoney = { currency: 'USDT', equity: 0, balance: 0, used: 0, free: 0, profit: 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);\n const used = equity - free;\n const profit = equity - balance;\n money.equity += equity;\n money.balance += balance;\n money.used += used;\n money.free += free;\n money.profit += profit;\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 const delta_balance = delta_equity - delta_profit;\n const delta_used = delta_equity; // all used\n const delta_free = 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 money.equity += delta_equity;\n money.profit += delta_profit;\n money.balance += delta_balance;\n money.used += delta_used;\n money.free += delta_free;\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 x.instType === 'SWAP'\n ? (mapProductIdToUsdtSwapProduct.get(product_id)?.value_scale ?? 1) * volume * closable_price\n : x.instType === 'MARGIN'\n ? (mapProductIdToMarginProduct.get(product_id)?.value_scale ?? 1) * volume * closable_price\n : 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 account_id: account_id,\n updated_at: Date.now(),\n money: money,\n currencies: [money],\n positions: positions,\n orders: orders.data.map((x): IOrder => {\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 },\n ),\n shareReplay(1),\n);\n\nconst sub = defer(() => accountUid$)\n .pipe(first())\n .subscribe((uid) => {\n publishAccountInfo(terminal, `okx/${uid}/trading`, tradingAccountInfo$);\n addAccountMarket(terminal, { account_id: `okx/${uid}/trading`, market_id: 'OKX' });\n publishAccountInfo(terminal, `okx/${uid}/funding/USDT`, fundingAccountInfo$);\n publishAccountInfo(terminal, `okx/${uid}/earning/USDT`, earningAccountInfo$);\n });\ndefer(() => terminal.dispose$).subscribe(() => sub.unsubscribe());\n\nconst assetBalance$ = defer(() => client.getAssetBalances({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst fundingAccountInfo$ = combineLatest([accountUid$, assetBalance$, marketIndexTickerUSDT$]).pipe(\n map(([uid, assetBalances, marketIndexTickerUSDT]): IAccountInfo => {\n const money: IAccountMoney = { currency: 'USDT', equity: 0, balance: 0, used: 0, free: 0, profit: 0 };\n const positions: IPosition[] = [];\n\n assetBalances.data.forEach((x) => {\n if (x.ccy === 'USDT') {\n money.equity += +x.bal;\n money.balance += +x.bal;\n money.free += +x.bal;\n } else {\n const price = marketIndexTickerUSDT.get(x.ccy + '-USDT') || 0;\n const productId = encodePath('SPOT', `${x.ccy}-USDT`);\n const valuation = price * +x.bal || 0;\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: valuation,\n });\n\n money.equity += valuation;\n money.balance += valuation;\n money.used += valuation;\n }\n });\n\n return {\n account_id: `okx/${uid}/funding/USDT`,\n updated_at: Date.now(),\n money: money,\n currencies: [money],\n positions: positions,\n orders: [],\n };\n }),\n shareReplay(1),\n);\n\nconst savingBalance$ = defer(() => client.getFinanceSavingsBalance({})).pipe(\n repeat({ delay: 5000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst earningAccountInfo$ = combineLatest([accountUid$, savingBalance$]).pipe(\n map(([uid, offers]): IAccountInfo => {\n const equity = offers.data.filter((x) => x.ccy === 'USDT').reduce((acc, x) => acc + +x.amt, 0);\n const balance = equity;\n const free = equity;\n const used = 0;\n const profit = 0;\n\n const money: IAccountMoney = {\n currency: 'USDT',\n equity,\n balance,\n used,\n free,\n profit,\n };\n return {\n account_id: `okx/${uid}/earning/USDT`,\n updated_at: Date.now(),\n money: money,\n currencies: [money],\n positions: [],\n orders: [],\n };\n }),\n shareReplay(1),\n);\n"]}
1
+ {"version":3,"file":"account.js","sourceRoot":"","sources":["../src/account.ts"],"names":[],"mappings":";;;AAAA,uDAM8B;AAE9B,+CAA4C;AAC5C,yCAA2C;AAC3C,+BAA4G;AAC5G,+BAA+B;AAC/B,uCAAyF;AAEzF,MAAM,QAAQ,GAAG,mBAAQ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IPosition,\n publishAccountInfo,\n} from '@yuants/data-account';\nimport { IOrder } from '@yuants/data-order';\nimport { Terminal } from '@yuants/protocol';\nimport { encodePath } from '@yuants/utils';\nimport { combineLatest, defer, filter, first, map, repeat, retry, shareReplay, withLatestFrom } from 'rxjs';\nimport { client } from './api';\nimport { mapProductIdToMarginProduct$, mapProductIdToUsdtSwapProduct$ } from './product';\n\nconst terminal = Terminal.fromNodeEnv();\n\nexport const accountPosition$ = defer(() => client.getAccountPositions({})).pipe(\n repeat({ delay: 5000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nexport const accountConfig$ = defer(() => client.getAccountConfig()).pipe(\n repeat({ delay: 10_000 }),\n retry({ delay: 10_000 }),\n shareReplay(1),\n);\n\nconst subAccountUids$ = defer(() => client.getSubAccountList()).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\nconst accountBalance$ = defer(() => client.getAccountBalance({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst accountUsdtBalance$ = accountBalance$.pipe(\n map((x) => x.data[0]?.details.find((x) => x.ccy === 'USDT')),\n filter((x): x is Exclude<typeof x, undefined> => !!x),\n shareReplay(1),\n);\n\nconst pendingOrders$ = defer(() => client.getTradeOrdersPending({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst marketIndexTickerUSDT$ = defer(() => client.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 tradingAccountInfo$ = accountPosition$.pipe(\n withLatestFrom(\n accountUid$,\n accountBalance$,\n pendingOrders$,\n mapProductIdToUsdtSwapProduct$,\n mapProductIdToMarginProduct$,\n marketIndexTickerUSDT$,\n ),\n map(\n ([\n positionsApi,\n uid,\n balanceApi,\n orders,\n mapProductIdToUsdtSwapProduct,\n mapProductIdToMarginProduct,\n marketIndexTickerUSDT,\n ]): IAccountInfo => {\n const account_id = `okx/${uid}/trading`;\n const money: IAccountMoney = { currency: 'USDT', equity: 0, balance: 0, used: 0, free: 0, profit: 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);\n const used = equity - free;\n const profit = equity - balance;\n money.equity += equity;\n money.balance += balance;\n money.used += used;\n money.free += free;\n money.profit += profit;\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 const delta_balance = delta_equity - delta_profit;\n const delta_used = delta_equity; // all used\n const delta_free = 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 money.equity += delta_equity;\n money.profit += delta_profit;\n money.balance += delta_balance;\n money.used += delta_used;\n money.free += delta_free;\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 x.instType === 'SWAP'\n ? (mapProductIdToUsdtSwapProduct.get(product_id)?.value_scale ?? 1) * volume * closable_price\n : x.instType === 'MARGIN'\n ? (mapProductIdToMarginProduct.get(product_id)?.value_scale ?? 1) * volume * closable_price\n : 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 account_id: account_id,\n updated_at: Date.now(),\n money: money,\n positions: positions,\n // orders: orders.data.map((x): IOrder => {\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 },\n ),\n shareReplay(1),\n);\n\nconst sub = defer(() => accountUid$)\n .pipe(first())\n .subscribe((uid) => {\n publishAccountInfo(terminal, `okx/${uid}/trading`, tradingAccountInfo$);\n addAccountMarket(terminal, { account_id: `okx/${uid}/trading`, market_id: 'OKX' });\n publishAccountInfo(terminal, `okx/${uid}/funding/USDT`, fundingAccountInfo$);\n publishAccountInfo(terminal, `okx/${uid}/earning/USDT`, earningAccountInfo$);\n });\ndefer(() => terminal.dispose$).subscribe(() => sub.unsubscribe());\n\nconst assetBalance$ = defer(() => client.getAssetBalances({})).pipe(\n repeat({ delay: 1000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst fundingAccountInfo$ = combineLatest([accountUid$, assetBalance$, marketIndexTickerUSDT$]).pipe(\n map(([uid, assetBalances, marketIndexTickerUSDT]): IAccountInfo => {\n const money: IAccountMoney = { currency: 'USDT', equity: 0, balance: 0, used: 0, free: 0, profit: 0 };\n const positions: IPosition[] = [];\n\n assetBalances.data.forEach((x) => {\n if (x.ccy === 'USDT') {\n money.equity += +x.bal;\n money.balance += +x.bal;\n money.free += +x.bal;\n } else {\n const price = marketIndexTickerUSDT.get(x.ccy + '-USDT') || 0;\n const productId = encodePath('SPOT', `${x.ccy}-USDT`);\n const valuation = price * +x.bal || 0;\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: valuation,\n });\n\n money.equity += valuation;\n money.balance += valuation;\n money.used += valuation;\n }\n });\n\n return {\n account_id: `okx/${uid}/funding/USDT`,\n updated_at: Date.now(),\n money: money,\n positions: positions,\n };\n }),\n shareReplay(1),\n);\n\nconst savingBalance$ = defer(() => client.getFinanceSavingsBalance({})).pipe(\n repeat({ delay: 5000 }),\n retry({ delay: 5000 }),\n shareReplay(1),\n);\n\nconst earningAccountInfo$ = combineLatest([accountUid$, savingBalance$]).pipe(\n map(([uid, offers]): IAccountInfo => {\n const equity = offers.data.filter((x) => x.ccy === 'USDT').reduce((acc, x) => acc + +x.amt, 0);\n const balance = equity;\n const free = equity;\n const used = 0;\n const profit = 0;\n\n const money: IAccountMoney = {\n currency: 'USDT',\n equity,\n balance,\n used,\n free,\n profit,\n };\n return {\n account_id: `okx/${uid}/earning/USDT`,\n updated_at: Date.now(),\n money: money,\n positions: [],\n };\n }),\n shareReplay(1),\n);\n"]}
package/lib/ohlc.js CHANGED
@@ -132,7 +132,8 @@ protocol_1.Terminal.fromNodeEnv().channel.publishChannel('ohlc', { pattern: `^OK
132
132
  console.info((0, utils_1.formatTime)(Date.now()), `subscribe`, series_id);
133
133
  return new rxjs_1.Observable((subscriber) => {
134
134
  console.info((0, utils_1.formatTime)(Date.now()), `subscribe`, candleType, instId);
135
- websocket_1.okxBusinessWsClient.subscribe(candleType, instId, async (data) => {
135
+ const okxBusinessWsClient = new websocket_1.OKXWsClient('wss://wspap.okx.com:8443/ws/v5/business');
136
+ okxBusinessWsClient.subscribe(candleType, instId, async (data) => {
136
137
  const created_at = Number(data[0]);
137
138
  const closed_at = created_at + offset;
138
139
  const open = data[1];
@@ -160,7 +161,7 @@ protocol_1.Terminal.fromNodeEnv().channel.publishChannel('ohlc', { pattern: `^OK
160
161
  subscriber.next(cancelData);
161
162
  });
162
163
  return () => {
163
- websocket_1.okxBusinessWsClient.unsubscribe(candleType, instId);
164
+ okxBusinessWsClient.unsubscribe(candleType, instId);
164
165
  };
165
166
  }).pipe((0, sql_1.writeToSQL)({
166
167
  tableName: 'ohlc',
package/lib/ohlc.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"ohlc.js","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AACA,qDAA2D;AAC3D,+CAA4C;AAC5C,yCAA4F;AAC5F,+BAA6D;AAC7D,+BAA+B;AAC/B,2CAAkD;AAClD,qCAAkF;AAElF,aAAa;AACb,gCAAgC;AAChC,8BAA8B;AAC9B,8CAA8C;AAE9C,MAAM,wBAAwB,GAA2B;IACvD,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,KAAK,EAAE,KAAK;IACZ,KAAK,EAAE,KAAK;IAEZ,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,KAAK,EAAE,KAAK;IAEZ,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;CACV,CAAC;AAEF,MAAM,2BAA2B,GAA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":["import { IOHLC } from '@yuants/data-ohlc';\nimport { createSeriesProvider } from '@yuants/data-series';\nimport { Terminal } from '@yuants/protocol';\nimport { convertDurationToOffset, decodePath, encodePath, formatTime } from '@yuants/utils';\nimport { firstValueFrom, Observable, of, timer } from 'rxjs';\nimport { client } from './api';\nimport { okxBusinessWsClient } from './websocket';\nimport { buildInsertManyIntoTableSQL, requestSQL, writeToSQL } from '@yuants/sql';\n\n// 时间粒度,默认值1m\n// 如 [1m/3m/5m/15m/30m/1H/2H/4H]\n// 香港时间开盘价k线:[6H/12H/1D/1W/1M]\n// UTC时间开盘价k线:[6Hutc/12Hutc/1Dutc/1Wutc/1Mutc]\n\nconst DURATION_TO_OKX_BAR_TYPE: Record<string, string> = {\n PT1M: '1m',\n PT3M: '3m',\n PT5M: '5m',\n PT15M: '15m',\n PT30M: '30m',\n\n PT1H: '1H',\n PT2H: '2H',\n PT4H: '4H',\n PT6H: '6H',\n PT12H: '12H',\n\n P1D: '1D',\n P1W: '1W',\n P1M: '1M',\n};\n\nconst DURATION_TO_OKX_CANDLE_TYPE: Record<string, string> = {\n PT1M: 'mark-price-candle1m',\n PT3M: 'mark-price-candle3m',\n PT5M: 'mark-price-candle5m',\n PT15M: 'mark-price-candle15m',\n PT30M: 'mark-price-candle30m',\n\n PT1H: 'mark-price-candle1H',\n PT2H: 'mark-price-candle2H',\n PT4H: 'mark-price-candle4H',\n PT6H: 'mark-price-candle6H',\n PT12H: 'mark-price-candle12H',\n\n P1D: 'mark-price-candle1D',\n P1W: 'mark-price-candle1W',\n P1M: 'mark-price-candle1M',\n};\n\ncreateSeriesProvider<IOHLC>(Terminal.fromNodeEnv(), {\n tableName: 'ohlc',\n series_id_prefix_parts: ['OKX'],\n reversed: true,\n serviceOptions: { concurrent: 1 },\n queryFn: async function* ({ series_id, ended_at }) {\n const [datasource_id, product_id, duration] = decodePath(series_id);\n const offset = convertDurationToOffset(duration);\n if (!datasource_id) {\n throw 'datasource_id is required';\n }\n if (!product_id) {\n throw 'product_id is required';\n }\n if (!offset) {\n throw 'duration is invalid';\n }\n const [instType, instId] = decodePath(product_id);\n if (!instId) {\n throw `invalid product_id: ${product_id}`;\n }\n\n const bar = DURATION_TO_OKX_BAR_TYPE[duration];\n if (!bar) {\n throw `unsupported duration: ${duration}`;\n }\n\n let currentStartTime = ended_at;\n\n while (true) {\n // 向前翻页,时间降序,不含 after 时间点\n const res = await client.getHistoryCandles({\n instId,\n bar,\n after: `${currentStartTime}`,\n limit: '100',\n });\n if (res.code !== '0') {\n throw `API failed: ${res.code} ${res.msg}`;\n }\n if (res.data.length === 0) break;\n currentStartTime = +res.data[res.data.length - 1][0];\n const data = res.data.map(\n (x): IOHLC => ({\n series_id,\n datasource_id,\n product_id,\n duration,\n created_at: formatTime(+x[0]),\n closed_at: formatTime(+x[0] + offset),\n open: x[1],\n high: x[2],\n low: x[3],\n close: x[4],\n volume: x[5],\n open_interest: '0',\n }),\n );\n yield data;\n await firstValueFrom(timer(1000));\n }\n },\n});\n\nTerminal.fromNodeEnv().channel.publishChannel('ohlc', { pattern: `^OKX/` }, (series_id) => {\n // const [] = decodePath(args)\n const [datasource_id, product_id, duration] = decodePath(series_id);\n const [, instId] = decodePath(product_id);\n const offset = convertDurationToOffset(duration);\n if (!datasource_id) {\n throw 'datasource_id is required';\n }\n if (!product_id) {\n throw 'product_id is required';\n }\n if (!offset) {\n throw 'duration is invalid';\n }\n const candleType = DURATION_TO_OKX_CANDLE_TYPE[duration];\n console.info(formatTime(Date.now()), `subscribe`, series_id);\n return new Observable<IOHLC>((subscriber) => {\n console.info(formatTime(Date.now()), `subscribe`, candleType, instId);\n okxBusinessWsClient.subscribe(candleType, instId, async (data: string[]) => {\n const created_at = Number(data[0]);\n const closed_at = created_at + offset;\n const open = data[1];\n const high = data[2];\n const low = data[3];\n const close = data[4];\n if (isNaN(closed_at)) {\n return;\n }\n console.info(formatTime(Date.now()), `insertData`, data);\n const cancelData: IOHLC = {\n closed_at: formatTime(closed_at),\n created_at: formatTime(created_at),\n open,\n high,\n low,\n close,\n series_id,\n datasource_id,\n duration,\n product_id,\n volume: '0',\n open_interest: '0',\n };\n subscriber.next(cancelData);\n });\n return () => {\n okxBusinessWsClient.unsubscribe(candleType, instId);\n };\n }).pipe(\n writeToSQL({\n tableName: 'ohlc',\n conflictKeys: ['created_at', 'series_id'],\n writeInterval: 1000,\n terminal: Terminal.fromNodeEnv(),\n keyFn: (x) => encodePath(x.created_at, x.series_id),\n }),\n );\n});\n"]}
1
+ {"version":3,"file":"ohlc.js","sourceRoot":"","sources":["../src/ohlc.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AACA,qDAA2D;AAC3D,+CAA4C;AAC5C,yCAA4F;AAC5F,+BAA6D;AAC7D,+BAA+B;AAC/B,2CAA0C;AAC1C,qCAAyC;AAEzC,aAAa;AACb,gCAAgC;AAChC,8BAA8B;AAC9B,8CAA8C;AAE9C,MAAM,wBAAwB,GAA2B;IACvD,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,KAAK,EAAE,KAAK;IACZ,KAAK,EAAE,KAAK;IAEZ,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,IAAI,EAAE,IAAI;IACV,KAAK,EAAE,KAAK;IAEZ,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;IACT,GAAG,EAAE,IAAI;CACV,CAAC;AAEF,MAAM,2BAA2B,GAA2B;IAC1D,IAAI,EAAE,qBAAqB;IAC3B,IAAI,EAAE,qBAAqB;IAC3B,IAAI,EAAE,qBAAqB;IAC3B,KAAK,EAAE,sBAAsB;IAC7B,KAAK,EAAE,sBAAsB;IAE7B,IAAI,EAAE,qBAAqB;IAC3B,IAAI,EAAE,qBAAqB;IAC3B,IAAI,EAAE,qBAAqB;IAC3B,IAAI,EAAE,qBAAqB;IAC3B,KAAK,EAAE,sBAAsB;IAE7B,GAAG,EAAE,qBAAqB;IAC1B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K,CAAC,SAAS,CAAC,EAAE;gBACpB,OAAO;aACR;YACD,OAAO,CAAC,IAAI,CAAC,IAAA,kBAAU,EAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,YAAY,EAAE,IAAI,CAAC,CAAC;YACzD,MAAM,UAAU,GAAU;gBACxB,SAAS,EAAE,IAAA,kBAAU,EAAC,SAAS,CAAC;gBAChC,UAAU,EAAE,IAAA,kBAAU,EAAC,UAAU,CAAC;gBAClC,IAAI;gBACJ,IAAI;gBACJ,GAAG;gBACH,KAAK;gBACL,SAAS;gBACT,aAAa;gBACb,QAAQ;gBACR,UAAU;gBACV,MAAM,EAAE,GAAG;gBACX,aAAa,EAAE,GAAG;aACnB,CAAC;YACF,UAAU,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;QAC9B,CAAC,CAAC,CAAC;QACH,OAAO,GAAG,EAAE;YACV,mBAAmB,CAAC,WAAW,CAAC,UAAU,EAAE,MAAM,CAAC,CAAC;QACtD,CAAC,CAAC;IACJ,CAAC,CAAC,CAAC,IAAI,CACL,IAAA,gBAAU,EAAC;QACT,SAAS,EAAE,MAAM;QACjB,YAAY,EAAE,CAAC,YAAY,EAAE,WAAW,CAAC;QACzC,aAAa,EAAE,IAAI;QACnB,QAAQ,EAAE,mBAAQ,CAAC,WAAW,EAAE;QAChC,KAAK,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,IAAA,kBAAU,EAAC,CAAC,CAAC,UAAU,EAAE,CAAC,CAAC,SAAS,CAAC;KACpD,CAAC,CACH,CAAC;AACJ,CAAC,CAAC,CAAC","sourcesContent":["import { IOHLC } from '@yuants/data-ohlc';\nimport { createSeriesProvider } from '@yuants/data-series';\nimport { Terminal } from '@yuants/protocol';\nimport { convertDurationToOffset, decodePath, encodePath, formatTime } from '@yuants/utils';\nimport { firstValueFrom, Observable, of, timer } from 'rxjs';\nimport { client } from './api';\nimport { OKXWsClient } from './websocket';\nimport { writeToSQL } from '@yuants/sql';\n\n// 时间粒度,默认值1m\n// 如 [1m/3m/5m/15m/30m/1H/2H/4H]\n// 香港时间开盘价k线:[6H/12H/1D/1W/1M]\n// UTC时间开盘价k线:[6Hutc/12Hutc/1Dutc/1Wutc/1Mutc]\n\nconst DURATION_TO_OKX_BAR_TYPE: Record<string, string> = {\n PT1M: '1m',\n PT3M: '3m',\n PT5M: '5m',\n PT15M: '15m',\n PT30M: '30m',\n\n PT1H: '1H',\n PT2H: '2H',\n PT4H: '4H',\n PT6H: '6H',\n PT12H: '12H',\n\n P1D: '1D',\n P1W: '1W',\n P1M: '1M',\n};\n\nconst DURATION_TO_OKX_CANDLE_TYPE: Record<string, string> = {\n PT1M: 'mark-price-candle1m',\n PT3M: 'mark-price-candle3m',\n PT5M: 'mark-price-candle5m',\n PT15M: 'mark-price-candle15m',\n PT30M: 'mark-price-candle30m',\n\n PT1H: 'mark-price-candle1H',\n PT2H: 'mark-price-candle2H',\n PT4H: 'mark-price-candle4H',\n PT6H: 'mark-price-candle6H',\n PT12H: 'mark-price-candle12H',\n\n P1D: 'mark-price-candle1D',\n P1W: 'mark-price-candle1W',\n P1M: 'mark-price-candle1M',\n};\n\ncreateSeriesProvider<IOHLC>(Terminal.fromNodeEnv(), {\n tableName: 'ohlc',\n series_id_prefix_parts: ['OKX'],\n reversed: true,\n serviceOptions: { concurrent: 1 },\n queryFn: async function* ({ series_id, ended_at }) {\n const [datasource_id, product_id, duration] = decodePath(series_id);\n const offset = convertDurationToOffset(duration);\n if (!datasource_id) {\n throw 'datasource_id is required';\n }\n if (!product_id) {\n throw 'product_id is required';\n }\n if (!offset) {\n throw 'duration is invalid';\n }\n const [instType, instId] = decodePath(product_id);\n if (!instId) {\n throw `invalid product_id: ${product_id}`;\n }\n\n const bar = DURATION_TO_OKX_BAR_TYPE[duration];\n if (!bar) {\n throw `unsupported duration: ${duration}`;\n }\n\n let currentStartTime = ended_at;\n\n while (true) {\n // 向前翻页,时间降序,不含 after 时间点\n const res = await client.getHistoryCandles({\n instId,\n bar,\n after: `${currentStartTime}`,\n limit: '100',\n });\n if (res.code !== '0') {\n throw `API failed: ${res.code} ${res.msg}`;\n }\n if (res.data.length === 0) break;\n currentStartTime = +res.data[res.data.length - 1][0];\n const data = res.data.map(\n (x): IOHLC => ({\n series_id,\n datasource_id,\n product_id,\n duration,\n created_at: formatTime(+x[0]),\n closed_at: formatTime(+x[0] + offset),\n open: x[1],\n high: x[2],\n low: x[3],\n close: x[4],\n volume: x[5],\n open_interest: '0',\n }),\n );\n yield data;\n await firstValueFrom(timer(1000));\n }\n },\n});\n\nTerminal.fromNodeEnv().channel.publishChannel('ohlc', { pattern: `^OKX/` }, (series_id) => {\n // const [] = decodePath(args)\n const [datasource_id, product_id, duration] = decodePath(series_id);\n const [, instId] = decodePath(product_id);\n const offset = convertDurationToOffset(duration);\n if (!datasource_id) {\n throw 'datasource_id is required';\n }\n if (!product_id) {\n throw 'product_id is required';\n }\n if (!offset) {\n throw 'duration is invalid';\n }\n const candleType = DURATION_TO_OKX_CANDLE_TYPE[duration];\n console.info(formatTime(Date.now()), `subscribe`, series_id);\n return new Observable<IOHLC>((subscriber) => {\n console.info(formatTime(Date.now()), `subscribe`, candleType, instId);\n const okxBusinessWsClient = new OKXWsClient('wss://wspap.okx.com:8443/ws/v5/business');\n okxBusinessWsClient.subscribe(candleType, instId, async (data: string[]) => {\n const created_at = Number(data[0]);\n const closed_at = created_at + offset;\n const open = data[1];\n const high = data[2];\n const low = data[3];\n const close = data[4];\n if (isNaN(closed_at)) {\n return;\n }\n console.info(formatTime(Date.now()), `insertData`, data);\n const cancelData: IOHLC = {\n closed_at: formatTime(closed_at),\n created_at: formatTime(created_at),\n open,\n high,\n low,\n close,\n series_id,\n datasource_id,\n duration,\n product_id,\n volume: '0',\n open_interest: '0',\n };\n subscriber.next(cancelData);\n });\n return () => {\n okxBusinessWsClient.unsubscribe(candleType, instId);\n };\n }).pipe(\n writeToSQL({\n tableName: 'ohlc',\n conflictKeys: ['created_at', 'series_id'],\n writeInterval: 1000,\n terminal: Terminal.fromNodeEnv(),\n keyFn: (x) => encodePath(x.created_at, x.series_id),\n }),\n );\n});\n"]}
@@ -1,14 +1,14 @@
1
- declare class OKXWsClient {
1
+ import { Subscription } from 'rxjs';
2
+ export declare class OKXWsClient {
2
3
  ws: WebSocket;
3
4
  connected: boolean;
4
5
  subscriptions: Set<string>;
5
6
  handlers: Record<string, Function>;
6
7
  pendingSub: string[];
8
+ keepAlive: Subscription;
7
9
  constructor(url: string);
8
10
  handleMessage(raw: any): void;
9
11
  subscribe(channel: string, instId?: string, handler?: Function): void;
10
12
  unsubscribe(channel: string, instId?: string): void;
11
13
  }
12
- export declare const okxBusinessWsClient: OKXWsClient;
13
- export {};
14
14
  //# sourceMappingURL=websocket.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../src/websocket.ts"],"names":[],"mappings":"AAGA,cAAM,WAAW;IACf,EAAE,EAAE,SAAS,CAAC;IACd,SAAS,EAAE,OAAO,CAAS;IAC3B,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAC3B,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACnC,UAAU,EAAE,MAAM,EAAE,CAAC;gBACT,GAAG,EAAE,MAAM;IAsBvB,aAAa,CAAC,GAAG,EAAE,GAAG;IActB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ;IA0B9D,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM;CAc7C;AAED,eAAO,MAAM,mBAAmB,aAA6D,CAAC"}
1
+ {"version":3,"file":"websocket.d.ts","sourceRoot":"","sources":["../src/websocket.ts"],"names":[],"mappings":"AACA,OAAO,EAAY,YAAY,EAAO,MAAM,MAAM,CAAC;AAGnD,qBAAa,WAAW;IACtB,EAAE,EAAE,SAAS,CAAC;IACd,SAAS,EAAE,OAAO,CAAS;IAC3B,aAAa,EAAE,GAAG,CAAC,MAAM,CAAC,CAAC;IAC3B,QAAQ,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;IACnC,UAAU,EAAE,MAAM,EAAE,CAAC;IACrB,SAAS,EAAE,YAAY,CAAC;gBACZ,GAAG,EAAE,MAAM;IAgCvB,aAAa,CAAC,GAAG,EAAE,GAAG;IAiBtB,SAAS,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,QAAQ;IA0B9D,WAAW,CAAC,OAAO,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM;CAiB7C"}
package/lib/websocket.js CHANGED
@@ -1,7 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.okxBusinessWsClient = void 0;
3
+ exports.OKXWsClient = void 0;
4
4
  const utils_1 = require("@yuants/utils");
5
+ const rxjs_1 = require("rxjs");
5
6
  // import WebSocket from 'ws';
6
7
  class OKXWsClient {
7
8
  constructor(url) {
@@ -9,6 +10,13 @@ class OKXWsClient {
9
10
  // this.instId = instId;
10
11
  this.ws = new WebSocket(url);
11
12
  this.pendingSub = [];
13
+ this.keepAlive = (0, rxjs_1.interval)(25000)
14
+ .pipe((0, rxjs_1.tap)(() => {
15
+ if (this.connected) {
16
+ this.ws.send('ping');
17
+ }
18
+ }))
19
+ .subscribe();
12
20
  // this.ws.
13
21
  this.ws.addEventListener('open', () => {
14
22
  this.connected = true;
@@ -28,6 +36,9 @@ class OKXWsClient {
28
36
  // 处理消息
29
37
  handleMessage(raw) {
30
38
  var _a, _b;
39
+ if (raw.data === 'pong') {
40
+ return;
41
+ }
31
42
  const msg = JSON.parse(raw.data);
32
43
  if ((_a = msg.arg) === null || _a === void 0 ? void 0 : _a.channel) {
33
44
  const channelId = (0, utils_1.encodePath)(msg.arg.channel, msg.arg.instId);
@@ -75,9 +86,12 @@ class OKXWsClient {
75
86
  };
76
87
  this.ws.send(JSON.stringify(unSubMsg));
77
88
  this.subscriptions.delete(channelId);
89
+ if (this.subscriptions.size === 0) {
90
+ this.keepAlive.unsubscribe();
91
+ }
78
92
  delete this.handlers[channelId];
79
93
  console.info(`📩 Sent unsubscribe for ${channelId}`);
80
94
  }
81
95
  }
82
- exports.okxBusinessWsClient = new OKXWsClient('wss://wspap.okx.com:8443/ws/v5/business');
96
+ exports.OKXWsClient = OKXWsClient;
83
97
  //# sourceMappingURL=websocket.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"websocket.js","sourceRoot":"","sources":["../src/websocket.ts"],"names":[],"mappings":";;;AAAA,yCAAuD;AACvD,8BAA8B;AAE9B,MAAM,WAAW;IAMf,YAAY,GAAW;QAJvB,cAAS,GAAY,KAAK,CAAC;QAKzB,wBAAwB;QACxB,IAAI,CAAC,EAAE,GAAG,IAAI,SAAS,CAAC,GAAG,CAAC,CAAC;QAC7B,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;QACrB,WAAW;QACX,IAAI,CAAC,EAAE,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;YACpC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;YACtB,OAAO,CAAC,IAAI,CAAC,IAAA,kBAAU,EAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,gBAAgB,CAAC,CAAC;YACvD,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE;gBACjC,MAAM,GAAG,GAAG,IAAI,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC;gBACpC,IAAI,GAAG,EAAE;oBACP,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;oBAClB,OAAO,CAAC,IAAI,CAAC,IAAA,kBAAU,EAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,yBAAyB,GAAG,EAAE,CAAC,CAAC;iBACtE;aACF;QACH,CAAC,CAAC,CAAC;QACH,I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CAAC;QACrC,OAAO,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;QAChC,OAAO,CAAC,IAAI,CAAC,2BAA2B,SAAS,EAAE,CAAC,CAAC;IACvD,CAAC;CACF;AAEY,QAAA,mBAAmB,GAAG,IAAI,WAAW,CAAC,yCAAyC,CAAC,CAAC","sourcesContent":["import { encodePath, formatTime } from '@yuants/utils';\n// import WebSocket from 'ws';\n\nclass OKXWsClient {\n ws: WebSocket;\n connected: boolean = false;\n subscriptions: Set<string>;\n handlers: Record<string, Function>;\n pendingSub: string[];\n constructor(url: string) {\n // this.instId = instId;\n this.ws = new WebSocket(url);\n this.pendingSub = [];\n // this.ws.\n this.ws.addEventListener('open', () => {\n this.connected = true;\n console.info(formatTime(Date.now()), '✅ WS connected');\n while (this.pendingSub.length > 0) {\n const msg = this.pendingSub.shift();\n if (msg) {\n this.ws.send(msg);\n console.info(formatTime(Date.now()), `📩 Sent subscribe for ${msg}`);\n }\n }\n });\n this.ws.addEventListener('message', (raw) => this.handleMessage(raw));\n this.subscriptions = new Set();\n this.handlers = {}; // key: channel, value: callback\n }\n\n // 处理消息\n handleMessage(raw: any) {\n const msg = JSON.parse(raw.data);\n if (msg.arg?.channel) {\n const channelId = encodePath(msg.arg.channel, msg.arg.instId);\n const data = msg.data?.[0];\n if (data && this.handlers[channelId]) {\n this.handlers[channelId](data, msg.arg);\n }\n } else if (msg.event) {\n console.info('Event:', msg);\n }\n }\n\n // 调用订阅\n subscribe(channel: string, instId?: string, handler?: Function) {\n const channelId = encodePath(channel, instId);\n if (this.subscriptions.has(channelId)) {\n console.info(formatTime(Date.now()), `⚠️ Already subscribed: ${channelId}`);\n return;\n }\n\n const subMsg = {\n op: 'subscribe',\n args: [{ channel, instId }],\n };\n\n if (this.connected) {\n this.ws.send(JSON.stringify(subMsg));\n console.info(formatTime(Date.now()), `📩 Sent subscribe for ${channelId}`);\n } else {\n this.pendingSub.push(JSON.stringify(subMsg));\n console.info(formatTime(Date.now()), `📩 add subscribe for ${channelId} to pending list`);\n }\n this.subscriptions.add(channelId);\n if (handler) {\n this.handlers[channelId] = handler;\n }\n }\n\n // 取消订阅\n unsubscribe(channel: string, instId?: string) {\n const channelId = encodePath(channel, instId);\n if (!this.subscriptions.has(channelId)) return;\n\n const unSubMsg = {\n op: 'unsubscribe',\n args: [{ channel, instId }],\n };\n\n this.ws.send(JSON.stringify(unSubMsg));\n this.subscriptions.delete(channelId);\n delete this.handlers[channelId];\n console.info(`📩 Sent unsubscribe for ${channelId}`);\n }\n}\n\nexport const okxBusinessWsClient = new OKXWsClient('wss://wspap.okx.com:8443/ws/v5/business');\n"]}
1
+ {"version":3,"file":"websocket.js","sourceRoot":"","sources":["../src/websocket.ts"],"names":[],"mappings":";;;AAAA,yCAAuD;AACvD,+BAAmD;AACnD,8BAA8B;AAE9B,MAAa,WAAW;IAOtB,YAAY,GAAW;QALvB,cAAS,GAAY,KAAK,CAAC;QAMzB,wBAAwB;QACxB,IAAI,CAAC,EAAE,GAAG,IAAI,SAAS,CAAC,GAAG,CAAC,CAAC;QAC7B,IAAI,CAAC,UAAU,GAAG,EAAE,CAAC;QACrB,IAAI,CAAC,SAAS,GAAG,IAAA,eAAQ,EAAC,KAAK,CAAC;aAC7B,IAAI,CACH,IAAA,UAAG,EAAC,GAAG,EAAE;YACP,IAAI,IAAI,CAAC,SAAS,EAAE;gBAClB,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC;aACtB;QACH,CAAC,CAAC,CACH;aACA,SAAS,EAAE,CAAC;QACf,WAAW;QACX,IAAI,CAAC,EAAE,CAAC,gBAAgB,CAAC,MAAM,EAAE,GAAG,EAAE;YACpC,IAAI,CAAC,SAAS,GAAG,IAAI,CAAC;YACtB,OAAO,CAAC,IAAI,CAAC,IAAA,kBAAU,EAAC,IAAI,CAAC,GAAG,EAAE,CAAC,EAAE,gBAAgB,CAAC,CAAC;YACvD,OAAO,IAAI,CAAC,UAAU,CAAC,MAAM,GAA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,kBAAU,EAAC,OAAO,EAAE,MAAM,CAAC,CAAC;QAC9C,IAAI,CAAC,IAAI,CAAC,aAAa,CAAC,GAAG,CAAC,SAAS,CAAC;YAAE,OAAO;QAE/C,MAAM,QAAQ,GAAG;YACf,EAAE,EAAE,aAAa;YACjB,IAAI,EAAE,CAAC,EAAE,OAAO,EAAE,MAAM,EAAE,CAAC;SAC5B,CAAC;QAEF,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,QAAQ,CAAC,CAAC,CAAC;QACvC,IAAI,CAAC,aAAa,CAAC,MAAM,CAAC,SAAS,CAAC,CAAC;QACrC,IAAI,IAAI,CAAC,aAAa,CAAC,IAAI,KAAK,CAAC,EAAE;YACjC,IAAI,CAAC,SAAS,CAAC,WAAW,EAAE,CAAC;SAC9B;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,SAAS,CAAC,CAAC;QAChC,OAAO,CAAC,IAAI,CAAC,2BAA2B,SAAS,EAAE,CAAC,CAAC;IACvD,CAAC;CACF;AAnGD,kCAmGC","sourcesContent":["import { encodePath, formatTime } from '@yuants/utils';\nimport { interval, Subscription, tap } from 'rxjs';\n// import WebSocket from 'ws';\n\nexport class OKXWsClient {\n ws: WebSocket;\n connected: boolean = false;\n subscriptions: Set<string>;\n handlers: Record<string, Function>;\n pendingSub: string[];\n keepAlive: Subscription;\n constructor(url: string) {\n // this.instId = instId;\n this.ws = new WebSocket(url);\n this.pendingSub = [];\n this.keepAlive = interval(25000)\n .pipe(\n tap(() => {\n if (this.connected) {\n this.ws.send('ping');\n }\n }),\n )\n .subscribe();\n // this.ws.\n this.ws.addEventListener('open', () => {\n this.connected = true;\n console.info(formatTime(Date.now()), '✅ WS connected');\n while (this.pendingSub.length > 0) {\n const msg = this.pendingSub.shift();\n if (msg) {\n this.ws.send(msg);\n console.info(formatTime(Date.now()), `📩 Sent subscribe for ${msg}`);\n }\n }\n });\n\n this.ws.addEventListener('message', (raw) => this.handleMessage(raw));\n this.subscriptions = new Set();\n this.handlers = {}; // key: channel, value: callback\n }\n\n // 处理消息\n handleMessage(raw: any) {\n if (raw.data === 'pong') {\n return;\n }\n const msg = JSON.parse(raw.data);\n if (msg.arg?.channel) {\n const channelId = encodePath(msg.arg.channel, msg.arg.instId);\n const data = msg.data?.[0];\n if (data && this.handlers[channelId]) {\n this.handlers[channelId](data, msg.arg);\n }\n } else if (msg.event) {\n console.info('Event:', msg);\n }\n }\n\n // 调用订阅\n subscribe(channel: string, instId?: string, handler?: Function) {\n const channelId = encodePath(channel, instId);\n if (this.subscriptions.has(channelId)) {\n console.info(formatTime(Date.now()), `⚠️ Already subscribed: ${channelId}`);\n return;\n }\n\n const subMsg = {\n op: 'subscribe',\n args: [{ channel, instId }],\n };\n\n if (this.connected) {\n this.ws.send(JSON.stringify(subMsg));\n console.info(formatTime(Date.now()), `📩 Sent subscribe for ${channelId}`);\n } else {\n this.pendingSub.push(JSON.stringify(subMsg));\n console.info(formatTime(Date.now()), `📩 add subscribe for ${channelId} to pending list`);\n }\n this.subscriptions.add(channelId);\n if (handler) {\n this.handlers[channelId] = handler;\n }\n }\n\n // 取消订阅\n unsubscribe(channel: string, instId?: string) {\n const channelId = encodePath(channel, instId);\n if (!this.subscriptions.has(channelId)) return;\n\n const unSubMsg = {\n op: 'unsubscribe',\n args: [{ channel, instId }],\n };\n\n this.ws.send(JSON.stringify(unSubMsg));\n this.subscriptions.delete(channelId);\n if (this.subscriptions.size === 0) {\n this.keepAlive.unsubscribe();\n }\n delete this.handlers[channelId];\n console.info(`📩 Sent unsubscribe for ${channelId}`);\n }\n}\n"]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@yuants/vendor-okx",
3
- "version": "0.17.1",
3
+ "version": "0.17.3",
4
4
  "bin": "lib/cli.js",
5
5
  "files": [
6
6
  "dist",
@@ -10,7 +10,7 @@
10
10
  "dependencies": {
11
11
  "@yuants/protocol": "0.37.2",
12
12
  "@yuants/transfer": "0.2.4",
13
- "@yuants/data-account": "0.3.3",
13
+ "@yuants/data-account": "0.3.4",
14
14
  "@yuants/data-order": "0.2.1",
15
15
  "@yuants/utils": "0.5.0",
16
16
  "@yuants/sql": "0.8.3",
package/temp/image-tag CHANGED
@@ -1 +1 @@
1
- sha-8e0b6ef6a5e54de9f49b72355c2845da5cb055a5
1
+ sha-d95979d74a67e3acd2992d0122adf5416f2953aa
@@ -1,6 +1,6 @@
1
1
  {
2
- "apps/vendor-okx/CHANGELOG.json": "065eafb1c1ec13c2d594c989bc205c94a874e9ce",
3
- "apps/vendor-okx/CHANGELOG.md": "1d22611d9bf572cbd8f0f7f901dcb555b4a14192",
2
+ "apps/vendor-okx/CHANGELOG.json": "87a6ba0fc2a5e991bf0e1effc2c23e42438992e2",
3
+ "apps/vendor-okx/CHANGELOG.md": "405b54aa42cb886fc0c9b5a441695bad90b5d95a",
4
4
  "apps/vendor-okx/README.md": "ac3d1f6c4c8d73066664699b0bb1b01db02e67a2",
5
5
  "apps/vendor-okx/api-extractor.json": "62f4fd324425b9a235f0c117975967aab09ced0c",
6
6
  "apps/vendor-okx/build/Dockerfile": "e625b9593e496c343297c9ce133d6cc4be9617a7",
@@ -8,8 +8,8 @@
8
8
  "apps/vendor-okx/config/rig.json": "f6c7b5537dc77a3170ba9f008bae3b6c3ee11956",
9
9
  "apps/vendor-okx/config/typescript.json": "854907e8a821f2050f6533368db160c649c25348",
10
10
  "apps/vendor-okx/etc/vendor-okx.api.md": "92c54ccd547d2ad81ca6088158ca193e85c208df",
11
- "apps/vendor-okx/package.json": "38623ab0fceb6799e6363732a5669cb9dd647648",
12
- "apps/vendor-okx/src/account.ts": "ee25dbdc249b056856d64d3a67e654d8aa2dc53b",
11
+ "apps/vendor-okx/package.json": "2fa5993fee4ef8b59320546bfa9c1dd4593f3ed2",
12
+ "apps/vendor-okx/src/account.ts": "7e8bb44845269a401a850aff2e5bf170b0811bd8",
13
13
  "apps/vendor-okx/src/api.ts": "0433a359ec1fb84c866c1875b630cbb861696533",
14
14
  "apps/vendor-okx/src/cli.ts": "9bf6b5559a6c6f33da20e74cc6c5d702c60ec891",
15
15
  "apps/vendor-okx/src/cluster.ts": "9971d8d5e6af16e09d2d1fe5e9f05ccee87281e8",
@@ -18,16 +18,16 @@
18
18
  "apps/vendor-okx/src/interest_rate.ts": "da173624ffed4cb083d797328174fe717809769c",
19
19
  "apps/vendor-okx/src/legacy_index.ts": "19f9e4af4b6b8ae846e0800b1bd52d5eeaa8a2ce",
20
20
  "apps/vendor-okx/src/logger.ts": "d25d427e74f46819a601e0cb17d5358b22c8db7b",
21
- "apps/vendor-okx/src/ohlc.ts": "ffbe2feadd43befb925984fdc704d00527717e92",
21
+ "apps/vendor-okx/src/ohlc.ts": "0b8c9b32238a4e26954dbd5360e129b3850df1ae",
22
22
  "apps/vendor-okx/src/order.ts": "09509ae5af4b212cc1c99c55c436b3b0ce8bda33",
23
23
  "apps/vendor-okx/src/product.ts": "a2e7feb86bc01fe3b1af6dcb9a9ebfba6be91434",
24
24
  "apps/vendor-okx/src/quote.ts": "b8dd019e6cceb50e486a9d41e54385bda4e21ab0",
25
- "apps/vendor-okx/src/websocket.ts": "7a22838b75247cdc3da90c8601100b6e8b34a350",
25
+ "apps/vendor-okx/src/websocket.ts": "59791907faef7adf2d1d4b6876cc6a476b5f7e3e",
26
26
  "apps/vendor-okx/tsconfig.json": "81da8f78196974b5d15da0edb6b2d9f48641063c",
27
27
  "apps/vendor-okx/.rush/temp/shrinkwrap-deps.json": "87df045a10e829fd62d50013da30fd929442d54c",
28
28
  "libraries/protocol/temp/package-deps.json": "d326f24867d9ae3fef16ff84ba537eabb7c8adf0",
29
29
  "libraries/transfer/temp/package-deps.json": "cb2786055e5e79cd7d9d0f5787e300ca1c0bf002",
30
- "libraries/data-account/temp/package-deps.json": "7fcf0ee1dd95c391d26c35e9c380a0fe3636c004",
30
+ "libraries/data-account/temp/package-deps.json": "0e5bd33f40288d3bc2a3b32cf51188a346a8dd34",
31
31
  "libraries/data-order/temp/package-deps.json": "58c4daa9bd7c6420bbf92efd003aa311ade858ae",
32
32
  "libraries/utils/temp/package-deps.json": "c747617da1c0a40843603c62a7477da641ff0aef",
33
33
  "libraries/sql/temp/package-deps.json": "1cb2248873d01ed732e8473f7b242a41f987fdd7",