Add volume-based contract auto-resolver and CLAUDE.md
- New lib/contract-resolver.ts: picks the best contract month for each symbol by comparing Yahoo Finance volume between the front month (Tradovate suggest API) and the roll target (rollcontract API) - lib/clients.ts: auto-resolves all enabled instruments 15s after startup and again daily at midnight via a setInterval check - lib/tradovate-class.ts: findFrontMonthContract checks resolver cache first before falling back to the suggest API - app/api/instruments/contracts/route.ts: GET returns cached contracts, POST triggers a fresh resolve - app/settings/page.tsx: shows active contract + rolled badge per symbol; auto-resolves on load if cache is empty; removed manual Resolve button - app/api/debug/route.ts: include entity data in recentEntityEvents - CLAUDE.md: instructs Claude to always work on main Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Sonnet 4.6
parent
881c210366
commit
d945e0038f
+48
-1
@@ -1,10 +1,12 @@
|
||||
import { TradovateClient } from './tradovate-class';
|
||||
import { getFirms } from './db';
|
||||
import { getFirms, getInstruments } from './db';
|
||||
import { resolveContracts } from './contract-resolver';
|
||||
|
||||
// Use global to persist the client pool across HMR reloads in dev mode
|
||||
const g = global as typeof globalThis & {
|
||||
__tradovateClients?: Map<number, TradovateClient>;
|
||||
__tradovateClientsInitialized?: boolean;
|
||||
__contractResolverTimer?: ReturnType<typeof setInterval>;
|
||||
};
|
||||
|
||||
function ensureMap(): Map<number, TradovateClient> {
|
||||
@@ -51,9 +53,54 @@ export function getClients(): Map<number, TradovateClient> {
|
||||
initClient(firm.id, firm.username, firm.password, firm.name);
|
||||
}
|
||||
console.log(`[clients] Initialized ${firms.length} Tradovate client(s)`);
|
||||
|
||||
// Auto-resolve contracts after clients have time to authenticate
|
||||
setTimeout(() => triggerContractResolve(), 15_000);
|
||||
|
||||
// Schedule daily resolve at midnight
|
||||
scheduleDailyResolve();
|
||||
} catch (err) {
|
||||
console.error('[clients] Failed to initialize clients', err);
|
||||
}
|
||||
}
|
||||
return map;
|
||||
}
|
||||
|
||||
// ── Contract auto-resolve ────────────────────────────────────────────────────
|
||||
|
||||
function triggerContractResolve(): void {
|
||||
const map = ensureMap();
|
||||
// Find first client with a valid access token
|
||||
let accessToken: string | null = null;
|
||||
for (const [, c] of map) {
|
||||
const token = (c as any).accessInfo?.accessToken;
|
||||
if (token) { accessToken = token; break; }
|
||||
}
|
||||
if (!accessToken) {
|
||||
console.log('[contract-resolver] No authenticated client yet — skipping resolve');
|
||||
return;
|
||||
}
|
||||
|
||||
const symbols = getInstruments().filter((i) => i.enabled).map((i) => i.symbol);
|
||||
console.log(`[contract-resolver] Resolving ${symbols.length} instruments...`);
|
||||
resolveContracts(symbols, accessToken)
|
||||
.then((results) => {
|
||||
const rolled = Object.entries(results).filter(([, v]) => v?.alternative && v.rolledVolume && v.frontVolume && v.rolledVolume > v.frontVolume);
|
||||
console.log(`[contract-resolver] Done — ${rolled.length} contract(s) rolled to higher-volume month`);
|
||||
})
|
||||
.catch((err) => console.error('[contract-resolver] Resolve failed:', err));
|
||||
}
|
||||
|
||||
function scheduleDailyResolve(): void {
|
||||
// Clear any existing timer (HMR safety)
|
||||
if (g.__contractResolverTimer) clearInterval(g.__contractResolverTimer);
|
||||
|
||||
// Check every minute if it's midnight (00:00)
|
||||
g.__contractResolverTimer = setInterval(() => {
|
||||
const now = new Date();
|
||||
if (now.getHours() === 0 && now.getMinutes() === 0) {
|
||||
console.log('[contract-resolver] Midnight resolve triggered');
|
||||
triggerContractResolve();
|
||||
}
|
||||
}, 60_000);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user