I'm using the quantmod package to fetch stock data. The code
Data = getSymbols('LON:ADN',src="google",auto.assign=FALSE, from = '2011-08-10')
Results in an xts as expected, however on closer examination it shows a volume of trades for 2012-10-21 (October 21st) which was a sunday, and is therefore clearly erroneous. Several other sundays are also included. Unfortunately the errors surrounding the weekends seem to have moved the rest of the data out of alignment.
Has anyone experienced similar problems fetching tickers with quantmod before, and if so, are they aware of a way around them?
Thanks
As you mentioned in the comments, this looks like a timezone issue, possibly due to POSIX date conversion in the xts function (see this answer).
I am able to reproduce the issue in a fresh R session when Sys.getenv("TZ") is an empty character string. Setting the timezone to any valid timezone (not all tested), for example, "America/Chicago" yields expected dates, i.e., no Sundays:
In a fresh session (December 16th 2012 was a Sunday):
Sys.getenv("TZ")
# [1] ""
library(quantmod)
Data <- getSymbols('LON:ADN',src="google",auto.assign=FALSE, from = '2011-08-10')
tail(index(Data))
# [1] "2012-12-13" "2012-12-16" "2012-12-17" "2012-12-18" "2012-12-19" "2012-12-20"
Then change the timezone
Sys.setenv(TZ="America/Chicago")
Data <- getSymbols('LON:ADN',src="google",auto.assign=FALSE, from = '2011-08-10')
tail(index(Data))
# [1] "2012-12-14" "2012-12-17" "2012-12-18" "2012-12-19" "2012-12-20" "2012-12-21"
No Sundays.
Related
I'm getting an error when running getOptionChain from quantmod package.
The code should get Option chain data and subset into a new list that contains only the puts.
The error I get is: Error in .Date(Exp/86400) : could not find function ".Date"
Same code, sometimes runs without error. If I shorten the list of Symbols, there's no error, but the error as far as I know is not related to a specific symbol, because I made to run successfully. Seems random but frequent error.
All symbols are weekly expirations and the desired output is the next weekly expiration, so from my understanding, there's no need to specify a exp date.
library(quantmod)
library(xts)
Symbols<-c ("AA","AAL","AAOI","AAPL","ABBV","ABC","ABNB","ABT","ACAD","ACB","ACN","ADBE","ADI","ADM","ADP",
"ADSK","AEO","AFL","AFRM","AG","AGNC","AHT","AIG","AKAM","ALGN","AMAT","AMBA","AMC","AMD","AMGN",
"AMPX","AMRN","AMRS","AMZN","ANET","ANF","ANY","APA","APO","APPH","APPS","APRN","APT","AR","ARVL")
Options.20221118 <- lapply(Symbols, getOptionChain)
names(Options.20221118) <- Symbols
only_puts_list <- lapply(Options.20221118, function(x) x$puts)
After upgrading to R 4.2.2 the issue is fixed.
gmailr is a fantastic package. I can get messages corresponding to particular criteria, but I would also like to know for sure which message with given criteria is the latest. I got a bit stumped there.
If I do > gm_message(msgs[[c(1)]]$messages[[c(1)]]$id) I get output in console:
Id: 173193ce84e6e040
To: lemon.hwww#gmail.com
From: "Name" <name.surname#gmail.com>
Date: Sat, 4 Jul 2020 10:50:52 +0100
Subject: Test Rsend 2020-07-04
Attachments: 'caseload_data.csv'
I am not sure how I could easily access Date: - if I could then I could employ lubridate. When I look at the quite complex list structure of the message, I also find an internalDate - which I am not quite sure how to deal with and I do not know if it is the same as Date: above.
library(gmailr)
# I authenticate properly and can use gmailr functions:
# I get messages with a particular attachment and subject
srch <- "filename:caseload subject:Rsend newer_than:2d"
msgs <- gm_messages(search = srch)
# I need to determine unequivocally which is the newest
# internalDate gives me:
gm_message(msgs[[c(1)]]$messages[[c(1)]]$id)$internalDate
[1] "1593856252000"
# if I presume it is Unix epoch:
as.POSIXct(as.numeric(gm_message(msgs[[c(1)]]$messages[[c(1)]]$id)$internalDate), origin="1970-01-01")
# I get:
[1] "52477-04-17 08:46:40 BST"
# which is due to the extra zeros at the end of `internalDate` -
# but since I do not know the format and function of `internalDate`
# I am not sure how to best deal with them.
Advice on how to best write a function that would give me the newest message out of all messages retrieved based on some search criteria would be much appreciated. Or a clarification on how to get the message date and how it relates to internalDate would answer this question as well.
I'm trying to learn lubridate. The lubridate documentation shows:
date <- ceiling_date(date, "month") - days(1)
[1] "2010-05-31 UTC
for date arithmetic. But if I try
mytoday <- now()
first_of_month <- floor_date(mytoday, "month")
first_of_month_last_year <- first_of_month - years(1)
to use date arithmetic to get the first of the month a year earlier I get an error message
Warning message:
Incompatible methods ("-.POSIXt", "Ops.ordered") for "-"
Any ideas what I'm doing wrong? Thanks.
Ran into the same problem.
In my case it was that I had two date/time related packages installed (chron and lubridate), which appears to conflict with each other to some degree.
I solved this by removing chron as I wasn't really using it.
If you are using multiple date/time related packages, I would suggest removing these packages, then reinstalling them, with lubridate last. However, you may run into other conflicts.
Hope this helps.
I am trying to use the code on the quantmod website for a 3d graph. I followed the instructions and entered the year as 2010 (since 2008 link was not found). However, when I enter this command at the R prompt:
chartSeries3d0(TR)
I get the following error:
Error in if (on == "years") { : missing value where TRUE/FALSE needed
I am relatively new to R, so can anyone help me fix it.
The Treasury has re-designed its website since that example was created, so the code is not downloading any data (no matter what year you enter). Instead of using the getUSTreasuries function, you can just pull the data from FRED.
library(quantmod)
source("http://www.quantmod.com/examples/chartSeries3d/chartSeries3d.alpha.R")
getSymbols("DGS1MO;DGS3MO;DGS6MO;DGS1;DGS2;DGS3;DGS5;DGS7;DGS10;DGS20;DGS30",
src="FRED")
TR <- merge(DGS1MO,DGS3MO,DGS6MO,DGS1,DGS2,DGS3,DGS5,
DGS7,DGS10,DGS20,DGS30, all=FALSE)
colnames(TR) <- c("1mo","3mo","6mo","1yr","2yr","3yr","5yr",
"7yr","10yr","20yr","30yr")
TR <- na.locf(TR)
chartSeries3d0(TR["2011"])
The function Sys.Date() returns a date object, but on different machines running R2.12 I get different outputs
> Sys.Date()
[1] "08/17/2011 00:00:00.000 UTC"
vs.
> Sys.Date()
[1] "2011-08-17"
The former looks like a POSIX object, even though it's still a date object. What possible differences in settings between the two environments may cause this problem?
Could it be that the systems are set to different locales? Try
Sys.getlocale();
Another possible scenario is that some nefarious soul "overwrote" the Sys.Date() function with another. Try running
base::Sys.Date();
on both systems. If you have a package, other than base, that is loading a function called Sys.Date() then you should call base::Sys.Date() to be sure you get the right one.