joshuaulrich / rfimport

getSymbols() reboot
16 stars 2 forks source link

import_ohlc_collection returns 'NA' element for missing ticker symbols #17

Open joshuaulrich opened 1 year ago

joshuaulrich commented 1 year ago

This is certainly wrong, but I'm not sure what the right answer is. Should the element for FOO be removed, or should there be an element named FOO that contains NULL, or an empty xts object, something else?

library(rfimport)
x <- sym_yahoo(c("AAPL", "FOO")) |> import_ohlc_collection(dates = "2023-01-01/2023-11-01")
## Warning: Unable to import "FOO".
## attempt to set an attribute on NULL

print(x)
## AAPL 
##              Open   High    Low  Close    Volume Adjusted
## 2023-01-03 130.28 130.90 124.17 125.07 112117500 124.5387
## 2023-01-04 126.89 128.66 125.08 126.36  89113600 125.8232
## 2023-01-05 127.13 127.77 124.76 125.02  80962700 124.4889
## 2023-01-06 126.01 130.29 124.89 129.62  87754700 129.0693
## 2023-01-09 130.47 133.41 129.89 130.15  70790800 129.5971
##        ...                                               
## 2023-10-26 170.37 171.38 165.67 166.89  70625300 166.8900
## 2023-10-27 166.91 168.96 166.83 168.22  58499100 168.2200
## 2023-10-30 169.02 171.17 168.87 170.29  51131000 170.2900
## 2023-10-31 169.35 170.90 167.90 170.77  44846000 170.7700
## 2023-11-01 171.00 174.23 170.12 173.97  56934900 173.9700
## 
## NA 
## data frame with 0 columns and 0 rows
ethanbsmith commented 1 year ago

if FOO is a valid symbol, but has no data for the date range requested, an xts object with no rows under the name FOO if FOO is an invalid symbol (no data for any dates), NULL under name FOO

in general, i always like results to have all items under the exact names and order that i requested them, unless there is a specific reason not to do so

joshuaulrich commented 1 year ago

Thanks for your thoughts!

I like that the result is a zero-length xts object for valid symbols with no data. The object will have the correct columns, so it can be manipulated like all other elements.

Invalid symbols will throw a warning. We could make an option that the symbol is a null element (the default) or that it's removed.