@blackcube/xgate-sdk 0.58.0 → 0.58.1

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/index.cjs CHANGED
@@ -1703,15 +1703,25 @@ exports.TradingService = class TradingService {
1703
1703
  * take-profit peut remplir en plusieurs fois. Seul l'historique porte le prix réellement obtenu, et
1704
1704
  * c'est lui qui doit servir au calcul du résultat — sinon le PnL affiché est une fiction.
1705
1705
  *
1706
- * On retient le **dernier** ordre de sortie rempli après `openedAt` : les précédents sont les
1707
- * take-profits partiels, celui-là est la clôture.
1706
+ * On lit les **exécutions** (`trades`), jamais les ordres : le prix d'un ordre est sa LIMITE — chez
1707
+ * hyperliquid, un market est un IOC borné à mark ± glissement, et son « prix » est cette borne, pas le
1708
+ * prix obtenu (BNB, 2026-10-01 : sortie lue à 844,44 pour des fills à 767,69). Le prix de sortie est la
1709
+ * moyenne des exécutions de sortie après `openedAt` — et avant `until` quand il est donné — pondérée par
1710
+ * leur taille : TP partiels et clôture compris, c'est le prix auquel la position est réellement sortie.
1708
1711
  */
1709
- async exitPrice(access, symbolXex, direction, openedAt) {
1712
+ async exitPrice(access, symbolXex, direction, openedAt, until) {
1710
1713
  const exit = direction === "long" ? "sell" : "buy";
1711
- const history = await this.perpOf(access).getHistory({ name: symbolXex });
1712
- const exits = history.filter((order) => order.name === symbolXex && order.side === exit).filter((order) => order.status === "filled" || order.status === "partiallyFilled").filter((order) => order.placedAt.getTime() > openedAt.getTime()).filter((order) => order.price !== null && Number(order.price) > 0).sort((left, right) => right.placedAt.getTime() - left.placedAt.getTime());
1713
- const last = exits[0];
1714
- return last === void 0 ? null : last.price;
1714
+ const fin = until === void 0 ? Number.POSITIVE_INFINITY : until.getTime();
1715
+ const fills = (await this.trades(access)).filter((fill) => fill.symbolXex === symbolXex && fill.side === exit).filter(
1716
+ (fill) => fill.filledAt.getTime() > openedAt.getTime() && fill.filledAt.getTime() <= fin
1717
+ ).filter((fill) => Number(fill.price) > 0 && Number(fill.size) > 0);
1718
+ const taille = fills.reduce((total, fill) => total + Number(fill.size), 0);
1719
+ if (taille === 0) {
1720
+ return null;
1721
+ }
1722
+ const moyenne = fills.reduce((total, fill) => total + Number(fill.price) * Number(fill.size), 0) / taille;
1723
+ const decimales = Math.max(...fills.map((fill) => (fill.price.split(".")[1] ?? "").length));
1724
+ return String(Number(moyenne.toFixed(decimales)));
1715
1725
  }
1716
1726
  /**
1717
1727
  * L'HISTORIQUE DES ORDRES du compte — ce qui a été soumis, rempli, annulé ou expiré.