[英]Warning: getSymbols, tq_get, getSymbols.yahoo does not return prices for to date
這實際上更像是一個警告,但我確實提出並回答了一個問題。 當在getSymbols
、 tq_get
、 getSymbols
指定to
date 參數時,該函數返回getSymbols
日期但不包括截止日期的價格,即使文檔 ( quantmod::getSymbols.yahoo
) 指出它將“通過此日期檢索數據”。 考慮以下來自tidyquant 小插圖的示例:
Ra <- c("AAPL", "GOOG", "NFLX") %>%
tq_get(get = "stock.prices",
from = "2010-01-01",
to = "2015-12-31")
人們可能期望結果包括 2015-12-31 的價格,但它只返回到 2015-12-30 的價格。 31號有價格; 市場是開放的,價格可以在雅虎財經上查看。
諸如 tidyquant 小插圖之類的文檔經常只顯示 head 函數的結果,因此這是一個容易錯過的細節。 如果在這個例子中,你想要 2015 年的表現,你就會錯過一天。
因此,對於這樣的問題:我如何獲得這些功能回到價格上漲及包括to
日期參數?
請注意, getSymbols
與源功能yahoo
,返回類型的index.class Date
。 如果您從 Tiingo 獲取數據,則返回的 index.class 是 POSIXct 類型,您可以按預期獲取數據並包括“ POSIXct
日期”。 IE
getSymbols("AAPL", src = "tiingo", to = "2015-12-31", api.key = "xxx"); tail(AAPL)
[1] "AAPL"
AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume
2015-12-23 107.27 108.85 107.2000 108.61 32657354
2015-12-24 109.00 109.00 107.9500 108.03 13596680
2015-12-28 107.59 107.69 106.1807 106.82 26704210
2015-12-29 106.96 109.43 106.8600 108.74 30931243
2015-12-30 108.58 108.70 107.1800 107.32 25213777
2015-12-31 107.01 107.03 104.8200 105.26 40912316
(注意:從 Tiingo 輸入您的個人 api.token,而不是我示例中的 3 'x')
檢查: ?getSymbols.tiingo
,它只能由getSymbols
在內部getSymbols
。 這個方法並不意味着直接調用,而是調用getSymbols("x", src="tiingo”)
。
如果您想使用 Yahoo 數據,簡單的解決方案是省略to
參數並將數據子集到您想要的結束日期:
tail(getSymbols('AAPL',auto.assign = F)['::2015-12-31'])
AAPL.Open AAPL.High AAPL.Low AAPL.Close AAPL.Volume AAPL.Adjusted
2015-12-23 107.27 108.85 107.20 108.61 32657400 103.01801
2015-12-24 109.00 109.00 107.95 108.03 13570400 102.46785
2015-12-28 107.59 107.69 106.18 106.82 26704200 101.32014
2015-12-29 106.96 109.43 106.86 108.74 30931200 103.14129
2015-12-30 108.58 108.70 107.18 107.32 25213800 101.79442
2015-12-31 107.01 107.03 104.82 105.26 40635300 99.84048
我的建議是在to
date 參數中添加一天。 我嘗試將我的時區更改為 UTC 沒有效果。 在示例中使to
參數:
as.Date("2015-12-31") + lubridate::days(1)
對我有用的是:
getSymbols("xxx",src="yahoo",from=xxx,to=end,periodicity="daily")
where end=Sys.Dat()+10
那是我得到當天價格的時候
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.