hedgequantx 2.9.194 → 2.9.195

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.9.194",
3
+ "version": "2.9.195",
4
4
  "description": "HedgeQuantX - Prop Futures Trading CLI",
5
5
  "main": "src/app.js",
6
6
  "bin": {
@@ -395,7 +395,11 @@ const executeAlgo = async ({ service, account, contract, config, strategy: strat
395
395
  return sym.includes(contract.name) || sym.includes(contractId);
396
396
  });
397
397
  if (pos && pos.quantity !== 0) {
398
- currentPosition = pos.quantity;
398
+ // Validate quantity is reasonable (prevent overflow values)
399
+ const qty = parseInt(pos.quantity);
400
+ if (!isNaN(qty) && Math.abs(qty) < 1000) {
401
+ currentPosition = qty;
402
+ }
399
403
  } else {
400
404
  currentPosition = 0;
401
405
  }
@@ -489,20 +493,46 @@ const executeAlgo = async ({ service, account, contract, config, strategy: strat
489
493
  clearInterval(liveLogInterval);
490
494
 
491
495
  // Flatten any open position before stopping
492
- if (currentPosition !== 0) {
493
- ui.addLog('system', `Flattening position: ${currentPosition > 0 ? 'LONG' : 'SHORT'} ${Math.abs(currentPosition)}`);
494
- sessionLogger.log('EXIT', `Flattening position: ${currentPosition}`);
496
+ // Get fresh position from service to avoid stale/corrupted values
497
+ let positionToFlatten = 0;
498
+ try {
499
+ const posResult = await service.getPositions(account.rithmicAccountId || account.accountId);
500
+ if (posResult.success && posResult.positions) {
501
+ const pos = posResult.positions.find(p => {
502
+ const sym = p.contractId || p.symbol || '';
503
+ return sym.includes(contract.name) || sym.includes(symbolCode);
504
+ });
505
+ if (pos && pos.quantity) {
506
+ const qty = parseInt(pos.quantity);
507
+ // Validate quantity is reasonable (not overflow)
508
+ if (!isNaN(qty) && Math.abs(qty) < 1000) {
509
+ positionToFlatten = qty;
510
+ }
511
+ }
512
+ }
513
+ } catch (e) {
514
+ // Use tracked position as fallback, but validate it
515
+ if (Math.abs(currentPosition) < 1000) {
516
+ positionToFlatten = currentPosition;
517
+ }
518
+ }
519
+
520
+ if (positionToFlatten !== 0) {
521
+ const side = positionToFlatten > 0 ? 'LONG' : 'SHORT';
522
+ const size = Math.abs(positionToFlatten);
523
+ ui.addLog('system', `Flattening position: ${side} ${size}`);
524
+ sessionLogger.log('EXIT', `Flattening position: ${side} ${size}`);
495
525
  try {
496
526
  const flattenResult = await service.placeOrder({
497
527
  accountId: account.rithmicAccountId || account.accountId,
498
528
  symbol: symbolCode,
499
529
  exchange: contract.exchange || 'CME',
500
530
  type: 2, // Market
501
- side: currentPosition > 0 ? 1 : 0, // Sell if long, Buy if short
502
- size: Math.abs(currentPosition)
531
+ side: positionToFlatten > 0 ? 1 : 0, // Sell if long, Buy if short
532
+ size: size
503
533
  });
504
534
  if (flattenResult.success) {
505
- ui.addLog('fill_' + (currentPosition > 0 ? 'sell' : 'buy'), `Position flattened @ market`);
535
+ ui.addLog('fill_' + (positionToFlatten > 0 ? 'sell' : 'buy'), `Position flattened @ market`);
506
536
  sessionLogger.log('EXIT', `Position flattened successfully`);
507
537
  } else {
508
538
  ui.addLog('error', `Flatten failed: ${flattenResult.error}`);