“Yahoo getQuote workaround for a novice” 可以翻译为 “Yahoo的getQuote初学者解决方案”。

huangapple go评论62阅读模式
英文:

Yahoo getQuote workaround for a novice

问题

我被所有这些术语搞晕了。我是R的新手,已经使用了几个月,以VBA调用Bert子例程的方式获取我的Excel投资组合的一些报价信息。我的(精简的)R步骤如下:

    local({r <- getOption("repos")
        r["CRAN"] <- "https://cran.microsoft.com/snapshot/2022-05-05/"
        options(repos=r)
      })
    library(quantmod)
    library(jsonlite)

    ticker <- "COKE"
    metrics <- yahooQF(
      c("Trade Time", "Last Trade (Price Only)", "Change in Percent",
        "Days High", "Days Low",
        "Dividend/Share", "Dividend Yield",
        "Ex-Dividend Date", "Dividend Pay Date",
        "Earnings/Share", "EPS Forward",
        "Earnings Start Time", "Earnings End Time",
        "P/E Ratio (RT)",
        "Previous Close", "Open",
        "50-day Moving Average", "200-day Moving Average"))
     quot <- getQuote(ticker, what=metrics)

从中我得到:在open.connection(con, "rb")中的错误:HTTP错误401。

我知道关于这个问题已经有过几个问题/建议,但作为一个新手,我不知道如何解决这个问题(我理解这是由于Yahoo的更改导致的)。有人能提供我能够理解并与我的代码集成以尝试解决getQuote问题的实际代码吗?

英文:

I am blown away by all of the jargon. I’m new to R, using it for a few months to get some quote info for my Excel portfolio as a VBA call to a Bert subroutine. My (stripped down) R steps are as follows:

    local({r <- getOption("repos")
        r["CRAN"] <- "https://cran.microsoft.com/snapshot/2022-05-05/"
        options(repos=r)
      })
    library (quantmod)
    library(jsonlite)

    ticker <- "COKE"
    metrics <- yahooQF(
      c("Trade Time", "Last Trade (Price Only)", "Change in Percent",
        "Days High", "Days Low",
        "Dividend/Share",   "Dividend Yield",
        "Ex-Dividend Date", "Dividend Pay Date",
        "Earnings/Share", "EPS Forward",
        "Earnings Start Time", "Earnings End Time",
        "P/E Ratio (RT)", 
        "Previous Close", "Open",
        "50-day Moving Average", "200-day Moving Average"))
     quot <- getQuote(ticker, what=metrics)

From which I get: Error in open.connection(con, “rb”) : HTTP error 401.

I know there have been several questions/suggestions about this, but being a novice I don’t see how to get around this problem (which I’m understanding is due to changes at Yahoo). Can anyone help out with actual code that I’ll understand and can integrate with mine to try to get around the getQuote problem.

答案1

得分: 2

TLDR:

更新quantmod到一个修复版本

remotes::install_github("ethanbsmith/quantmod@382_add_session_getQuote")

详细说明:

我设置了以下断点:

debugonce(getQuote)
debugonce(getQuote.yahoo)

getQuote.yahoo()中,我提取了代表您请求的以下URL:

Browse[2]> URL
[1] "https://query1.finance.yahoo.com/v7/finance/quote?symbols=COKE&fields=regularMarketPrice,regularMarketChangePercent,regularMarketDayHigh,regularMarketDayLow,trailingAnnualDividendRate,trailingAnnualDividendYield,exDividendDate,dividendDate,epsTrailingTwelveMonths,epsForward,earningsTimestampStart,earningsTimestampEnd,regularMarketPreviousClose,regularMarketOpen,fiftyDayAverage,twoHundredDayAverage"

访问此URL会导致以下JSON响应:

{"finance":{"result":null,"error":{"code":"Unauthorized","description":"Invalid Crumb. For Developers - [...] "}}}

稍微搜索"yahoo finance invalid crumb"引导我找到了以下quantmod问题:https://github.com/joshuaulrich/quantmod/issues/382

> packageVersion('quantmod')
[1] ‘0.4.22

如建议的那样,我升级到包含此问题修复的quantmod指定版本...

remotes::install_github("ethanbsmith/quantmod@382_add_session_getQuote")
packageVersion("quantmod")
[1] ‘0.4.22.1
library (quantmod)
library(jsonlite)

ticker <- "COKE"
metrics <- yahooQF(
  c("Trade Time", "Last Trade (Price Only)", "Change in Percent",
    "Days High", "Days Low",
    "Dividend/Share",   "Dividend Yield",
    "Ex-Dividend Date", "Dividend Pay Date",
    "Earnings/Share", "EPS Forward",
    "Earnings Start Time", "Earnings End Time",
    "P/E Ratio (RT)", 
    "Previous Close", "Open",
    "50-day Moving Average", "200-day Moving Average"))
quot <- getQuote(ticker, what=metrics)
print(quot)
              Trade Time   Last   % Change   High     Low Dividend/Share Dividend Yield Ex-Dividend Date Dividend Pay Date Earnings/Share EPS Forward
COKE 2023-05-31 16:00:04 661.74 -0.9104275 670.49 658.445           1.25    0.001871762       1682553600        1683849600          48.35       38.94
     Earnings Start Time Earnings End Time P. Close   Open 50-day MA 200-day MA
COKE          1690801140        1691150400   667.82 667.14   582.332   511.4602

...我们又回到正常运行了!

英文:

TLDR:

