hedgequantx 1.2.52 → 1.2.54
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 +1 -1
- package/src/pages/algo.js +443 -2
- package/src/services/rithmic/index.js +112 -0
- package/src/services/session.js +50 -6
- package/src/services/tradovate/index.js +22 -0
package/package.json
CHANGED
package/src/pages/algo.js
CHANGED
|
@@ -30,7 +30,7 @@ const algoTradingMenu = async (service) => {
|
|
|
30
30
|
message: chalk.white.bold('Select Mode:'),
|
|
31
31
|
choices: [
|
|
32
32
|
{ name: chalk.cyan('One Account'), value: 'one_account' },
|
|
33
|
-
{ name: chalk.
|
|
33
|
+
{ name: chalk.green('Copy Trading'), value: 'copy_trading' },
|
|
34
34
|
new inquirer.Separator(),
|
|
35
35
|
{ name: chalk.yellow('< Back'), value: 'back' }
|
|
36
36
|
],
|
|
@@ -44,7 +44,7 @@ const algoTradingMenu = async (service) => {
|
|
|
44
44
|
await oneAccountMenu(service);
|
|
45
45
|
break;
|
|
46
46
|
case 'copy_trading':
|
|
47
|
-
|
|
47
|
+
await copyTradingMenu();
|
|
48
48
|
break;
|
|
49
49
|
case 'back':
|
|
50
50
|
return 'back';
|
|
@@ -509,6 +509,447 @@ const executeSignal = async (service, account, contract, numContracts, signal) =
|
|
|
509
509
|
}
|
|
510
510
|
};
|
|
511
511
|
|
|
512
|
+
/**
|
|
513
|
+
* Copy Trading Menu
|
|
514
|
+
*/
|
|
515
|
+
const copyTradingMenu = async () => {
|
|
516
|
+
console.log();
|
|
517
|
+
console.log(chalk.gray(getSeparator()));
|
|
518
|
+
console.log(chalk.green.bold(' Copy Trading Setup'));
|
|
519
|
+
console.log(chalk.gray(getSeparator()));
|
|
520
|
+
console.log();
|
|
521
|
+
|
|
522
|
+
// Get all active accounts from all connections
|
|
523
|
+
const allAccounts = await connections.getAllAccounts();
|
|
524
|
+
const activeAccounts = allAccounts.filter(acc => acc.status === 0);
|
|
525
|
+
|
|
526
|
+
if (activeAccounts.length < 2) {
|
|
527
|
+
console.log(chalk.red(' [X] You need at least 2 active accounts for copy trading.'));
|
|
528
|
+
console.log(chalk.gray(' Connect more prop firm accounts first.'));
|
|
529
|
+
console.log();
|
|
530
|
+
await inquirer.prompt([{ type: 'input', name: 'continue', message: 'Press Enter to continue...' }]);
|
|
531
|
+
return;
|
|
532
|
+
}
|
|
533
|
+
|
|
534
|
+
// Step 1: Select Lead Account
|
|
535
|
+
console.log(chalk.cyan.bold(' Step 1: Select Lead Account'));
|
|
536
|
+
console.log(chalk.gray(' The lead account is the master account whose trades will be copied.'));
|
|
537
|
+
console.log();
|
|
538
|
+
|
|
539
|
+
const leadChoices = activeAccounts.map(acc => ({
|
|
540
|
+
name: chalk.cyan(`${acc.accountName || acc.name} - ${acc.propfirm} - $${acc.balance.toLocaleString()}`),
|
|
541
|
+
value: acc
|
|
542
|
+
}));
|
|
543
|
+
leadChoices.push(new inquirer.Separator());
|
|
544
|
+
leadChoices.push({ name: chalk.yellow('< Back'), value: 'back' });
|
|
545
|
+
|
|
546
|
+
const { leadAccount } = await inquirer.prompt([
|
|
547
|
+
{
|
|
548
|
+
type: 'list',
|
|
549
|
+
name: 'leadAccount',
|
|
550
|
+
message: chalk.white.bold('Lead Account:'),
|
|
551
|
+
choices: leadChoices,
|
|
552
|
+
pageSize: 15,
|
|
553
|
+
loop: false
|
|
554
|
+
}
|
|
555
|
+
]);
|
|
556
|
+
|
|
557
|
+
if (leadAccount === 'back') return;
|
|
558
|
+
|
|
559
|
+
// Step 2: Select Follower Account
|
|
560
|
+
console.log();
|
|
561
|
+
console.log(chalk.cyan.bold(' Step 2: Select Follower Account'));
|
|
562
|
+
console.log(chalk.gray(' The follower account will copy trades from the lead account.'));
|
|
563
|
+
console.log();
|
|
564
|
+
|
|
565
|
+
const followerChoices = activeAccounts
|
|
566
|
+
.filter(acc => acc.accountId !== leadAccount.accountId)
|
|
567
|
+
.map(acc => ({
|
|
568
|
+
name: chalk.cyan(`${acc.accountName || acc.name} - ${acc.propfirm} - $${acc.balance.toLocaleString()}`),
|
|
569
|
+
value: acc
|
|
570
|
+
}));
|
|
571
|
+
followerChoices.push(new inquirer.Separator());
|
|
572
|
+
followerChoices.push({ name: chalk.yellow('< Back'), value: 'back' });
|
|
573
|
+
|
|
574
|
+
const { followerAccount } = await inquirer.prompt([
|
|
575
|
+
{
|
|
576
|
+
type: 'list',
|
|
577
|
+
name: 'followerAccount',
|
|
578
|
+
message: chalk.white.bold('Follower Account:'),
|
|
579
|
+
choices: followerChoices,
|
|
580
|
+
pageSize: 15,
|
|
581
|
+
loop: false
|
|
582
|
+
}
|
|
583
|
+
]);
|
|
584
|
+
|
|
585
|
+
if (followerAccount === 'back') return;
|
|
586
|
+
|
|
587
|
+
// Step 3: Select Lead Symbol
|
|
588
|
+
console.log();
|
|
589
|
+
console.log(chalk.cyan.bold(' Step 3: Configure Lead Symbol'));
|
|
590
|
+
console.log();
|
|
591
|
+
|
|
592
|
+
const { leadSymbol } = await inquirer.prompt([
|
|
593
|
+
{
|
|
594
|
+
type: 'list',
|
|
595
|
+
name: 'leadSymbol',
|
|
596
|
+
message: chalk.white.bold('Lead Symbol:'),
|
|
597
|
+
choices: FUTURES_SYMBOLS.map(s => ({
|
|
598
|
+
name: chalk.cyan(s.name),
|
|
599
|
+
value: s
|
|
600
|
+
})),
|
|
601
|
+
pageSize: 15,
|
|
602
|
+
loop: false
|
|
603
|
+
}
|
|
604
|
+
]);
|
|
605
|
+
|
|
606
|
+
const { leadContracts } = await inquirer.prompt([
|
|
607
|
+
{
|
|
608
|
+
type: 'input',
|
|
609
|
+
name: 'leadContracts',
|
|
610
|
+
message: chalk.white.bold('Lead Number of Contracts:'),
|
|
611
|
+
default: '1',
|
|
612
|
+
validate: (input) => {
|
|
613
|
+
const num = parseInt(input);
|
|
614
|
+
if (isNaN(num) || num <= 0 || num > 100) {
|
|
615
|
+
return 'Please enter a valid number between 1 and 100';
|
|
616
|
+
}
|
|
617
|
+
return true;
|
|
618
|
+
},
|
|
619
|
+
filter: (input) => parseInt(input)
|
|
620
|
+
}
|
|
621
|
+
]);
|
|
622
|
+
|
|
623
|
+
// Step 4: Select Follower Symbol
|
|
624
|
+
console.log();
|
|
625
|
+
console.log(chalk.cyan.bold(' Step 4: Configure Follower Symbol'));
|
|
626
|
+
console.log();
|
|
627
|
+
|
|
628
|
+
const { followerSymbol } = await inquirer.prompt([
|
|
629
|
+
{
|
|
630
|
+
type: 'list',
|
|
631
|
+
name: 'followerSymbol',
|
|
632
|
+
message: chalk.white.bold('Follower Symbol:'),
|
|
633
|
+
choices: FUTURES_SYMBOLS.map(s => ({
|
|
634
|
+
name: chalk.cyan(s.name),
|
|
635
|
+
value: s
|
|
636
|
+
})),
|
|
637
|
+
pageSize: 15,
|
|
638
|
+
loop: false
|
|
639
|
+
}
|
|
640
|
+
]);
|
|
641
|
+
|
|
642
|
+
const { followerContracts } = await inquirer.prompt([
|
|
643
|
+
{
|
|
644
|
+
type: 'input',
|
|
645
|
+
name: 'followerContracts',
|
|
646
|
+
message: chalk.white.bold('Follower Number of Contracts:'),
|
|
647
|
+
default: '1',
|
|
648
|
+
validate: (input) => {
|
|
649
|
+
const num = parseInt(input);
|
|
650
|
+
if (isNaN(num) || num <= 0 || num > 100) {
|
|
651
|
+
return 'Please enter a valid number between 1 and 100';
|
|
652
|
+
}
|
|
653
|
+
return true;
|
|
654
|
+
},
|
|
655
|
+
filter: (input) => parseInt(input)
|
|
656
|
+
}
|
|
657
|
+
]);
|
|
658
|
+
|
|
659
|
+
// Configuration Summary
|
|
660
|
+
console.log();
|
|
661
|
+
console.log(chalk.gray(getSeparator()));
|
|
662
|
+
console.log(chalk.white.bold(' Copy Trading Configuration'));
|
|
663
|
+
console.log(chalk.gray(getSeparator()));
|
|
664
|
+
console.log();
|
|
665
|
+
console.log(chalk.white(' LEAD ACCOUNT'));
|
|
666
|
+
console.log(chalk.white(` Account: ${chalk.cyan(leadAccount.accountName || leadAccount.name)}`));
|
|
667
|
+
console.log(chalk.white(` PropFirm: ${chalk.magenta(leadAccount.propfirm)}`));
|
|
668
|
+
console.log(chalk.white(` Symbol: ${chalk.cyan(leadSymbol.name)}`));
|
|
669
|
+
console.log(chalk.white(` Contracts: ${chalk.cyan(leadContracts)}`));
|
|
670
|
+
console.log();
|
|
671
|
+
console.log(chalk.white(' FOLLOWER ACCOUNT'));
|
|
672
|
+
console.log(chalk.white(` Account: ${chalk.cyan(followerAccount.accountName || followerAccount.name)}`));
|
|
673
|
+
console.log(chalk.white(` PropFirm: ${chalk.magenta(followerAccount.propfirm)}`));
|
|
674
|
+
console.log(chalk.white(` Symbol: ${chalk.cyan(followerSymbol.name)}`));
|
|
675
|
+
console.log(chalk.white(` Contracts: ${chalk.cyan(followerContracts)}`));
|
|
676
|
+
console.log();
|
|
677
|
+
console.log(chalk.gray(getSeparator()));
|
|
678
|
+
console.log();
|
|
679
|
+
|
|
680
|
+
const { launch } = await inquirer.prompt([
|
|
681
|
+
{
|
|
682
|
+
type: 'list',
|
|
683
|
+
name: 'launch',
|
|
684
|
+
message: chalk.white.bold('Ready to launch Copy Trading?'),
|
|
685
|
+
choices: [
|
|
686
|
+
{ name: chalk.green.bold('[>] Launch Copy Trading'), value: 'launch' },
|
|
687
|
+
{ name: chalk.yellow('< Back'), value: 'back' }
|
|
688
|
+
],
|
|
689
|
+
loop: false
|
|
690
|
+
}
|
|
691
|
+
]);
|
|
692
|
+
|
|
693
|
+
if (launch === 'back') return;
|
|
694
|
+
|
|
695
|
+
// Launch Copy Trading
|
|
696
|
+
await launchCopyTrading({
|
|
697
|
+
lead: {
|
|
698
|
+
account: leadAccount,
|
|
699
|
+
symbol: leadSymbol,
|
|
700
|
+
contracts: leadContracts,
|
|
701
|
+
service: leadAccount.service
|
|
702
|
+
},
|
|
703
|
+
follower: {
|
|
704
|
+
account: followerAccount,
|
|
705
|
+
symbol: followerSymbol,
|
|
706
|
+
contracts: followerContracts,
|
|
707
|
+
service: followerAccount.service
|
|
708
|
+
}
|
|
709
|
+
});
|
|
710
|
+
};
|
|
711
|
+
|
|
712
|
+
/**
|
|
713
|
+
* Launch Copy Trading
|
|
714
|
+
*/
|
|
715
|
+
const launchCopyTrading = async (config) => {
|
|
716
|
+
const { lead, follower } = config;
|
|
717
|
+
|
|
718
|
+
console.log();
|
|
719
|
+
console.log(chalk.green.bold(' [>] Launching Copy Trading...'));
|
|
720
|
+
console.log();
|
|
721
|
+
|
|
722
|
+
let isRunning = true;
|
|
723
|
+
let lastLeadPosition = null;
|
|
724
|
+
const logs = [];
|
|
725
|
+
const MAX_LOGS = 15;
|
|
726
|
+
|
|
727
|
+
const stats = {
|
|
728
|
+
copiedTrades: 0,
|
|
729
|
+
leadTrades: 0,
|
|
730
|
+
followerTrades: 0,
|
|
731
|
+
errors: 0
|
|
732
|
+
};
|
|
733
|
+
|
|
734
|
+
const addLog = (type, message) => {
|
|
735
|
+
const timestamp = new Date().toLocaleTimeString();
|
|
736
|
+
logs.push({ timestamp, type, message });
|
|
737
|
+
if (logs.length > MAX_LOGS) logs.shift();
|
|
738
|
+
};
|
|
739
|
+
|
|
740
|
+
const displayUI = () => {
|
|
741
|
+
console.clear();
|
|
742
|
+
console.log();
|
|
743
|
+
console.log(chalk.gray(getSeparator()));
|
|
744
|
+
console.log(chalk.green.bold(' HQX Copy Trading'));
|
|
745
|
+
console.log(chalk.gray(getSeparator()));
|
|
746
|
+
console.log(chalk.white(` Status: ${isRunning ? chalk.green('RUNNING') : chalk.red('STOPPED')}`));
|
|
747
|
+
console.log(chalk.gray(getSeparator()));
|
|
748
|
+
console.log();
|
|
749
|
+
|
|
750
|
+
// Lead info
|
|
751
|
+
console.log(chalk.white.bold(' LEAD'));
|
|
752
|
+
console.log(chalk.white(` ${chalk.cyan(lead.account.accountName)} @ ${chalk.magenta(lead.account.propfirm)}`));
|
|
753
|
+
console.log(chalk.white(` ${chalk.cyan(lead.symbol.value)} x ${lead.contracts}`));
|
|
754
|
+
console.log();
|
|
755
|
+
|
|
756
|
+
// Follower info
|
|
757
|
+
console.log(chalk.white.bold(' FOLLOWER'));
|
|
758
|
+
console.log(chalk.white(` ${chalk.cyan(follower.account.accountName)} @ ${chalk.magenta(follower.account.propfirm)}`));
|
|
759
|
+
console.log(chalk.white(` ${chalk.cyan(follower.symbol.value)} x ${follower.contracts}`));
|
|
760
|
+
console.log();
|
|
761
|
+
|
|
762
|
+
// Stats
|
|
763
|
+
console.log(chalk.gray(getSeparator()));
|
|
764
|
+
console.log(chalk.white(' Stats: ') +
|
|
765
|
+
chalk.gray('Lead Trades: ') + chalk.cyan(stats.leadTrades) +
|
|
766
|
+
chalk.gray(' | Copied: ') + chalk.green(stats.copiedTrades) +
|
|
767
|
+
chalk.gray(' | Errors: ') + chalk.red(stats.errors)
|
|
768
|
+
);
|
|
769
|
+
console.log(chalk.gray(getSeparator()));
|
|
770
|
+
|
|
771
|
+
// Logs
|
|
772
|
+
console.log(chalk.white.bold(' Activity Log'));
|
|
773
|
+
console.log(chalk.gray(getSeparator()));
|
|
774
|
+
|
|
775
|
+
if (logs.length === 0) {
|
|
776
|
+
console.log(chalk.gray(' Monitoring lead account for trades...'));
|
|
777
|
+
} else {
|
|
778
|
+
const typeColors = {
|
|
779
|
+
info: chalk.cyan,
|
|
780
|
+
success: chalk.green,
|
|
781
|
+
trade: chalk.green.bold,
|
|
782
|
+
copy: chalk.yellow.bold,
|
|
783
|
+
error: chalk.red,
|
|
784
|
+
warning: chalk.yellow
|
|
785
|
+
};
|
|
786
|
+
|
|
787
|
+
logs.forEach(log => {
|
|
788
|
+
const color = typeColors[log.type] || chalk.white;
|
|
789
|
+
const icon = log.type === 'trade' ? '[>]' :
|
|
790
|
+
log.type === 'copy' ? '[+]' :
|
|
791
|
+
log.type === 'error' ? '[X]' :
|
|
792
|
+
log.type === 'success' ? '[OK]' : '[.]';
|
|
793
|
+
console.log(chalk.gray(` [${log.timestamp}]`) + ' ' + color(`${icon} ${log.message}`));
|
|
794
|
+
});
|
|
795
|
+
}
|
|
796
|
+
|
|
797
|
+
console.log(chalk.gray(getSeparator()));
|
|
798
|
+
console.log();
|
|
799
|
+
console.log(chalk.yellow(' Press X to stop copy trading...'));
|
|
800
|
+
console.log();
|
|
801
|
+
};
|
|
802
|
+
|
|
803
|
+
addLog('info', 'Copy trading initialized');
|
|
804
|
+
addLog('info', `Monitoring ${lead.account.accountName} for position changes`);
|
|
805
|
+
displayUI();
|
|
806
|
+
|
|
807
|
+
// Position monitoring loop
|
|
808
|
+
const monitorInterval = setInterval(async () => {
|
|
809
|
+
if (!isRunning) return;
|
|
810
|
+
|
|
811
|
+
try {
|
|
812
|
+
// Get lead positions
|
|
813
|
+
const leadPositions = await lead.service.getPositions(lead.account.rithmicAccountId || lead.account.accountId);
|
|
814
|
+
|
|
815
|
+
let currentLeadPosition = null;
|
|
816
|
+
if (leadPositions.success && leadPositions.positions) {
|
|
817
|
+
currentLeadPosition = leadPositions.positions.find(p =>
|
|
818
|
+
p.symbol === lead.symbol.value ||
|
|
819
|
+
p.symbol?.includes(lead.symbol.searchText)
|
|
820
|
+
);
|
|
821
|
+
}
|
|
822
|
+
|
|
823
|
+
// Detect position changes
|
|
824
|
+
const hadPosition = lastLeadPosition && lastLeadPosition.quantity !== 0;
|
|
825
|
+
const hasPosition = currentLeadPosition && currentLeadPosition.quantity !== 0;
|
|
826
|
+
|
|
827
|
+
if (!hadPosition && hasPosition) {
|
|
828
|
+
// New position opened
|
|
829
|
+
stats.leadTrades++;
|
|
830
|
+
const side = currentLeadPosition.quantity > 0 ? 'LONG' : 'SHORT';
|
|
831
|
+
addLog('trade', `Lead opened ${side} ${Math.abs(currentLeadPosition.quantity)} @ ${currentLeadPosition.averagePrice || 'MKT'}`);
|
|
832
|
+
|
|
833
|
+
// Copy to follower
|
|
834
|
+
await copyTradeToFollower(follower, currentLeadPosition, 'open');
|
|
835
|
+
stats.copiedTrades++;
|
|
836
|
+
displayUI();
|
|
837
|
+
|
|
838
|
+
} else if (hadPosition && !hasPosition) {
|
|
839
|
+
// Position closed
|
|
840
|
+
addLog('trade', `Lead closed position`);
|
|
841
|
+
|
|
842
|
+
// Close follower position
|
|
843
|
+
await copyTradeToFollower(follower, lastLeadPosition, 'close');
|
|
844
|
+
stats.copiedTrades++;
|
|
845
|
+
displayUI();
|
|
846
|
+
|
|
847
|
+
} else if (hadPosition && hasPosition && lastLeadPosition.quantity !== currentLeadPosition.quantity) {
|
|
848
|
+
// Position size changed
|
|
849
|
+
const diff = currentLeadPosition.quantity - lastLeadPosition.quantity;
|
|
850
|
+
const action = diff > 0 ? 'added' : 'reduced';
|
|
851
|
+
addLog('trade', `Lead ${action} ${Math.abs(diff)} contracts`);
|
|
852
|
+
|
|
853
|
+
// Adjust follower position
|
|
854
|
+
await copyTradeToFollower(follower, { ...currentLeadPosition, quantityChange: diff }, 'adjust');
|
|
855
|
+
stats.copiedTrades++;
|
|
856
|
+
displayUI();
|
|
857
|
+
}
|
|
858
|
+
|
|
859
|
+
lastLeadPosition = currentLeadPosition ? { ...currentLeadPosition } : null;
|
|
860
|
+
|
|
861
|
+
} catch (error) {
|
|
862
|
+
stats.errors++;
|
|
863
|
+
addLog('error', `Monitor error: ${error.message}`);
|
|
864
|
+
displayUI();
|
|
865
|
+
}
|
|
866
|
+
}, 2000); // Check every 2 seconds
|
|
867
|
+
|
|
868
|
+
// Wait for stop key
|
|
869
|
+
await waitForStopKey();
|
|
870
|
+
|
|
871
|
+
// Cleanup
|
|
872
|
+
isRunning = false;
|
|
873
|
+
clearInterval(monitorInterval);
|
|
874
|
+
|
|
875
|
+
console.log();
|
|
876
|
+
console.log(chalk.green(' [OK] Copy trading stopped'));
|
|
877
|
+
console.log();
|
|
878
|
+
console.log(chalk.gray(getSeparator()));
|
|
879
|
+
console.log(chalk.white.bold(' Session Summary'));
|
|
880
|
+
console.log(chalk.gray(getSeparator()));
|
|
881
|
+
console.log(chalk.white(` Lead Trades: ${chalk.cyan(stats.leadTrades)}`));
|
|
882
|
+
console.log(chalk.white(` Copied Trades: ${chalk.green(stats.copiedTrades)}`));
|
|
883
|
+
console.log(chalk.white(` Errors: ${chalk.red(stats.errors)}`));
|
|
884
|
+
console.log(chalk.gray(getSeparator()));
|
|
885
|
+
console.log();
|
|
886
|
+
|
|
887
|
+
await inquirer.prompt([{ type: 'input', name: 'continue', message: 'Press Enter to continue...' }]);
|
|
888
|
+
};
|
|
889
|
+
|
|
890
|
+
/**
|
|
891
|
+
* Copy trade to follower account
|
|
892
|
+
*/
|
|
893
|
+
const copyTradeToFollower = async (follower, position, action) => {
|
|
894
|
+
try {
|
|
895
|
+
const service = follower.service;
|
|
896
|
+
const accountId = follower.account.rithmicAccountId || follower.account.accountId;
|
|
897
|
+
|
|
898
|
+
if (action === 'open') {
|
|
899
|
+
// Open new position
|
|
900
|
+
const side = position.quantity > 0 ? 0 : 1; // 0=Buy, 1=Sell
|
|
901
|
+
const result = await service.placeOrder({
|
|
902
|
+
accountId: accountId,
|
|
903
|
+
symbol: follower.symbol.value,
|
|
904
|
+
exchange: 'CME',
|
|
905
|
+
size: follower.contracts,
|
|
906
|
+
side: side,
|
|
907
|
+
type: 2 // Market
|
|
908
|
+
});
|
|
909
|
+
|
|
910
|
+
if (result.success) {
|
|
911
|
+
console.log(chalk.green(` [+] Follower: Opened ${side === 0 ? 'LONG' : 'SHORT'} ${follower.contracts} ${follower.symbol.value}`));
|
|
912
|
+
} else {
|
|
913
|
+
throw new Error(result.error || 'Order failed');
|
|
914
|
+
}
|
|
915
|
+
|
|
916
|
+
} else if (action === 'close') {
|
|
917
|
+
// Close position
|
|
918
|
+
const result = await service.closePosition(accountId, follower.symbol.value);
|
|
919
|
+
|
|
920
|
+
if (result.success) {
|
|
921
|
+
console.log(chalk.green(` [+] Follower: Closed position`));
|
|
922
|
+
} else {
|
|
923
|
+
throw new Error(result.error || 'Close failed');
|
|
924
|
+
}
|
|
925
|
+
|
|
926
|
+
} else if (action === 'adjust') {
|
|
927
|
+
// Adjust position size
|
|
928
|
+
const side = position.quantityChange > 0 ? 0 : 1;
|
|
929
|
+
const size = Math.abs(position.quantityChange);
|
|
930
|
+
const adjustedSize = Math.round(size * (follower.contracts / Math.abs(position.quantity - position.quantityChange)));
|
|
931
|
+
|
|
932
|
+
if (adjustedSize > 0) {
|
|
933
|
+
const result = await service.placeOrder({
|
|
934
|
+
accountId: accountId,
|
|
935
|
+
symbol: follower.symbol.value,
|
|
936
|
+
exchange: 'CME',
|
|
937
|
+
size: adjustedSize,
|
|
938
|
+
side: side,
|
|
939
|
+
type: 2
|
|
940
|
+
});
|
|
941
|
+
|
|
942
|
+
if (result.success) {
|
|
943
|
+
console.log(chalk.green(` [+] Follower: Adjusted by ${side === 0 ? '+' : '-'}${adjustedSize}`));
|
|
944
|
+
}
|
|
945
|
+
}
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
} catch (error) {
|
|
949
|
+
console.log(chalk.red(` [X] Follower error: ${error.message}`));
|
|
950
|
+
}
|
|
951
|
+
};
|
|
952
|
+
|
|
512
953
|
/**
|
|
513
954
|
* Wait for X key to stop
|
|
514
955
|
*/
|
|
@@ -599,6 +599,118 @@ class RithmicService extends EventEmitter {
|
|
|
599
599
|
return { success: true, trades: [] };
|
|
600
600
|
}
|
|
601
601
|
|
|
602
|
+
/**
|
|
603
|
+
* Get market status
|
|
604
|
+
*/
|
|
605
|
+
async getMarketStatus(accountId) {
|
|
606
|
+
const marketHours = this.checkMarketHours();
|
|
607
|
+
return {
|
|
608
|
+
success: true,
|
|
609
|
+
isOpen: marketHours.isOpen,
|
|
610
|
+
message: marketHours.message,
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
/**
|
|
615
|
+
* Get token (stub - Rithmic uses WebSocket, not tokens)
|
|
616
|
+
*/
|
|
617
|
+
getToken() {
|
|
618
|
+
return this.loginInfo ? 'connected' : null;
|
|
619
|
+
}
|
|
620
|
+
|
|
621
|
+
/**
|
|
622
|
+
* Search contracts (stub - would need TICKER_PLANT)
|
|
623
|
+
*/
|
|
624
|
+
async searchContracts(searchText) {
|
|
625
|
+
// Common futures contracts
|
|
626
|
+
const contracts = [
|
|
627
|
+
{ symbol: 'ESH5', name: 'E-mini S&P 500 Mar 2025', exchange: 'CME' },
|
|
628
|
+
{ symbol: 'NQH5', name: 'E-mini NASDAQ-100 Mar 2025', exchange: 'CME' },
|
|
629
|
+
{ symbol: 'CLH5', name: 'Crude Oil Mar 2025', exchange: 'NYMEX' },
|
|
630
|
+
{ symbol: 'GCG5', name: 'Gold Feb 2025', exchange: 'COMEX' },
|
|
631
|
+
{ symbol: 'MESH5', name: 'Micro E-mini S&P 500 Mar 2025', exchange: 'CME' },
|
|
632
|
+
{ symbol: 'MNQH5', name: 'Micro E-mini NASDAQ-100 Mar 2025', exchange: 'CME' },
|
|
633
|
+
];
|
|
634
|
+
const search = searchText.toUpperCase();
|
|
635
|
+
return contracts.filter(c => c.symbol.includes(search) || c.name.toUpperCase().includes(search));
|
|
636
|
+
}
|
|
637
|
+
|
|
638
|
+
/**
|
|
639
|
+
* Place order via ORDER_PLANT
|
|
640
|
+
*/
|
|
641
|
+
async placeOrder(orderData) {
|
|
642
|
+
if (!this.orderConn || !this.loginInfo) {
|
|
643
|
+
return { success: false, error: 'Not connected' };
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
try {
|
|
647
|
+
this.orderConn.send('RequestNewOrder', {
|
|
648
|
+
templateId: REQ.NEW_ORDER,
|
|
649
|
+
userMsg: ['HQX'],
|
|
650
|
+
fcmId: this.loginInfo.fcmId,
|
|
651
|
+
ibId: this.loginInfo.ibId,
|
|
652
|
+
accountId: orderData.accountId,
|
|
653
|
+
symbol: orderData.symbol,
|
|
654
|
+
exchange: orderData.exchange || 'CME',
|
|
655
|
+
quantity: orderData.size,
|
|
656
|
+
transactionType: orderData.side === 0 ? 1 : 2, // 1=Buy, 2=Sell
|
|
657
|
+
duration: 1, // DAY
|
|
658
|
+
orderType: orderData.type === 2 ? 1 : 2, // 1=Market, 2=Limit
|
|
659
|
+
price: orderData.price || 0,
|
|
660
|
+
});
|
|
661
|
+
|
|
662
|
+
return { success: true, message: 'Order submitted' };
|
|
663
|
+
} catch (error) {
|
|
664
|
+
return { success: false, error: error.message };
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
|
|
668
|
+
/**
|
|
669
|
+
* Cancel order
|
|
670
|
+
*/
|
|
671
|
+
async cancelOrder(orderId) {
|
|
672
|
+
if (!this.orderConn || !this.loginInfo) {
|
|
673
|
+
return { success: false, error: 'Not connected' };
|
|
674
|
+
}
|
|
675
|
+
|
|
676
|
+
try {
|
|
677
|
+
this.orderConn.send('RequestCancelOrder', {
|
|
678
|
+
templateId: REQ.CANCEL_ORDER,
|
|
679
|
+
userMsg: ['HQX'],
|
|
680
|
+
fcmId: this.loginInfo.fcmId,
|
|
681
|
+
ibId: this.loginInfo.ibId,
|
|
682
|
+
orderId: orderId,
|
|
683
|
+
});
|
|
684
|
+
|
|
685
|
+
return { success: true };
|
|
686
|
+
} catch (error) {
|
|
687
|
+
return { success: false, error: error.message };
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
|
|
691
|
+
/**
|
|
692
|
+
* Close position (market order to flatten)
|
|
693
|
+
*/
|
|
694
|
+
async closePosition(accountId, symbol) {
|
|
695
|
+
// Get current position
|
|
696
|
+
const positions = Array.from(this.positions.values());
|
|
697
|
+
const position = positions.find(p => p.accountId === accountId && p.symbol === symbol);
|
|
698
|
+
|
|
699
|
+
if (!position) {
|
|
700
|
+
return { success: false, error: 'Position not found' };
|
|
701
|
+
}
|
|
702
|
+
|
|
703
|
+
// Place opposite order
|
|
704
|
+
return this.placeOrder({
|
|
705
|
+
accountId,
|
|
706
|
+
symbol,
|
|
707
|
+
exchange: position.exchange,
|
|
708
|
+
size: Math.abs(position.quantity),
|
|
709
|
+
side: position.quantity > 0 ? 1 : 0, // Sell if long, Buy if short
|
|
710
|
+
type: 2, // Market
|
|
711
|
+
});
|
|
712
|
+
}
|
|
713
|
+
|
|
602
714
|
/**
|
|
603
715
|
* Get order history
|
|
604
716
|
* Uses RequestShowOrderHistorySummary (template 324)
|
package/src/services/session.js
CHANGED
|
@@ -8,6 +8,8 @@ const path = require('path');
|
|
|
8
8
|
const os = require('os');
|
|
9
9
|
const { encrypt, decrypt, maskSensitive } = require('../security');
|
|
10
10
|
const { ProjectXService } = require('./projectx');
|
|
11
|
+
const { RithmicService } = require('./rithmic');
|
|
12
|
+
const { TradovateService } = require('./tradovate');
|
|
11
13
|
|
|
12
14
|
const SESSION_DIR = path.join(os.homedir(), '.hedgequantx');
|
|
13
15
|
const SESSION_FILE = path.join(SESSION_DIR, 'session.enc');
|
|
@@ -107,11 +109,22 @@ const connections = {
|
|
|
107
109
|
* Saves all sessions to encrypted storage
|
|
108
110
|
*/
|
|
109
111
|
saveToStorage() {
|
|
110
|
-
const sessions = this.services.map(conn =>
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
112
|
+
const sessions = this.services.map(conn => {
|
|
113
|
+
const session = {
|
|
114
|
+
type: conn.type,
|
|
115
|
+
propfirm: conn.propfirm,
|
|
116
|
+
propfirmKey: conn.service.propfirmKey || conn.propfirmKey,
|
|
117
|
+
};
|
|
118
|
+
|
|
119
|
+
if (conn.type === 'projectx') {
|
|
120
|
+
session.token = conn.service.token || conn.token;
|
|
121
|
+
} else if (conn.type === 'rithmic' || conn.type === 'tradovate') {
|
|
122
|
+
// Save encrypted credentials for reconnection
|
|
123
|
+
session.credentials = conn.service.credentials;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
return session;
|
|
127
|
+
});
|
|
115
128
|
storage.save(sessions);
|
|
116
129
|
},
|
|
117
130
|
|
|
@@ -125,7 +138,7 @@ const connections = {
|
|
|
125
138
|
for (const session of sessions) {
|
|
126
139
|
try {
|
|
127
140
|
if (session.type === 'projectx' && session.token) {
|
|
128
|
-
const propfirmKey = session.propfirm.toLowerCase().replace(/ /g, '_');
|
|
141
|
+
const propfirmKey = session.propfirmKey || session.propfirm.toLowerCase().replace(/ /g, '_');
|
|
129
142
|
const service = new ProjectXService(propfirmKey);
|
|
130
143
|
service.token = session.token;
|
|
131
144
|
|
|
@@ -136,10 +149,41 @@ const connections = {
|
|
|
136
149
|
type: session.type,
|
|
137
150
|
service,
|
|
138
151
|
propfirm: session.propfirm,
|
|
152
|
+
propfirmKey: propfirmKey,
|
|
139
153
|
token: session.token,
|
|
140
154
|
connectedAt: new Date()
|
|
141
155
|
});
|
|
142
156
|
}
|
|
157
|
+
} else if (session.type === 'rithmic' && session.credentials) {
|
|
158
|
+
const propfirmKey = session.propfirmKey || 'apex_rithmic';
|
|
159
|
+
const service = new RithmicService(propfirmKey);
|
|
160
|
+
|
|
161
|
+
// Try to reconnect
|
|
162
|
+
const result = await service.login(session.credentials.username, session.credentials.password);
|
|
163
|
+
if (result.success) {
|
|
164
|
+
this.services.push({
|
|
165
|
+
type: session.type,
|
|
166
|
+
service,
|
|
167
|
+
propfirm: session.propfirm,
|
|
168
|
+
propfirmKey: propfirmKey,
|
|
169
|
+
connectedAt: new Date()
|
|
170
|
+
});
|
|
171
|
+
}
|
|
172
|
+
} else if (session.type === 'tradovate' && session.credentials) {
|
|
173
|
+
const propfirmKey = session.propfirmKey || 'tradovate';
|
|
174
|
+
const service = new TradovateService(propfirmKey);
|
|
175
|
+
|
|
176
|
+
// Try to reconnect
|
|
177
|
+
const result = await service.login(session.credentials.username, session.credentials.password);
|
|
178
|
+
if (result.success) {
|
|
179
|
+
this.services.push({
|
|
180
|
+
type: session.type,
|
|
181
|
+
service,
|
|
182
|
+
propfirm: session.propfirm,
|
|
183
|
+
propfirmKey: propfirmKey,
|
|
184
|
+
connectedAt: new Date()
|
|
185
|
+
});
|
|
186
|
+
}
|
|
143
187
|
}
|
|
144
188
|
} catch (e) {
|
|
145
189
|
// Invalid session - skip
|
|
@@ -22,6 +22,7 @@ class TradovateService extends EventEmitter {
|
|
|
22
22
|
this.isDemo = true; // Default to demo
|
|
23
23
|
this.ws = null;
|
|
24
24
|
this.renewalTimer = null;
|
|
25
|
+
this.credentials = null; // Store for session restore
|
|
25
26
|
}
|
|
26
27
|
|
|
27
28
|
/**
|
|
@@ -71,6 +72,7 @@ class TradovateService extends EventEmitter {
|
|
|
71
72
|
this.userId = result.userId;
|
|
72
73
|
this.tokenExpiration = new Date(result.expirationTime);
|
|
73
74
|
this.user = { userName: result.name, userId: result.userId };
|
|
75
|
+
this.credentials = { username, password }; // Store for session restore
|
|
74
76
|
|
|
75
77
|
// Setup token renewal
|
|
76
78
|
this.setupTokenRenewal();
|
|
@@ -257,6 +259,25 @@ class TradovateService extends EventEmitter {
|
|
|
257
259
|
return this.user;
|
|
258
260
|
}
|
|
259
261
|
|
|
262
|
+
/**
|
|
263
|
+
* Get market status
|
|
264
|
+
*/
|
|
265
|
+
async getMarketStatus(accountId) {
|
|
266
|
+
const marketHours = this.checkMarketHours();
|
|
267
|
+
return {
|
|
268
|
+
success: true,
|
|
269
|
+
isOpen: marketHours.isOpen,
|
|
270
|
+
message: marketHours.message,
|
|
271
|
+
};
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Get token
|
|
276
|
+
*/
|
|
277
|
+
getToken() {
|
|
278
|
+
return this.accessToken;
|
|
279
|
+
}
|
|
280
|
+
|
|
260
281
|
/**
|
|
261
282
|
* Get orders for an account
|
|
262
283
|
*/
|
|
@@ -510,6 +531,7 @@ class TradovateService extends EventEmitter {
|
|
|
510
531
|
this.mdAccessToken = null;
|
|
511
532
|
this.accounts = [];
|
|
512
533
|
this.user = null;
|
|
534
|
+
this.credentials = null;
|
|
513
535
|
}
|
|
514
536
|
|
|
515
537
|
/**
|