hedgequantx 1.2.53 → 1.2.55
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/app.js +30 -38
- package/src/pages/algo.js +443 -2
package/package.json
CHANGED
package/src/app.js
CHANGED
|
@@ -656,66 +656,57 @@ const dashboardMenu = async (service) => {
|
|
|
656
656
|
* Handles the update process with auto-restart
|
|
657
657
|
*/
|
|
658
658
|
const handleUpdate = async () => {
|
|
659
|
-
const { spawn } = require('child_process');
|
|
659
|
+
const { spawn, execSync: exec } = require('child_process');
|
|
660
660
|
const pkg = require('../package.json');
|
|
661
661
|
const currentVersion = pkg.version;
|
|
662
662
|
const spinner = ora('Checking for updates...').start();
|
|
663
663
|
|
|
664
664
|
try {
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
const behindCount = execSync('git rev-list HEAD..origin/main --count', { cwd: cliPath, stdio: 'pipe' }).toString().trim();
|
|
665
|
+
// Check latest version on npm
|
|
666
|
+
spinner.text = 'Checking npm registry...';
|
|
667
|
+
let latestVersion;
|
|
668
|
+
try {
|
|
669
|
+
latestVersion = exec('npm view hedgequantx version', { stdio: 'pipe' }).toString().trim();
|
|
670
|
+
} catch (e) {
|
|
671
|
+
spinner.fail('Cannot reach npm registry');
|
|
672
|
+
return;
|
|
673
|
+
}
|
|
675
674
|
|
|
676
|
-
if (
|
|
675
|
+
if (currentVersion === latestVersion) {
|
|
677
676
|
spinner.succeed('Already up to date!');
|
|
678
677
|
console.log(chalk.cyan(` Version: v${currentVersion}`));
|
|
679
|
-
console.log(chalk.gray(` Commit: ${beforeCommit}`));
|
|
680
678
|
return;
|
|
681
679
|
}
|
|
682
680
|
|
|
683
|
-
//
|
|
684
|
-
spinner.text =
|
|
681
|
+
// Update via npm
|
|
682
|
+
spinner.text = `Updating v${currentVersion} -> v${latestVersion}...`;
|
|
685
683
|
try {
|
|
686
|
-
|
|
684
|
+
exec('npm install -g hedgequantx@latest', { stdio: 'pipe' });
|
|
687
685
|
} catch (e) {
|
|
688
|
-
//
|
|
689
|
-
|
|
686
|
+
// Try with sudo on Unix systems
|
|
687
|
+
if (process.platform !== 'win32') {
|
|
688
|
+
try {
|
|
689
|
+
exec('sudo npm install -g hedgequantx@latest', { stdio: 'pipe' });
|
|
690
|
+
} catch (e2) {
|
|
691
|
+
spinner.fail('Update failed - try manually: npm install -g hedgequantx@latest');
|
|
692
|
+
return;
|
|
693
|
+
}
|
|
694
|
+
} else {
|
|
695
|
+
spinner.fail('Update failed - try manually: npm install -g hedgequantx@latest');
|
|
696
|
+
return;
|
|
697
|
+
}
|
|
690
698
|
}
|
|
691
699
|
|
|
692
|
-
// Pull latest
|
|
693
|
-
spinner.text = 'Downloading updates...';
|
|
694
|
-
execSync('git pull origin main', { cwd: cliPath, stdio: 'pipe' });
|
|
695
|
-
const afterCommit = execSync('git rev-parse --short HEAD', { cwd: cliPath, stdio: 'pipe' }).toString().trim();
|
|
696
|
-
|
|
697
|
-
// Install dependencies
|
|
698
|
-
spinner.text = 'Installing dependencies...';
|
|
699
|
-
try {
|
|
700
|
-
execSync('npm install --silent', { cwd: cliPath, stdio: 'pipe' });
|
|
701
|
-
} catch (e) { /* ignore */ }
|
|
702
|
-
|
|
703
|
-
// Get new version
|
|
704
|
-
delete require.cache[require.resolve('../package.json')];
|
|
705
|
-
const newPkg = require('../package.json');
|
|
706
|
-
const newVersion = newPkg.version;
|
|
707
|
-
|
|
708
700
|
spinner.succeed('CLI updated!');
|
|
709
701
|
console.log();
|
|
710
|
-
console.log(chalk.green(` Version: v${currentVersion} -> v${
|
|
711
|
-
console.log(chalk.gray(` Commits: ${beforeCommit} -> ${afterCommit} (${behindCount} new)`));
|
|
702
|
+
console.log(chalk.green(` Version: v${currentVersion} -> v${latestVersion}`));
|
|
712
703
|
console.log();
|
|
713
704
|
console.log(chalk.cyan(' Restarting...'));
|
|
714
705
|
console.log();
|
|
715
706
|
|
|
716
707
|
// Restart CLI
|
|
717
|
-
const
|
|
718
|
-
|
|
708
|
+
const cliPath = exec('npm root -g', { stdio: 'pipe' }).toString().trim();
|
|
709
|
+
const child = spawn(process.argv[0], [path.join(cliPath, 'hedgequantx', 'bin', 'cli.js')], {
|
|
719
710
|
stdio: 'inherit',
|
|
720
711
|
shell: true
|
|
721
712
|
});
|
|
@@ -729,6 +720,7 @@ const handleUpdate = async () => {
|
|
|
729
720
|
|
|
730
721
|
} catch (error) {
|
|
731
722
|
spinner.fail('Update failed: ' + error.message);
|
|
723
|
+
console.log(chalk.yellow(' Try manually: npm install -g hedgequantx@latest'));
|
|
732
724
|
}
|
|
733
725
|
};
|
|
734
726
|
|
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
|
*/
|