hedgequantx 2.6.146 → 2.6.147

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hedgequantx",
3
- "version": "2.6.146",
3
+ "version": "2.6.147",
4
4
  "description": "HedgeQuantX - Prop Futures Trading CLI",
5
5
  "main": "src/app.js",
6
6
  "bin": {
@@ -2009,33 +2009,78 @@ const launchMultiSymbolRithmic = async (service, account, contracts, config) =>
2009
2009
  }
2010
2010
 
2011
2011
  // ═══════════════════════════════════════════════════════════════════════════
2012
- // EMERGENCY STOP
2012
+ // EMERGENCY STOP - Force close ALL positions
2013
2013
  // ═══════════════════════════════════════════════════════════════════════════
2014
2014
  const emergencyStopAll = async () => {
2015
- ui.addLog('warning', '████ EMERGENCY STOP ████');
2015
+ ui.addLog('warning', '████ EMERGENCY STOP INITIATED ████');
2016
+ ui.render(stats);
2017
+
2018
+ const TIMEOUT_MS = 5000;
2019
+ const withTimeout = (promise, ms) => Promise.race([
2020
+ promise,
2021
+ new Promise((_, reject) => setTimeout(() => reject(new Error('TIMEOUT')), ms))
2022
+ ]);
2016
2023
 
2017
2024
  try {
2018
- // Use Rithmic emergencyStop to flatten all positions with timeout
2019
- const stopPromise = (async () => {
2025
+ // Step 1: Cancel all pending orders
2026
+ ui.addLog('info', 'Cancelling all orders...');
2027
+ ui.render(stats);
2028
+ try {
2029
+ if (service && typeof service.cancelAllOrders === 'function') {
2030
+ await withTimeout(service.cancelAllOrders(rithmicAccountId), TIMEOUT_MS);
2031
+ }
2032
+ } catch (e) {
2033
+ ui.addLog('warning', `Cancel orders: ${e.message}`);
2034
+ }
2035
+
2036
+ // Step 2: Force close each symbol's position via fastExit
2037
+ ui.addLog('info', 'Closing all positions...');
2038
+ ui.render(stats);
2039
+
2040
+ for (const [symbolName, symStats] of Object.entries(stats.symbolStats)) {
2041
+ const posQty = Math.abs(symStats.position || 0);
2042
+ if (posQty === 0) continue;
2043
+
2044
+ const closeSide = symStats.position > 0 ? 1 : 0; // Sell if long, Buy if short
2045
+ const sideStr = closeSide === 1 ? 'SELL' : 'BUY';
2046
+
2047
+ ui.addLog('info', `Closing [${symbolName}] ${sideStr} ${posQty}x...`);
2048
+ ui.render(stats);
2049
+
2050
+ try {
2051
+ // Try fastExit first (fastest)
2052
+ if (service && typeof service.fastExit === 'function') {
2053
+ const result = await withTimeout(service.fastExit({
2054
+ accountId: rithmicAccountId,
2055
+ symbol: symbolName,
2056
+ exchange: contractInfoMap[symbolName]?.exchange || 'CME',
2057
+ size: posQty,
2058
+ side: closeSide,
2059
+ }), TIMEOUT_MS);
2060
+
2061
+ if (result.success) {
2062
+ ui.addLog('success', `[${symbolName}] FLATTENED`);
2063
+ symStats.position = 0;
2064
+ } else {
2065
+ ui.addLog('error', `[${symbolName}] Exit failed: ${result.error}`);
2066
+ }
2067
+ }
2068
+ } catch (e) {
2069
+ ui.addLog('error', `[${symbolName}] Exit error: ${e.message}`);
2070
+ }
2071
+ }
2072
+
2073
+ // Step 3: Fallback - call service.emergencyStop for any remaining
2074
+ try {
2020
2075
  if (service && typeof service.emergencyStop === 'function') {
2021
- await service.emergencyStop(rithmicAccountId);
2022
- ui.addLog('info', 'Emergency stop executed - all positions flattened');
2023
- } else if (service && typeof service.flattenAll === 'function') {
2024
- await service.flattenAll(rithmicAccountId);
2025
- ui.addLog('info', 'Flatten all executed - all positions closed');
2026
- } else if (service && typeof service.cancelAllOrders === 'function') {
2027
- await service.cancelAllOrders(rithmicAccountId);
2028
- ui.addLog('info', 'All orders cancelled');
2076
+ await withTimeout(service.emergencyStop(rithmicAccountId), TIMEOUT_MS);
2029
2077
  }
2030
- })();
2078
+ } catch (e) {
2079
+ // Silent - already tried individual closes
2080
+ }
2031
2081
 
2032
- // Timeout after 3 seconds
2033
- const timeoutPromise = new Promise(resolve => setTimeout(() => {
2034
- ui.addLog('warning', 'Emergency stop timeout - proceeding to summary');
2035
- resolve();
2036
- }, 3000));
2082
+ ui.addLog('success', '████ EMERGENCY STOP COMPLETE ████');
2037
2083
 
2038
- await Promise.race([stopPromise, timeoutPromise]);
2039
2084
  } catch (e) {
2040
2085
  ui.addLog('error', `Emergency stop error: ${e.message}`);
2041
2086
  }