Home > front end >  How do you count the number of api calls in tidyquant?
How do you count the number of api calls in tidyquant?

Time:04-05

For example, I'm getting stock prices from yahoo finance using tidyquant in r.

library(tidyquant)
tq_get(c("AAPL","AMD"), from = '20222-03-01', to = '2022-03-02')

This will result in 4 rows.

How many api calls does this result in?

CodePudding user response:

Assuming there is one API call per ticker and day:

tickers <- c("AAPL", "AMD")
from <- "2022-03-01"
to <- "2022-03-02"

length(tickers) * length(seq(as.Date(from), as.Date(to), by = "day"))
#> [1] 4

Created on 2022-04-05 by the reprex package (v2.0.0)

However, you can also use much less calls if you like.

CodePudding user response:

2 api calls.

Each ticker and date combination (from, to) and daily, weekly or monthly data is one api call. The api call is based on quantmod's getSymbols.yahoo which builds the query via .yahooURL. This url is a combination of the ticker, from, to, period (daily, weekly, monthly), event (historical data, dividend data or split data).

You can see this in action if you look at the yahoo finance historical data and then look at the download link. E.g. for MSFT: https://finance.yahoo.com/quote/MSFT/history?p=MSFT

1 api call for historical MSFT data:

https://query1.finance.yahoo.com/v7/finance/download/MSFT?period1=1617621728&period2=1649157728&interval=1d&events=history&includeAdjustedClose=true

So in your example, 2 queries will be sent to the yahoo servers.

  • Related