fix: market token collision via tradingSymbol lookup; correct avg_price

This commit is contained in:
Manohar 2026-05-15 04:15:14 +00:00
parent 206127976d
commit c53c188ae4

View file

@ -51,33 +51,34 @@ function loadFromCache(): MarketQuote[] {
async function fetchFromAngel(): Promise<MarketQuote[]> {
const headers = await getAuthHeaders();
// All tokens hardcoded - build token map directly
// Build exchange->tokens map and a tradingSymbol->key lookup
const tokenMap: Record<string, string[]> = {};
const tokenIndex: Record<string, string> = {};
// We match by tradingSymbol because exchangeType is sometimes undefined in response
const symToKey: Record<string, string> = {
BSX: SENSEX, // BSE token 1
USDINR: USDINR, // CDS token 1
NIFTY: NIFTY50,
BANKNIFTY: BANKNIFTY,
INDIA VIX: INDIAVIX,
};
for (const idx of INDEX_TOKENS) {
if (!tokenMap[idx.exchange]) tokenMap[idx.exchange] = [];
tokenMap[idx.exchange].push(idx.token);
tokenIndex[idx.token] = idx.key;
}
const r = await axios.post(BASE + '/rest/secure/angelbroking/market/v1/quote/',
{ mode: 'FULL', exchangeTokens: tokenMap }, { headers, timeout: 8000 });
const r = await axios.post(
BASE + /rest/secure/angelbroking/market/v1/quote/,
{ mode: FULL, exchangeTokens: tokenMap },
{ headers, timeout: 8000 }
);
const fetched: any[] = r.data?.data?.fetched ?? [];
if (!fetched.length) return [];
const LABELS: Record<string, { label: string; unit: string }> = {
SENSEX: { label: 'SENSEX', unit: 'pts' },
NIFTY50: { label: 'NIFTY 50', unit: 'pts' },
BANKNIFTY: { label: 'BANK NIFTY', unit: 'pts' },
INDIAVIX: { label: 'INDIA VIX', unit: '%' },
CRUDEOIL: { label: 'Crude Oil', unit: '\u20b9/bbl' },
USDINR: { label: 'INR/USD', unit: '\u20b9' },
};
return fetched.map((q: any) => {
const key = tokenIndex[q.symbolToken] ?? q.tradingSymbol;
const meta = LABELS[key] ?? { label: q.tradingSymbol, unit: '' };
const key = symToKey[q.tradingSymbol] ?? q.tradingSymbol;
const meta = LABELS[key] ?? { label: q.tradingSymbol, unit: };
const ltp = parseFloat(q.ltp) || 0;
const close = parseFloat(q.close) || 0;
const change = parseFloat(q.netChange) || (ltp - close);
@ -85,7 +86,6 @@ async function fetchFromAngel(): Promise<MarketQuote[]> {
return { key, label: meta.label, price: ltp, change, changePct, unit: meta.unit, stale: false };
});
}
export async function fetchMarketData(): Promise<MarketQuote[]> {
try {
const live = await fetchFromAngel();