I have a similar issue to getSymbols (quantmod) giving wrong dates that does not solve by adding a TZ. My settings are:
R version 3.3.3 (2017-03-06)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows >= 8 x64 (build 9200)
locale:
[1] LC_COLLATE=English_Australia.1252 LC_CTYPE=English_Australia.1252
LC_MONETARY=English_Australia.1252 LC_NUMERIC=C
LC_TIME=English_Australia.1252
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] quantmod_0.4-13 TTR_0.23-2 xts_0.10-1 zoo_1.8-0
My timezone:
Sys.timezone()
> [1] "Australia/Sydney"
> Data <- getSymbols('BHP.AX',src="yahoo",auto.assign=FALSE, from = '2017-
10-10')
> weekdays(head(index(Data),20))
[1] "Monday" "Tuesday" "Wednesday" "Thursday" "Sunday" "Monday"
"Tuesday" "Wednesday" "Thursday" "Sunday" "Monday" "Tuesday"
"Wednesday" "Thursday" "Sunday"
[16] "Monday" "Tuesday" "Wednesday" "Thursday" "Sunday"
As you can see, the data returns Sundays. I've also seen similar returns using the Alpha Vantage option - Fridays are regularly omitted in the tail of the returned data. Any tips to avoid this would be appreciated!
I'm not sure of the cause, and without carefully inspecting the source I'd be speculating. And I would guess that because Monday in Australia (where the stock is traded, .AX
) typically corresponds to Sunday in North America, plus or minus a few hours depending on the time zone, the data is recorded using the North American date.
I also get that the Sun to Thurs dates are returned even if I set my timezone to US EST before making the data call.
Sys.timezone()
#[1] "America/New_York"
unique(weekdays(index(Data)))
# [1] "Monday" "Tuesday" "Wednesday" "Thursday" "Sunday"
But here is a solution to your problem:
library(lubridate)
index(Data) <- index(Data) + days(1)
unique(weekdays(index(Data)))
# [1] "Tuesday" "Wednesday" "Thursday" "Friday" "Monday"
Or without using lubridate
, if you are sure your time index is type "Date" and not "POSIXct", this also works (adding plus 1 increments the date):
index(Data) <- index(Data) + 1
The above shows that no Saturday or Sunday dates are returned, as expected (the stock doesn't trade weekends).
Adding one day gives sensible results. Look at the most recent dates after adjustment:
tail(Data)
BHP.AX.Open BHP.AX.High BHP.AX.Low BHP.AX.Close BHP.AX.Volume BHP.AX.Adjusted
2017-12-29 29.65 29.760 29.51 29.57 4428226 29.57
2018-01-02 29.57 29.750 29.50 29.68 3252955 29.68
2018-01-03 30.24 30.350 30.17 30.18 6788783 30.18
2018-01-04 30.42 30.605 30.30 30.33 5501131 30.33
2018-01-05 30.65 30.690 30.51 30.58 5835685 30.58
2018-01-08 30.55 30.660 30.44 30.55 3274512 30.55
> unique(weekdays(index(Data)))
2017-12-29 was a Friday. 2018-01-01 was a public holiday, and 2018-01-02 was a Tuesday.