quantmod icon indicating copy to clipboard operation
quantmod copied to clipboard

Unexpected Behavior in adjustOHLC

Open my-R-help opened this issue 8 years ago • 2 comments

The following two examples produce different results, although I initially thought they would/should yield identical results:

> x = getSymbols("GOOGL", from = "2014-04-02", to = "2014-04-04", auto.assign = F)
> adjustOHLC(x)
           GOOGL.Open GOOGL.High GOOGL.Low GOOGL.Close GOOGL.Volume GOOGL.Adjusted
2014-04-02    1141.90    1144.80   1124.00     1135.10      4168000       568.1181
2014-04-03     573.39     588.30    566.01      571.50      4018300       571.5000
2014-04-04     578.55     579.72    544.49      545.25      5363700       545.2500

> GOOGL = getSymbols("GOOGL", from = "2014-04-02", to = "2014-04-04", auto.assign = F)
> adjustOHLC(GOOGL)
           GOOGL.Open GOOGL.High GOOGL.Low GOOGL.Close GOOGL.Volume GOOGL.Adjusted
2014-04-02   571.5216    572.973  562.5626    568.1181      4168000       568.1181
2014-04-03   573.3900    588.300  566.0100    571.5000      4018300       571.5000
2014-04-04   578.5500    579.720  544.4900    545.2500      5363700       545.2500

I do understand that this is due to the fact that adjustOHLC deparses its first argument. But this kind of potential pitfall should be strongly emphasized in the documentation ?adjustOHLC, because it is not obvious at all from cursory reading. In fact, initially I (wrongly) thought that adjustOHLC grabs the symbol name from the column names of the xts object.

Alternatively (and I think this might be a better solution), one could add an attribute to the return value of getSymbols that keeps track of the symbol name and is later grabbed by adjustOHLC or other functions as needed.

my-R-help avatar Apr 22 '17 14:04 my-R-help

Thanks for the report. I agree that this is an issue, and it's going to occur more frequently once auto.assign = FALSE is the default in version 0.5-0.

I've thought about adding a "symbol" attribute to the xts object to keep track of the ticker symbol for that reason, and for tracking the original symbol when the ticker is re-mapped via setSymbolLookup(). That's going to require a bit more thought and work though.

Perhaps a quick patch would be to check the object column names against the deparsed symbol name, warn if they differ, and then use the column name symbol to import the split and dividend data.

joshuaulrich avatar Apr 22 '17 14:04 joshuaulrich

Yes, that sounds like a good idea. At least the user would get a warning that something might be wrong.

Another issue is when people merge two xts objects, e.g. after calculating the returns on each one of them. I think in that case it would make more sense to have a "symbol" attribute that keeps track of all the stocks that are in there. But I do understand that implementing this would be quite a bit more work.

my-R-help avatar Apr 22 '17 14:04 my-R-help