@gbozee/ultimate 0.0.2-next.72 → 0.0.2-next.74

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.
@@ -75757,6 +75757,21 @@ class BaseExchange {
75757
75757
  });
75758
75758
  }
75759
75759
  }
75760
+ async previewSpotMarginHedge(_payload) {
75761
+ throw new Error("Spot margin hedge preview is not supported on this exchange");
75762
+ }
75763
+ async previewFuturesReplay(_payload) {
75764
+ throw new Error("Futures replay preview is not supported on this exchange");
75765
+ }
75766
+ async placeSpotMarginHedge(_payload) {
75767
+ throw new Error("Spot margin hedge placement is not supported on this exchange");
75768
+ }
75769
+ async placeFuturesReplay(_payload) {
75770
+ throw new Error("Futures replay placement is not supported on this exchange");
75771
+ }
75772
+ async placeFuturesExactTrade(_payload) {
75773
+ throw new Error("Futures exact trade placement is not supported on this exchange");
75774
+ }
75760
75775
  async placeSpotLimitOrders(_payload) {}
75761
75776
  async buildCumulative(payload) {
75762
75777
  const {
@@ -76164,6 +76179,389 @@ class BaseExchange {
76164
76179
 
76165
76180
  // src/exchanges/binance/index.ts
76166
76181
  var import_p_limit = __toESM(require_p_limit(), 1);
76182
+
76183
+ // src/exchanges/binance/futures-replay.ts
76184
+ function roundNumber(value2, digits = 8) {
76185
+ return Number(value2.toFixed(digits));
76186
+ }
76187
+ function parseNumber2(value2) {
76188
+ if (value2 === undefined || value2 === null || value2 === "")
76189
+ return;
76190
+ const parsed = Number(value2);
76191
+ if (!Number.isFinite(parsed) || parsed <= 0)
76192
+ return;
76193
+ return roundNumber(parsed);
76194
+ }
76195
+ function normalizeOrderInput(order) {
76196
+ const stop = parseNumber2(order.stop) ?? parseNumber2(order.stopPrice) ?? parseNumber2(order.triggerPrice);
76197
+ const type = order.type ? order.type.toUpperCase() : undefined;
76198
+ const quantity = order.quantity ?? order.qty ?? 0;
76199
+ return {
76200
+ price: roundNumber(Number(order.price)),
76201
+ quantity: roundNumber(Number(quantity)),
76202
+ ...stop ? { stop } : {},
76203
+ ...type && type !== "LIMIT" ? { type } : {}
76204
+ };
76205
+ }
76206
+ function sortReplayOrders(orders) {
76207
+ return [...orders].sort((left, right) => {
76208
+ const leftTrigger = left.stop ?? left.price;
76209
+ const rightTrigger = right.stop ?? right.price;
76210
+ if (leftTrigger !== rightTrigger)
76211
+ return leftTrigger - rightTrigger;
76212
+ if (left.price !== right.price)
76213
+ return left.price - right.price;
76214
+ return left.quantity - right.quantity;
76215
+ });
76216
+ }
76217
+ function buildReplayStopLimitPrice(payload) {
76218
+ const spread = payload.stop > 30000 ? 1.00005 : 1.0002;
76219
+ return roundNumber(payload.kind === "long" ? payload.stop * spread ** -1 : payload.stop * spread);
76220
+ }
76221
+ function normalizeRequestedEntryOrders(orders) {
76222
+ return sortReplayOrders(orders.map((order) => ({
76223
+ price: roundNumber(Number(order.price)),
76224
+ quantity: roundNumber(Number(order.quantity))
76225
+ })));
76226
+ }
76227
+ function normalizeRequestedStopOrders(payload) {
76228
+ return sortReplayOrders(payload.orders.map((order) => {
76229
+ const stop = parseNumber2(order.stop) ?? roundNumber(Number(order.price));
76230
+ return {
76231
+ price: order.price !== undefined ? roundNumber(Number(order.price)) : buildReplayStopLimitPrice({
76232
+ kind: payload.kind,
76233
+ stop
76234
+ }),
76235
+ quantity: roundNumber(Number(order.quantity)),
76236
+ stop,
76237
+ type: (order.type || "STOP").toUpperCase()
76238
+ };
76239
+ }));
76240
+ }
76241
+ function normalizeRequestedTakeProfitOrders(orders) {
76242
+ return sortReplayOrders(orders.map((order) => {
76243
+ const stop = parseNumber2(order.stop);
76244
+ const type = order.type?.toUpperCase();
76245
+ return {
76246
+ price: roundNumber(Number(order.price)),
76247
+ quantity: roundNumber(Number(order.quantity)),
76248
+ ...stop ? { stop } : {},
76249
+ ...type ? { type } : stop ? { type: "TAKE_PROFIT" } : {}
76250
+ };
76251
+ }));
76252
+ }
76253
+ function buildOrderKey(order) {
76254
+ return [
76255
+ order.type || "LIMIT",
76256
+ order.price,
76257
+ order.quantity,
76258
+ order.stop ?? ""
76259
+ ].join(":");
76260
+ }
76261
+ function buildComparisonBucket(options) {
76262
+ const expected = sortReplayOrders(options.expected);
76263
+ const actual = sortReplayOrders(options.actual);
76264
+ const expected_keys = expected.map(buildOrderKey);
76265
+ const actual_keys = actual.map(buildOrderKey);
76266
+ return {
76267
+ expected,
76268
+ actual,
76269
+ missing: expected_keys.filter((key) => !actual_keys.includes(key)),
76270
+ extra: actual_keys.filter((key) => !expected_keys.includes(key))
76271
+ };
76272
+ }
76273
+ function buildFuturesReplaySnapshot(payload) {
76274
+ const entry_side = payload.kind === "long" ? "buy" : "sell";
76275
+ const close_side = payload.kind === "long" ? "sell" : "buy";
76276
+ const entry_orders = [];
76277
+ const stop_orders = [];
76278
+ const take_profit_orders = [];
76279
+ for (const order of payload.open_orders) {
76280
+ if (order.kind !== payload.kind)
76281
+ continue;
76282
+ const normalized_order = normalizeOrderInput(order);
76283
+ const type = (normalized_order.type || "LIMIT").toUpperCase();
76284
+ if (order.side === entry_side) {
76285
+ entry_orders.push(normalized_order);
76286
+ continue;
76287
+ }
76288
+ if (order.side !== close_side)
76289
+ continue;
76290
+ if (type.includes("TAKE_PROFIT") || normalized_order.stop === undefined) {
76291
+ take_profit_orders.push(normalized_order);
76292
+ continue;
76293
+ }
76294
+ stop_orders.push(normalized_order);
76295
+ }
76296
+ return {
76297
+ symbol: payload.symbol,
76298
+ kind: payload.kind,
76299
+ current_price: payload.current_price,
76300
+ position: payload.position,
76301
+ open_orders: payload.open_orders.filter((order) => order.kind === payload.kind),
76302
+ entry_orders: sortReplayOrders(entry_orders),
76303
+ stop_orders: sortReplayOrders(stop_orders),
76304
+ take_profit_orders: sortReplayOrders(take_profit_orders)
76305
+ };
76306
+ }
76307
+ function inferFuturesReplayInputFromLiveOrders(payload) {
76308
+ const snapshot = buildFuturesReplaySnapshot(payload);
76309
+ return {
76310
+ symbol: payload.symbol,
76311
+ kind: payload.kind,
76312
+ current_price: payload.current_price,
76313
+ position: payload.position,
76314
+ entry_orders: snapshot.entry_orders,
76315
+ stop_orders: snapshot.stop_orders,
76316
+ take_profit_orders: snapshot.take_profit_orders
76317
+ };
76318
+ }
76319
+ function compareFuturesReplayOrders(payload) {
76320
+ const entry = buildComparisonBucket({
76321
+ expected: payload.entry_orders,
76322
+ actual: payload.snapshot.entry_orders
76323
+ });
76324
+ const stop = buildComparisonBucket({
76325
+ expected: payload.stop_orders,
76326
+ actual: payload.snapshot.stop_orders
76327
+ });
76328
+ const take_profit = buildComparisonBucket({
76329
+ expected: payload.take_profit_orders,
76330
+ actual: payload.snapshot.take_profit_orders
76331
+ });
76332
+ return {
76333
+ matches: entry.missing.length === 0 && entry.extra.length === 0 && stop.missing.length === 0 && stop.extra.length === 0 && take_profit.missing.length === 0 && take_profit.extra.length === 0,
76334
+ entry,
76335
+ stop,
76336
+ take_profit
76337
+ };
76338
+ }
76339
+ function buildFuturesReplayPlan(payload) {
76340
+ return {
76341
+ symbol: payload.symbol,
76342
+ kind: payload.kind,
76343
+ orders_to_cancel: payload.snapshot.open_orders,
76344
+ orders_to_create: {
76345
+ entry: sortReplayOrders(payload.entry_orders),
76346
+ stop: sortReplayOrders(payload.stop_orders),
76347
+ take_profit: sortReplayOrders(payload.take_profit_orders)
76348
+ }
76349
+ };
76350
+ }
76351
+ function normalizeFuturesReplayInput(payload) {
76352
+ return {
76353
+ entry_orders: normalizeRequestedEntryOrders(payload.entry_orders),
76354
+ stop_orders: normalizeRequestedStopOrders({
76355
+ kind: payload.kind,
76356
+ orders: payload.stop_orders
76357
+ }),
76358
+ take_profit_orders: normalizeRequestedTakeProfitOrders(payload.take_profit_orders)
76359
+ };
76360
+ }
76361
+
76362
+ // src/exchanges/binance/spot-margin-hedge.ts
76363
+ function roundNumber2(value2, digits = 8) {
76364
+ return Number(value2.toFixed(digits));
76365
+ }
76366
+ var STOP_LIMIT_SPREAD_PERCENT = 0.0005;
76367
+ function normalizeOrderInput2(order) {
76368
+ return {
76369
+ price: roundNumber2(order.price),
76370
+ quantity: roundNumber2(order.quantity)
76371
+ };
76372
+ }
76373
+ function sortOrderInputs(orders) {
76374
+ return [...orders].map(normalizeOrderInput2).sort((left, right) => left.price - right.price);
76375
+ }
76376
+ function normalizeOrderForComparison(order) {
76377
+ const type = (order.type || "limit").toLowerCase();
76378
+ const raw_stop = "stop" in order && order.stop !== undefined ? order.stop : ("stopPrice" in order) ? order.stopPrice : undefined;
76379
+ const parsed_stop = raw_stop === undefined || raw_stop === null || raw_stop === "" ? undefined : Number(raw_stop);
76380
+ return {
76381
+ side: order.side,
76382
+ type,
76383
+ price: roundNumber2(order.price),
76384
+ quantity: roundNumber2(order.quantity),
76385
+ stop: parsed_stop !== undefined && Number.isFinite(parsed_stop) && parsed_stop > 0 ? roundNumber2(parsed_stop) : undefined
76386
+ };
76387
+ }
76388
+ function normalizeOrders(orders) {
76389
+ return [...orders].map(normalizeOrderForComparison).sort((left, right) => {
76390
+ if (left.side !== right.side)
76391
+ return left.side.localeCompare(right.side);
76392
+ if (left.type !== right.type)
76393
+ return left.type.localeCompare(right.type);
76394
+ if (left.price !== right.price)
76395
+ return left.price - right.price;
76396
+ return left.quantity - right.quantity;
76397
+ });
76398
+ }
76399
+ function toPlannedOrders(orders) {
76400
+ return normalizeOrders(orders).map((order) => ({
76401
+ side: order.side,
76402
+ type: order.type,
76403
+ price: order.price,
76404
+ quantity: order.quantity,
76405
+ stop: order.stop
76406
+ }));
76407
+ }
76408
+ function buildOrderKey2(order) {
76409
+ return [
76410
+ order.side,
76411
+ order.type,
76412
+ order.price,
76413
+ order.quantity,
76414
+ order.stop ?? ""
76415
+ ].join(":");
76416
+ }
76417
+ function buildDiff(options) {
76418
+ const expectedKeys = options.expected.map(buildOrderKey2);
76419
+ const actualKeys = options.actual.map(buildOrderKey2);
76420
+ return {
76421
+ missing: expectedKeys.filter((key) => !actualKeys.includes(key)),
76422
+ extra: actualKeys.filter((key) => !expectedKeys.includes(key))
76423
+ };
76424
+ }
76425
+ function getQuoteAsset(symbol) {
76426
+ const quoteAssets = ["USDT", "USDC", "BUSD", "BTC", "ETH"];
76427
+ const target = quoteAssets.find((asset) => symbol.endsWith(asset));
76428
+ return target || symbol.slice(-4);
76429
+ }
76430
+ function sumNotional(orders) {
76431
+ return roundNumber2(orders.reduce((sum, order) => sum + order.price * order.quantity, 0), 8);
76432
+ }
76433
+ function sumQuantity(orders) {
76434
+ return roundNumber2(orders.reduce((sum, order) => sum + order.quantity, 0), 8);
76435
+ }
76436
+ function buildLongOrderPlacement(options) {
76437
+ const { order, current_price } = options;
76438
+ if (order.price > current_price) {
76439
+ return {
76440
+ side: "buy",
76441
+ type: "stop_loss_limit",
76442
+ price: order.price,
76443
+ stop: roundNumber2(order.price * (1 - STOP_LIMIT_SPREAD_PERCENT)),
76444
+ quantity: order.quantity
76445
+ };
76446
+ }
76447
+ return {
76448
+ side: "buy",
76449
+ type: "limit",
76450
+ price: order.price,
76451
+ quantity: order.quantity
76452
+ };
76453
+ }
76454
+ function inferSpotMarginHedgeInputFromLiveOrders(options) {
76455
+ const long_orders = sortOrderInputs([
76456
+ ...options.spot_orders.filter((order) => order.side === "buy").map((order) => ({
76457
+ price: order.price,
76458
+ quantity: order.quantity
76459
+ })),
76460
+ ...options.margin_orders.filter((order) => order.side === "buy").map((order) => ({
76461
+ price: order.price,
76462
+ quantity: order.quantity
76463
+ }))
76464
+ ]);
76465
+ const short_orders = sortOrderInputs([
76466
+ ...options.spot_orders.filter((order) => order.side === "sell").map((order) => ({
76467
+ price: order.price,
76468
+ quantity: order.quantity
76469
+ })),
76470
+ ...options.margin_orders.filter((order) => order.side === "sell").map((order) => ({
76471
+ price: order.price,
76472
+ quantity: order.quantity
76473
+ }))
76474
+ ]);
76475
+ return {
76476
+ symbol: options.symbol,
76477
+ margin_type: options.margin_type,
76478
+ borrow_amount: sumNotional(long_orders),
76479
+ long_orders,
76480
+ short_orders,
76481
+ placement_overrides: {
76482
+ spot: toPlannedOrders(options.spot_orders),
76483
+ margin: toPlannedOrders(options.margin_orders)
76484
+ }
76485
+ };
76486
+ }
76487
+ function buildSpotMarginHedgePlan(options) {
76488
+ const max_spot_buy_orders = options.max_spot_buy_orders ?? 5;
76489
+ const long_orders = sortOrderInputs(options.long_orders);
76490
+ const short_orders = sortOrderInputs(options.short_orders);
76491
+ const spot_long_orders = long_orders.slice(0, max_spot_buy_orders);
76492
+ const margin_long_orders = long_orders.slice(max_spot_buy_orders);
76493
+ const default_spot_orders_to_create = [
76494
+ ...spot_long_orders.map((order) => buildLongOrderPlacement({
76495
+ order,
76496
+ current_price: options.current_price
76497
+ })),
76498
+ ...short_orders.map((order) => ({
76499
+ side: "sell",
76500
+ type: "limit",
76501
+ price: order.price,
76502
+ quantity: order.quantity
76503
+ }))
76504
+ ];
76505
+ const default_margin_orders_to_create = margin_long_orders.map((order) => buildLongOrderPlacement({
76506
+ order,
76507
+ current_price: options.current_price
76508
+ }));
76509
+ const spot_orders_to_create = options.placement_overrides?.spot || default_spot_orders_to_create;
76510
+ const margin_orders_to_create = options.placement_overrides?.margin || default_margin_orders_to_create;
76511
+ const borrow_delta = roundNumber2(Math.max(0, options.borrow_amount - options.snapshot.borrowed_quote_amount));
76512
+ const spot_long_notional = sumNotional(spot_long_orders);
76513
+ const transfer_to_spot_amount = roundNumber2(Math.max(0, spot_long_notional - options.snapshot.spot_quote_free));
76514
+ const short_quantity = sumQuantity(short_orders);
76515
+ const transfer_base_to_spot_amount = roundNumber2(Math.max(0, short_quantity - options.snapshot.spot_base_free));
76516
+ return {
76517
+ borrow_amount: options.borrow_amount,
76518
+ borrow_delta,
76519
+ transfer_to_spot_amount,
76520
+ transfer_to_spot_asset: getQuoteAsset(options.symbol),
76521
+ transfer_base_to_spot_amount,
76522
+ orders_to_cancel: {
76523
+ spot: options.snapshot.spot_orders,
76524
+ margin: options.snapshot.margin_orders
76525
+ },
76526
+ orders_to_create: {
76527
+ spot: spot_orders_to_create,
76528
+ margin: margin_orders_to_create
76529
+ },
76530
+ normalized: {
76531
+ spot: normalizeOrders(spot_orders_to_create),
76532
+ margin: normalizeOrders(margin_orders_to_create)
76533
+ }
76534
+ };
76535
+ }
76536
+ function compareSpotMarginHedgeOrders(options) {
76537
+ const actualSpot = normalizeOrders(options.spot_orders);
76538
+ const actualMargin = normalizeOrders(options.margin_orders);
76539
+ const spotDiff = buildDiff({
76540
+ expected: options.plan.normalized.spot,
76541
+ actual: actualSpot
76542
+ });
76543
+ const marginDiff = buildDiff({
76544
+ expected: options.plan.normalized.margin,
76545
+ actual: actualMargin
76546
+ });
76547
+ return {
76548
+ matches: spotDiff.missing.length === 0 && spotDiff.extra.length === 0 && marginDiff.missing.length === 0 && marginDiff.extra.length === 0,
76549
+ spot: {
76550
+ expected: options.plan.normalized.spot,
76551
+ actual: actualSpot,
76552
+ missing: spotDiff.missing,
76553
+ extra: spotDiff.extra
76554
+ },
76555
+ margin: {
76556
+ expected: options.plan.normalized.margin,
76557
+ actual: actualMargin,
76558
+ missing: marginDiff.missing,
76559
+ extra: marginDiff.extra
76560
+ }
76561
+ };
76562
+ }
76563
+
76564
+ // src/exchanges/binance/index.ts
76167
76565
  var CONSTANTS = {
76168
76566
  SPOT_TO_FIAT: "MAIN_C2C",
76169
76567
  SPOT_TO_USDT_FUTURE: "MAIN_UMFUTURE",
@@ -76652,12 +77050,20 @@ async function getCrossMarginAccountDetails(client) {
76652
77050
  async function cancelMarginOrders(client, symbol, isIsolated = false, orders) {
76653
77051
  try {
76654
77052
  if (orders && orders.length > 0) {
76655
- const cancelPromises = orders.map((order) => client.marginAccountCancelOrder({
76656
- symbol,
76657
- orderId: order.orderId,
76658
- origClientOrderId: order.origClientOrderId,
76659
- isIsolated: isIsolated ? "TRUE" : "FALSE"
76660
- }));
77053
+ const cancelPromises = orders.map((order) => {
77054
+ const payload = {
77055
+ symbol,
77056
+ isIsolated: isIsolated ? "TRUE" : "FALSE"
77057
+ };
77058
+ if (order.orderId !== undefined) {
77059
+ payload.orderId = order.orderId;
77060
+ }
77061
+ const origClientOrderId = order.origClientOrderId ?? order.clientOrderId;
77062
+ if (origClientOrderId !== undefined) {
77063
+ payload.origClientOrderId = origClientOrderId;
77064
+ }
77065
+ return client.marginAccountCancelOrder(payload);
77066
+ });
76661
77067
  return await Promise.all(cancelPromises);
76662
77068
  } else {
76663
77069
  return await client.marginAccountCancelOpenOrders({
@@ -76912,7 +77318,7 @@ async function placeStopOrder(client, payload) {
76912
77318
  });
76913
77319
  }
76914
77320
  }
76915
- const spread = 1.00005;
77321
+ const spread = payload.stop > 30000 ? 1.00005 : 1.0002;
76916
77322
  const order = {
76917
77323
  kind: payload.kind,
76918
77324
  side: payload.kind === "long" ? "sell" : "buy",
@@ -77319,6 +77725,16 @@ async function getAllOpenOrders(payload) {
77319
77725
  const response = await client.getAllOpenOrders();
77320
77726
  return response;
77321
77727
  }
77728
+ function getPrintfFormatFromStepSize(stepSize, fallback) {
77729
+ if (stepSize === undefined || stepSize === null) {
77730
+ return fallback;
77731
+ }
77732
+ const normalized = String(stepSize);
77733
+ const [, decimalPart = ""] = normalized.split(".");
77734
+ const trimmed = decimalPart.replace(/0+$/, "");
77735
+ const digits = trimmed.length;
77736
+ return `%.${digits}f`;
77737
+ }
77322
77738
 
77323
77739
  class BinanceExchange extends BaseExchange {
77324
77740
  main_client;
@@ -77755,6 +78171,54 @@ class BinanceExchange extends BaseExchange {
77755
78171
  throw error;
77756
78172
  }
77757
78173
  }
78174
+ async getFuturesSymbolFormats(symbol) {
78175
+ if (typeof this.client.getExchangeInfo !== "function") {
78176
+ return {
78177
+ price_places: "%.8f",
78178
+ decimal_places: "%.8f"
78179
+ };
78180
+ }
78181
+ try {
78182
+ const exchange_info = await this.client.getExchangeInfo();
78183
+ const symbol_info = exchange_info?.symbols?.find((item) => item.symbol === symbol.toUpperCase());
78184
+ const price_filter = symbol_info?.filters?.find((filter) => filter.filterType === "PRICE_FILTER");
78185
+ const lot_size_filter = symbol_info?.filters?.find((filter) => filter.filterType === "LOT_SIZE");
78186
+ return {
78187
+ price_places: getPrintfFormatFromStepSize(price_filter?.tickSize, "%.8f"),
78188
+ decimal_places: getPrintfFormatFromStepSize(lot_size_filter?.stepSize, "%.8f")
78189
+ };
78190
+ } catch (error) {
78191
+ console.error("Error fetching futures symbol formats:", error);
78192
+ return {
78193
+ price_places: "%.8f",
78194
+ decimal_places: "%.8f"
78195
+ };
78196
+ }
78197
+ }
78198
+ async getSpotSymbolFormats(symbol) {
78199
+ if (!this.main_client || typeof this.main_client.getExchangeInfo !== "function") {
78200
+ return {
78201
+ price_places: "%.8f",
78202
+ decimal_places: "%.8f"
78203
+ };
78204
+ }
78205
+ try {
78206
+ const exchange_info = await this.main_client.getExchangeInfo();
78207
+ const symbol_info = exchange_info?.symbols?.find((item) => item.symbol === symbol.toUpperCase());
78208
+ const price_filter = symbol_info?.filters?.find((filter) => filter.filterType === "PRICE_FILTER");
78209
+ const lot_size_filter = symbol_info?.filters?.find((filter) => filter.filterType === "LOT_SIZE");
78210
+ return {
78211
+ price_places: getPrintfFormatFromStepSize(price_filter?.tickSize, "%.8f"),
78212
+ decimal_places: getPrintfFormatFromStepSize(lot_size_filter?.stepSize, "%.8f")
78213
+ };
78214
+ } catch (error) {
78215
+ console.error("Error fetching spot symbol formats:", error);
78216
+ return {
78217
+ price_places: "%.8f",
78218
+ decimal_places: "%.8f"
78219
+ };
78220
+ }
78221
+ }
77758
78222
  async createMarginLimitOrders(payload) {
77759
78223
  if (!this.main_client) {
77760
78224
  throw new Error("Main client not available for margin trading");
@@ -77785,6 +78249,317 @@ class BinanceExchange extends BaseExchange {
77785
78249
  }
77786
78250
  return await getMarginOpenOrders(this.main_client, symbol, isIsolated);
77787
78251
  }
78252
+ async getFuturesReplaySnapshot(payload) {
78253
+ const symbol = payload.symbol.toUpperCase();
78254
+ const [open_orders, position_info, current_price] = await Promise.all([
78255
+ this.getOpenOrders({ symbol }),
78256
+ this.getPositionInfo(symbol),
78257
+ this.getCurrentPrice(symbol)
78258
+ ]);
78259
+ return buildFuturesReplaySnapshot({
78260
+ symbol,
78261
+ kind: payload.kind,
78262
+ current_price,
78263
+ position: {
78264
+ size: position_info[payload.kind]?.size || 0
78265
+ },
78266
+ open_orders
78267
+ });
78268
+ }
78269
+ async previewFuturesReplay(payload) {
78270
+ const snapshot = payload.snapshot ? {
78271
+ ...payload.snapshot,
78272
+ current_price: payload.snapshot.current_price ?? await this.getCurrentPrice(payload.symbol)
78273
+ } : await this.getFuturesReplaySnapshot({
78274
+ symbol: payload.symbol,
78275
+ kind: payload.kind
78276
+ });
78277
+ const shouldInferEntryOrders = !payload.entry_orders || payload.entry_orders.length === 0;
78278
+ const shouldInferStopOrders = !payload.stop_orders || payload.stop_orders.length === 0;
78279
+ const shouldInferTakeProfitOrders = !payload.take_profit_orders || payload.take_profit_orders.length === 0;
78280
+ const inferred_input = shouldInferEntryOrders || shouldInferStopOrders || shouldInferTakeProfitOrders ? inferFuturesReplayInputFromLiveOrders({
78281
+ symbol: payload.symbol,
78282
+ kind: payload.kind,
78283
+ current_price: snapshot.current_price,
78284
+ position: snapshot.position,
78285
+ open_orders: snapshot.open_orders
78286
+ }) : undefined;
78287
+ const explicit_input = normalizeFuturesReplayInput({
78288
+ kind: payload.kind,
78289
+ entry_orders: payload.entry_orders || [],
78290
+ stop_orders: payload.stop_orders || [],
78291
+ take_profit_orders: payload.take_profit_orders || []
78292
+ });
78293
+ const entry_orders = shouldInferEntryOrders ? inferred_input?.entry_orders || [] : explicit_input.entry_orders;
78294
+ const stop_orders = shouldInferStopOrders ? inferred_input?.stop_orders || [] : explicit_input.stop_orders;
78295
+ const take_profit_orders = shouldInferTakeProfitOrders ? inferred_input?.take_profit_orders || [] : explicit_input.take_profit_orders;
78296
+ const requested_input = {
78297
+ symbol: payload.symbol,
78298
+ kind: payload.kind,
78299
+ entry_orders,
78300
+ stop_orders,
78301
+ take_profit_orders
78302
+ };
78303
+ const plan = buildFuturesReplayPlan({
78304
+ ...requested_input,
78305
+ snapshot
78306
+ });
78307
+ const comparison = compareFuturesReplayOrders({
78308
+ ...requested_input,
78309
+ snapshot
78310
+ });
78311
+ return {
78312
+ snapshot,
78313
+ inferred_input,
78314
+ requested_input,
78315
+ plan,
78316
+ comparison
78317
+ };
78318
+ }
78319
+ async placeFuturesReplay(payload) {
78320
+ const preview = await this.previewFuturesReplay(payload);
78321
+ if (!payload.place) {
78322
+ return preview;
78323
+ }
78324
+ const symbol = payload.symbol.toUpperCase();
78325
+ await cancelAllOrders(this.client, symbol, {
78326
+ kind: payload.kind
78327
+ });
78328
+ const refreshed_snapshot = await this.getFuturesReplaySnapshot({
78329
+ symbol,
78330
+ kind: payload.kind
78331
+ });
78332
+ const execution_plan = buildFuturesReplayPlan({
78333
+ symbol,
78334
+ kind: payload.kind,
78335
+ entry_orders: preview.requested_input.entry_orders,
78336
+ stop_orders: preview.requested_input.stop_orders,
78337
+ take_profit_orders: preview.requested_input.take_profit_orders,
78338
+ snapshot: refreshed_snapshot
78339
+ });
78340
+ const { price_places, decimal_places } = await this.getFuturesSymbolFormats(symbol);
78341
+ const entry_orders = execution_plan.orders_to_create.entry.map((order) => ({
78342
+ ...order,
78343
+ side: payload.kind === "long" ? "buy" : "sell",
78344
+ kind: payload.kind
78345
+ }));
78346
+ const stop_orders = execution_plan.orders_to_create.stop.map((order) => ({
78347
+ ...order,
78348
+ side: payload.kind === "long" ? "sell" : "buy",
78349
+ kind: payload.kind,
78350
+ type: order.type || "STOP"
78351
+ }));
78352
+ const take_profit_orders = execution_plan.orders_to_create.take_profit.map((order) => ({
78353
+ ...order,
78354
+ side: payload.kind === "long" ? "sell" : "buy",
78355
+ kind: payload.kind,
78356
+ ...order.stop ? { type: order.type || "TAKE_PROFIT" } : {}
78357
+ }));
78358
+ const execution = entry_orders.length > 0 || stop_orders.length > 0 || take_profit_orders.length > 0 ? await createLimitPurchaseOrdersParallel(this.client, symbol, price_places, decimal_places, [...entry_orders, ...stop_orders, ...take_profit_orders]) : [];
78359
+ return {
78360
+ ...preview,
78361
+ execution_plan,
78362
+ refreshed_snapshot,
78363
+ execution
78364
+ };
78365
+ }
78366
+ async placeFuturesExactTrade(payload) {
78367
+ return await this.placeFuturesReplay(payload);
78368
+ }
78369
+ async getSpotMarginHedgeSnapshot(payload) {
78370
+ if (!this.main_client) {
78371
+ throw new Error("Main client not available for spot and margin trading");
78372
+ }
78373
+ if (payload.margin_type !== "isolated") {
78374
+ throw new Error(`Unsupported margin type: ${payload.margin_type}. Only isolated is supported for replay right now.`);
78375
+ }
78376
+ const symbol = payload.symbol.toUpperCase();
78377
+ const quoteAssets = ["USDT", "USDC", "BUSD", "BTC", "ETH"];
78378
+ const quoteAsset = quoteAssets.find((asset) => symbol.endsWith(asset)) || symbol.slice(-4);
78379
+ const baseAsset = symbol.slice(0, symbol.length - quoteAsset.length);
78380
+ const [spot_orders, margin_orders, spot_balances, isolated_margin_position] = await Promise.all([
78381
+ this.getSpotOpenOrders(symbol),
78382
+ this.getMarginOpenOrders(symbol, true),
78383
+ this.getSpotBalances([baseAsset, quoteAsset]),
78384
+ this.getIsolatedMarginPosition(symbol)
78385
+ ]);
78386
+ const current_price = await this.getSpotCurrentPrice(symbol);
78387
+ const spot_quote_balance = spot_balances.find((balance) => balance.asset.toUpperCase() === quoteAsset);
78388
+ const spot_base_balance = spot_balances.find((balance) => balance.asset.toUpperCase() === baseAsset);
78389
+ const isolated_asset = isolated_margin_position?.assets?.[0];
78390
+ const borrowed_quote_amount = parseFloat(isolated_asset?.quoteAsset?.borrowed?.toString?.() || "0");
78391
+ return {
78392
+ symbol,
78393
+ margin_type: payload.margin_type,
78394
+ quote_asset: quoteAsset,
78395
+ base_asset: baseAsset,
78396
+ borrowed_quote_amount,
78397
+ spot_quote_free: parseFloat(spot_quote_balance?.free?.toString?.() || "0"),
78398
+ spot_base_free: parseFloat(spot_base_balance?.free?.toString?.() || "0"),
78399
+ spot_orders,
78400
+ margin_orders,
78401
+ current_price,
78402
+ spot_balances,
78403
+ isolated_margin_position
78404
+ };
78405
+ }
78406
+ async previewSpotMarginHedge(payload) {
78407
+ const snapshot = payload.snapshot ? {
78408
+ ...payload.snapshot,
78409
+ current_price: payload.snapshot.current_price ?? await this.getSpotCurrentPrice(payload.symbol)
78410
+ } : await this.getSpotMarginHedgeSnapshot({
78411
+ symbol: payload.symbol,
78412
+ margin_type: payload.margin_type
78413
+ });
78414
+ const shouldInferLongOrders = !payload.long_orders || payload.long_orders.length === 0;
78415
+ const shouldInferShortOrders = !payload.short_orders || payload.short_orders.length === 0;
78416
+ const inferred_input = shouldInferLongOrders || shouldInferShortOrders ? inferSpotMarginHedgeInputFromLiveOrders({
78417
+ symbol: payload.symbol,
78418
+ margin_type: payload.margin_type,
78419
+ spot_orders: snapshot.spot_orders,
78420
+ margin_orders: snapshot.margin_orders
78421
+ }) : undefined;
78422
+ const long_orders = shouldInferLongOrders ? inferred_input?.long_orders || [] : payload.long_orders || [];
78423
+ const short_orders = shouldInferShortOrders ? inferred_input?.short_orders || [] : payload.short_orders || [];
78424
+ const borrow_amount = payload.borrow_amount ?? (long_orders.length > 0 ? sumNotional(long_orders) : inferred_input?.borrow_amount ?? 0);
78425
+ const requested_input = {
78426
+ borrow_amount,
78427
+ symbol: payload.symbol,
78428
+ margin_type: payload.margin_type,
78429
+ long_orders,
78430
+ short_orders,
78431
+ current_price: snapshot.current_price,
78432
+ max_spot_buy_orders: payload.max_spot_buy_orders,
78433
+ placement_overrides: inferred_input?.placement_overrides
78434
+ };
78435
+ const plan = buildSpotMarginHedgePlan({
78436
+ ...requested_input,
78437
+ snapshot
78438
+ });
78439
+ const comparison = compareSpotMarginHedgeOrders({
78440
+ plan,
78441
+ spot_orders: snapshot.spot_orders,
78442
+ margin_orders: snapshot.margin_orders
78443
+ });
78444
+ return {
78445
+ snapshot,
78446
+ inferred_input,
78447
+ requested_input,
78448
+ plan,
78449
+ comparison
78450
+ };
78451
+ }
78452
+ async placeSpotMarginHedge(payload) {
78453
+ const preview = await this.previewSpotMarginHedge(payload);
78454
+ if (!payload.place) {
78455
+ return preview;
78456
+ }
78457
+ if (!this.main_client) {
78458
+ throw new Error("Main client not available for spot and margin trading");
78459
+ }
78460
+ if (payload.margin_type !== "isolated") {
78461
+ throw new Error(`Unsupported margin type: ${payload.margin_type}. Only isolated placement is supported right now.`);
78462
+ }
78463
+ const symbol = payload.symbol.toUpperCase();
78464
+ const quoteAssets = ["USDT", "USDC", "BUSD", "BTC", "ETH"];
78465
+ const quote_asset = quoteAssets.find((asset) => symbol.endsWith(asset)) || symbol.slice(-4);
78466
+ const base_asset = symbol.slice(0, symbol.length - quote_asset.length);
78467
+ const requested_input = preview.requested_input;
78468
+ if (preview.snapshot.spot_orders.length > 0) {
78469
+ await this.cancelSpotOrders(symbol, preview.snapshot.spot_orders.map((order) => ({
78470
+ orderId: order.orderId || order.order_id || order.id,
78471
+ origClientOrderId: order.origClientOrderId
78472
+ })));
78473
+ }
78474
+ if (preview.snapshot.margin_orders.length > 0) {
78475
+ await this.cancelMarginOrders(symbol, true, preview.snapshot.margin_orders.map((order) => ({
78476
+ orderId: order.orderId || order.order_id || order.id,
78477
+ origClientOrderId: order.origClientOrderId
78478
+ })));
78479
+ }
78480
+ const refreshed_snapshot = await this.getSpotMarginHedgeSnapshot({
78481
+ symbol,
78482
+ margin_type: payload.margin_type
78483
+ });
78484
+ const symbol_formats = await this.getSpotSymbolFormats(symbol);
78485
+ const execution_plan = buildSpotMarginHedgePlan({
78486
+ borrow_amount: requested_input.borrow_amount,
78487
+ symbol: requested_input.symbol,
78488
+ margin_type: requested_input.margin_type,
78489
+ long_orders: requested_input.long_orders,
78490
+ short_orders: requested_input.short_orders,
78491
+ current_price: refreshed_snapshot.current_price,
78492
+ max_spot_buy_orders: requested_input.max_spot_buy_orders,
78493
+ snapshot: refreshed_snapshot
78494
+ });
78495
+ let borrow_result = null;
78496
+ if (execution_plan.borrow_delta > 0) {
78497
+ borrow_result = await this.main_client.submitMarginAccountBorrowRepay({
78498
+ asset: quote_asset,
78499
+ amount: execution_plan.borrow_delta,
78500
+ isIsolated: "TRUE",
78501
+ symbol,
78502
+ type: "BORROW"
78503
+ });
78504
+ }
78505
+ let quote_transfer_result = null;
78506
+ if (execution_plan.transfer_to_spot_amount > 0) {
78507
+ quote_transfer_result = await this.main_client.isolatedMarginAccountTransfer({
78508
+ asset: quote_asset,
78509
+ amount: execution_plan.transfer_to_spot_amount,
78510
+ symbol,
78511
+ transFrom: "ISOLATED_MARGIN",
78512
+ transTo: "SPOT"
78513
+ });
78514
+ }
78515
+ let base_transfer_result = null;
78516
+ if (execution_plan.transfer_base_to_spot_amount > 0) {
78517
+ base_transfer_result = await this.main_client.isolatedMarginAccountTransfer({
78518
+ asset: base_asset,
78519
+ amount: execution_plan.transfer_base_to_spot_amount,
78520
+ symbol,
78521
+ transFrom: "ISOLATED_MARGIN",
78522
+ transTo: "SPOT"
78523
+ });
78524
+ }
78525
+ const spot_result = execution_plan.orders_to_create.spot.length > 0 ? await this.createSpotLimitOrders({
78526
+ symbol,
78527
+ orders: execution_plan.orders_to_create.spot,
78528
+ price_places: symbol_formats.price_places,
78529
+ decimal_places: symbol_formats.decimal_places
78530
+ }) : [];
78531
+ const margin_result = execution_plan.orders_to_create.margin.length > 0 ? await this.createMarginLimitOrders({
78532
+ symbol,
78533
+ orders: execution_plan.orders_to_create.margin,
78534
+ isIsolated: true,
78535
+ price_places: symbol_formats.price_places,
78536
+ decimal_places: symbol_formats.decimal_places
78537
+ }) : [];
78538
+ return {
78539
+ ...preview,
78540
+ execution_plan,
78541
+ refreshed_snapshot,
78542
+ execution: {
78543
+ borrow: borrow_result ? {
78544
+ asset: quote_asset,
78545
+ amount: execution_plan.borrow_delta,
78546
+ response: borrow_result
78547
+ } : null,
78548
+ quote_transfer: quote_transfer_result ? {
78549
+ asset: quote_asset,
78550
+ amount: execution_plan.transfer_to_spot_amount,
78551
+ response: quote_transfer_result
78552
+ } : null,
78553
+ base_transfer: base_transfer_result ? {
78554
+ asset: base_asset,
78555
+ amount: execution_plan.transfer_base_to_spot_amount,
78556
+ response: base_transfer_result
78557
+ } : null,
78558
+ spot_orders: spot_result,
78559
+ margin_orders: margin_result
78560
+ }
78561
+ };
78562
+ }
77788
78563
  async getLastUserTrade(payload) {
77789
78564
  return await getLastUserTrade(this.client, payload.symbol);
77790
78565
  }