fix: avg_price from netprice, hardcode VIX/crude/USDINR tokens, UI market cache fallback
This commit is contained in:
parent
580c03e959
commit
aba1dcbdda
3 changed files with 10 additions and 16 deletions
|
|
@ -328,12 +328,9 @@ async function loadMarket(){
|
|||
{key:'USDINR', label:'INR/USD', unit:'\u20b9'},
|
||||
];
|
||||
const map=Object.fromEntries(data.map(q=>[q.key,q]));
|
||||
grid.innerHTML=SLOTS.map(slot=>{
|
||||
if(!window._mktCache)window._mktCache={};(data||[]).forEach(function(q){window._mktCache[q.key]=q;});grid.innerHTML=SLOTS.map(slot=>{
|
||||
const q=map[slot.key];
|
||||
if(!q){
|
||||
// Placeholder — keep slot, show last known or dash
|
||||
return '<div class="card mcard"><div class="mlbl">'+slot.label+'</div><div class="mprice" style="color:var(--text3)">\u2014</div><div class="mchg" style="color:var(--text3)">no data</div></div>';
|
||||
}
|
||||
if(!q){var c=(window._mktCache||{})[slot.key];if(c){q=c;}else{return '<div class="card mcard"><div class="mlbl">+slot.label+"</div><div class="mprice" style="color:var(--text3)">—</div><div class="mchg" style="color:var(--text3)">—</div></div>';}}
|
||||
const up=q.changePct>=0,cl=up?'up':'dn',arrow=up?'\u25b2':'\u25bc';
|
||||
const price=q.price>10000?q.price.toLocaleString('en-IN',{maximumFractionDigits:0}):q.price.toFixed(2);
|
||||
return '<div class="card mcard"><div class="mlbl">'+slot.label+'</div>'+
|
||||
|
|
|
|||
|
|
@ -61,9 +61,9 @@ function normalisePosition(p: AngelPosition): Position | null {
|
|||
const ltp = parseFloat(p.ltp) || 0;
|
||||
const unrealised = parseFloat(p.unrealised) || 0;
|
||||
const realised = parseFloat(p.realised) || 0;
|
||||
const netAmount = parseFloat(p.netamount) || 0;
|
||||
// avgPrice = total cost / qty (approximate, fine for display)
|
||||
const avgPrice = netqty !== 0 ? Math.abs(netAmount / netqty) : 0;
|
||||
const netprice = parseFloat(p.netprice) || 0;
|
||||
const cfAvg = netqty > 0 ? (parseFloat(p.cfbuyavgprice)||0) : (parseFloat(p.cfsellavgprice)||0);
|
||||
const avgPrice = netprice || Math.abs(cfAvg) || 0;
|
||||
|
||||
return {
|
||||
key: `${p.exchange}:${p.tradingsymbol}`,
|
||||
|
|
|
|||
|
|
@ -15,6 +15,8 @@ const INDEX_TOKENS = [
|
|||
{ key: 'NIFTY50', exchange: 'NSE', token: '26000', label: 'NIFTY 50', unit: 'pts' },
|
||||
{ key: 'BANKNIFTY', exchange: 'NSE', token: '26009', label: 'BANK NIFTY', unit: 'pts' },
|
||||
{ key: 'INDIAVIX', exchange: 'NSE', token: '26017', label: 'INDIA VIX', unit: '%' },
|
||||
{ key: 'CRUDEOIL', exchange: 'MCX', token: '234230', label: 'Crude Oil', unit: '₹/bbl' },
|
||||
{ key: 'USDINR', exchange: 'CDS', token: '1', label: 'INR/USD', unit: '₹' },
|
||||
];
|
||||
|
||||
async function searchToken(exchange: string, query: string): Promise<string | null> {
|
||||
|
|
@ -49,19 +51,14 @@ function loadFromCache(): MarketQuote[] {
|
|||
async function fetchFromAngel(): Promise<MarketQuote[]> {
|
||||
const headers = await getAuthHeaders();
|
||||
|
||||
const [crudeToken, usdinrToken] = await Promise.all([
|
||||
searchToken('MCX', 'CRUDEOIL'),
|
||||
searchToken('CDS', 'USDINR'),
|
||||
]);
|
||||
|
||||
const tokenMap: Record<string, string[]> = { NSE: [], BSE: [] };
|
||||
// All tokens hardcoded - build token map directly
|
||||
const tokenMap: Record<string, string[]> = {};
|
||||
const tokenIndex: Record<string, string> = {};
|
||||
for (const idx of INDEX_TOKENS) {
|
||||
if (!tokenMap[idx.exchange]) tokenMap[idx.exchange] = [];
|
||||
tokenMap[idx.exchange].push(idx.token);
|
||||
tokenIndex[idx.token] = idx.key;
|
||||
}
|
||||
if (crudeToken) { tokenMap['MCX'] = [crudeToken]; tokenIndex[crudeToken] = 'CRUDEOIL'; }
|
||||
if (usdinrToken) { tokenMap['CDS'] = [usdinrToken]; tokenIndex[usdinrToken] = 'USDINR'; }
|
||||
|
||||
const r = await axios.post(BASE + '/rest/secure/angelbroking/market/v1/quote/',
|
||||
{ mode: 'FULL', exchangeTokens: tokenMap }, { headers, timeout: 8000 });
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue