Resolve symbol from positioned contract in copy-trade

Instead of relying on the scheduler state (which may be 'Auto'), look up
each enabled symbol's front-month contract and match by contractId to
determine which symbol the positioned accounts are actually trading.

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
Brandon Li
2026-03-26 02:55:18 -05:00
co-authored by Claude Opus 4.6
parent df793bfd70
commit 9a24692c1a
+24 -10
View File
@@ -326,7 +326,7 @@ export async function copyTrade() {
// Find all accounts with open positions to determine direction + symbol // Find all accounts with open positions to determine direction + symbol
let resolvedAction: 'Buy' | 'Sell' | null = null; let resolvedAction: 'Buy' | 'Sell' | null = null;
let resolvedSymbol: string | null = null; let positionContractId: number | null = null;
let positionedCount = 0; let positionedCount = 0;
for (const firm of firms) { for (const firm of firms) {
@@ -338,11 +338,7 @@ export async function copyTrade() {
positionedCount++; positionedCount++;
if (!resolvedAction) { if (!resolvedAction) {
resolvedAction = pos.netPos > 0 ? 'Buy' : 'Sell'; resolvedAction = pos.netPos > 0 ? 'Buy' : 'Sell';
} positionContractId = pos.contractId;
// Determine the symbol from the scheduler state (positions only have contractId)
if (!resolvedSymbol) {
const state = getState();
resolvedSymbol = state.symbol === 'Auto' ? null : state.symbol;
} }
} }
} }
@@ -352,11 +348,29 @@ export async function copyTrade() {
return []; return [];
} }
// Fall back to enabled instruments if symbol unknown // Resolve symbol from the positioned contract's contractId
let resolvedSymbol: string | null = null;
const instruments = getInstruments();
const enabledSymbols = instruments.filter(i => i.enabled).map(i => i.symbol);
// Try each enabled symbol to find which one matches the positioned contractId
for (const firm of firms) {
const client = clients.get(firm.id);
if (!client) continue;
for (const sym of enabledSymbols) {
const contract = await client.findFrontMonthContract(sym);
if (contract && contract.id === positionContractId) {
resolvedSymbol = sym;
break;
}
}
if (resolvedSymbol) break;
}
// Fall back to scheduler symbol or first enabled
if (!resolvedSymbol) { if (!resolvedSymbol) {
const instruments = getInstruments(); const state = getState();
const enabled = instruments.filter(i => i.enabled).map(i => i.symbol); resolvedSymbol = state.symbol !== 'Auto' ? state.symbol : (enabledSymbols[0] ?? 'NQ');
resolvedSymbol = enabled[0] ?? 'NQ';
} }
const pointValue = POINT_VALUES[resolvedSymbol]; const pointValue = POINT_VALUES[resolvedSymbol];