@adaptic/utils 0.0.995 → 0.0.997
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 +38 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.mjs +38 -3
- package/dist/index.mjs.map +1 -1
- package/dist/types/alpaca/legacy/positions.d.ts.map +1 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -6945,8 +6945,38 @@ async function closePosition$1(auth, symbolOrAssetId, params) {
|
|
|
6945
6945
|
account: auth.adapticAccountId || "direct",
|
|
6946
6946
|
symbol: normalizedSymbol,
|
|
6947
6947
|
});
|
|
6948
|
-
|
|
6949
|
-
|
|
6948
|
+
try {
|
|
6949
|
+
await cancelOrder$1(auth, order.id);
|
|
6950
|
+
cancelledCount++;
|
|
6951
|
+
}
|
|
6952
|
+
catch (cancelError) {
|
|
6953
|
+
// Alpaca rejects DELETE on an order that is already in
|
|
6954
|
+
// pending_cancel / pending_replace with HTTP 422 code
|
|
6955
|
+
// 42210000 ("order pending cancel"). The order is already
|
|
6956
|
+
// dying — which is exactly what this sweep wants — so
|
|
6957
|
+
// aborting the entire position close here is wrong: it
|
|
6958
|
+
// left REDUCE/EXIT risk actions permanently blocked when a
|
|
6959
|
+
// protective stop wedged in pending_cancel at the broker
|
|
6960
|
+
// (observed live 2026-06-10: an 8-day-old pending_cancel
|
|
6961
|
+
// trailing stop on a naked short made every exit attempt
|
|
6962
|
+
// throw). Treat already-cancelling as success-in-progress
|
|
6963
|
+
// and let the subsequent close attempt get the broker's
|
|
6964
|
+
// authoritative answer on held quantity. Any other cancel
|
|
6965
|
+
// failure still aborts the close as before.
|
|
6966
|
+
const message = cancelError instanceof Error
|
|
6967
|
+
? cancelError.message
|
|
6968
|
+
: String(cancelError);
|
|
6969
|
+
const alreadyCancelling = message.includes("42210000") ||
|
|
6970
|
+
message.includes("order pending cancel");
|
|
6971
|
+
if (!alreadyCancelling) {
|
|
6972
|
+
throw cancelError;
|
|
6973
|
+
}
|
|
6974
|
+
getLogger().warn(`Order ${order.id} (${order.symbol}) is already pending cancel at the broker — proceeding with position close without it`, {
|
|
6975
|
+
account: auth.adapticAccountId || "direct",
|
|
6976
|
+
symbol: normalizedSymbol,
|
|
6977
|
+
type: "warn",
|
|
6978
|
+
});
|
|
6979
|
+
}
|
|
6950
6980
|
}
|
|
6951
6981
|
}
|
|
6952
6982
|
if (cancelledCount > 0) {
|
|
@@ -6962,12 +6992,17 @@ async function closePosition$1(auth, symbolOrAssetId, params) {
|
|
|
6962
6992
|
const verifyDelayMs = 500;
|
|
6963
6993
|
for (let attempt = 1; attempt <= maxVerifyAttempts; attempt++) {
|
|
6964
6994
|
await new Promise((r) => setTimeout(r, verifyDelayMs));
|
|
6965
|
-
const
|
|
6995
|
+
const allRemainingOrders = isCryptoSymbol$1(symbolOrAssetId)
|
|
6966
6996
|
? (await getOrders$1(auth, { status: "open" })).filter((o) => o.symbol.replace(/[-/]/g, "") === normalizedSymbol)
|
|
6967
6997
|
: await getOrders$1(auth, {
|
|
6968
6998
|
status: "open",
|
|
6969
6999
|
symbols: [normalizedSymbol],
|
|
6970
7000
|
});
|
|
7001
|
+
// Orders stuck in pending_cancel / pending_replace are already
|
|
7002
|
+
// terminalising (or wedged — see the 42210000 handling above);
|
|
7003
|
+
// waiting longer will not change them, so they must not make
|
|
7004
|
+
// the verification spin to exhaustion on every close attempt.
|
|
7005
|
+
const remainingOrders = allRemainingOrders.filter((o) => o.status !== "pending_cancel" && o.status !== "pending_replace");
|
|
6971
7006
|
if (remainingOrders.length === 0) {
|
|
6972
7007
|
getLogger().info(`Cancel verification passed for ${normalizedSymbol} (attempt ${attempt}/${maxVerifyAttempts})`, {
|
|
6973
7008
|
account: auth.adapticAccountId || "direct",
|