I created a daily investment digest customized for my portfolio showing current and 30-day market trends, key news, upcoming IPOs/Earnings and correlating these to provide short/long-term investment guidance.
One thing that required fixing was the indices returning incorrect %change. Yahoo Finance's API doesn't return a clean "today's change" field — regularMarketChange comes back as null, so the original code fell back to chartPreviousClose to calculate the change.
The problem: chartPreviousClose is the closing price from before the 30-day chart window (i.e., ~31 days ago), not yesterday's close. So the math was wrong — it was comparing today's price to a month-old price.
The fix was to ignore those fields entirely and use the raw candle array instead:
- closes[closes.length - 1] = today's live intraday price (Yahoo always appends today as the last candle)
- closes[closes.length - 2] = yesterday's actual close
Subtracting those two gives the correct day change.