gadicc / node-yahoo-finance2

Unofficial API for Yahoo Finance
https://www.npmjs.com/package/yahoo-finance2
MIT License
353 stars 58 forks source link

ISSUE : Getting all quotes through loop condition #780

Open kkh3868 opened 3 weeks ago

kkh3868 commented 3 weeks ago

Bug Report

Describe the bug

    for (const stock of stockInformation) {
      console.log(`Symbol: ${stock.symbol}, Quantity: ${stock.quantity}`);

      const quote = parseFloat(await yf2.quote(stock.symbol)); // this line may cause the error

      console.log(`quote : ${quote }`);
      const totalValuePerStock = quote * stock.quantity;

      totalValue += totalValuePerStock;
      symbols.push(stock.symbol);
      quantities.push(stock.quantity);
      totalValuePerStocks.push(totalValuePerStock);
    }

the result is.....

memberId : 1 Symbol: AAPL, Quantity: 14 Please consider completing the survey at https://bit.ly/yahoo-finance-api-feedback if you haven't already; for more info see https://github.com/gadicc/node-yahoo-finance2/issues/764#issuecomment-2056623851. Fetching crumb and cookies from https://finance.yahoo.com/quote/AAPL... We expected a redirect to guce.yahoo.com, but got https://finance.yahoo.com/quote/AAPL/ We'll try to continue anyway - you can safely ignore this if the request succeeds Success. Cookie expires on Sun Jun 08 2025 04:55:29 GMT+0900 (대한민국 표준시) fetch https://query1.finance.yahoo.com/v1/test/getcrumb New crumb: X2Q5c9WZbWY quote : NaN Symbol: TSLA, Quantity: 15 quote : NaN Symbol: ALI=F, Quantity: 1 quote : NaN Symbol: NVDA, Quantity: 6 quote : NaN

Minimal Reproduction

this yahoofinance.quote function occurs crumb error

Environment

Browser or Node: Node version (if applicable): v20.11.1 Npm version: 10.8.1 Browser verion (if applicable): chrome 125.0.6422.142 Library version (e.g. 1.10.1): 2.11.3

Additional Context

glaucoheitor commented 3 weeks ago

quote returns an object, Seems like you are trying to parseFloat that object instead of the current price. The current price would be the regularMarketPrice

You need to do something like this:

const quote = await yf2.quote(stock.symbol)
const currentPrice = quote?.regularMarketPrice

See https://github.com/gadicc/node-yahoo-finance2/blob/devel/docs/modules/quote.md

kkh3868 commented 3 weeks ago

@glaucoheitor thanks for your help! it really works!!