fix: market token collision via tradingSymbol lookup; correct avg_price
This commit is contained in:
parent
206127976d
commit
c53c188ae4
1 changed files with 17 additions and 17 deletions
|
|
@ -51,33 +51,34 @@ function loadFromCache(): MarketQuote[] {
|
||||||
async function fetchFromAngel(): Promise<MarketQuote[]> {
|
async function fetchFromAngel(): Promise<MarketQuote[]> {
|
||||||
const headers = await getAuthHeaders();
|
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 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) {
|
for (const idx of INDEX_TOKENS) {
|
||||||
if (!tokenMap[idx.exchange]) tokenMap[idx.exchange] = [];
|
if (!tokenMap[idx.exchange]) tokenMap[idx.exchange] = [];
|
||||||
tokenMap[idx.exchange].push(idx.token);
|
tokenMap[idx.exchange].push(idx.token);
|
||||||
tokenIndex[idx.token] = idx.key;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const r = await axios.post(BASE + '/rest/secure/angelbroking/market/v1/quote/',
|
const r = await axios.post(
|
||||||
{ mode: 'FULL', exchangeTokens: tokenMap }, { headers, timeout: 8000 });
|
BASE + /rest/secure/angelbroking/market/v1/quote/,
|
||||||
|
{ mode: FULL, exchangeTokens: tokenMap },
|
||||||
|
{ headers, timeout: 8000 }
|
||||||
|
);
|
||||||
|
|
||||||
const fetched: any[] = r.data?.data?.fetched ?? [];
|
const fetched: any[] = r.data?.data?.fetched ?? [];
|
||||||
if (!fetched.length) return [];
|
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) => {
|
return fetched.map((q: any) => {
|
||||||
const key = tokenIndex[q.symbolToken] ?? q.tradingSymbol;
|
const key = symToKey[q.tradingSymbol] ?? q.tradingSymbol;
|
||||||
const meta = LABELS[key] ?? { label: q.tradingSymbol, unit: '' };
|
const meta = LABELS[key] ?? { label: q.tradingSymbol, unit: };
|
||||||
const ltp = parseFloat(q.ltp) || 0;
|
const ltp = parseFloat(q.ltp) || 0;
|
||||||
const close = parseFloat(q.close) || 0;
|
const close = parseFloat(q.close) || 0;
|
||||||
const change = parseFloat(q.netChange) || (ltp - close);
|
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 };
|
return { key, label: meta.label, price: ltp, change, changePct, unit: meta.unit, stale: false };
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function fetchMarketData(): Promise<MarketQuote[]> {
|
export async function fetchMarketData(): Promise<MarketQuote[]> {
|
||||||
try {
|
try {
|
||||||
const live = await fetchFromAngel();
|
const live = await fetchFromAngel();
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue