How to get ETF Financial information (e.g. NAV) from Yahoo (with Quantmod)?

假装没事ソ 提交于 2021-02-07 09:50:15

问题


I know that I can use the quantmod package to get stock financial information easily from yahoo. For example, if I want to get the Volume, P/E ratio and Dividend Yield:

> library(quantmod)
> AAPL <- getSymbols("AAPL")
Warning message:
In download.file(paste(yahoo.URL, "s=", Symbols.name, "&a=", from.m,  :
  downloaded length 167808 != reported length 200
> what_metrics <- yahooQF(c("Name",
+                    "Volume", 
+                    "P/E Ratio",
+                    "Dividend Yield"
+                    
+ ))
> 
> getQuote(AAPL, what=what_metrics)
              Trade Time       Name   Volume P/E Ratio Dividend Yield
AAPL 2016-03-02 04:00:00 Apple Inc. 33143834     10.72           2.15
> 
> 
> yahooQF()

 1:   Ask                                                        2:   Average Daily Volume                                    
 3:   Ask Size                                                   4:   Bid                                                     
 5:   Ask (Real-time)                                            6:   Bid (Real-time)                                         
 7:   Book Value                                                 8:   Bid Size                                                
 9:   Change & Percent Change                                   10:   Change                                                  
11:   Commission                                                12:   Change (Real-time)                                      
13:   After Hours Change (Real-time)                            14:   Dividend/Share                                          
15:   Last Trade Date                                           16:   Trade Date                                              
17:   Earnings/Share                                            18:   Error Indication (returned for symbol changed / invalid)
19:   EPS Estimate Current Year                                 20:   EPS Estimate Next Year                                  
21:   EPS Estimate Next Quarter                                 22:   Float Shares                                            
23:   Days Low                                                  24:   Days High                                               
25:   52-week Low                                               26:   52-week High                                            
27:   Holdings Gain Percent                                     28:   Annualized Gain                                         
29:   Holdings Gain                                             30:   Holdings Gain Percent (Real-time)                       
31:   Holdings Gain (Real-time)                                 32:   More Info                                               
33:   Order Book (Real-time)                                    34:   Market Capitalization                                   
35:   Market Cap (Real-time)                                    36:   EBITDA                                                  
37:   Change From 52-week Low                                   38:   Percent Change From 52-week Low                         
39:   Last Trade (Real-time) With Time                          40:   Change Percent (Real-time)                              
41:   Last Trade Size                                           42:   Change From 52-week High                                
43:   Percent Change From 52-week High                          44:   Last Trade (With Time)                                  
45:   Last Trade (Price Only)                                   46:   High Limit                                              
47:   Low Limit                                                 48:   Days Range                                              
49:   Days Range (Real-time)                                    50:   50-day Moving Average                                   
51:   200-day Moving Average                                    52:   Change From 200-day Moving Average                      
53:   Percent Change From 200-day Moving Average                54:   Change From 50-day Moving Average                       
55:   Percent Change From 50-day Moving Average                 56:   Name                                                    
57:   Notes                                                     58:   Open                                                    
59:   Previous Close                                            60:   Price Paid                                              
61:   Change in Percent                                         62:   Price/Sales                                             
63:   Price/Book                                                64:   Ex-Dividend Date                                        
65:   P/E Ratio                                                 66:   Dividend Pay Date                                       
67:   P/E Ratio (Real-time)                                     68:   PEG Ratio                                               
69:   Price/EPS Estimate Current Year                           70:   Price/EPS Estimate Next Year                            
71:   Symbol                                                    72:   Shares Owned                                            
73:   Short Ratio                                               74:   Last Trade Time                                         
75:   Trade Links                                               76:   Ticker Trend                                            
77:   1 yr Target Price                                         78:   Volume                                                  
79:   Holdings Value                                            80:   Holdings Value (Real-time)                              
81:   52-week Range                                             82:   Days Value Change                                       
83:   Days Value Change (Real-time)                             84:   Stock Exchange                                          
85:   Dividend Yield           

Is there a similar method for downloading Financial information specific for ETFs (using yahoo or any other sources) ?

For example, how can I download the NAV or Net Asset for QQQ (an ETF fund)? There are no NAV for yahooQF

Thanks in advance


回答1:


library(FinancialInstrument)
currency(c("USD", "EUR"))
exchange_rate("EURUSD")
stock(c("SPY", "QQQ", "AAPL", "GS"), currency="USD")
ls_stocks()
ls_instruments()
update_instruments.yahoo(ls_stocks())
update_instruments.masterDATA(ls_stocks())
getInstrument("SPY")
getInstrument("QQQ")

## > getInstrument("QQQ")
## primary_id          :"QQQ"
## currency            :"USD"
## multiplier          :1
## tick_size           :0.01
## identifiers         : list()
## type                :"stock"
## name                :"PowerShares QQQ Trust, Series 1"
## exchange            :"NGM"
## avg.volume          :45327500
## EPS                 :23.6
## EPS.current.year.est:1.34
## book.value          :0
## range.52wk          :"84.74 - 115.75"
## defined.by          :"yahoo;masterDATA"
## updated             : POSIXct, format: "2016-03-06 02:54:10"
## Fund.Type           :"US Equity ETF"

you can also use this:

library(qmao)
getQuote("QQQ")
getHoldings("QQQ") ## for holdings of ETF / MF  



回答2:


Is this along the lines of what you're after? It takes the data from Google, not Yahoo, but you can at least get the last few financial statements.

library(quantmod)
ticker = "QQQ"
statements = getFinancials(ticker, auto.assign=FALSE)
balsheet = viewFinancials(statements, type="BS", period='A')
print(balsheet)

##                                              2015-09-30 2014-09-30 2013-09-30 2012-09-30
## Cash & Equivalents                                   NA         NA         NA         NA
## Short Term Investments                               NA         NA         NA         NA
## Cash and Short Term Investments                  131.43     132.08     148.68     112.70
## Accounts Receivable - Trade, Net                     NA         NA         NA         NA
## Receivables - Other                                  NA         NA         NA         NA
## Total Receivables, Net                           932.05    1842.94     446.23     816.86
## Total Inventory                                      NA         NA         NA         NA
## Prepaid Expenses                                     NA         NA         NA         NA
## Other Current Assets, Total                          NA         NA         NA         NA
## Total Current Assets                                 NA         NA         NA         NA
## Property/Plant/Equipment, Total - Gross              NA         NA         NA         NA
## Accumulated Depreciation, Total                      NA         NA         NA         NA
## Goodwill, Net                                        NA         NA         NA         NA
## Intangibles, Net                                     NA         NA         NA         NA
## Long Term Investments                          36804.99   42033.21   38231.09   34622.08
## Other Long Term Assets, Total                        NA         NA         NA         NA
## Total Assets                                   37877.83   44031.17   38841.95   35551.64
## Accounts Payable                                     NA         NA         NA         NA
## Accrued Expenses                                   0.44       0.37       0.36       0.34
## Notes Payable/Short Term Debt                      0.00       0.00       0.00       0.00
## Current Port. of LT Debt/Capital Leases              NA         NA         NA         NA
## Other Current liabilities, Total                1054.28    1972.99     590.66     922.16
## Total Current Liabilities                            NA         NA         NA         NA
## Long Term Debt                                       NA         NA         NA         NA
## Capital Lease Obligations                            NA         NA         NA         NA
## Total Long Term Debt                               0.00       0.00       0.00       0.00
## Total Debt                                         0.00       0.00       0.00       0.00
## Deferred Income Tax                                  NA         NA         NA         NA
## Minority Interest                                    NA         NA         NA         NA
## Other Liabilities, Total                             NA         NA         NA         NA
## Total Liabilities                               1054.72    1973.36     591.02     922.50
## Redeemable Preferred Stock, Total                    NA         NA         NA         NA
## Preferred Stock - Non Redeemable, Net                NA         NA         NA         NA
## Common Stock, Total                                  NA         NA         NA         NA
## Additional Paid-In Capital                     48044.55   49073.07   46810.78   46058.95
## Retained Earnings (Accumulated Deficit)        -6355.52   -7936.45   -8688.78   -9479.67
## Treasury Stock - Common                              NA         NA         NA         NA
## Other Equity, Total                                  NA         NA         NA         NA
## Total Equity                                   36823.11   42057.80   38250.93   34629.13
## Total Liabilities & Shareholders&#39; Equity   37877.83   44031.17   38841.95   35551.64
##     Shares Outs - Common Stock Primary Issue             NA         NA         NA         NA
## Total Common Shares Outstanding                  361.45     425.80     485.40     504.70
## attr(,"col_desc")
## [1] "As of 2015-09-30" "As of 2014-09-30" "As of 2013-09-30" "As of 2012-09-30"

From that you can calculate the NAV, assuming NAV = Assets - Liabilities.

nav = balsheet["Total Assets", 1] - balsheet["Total Liabilities", 1]
print(nav)

## [1] 36823.11



回答3:


Does this do what you want?

require(quantmod)
setwd("C:/Users/your_path_here/downloads")
stocks <- c("AXP","BA","CAT","CSCO","CVX","DD","DIS","GE","GS","HD","IBM","INTC","JNJ","JPM","KO","MCD","MMM","MRK","MSFT","NKE","PFE","PG","T","TRV","UNH","UTX","V","VZ","WMT","XOM")

# equityList <- read.csv("EquityList.csv", header = FALSE, stringsAsFactors = FALSE)
# names(equityList) <- c ("Ticker")

for (i in 1 : length(stocks)) {   
        temp<-getFinancials(stocks[i],src="google",auto.assign=FALSE)
        write.csv(temp$IS$A,paste(stocks[i],"_Income_Statement(Annual).csv",sep=""))
        write.csv(temp$BS$A,paste(stocks[i],"_Balance_Sheet(Annual).csv",sep=""))
        write.csv(temp$CF$A,paste(stocks[i],"_Cash_Flow(Annual).csv",sep=""))
        write.csv(temp$IS$A,paste(stocks[i],"_Income_Statement(Quarterly).csv",sep=""))
        write.csv(temp$BS$A,paste(stocks[i],"_Balance_Sheet(Quaterly).csv",sep=""))
        write.csv(temp$CF$A,paste(stocks[i],"_Cash_Flow(Quaterly).csv",sep=""))
}


来源:https://stackoverflow.com/questions/35741414/how-to-get-etf-financial-information-e-g-nav-from-yahoo-with-quantmod

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!