Update quantmod to a patched version

remotes::install_github(&quot;ethanbsmith/quantmod@382_add_session_getQuote&quot;)

DETAILED EXPLANATION:

I set the following breakpoints:

debugonce(getQuote)
debugonce(getQuote.yahoo)

In the getQuote.yahoo() I extracted the following URL which represents your request:

Browse[2]&gt; URL
[1] &quot;https://query1.finance.yahoo.com/v7/finance/quote?symbols=COKE&amp;fields=regularMarketPrice,regularMarketChangePercent,regularMarketDayHigh,regularMarketDayLow,trailingAnnualDividendRate,trailingAnnualDividendYield,exDividendDate,dividendDate,epsTrailingTwelveMonths,epsForward,earningsTimestampStart,earningsTimestampEnd,regularMarketPreviousClose,regularMarketOpen,fiftyDayAverage,twoHundredDayAverage&quot;

Visting this url results in the following json reponse:

{&quot;finance&quot;:{&quot;result&quot;:null,&quot;error&quot;:{&quot;code&quot;:&quot;Unauthorized&quot;,&quot;description&quot;:&quot;Invalid Crumb. For Developers - [...] &quot;}}}

A little googling "yahoo finance invalid crumb" lead me to the following quantmod issue: https://github.com/joshuaulrich/quantmod/issues/382

&gt; packageVersion(&#39;quantmod&#39;)
[1] ‘0.4.22’

As suggested in the issue I upgrade to the specified version of quantmod that contains a patch for the issue...

remotes::install_github(&quot;ethanbsmith/quantmod@382_add_session_getQuote&quot;)
packageVersion(&quot;quantmod&quot;)
[1] ‘0.4.22.1’
library (quantmod)
library(jsonlite)

ticker &lt;- &quot;COKE&quot;
metrics &lt;- yahooQF(
  c(&quot;Trade Time&quot;, &quot;Last Trade (Price Only)&quot;, &quot;Change in Percent&quot;,
    &quot;Days High&quot;, &quot;Days Low&quot;,
    &quot;Dividend/Share&quot;,   &quot;Dividend Yield&quot;,
    &quot;Ex-Dividend Date&quot;, &quot;Dividend Pay Date&quot;,
    &quot;Earnings/Share&quot;, &quot;EPS Forward&quot;,
    &quot;Earnings Start Time&quot;, &quot;Earnings End Time&quot;,
    &quot;P/E Ratio (RT)&quot;, 
    &quot;Previous Close&quot;, &quot;Open&quot;,
    &quot;50-day Moving Average&quot;, &quot;200-day Moving Average&quot;))
quot &lt;- getQuote(ticker, what=metrics)
print(quot)
              Trade Time   Last   % Change   High     Low Dividend/Share Dividend Yield Ex-Dividend Date Dividend Pay Date Earnings/Share EPS Forward
COKE 2023-05-31 16:00:04 661.74 -0.9104275 670.49 658.445           1.25    0.001871762       1682553600        1683849600          48.35       38.94
     Earnings Start Time Earnings End Time P. Close   Open 50-day MA 200-day MA
COKE          1690801140        1691150400   667.82 667.14   582.332   511.4602

... and we're back in business!

答案2

得分: 0

这个问题的一些答案表明,Yahoo! 有意禁用了这个API。

根据这个消息,我已经开始使用Tiingo(https://www.tiingo.com/),它可以提供大部分相同的数据。注册后你会被分配一个API密钥。免费级别相当慷慨,如果你需要更多的数据/带宽,还有付费级别(每月30美元)。

以下是你可以使用的一个脚本(R):

# 定义一个从Tiingo获取行情的函数
getQuotesTiingo <- function(tickers, apiKey) {
  url <- paste0("https://api.tiingo.com/iex/?token=", apiKey)
  url <- paste0(url, "&tickers=", paste(tickers, collapse = ","))
  jsonlite::fromJSON(curl::curl(url))
}

# 设置股票代码数组
ticks <- c("MSFT", "LRCX", "FERG", "AMP")

# 提供你的API密钥
tiingoKey <- "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

# 从Tiingo获取行情并作为data.frame返回
getQuotesTiingo(ticks, tiingoKey)
英文:

Some of the answers to this question: https://stackoverflow.com/questions/76059562/yahoo-finance-api-get-quotes-returns-invalid-cookie indicate that Yahoo! has disabled this API intentionally.

Based on that news, I've begun using Tiingo (https://www.tiingo.com/) which can provide most of the same data. Register and you will be assigned an API Key. The free level is fairly generous and there's a paid level ($30/mo) if you need more data/bandwidth.

Here's a script (R) you can use:

# define a function to get quotes from Tiingo
getQuotesTiingo &lt;- function(tickers, apiKey) {
  url &lt;- paste0(&quot;https://api.tiingo.com/iex/?token=&quot;, apiKey)
  url &lt;- paste0(url, &quot;&amp;tickers=&quot;, paste(tickers, collapse = &quot;,&quot;))
  jsonlite::fromJSON(curl::curl(url))
}

# set the ticker symbol array
ticks &lt;- c(&quot;MSFT&quot;, &quot;LRCX&quot;, &quot;FERG&quot;, &quot;AMP&quot;)

# provide your API Key
tiingoKey &lt;- &quot;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&quot;

# get the quotes from Tiingo as a data.frame
getQuotesTiingo(ticks, tiingoKey)

huangapple
  • 本文由 发表于 2023年6月1日 05:44:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定