fix: consolidate 3 fetchBars calls into 1 per stock and add 500ms delay between sequential loads to avoid rate limiting
This commit is contained in:
+19
-9
@@ -210,20 +210,29 @@ export default function Analyze() {
|
||||
loadPortfolio();
|
||||
}, []);
|
||||
|
||||
// Auto-load indicators for stocks that don't have them yet
|
||||
// Auto-load indicators for stocks that don't have them yet (sequential with delay to avoid rate limits)
|
||||
useEffect(() => {
|
||||
const unloaded = stocks.filter((s) => !s.indicatorsLoading && s.indicators.rsi == null);
|
||||
if (unloaded.length === 0) return;
|
||||
|
||||
unloaded.forEach(async (stock) => {
|
||||
setStocks((s) => s.map((st) => st.id === stock.id ? { ...st, indicatorsLoading: true } : st));
|
||||
const indicators = await loadIndicators(stock.ticker);
|
||||
if (indicators) {
|
||||
setStocks((s) => s.map((st) => st.id === stock.id ? { ...st, indicators, indicatorsLoading: false } : st));
|
||||
} else {
|
||||
setStocks((s) => s.map((st) => st.id === stock.id ? { ...st, indicatorsLoading: false } : st));
|
||||
let cancelled = false;
|
||||
const loadSequential = async () => {
|
||||
for (const stock of unloaded) {
|
||||
if (cancelled) break;
|
||||
setStocks((s) => s.map((st) => st.id === stock.id ? { ...st, indicatorsLoading: true } : st));
|
||||
const indicators = await loadIndicators(stock.ticker);
|
||||
if (cancelled) break;
|
||||
if (indicators) {
|
||||
setStocks((s) => s.map((st) => st.id === stock.id ? { ...st, indicators, indicatorsLoading: false } : st));
|
||||
} else {
|
||||
setStocks((s) => s.map((st) => st.id === stock.id ? { ...st, indicatorsLoading: false } : st));
|
||||
}
|
||||
// 500ms delay between each stock to avoid rate limiting
|
||||
await new Promise((r) => setTimeout(r, 500));
|
||||
}
|
||||
});
|
||||
};
|
||||
loadSequential();
|
||||
return () => { cancelled = true; };
|
||||
}, [stocks]);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -411,6 +420,7 @@ export default function Analyze() {
|
||||
} else {
|
||||
setStocks((s) => s.map((st) => st.id === stock.id ? { ...st, indicatorsLoading: false } : st));
|
||||
}
|
||||
await new Promise((r) => setTimeout(r, 500));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user