I can generate quarterly OHLC date from a daily time series:
library(quantmod)
getSymbols("SPY", from="2000-01-01", to=Sys.Date())
tail(SPY)
dfQ <- to.quarterly(SPY[,6])
tail(dfQ)
I can also generate the quarterly mean:
dfmean1 <- apply.quarterly(xts(SPY[,6]), FUN = mean)
tail(dfmean1)
However I am having problems merging the two, with an index showing the first date of the quarter (rather than the last date of the quarter).
Thank you for your help
I think you have two questions here. The first is how to have a mean column in OHLC quarterly data. The second is how to have datestamps for the start of each quarter, instead of "last" datestamps. The xts/quantmod packages assume you want "last" datestamps, so go with the flow, and just replace the datestamps at the end.
To have mean with OHLC I've found it best just to do the OHLC calculation myself. So instead of passing mean to apply.quarterly(), do this:
bars = apply.quarterly(xts(SPY[,6]), FUN = function(x){
d=coredata(x);
c(first(d),max(d),min(d),last(d),mean(d))
} )
colnames(bars)=c("open","high","low","close","mean")
This gives:
...
2013-09-30 159.71 171.28 159.56 167.10 165.9822
2013-12-31 168.43 184.69 164.59 184.69 176.1416
2014-01-08 182.92 183.52 182.36 183.52 183.0340
Then to fix the datestamps:
index(bars) = as.Date(as.yearqtr(index(bars)))
To understand that, start by looking at index(bars), then look at as.yearqtr(index(bars)), which gives:
[1] "2000 Q1" "2000 Q2" "2000 Q3" ...
... "2013 Q3" "2013 Q4" "2014 Q1"
Then, as luck would have it, as.Date() gives you the datestamp of the start of each quarter.
The final bit is to assign the new index back to the bars object with index(bars) = ... (or index(bars) <- ... if you prefer).
By the way, there is also a indexAt="lastof" or indexAt="firstof" parameter you could give to to.quarterly(). Experiment with this, but in my tests it was not quite useful enough.
Related
Let say I have date as follows:
Date = as.Date('2020-11-30')
Now I want to determine the quarter for this date, So I can use the zoo package
library(zoo)
as.yearqtr(Date). ### [1] "2020 Q4"
However I want to determine the quarter with respect to a date, say
Date1 = as.Date("2020-05-31")
So with respect to this date, the quarter of Date should be Q2.
Is there any way to set up the base in the quarter calculation?
Any pointer will be highly appreciated.
Thanks,
if we want to extract the quarter, use format
format(as.yearqtr(Date1), 'Q%q')
[1] "Q2"
Or if it is based on difference, try
paste0("Q", (as.yearqtr(Date) - as.yearqtr(Date1)) * 4)
[1] "Q2"
I am having some difficulty get the following reqHistoricalData request in IBrokers to work. I am try to feed a data frame of date time and stock tickers pair to retrieve the intraday tickers at 5 minute intervals for historical research. I am looking to retrieve specific pair of stock ticker and a date. I can get this to work on a manual input but I have a data frame of 950 of these to do and want to make this work in a loop. This done in r using the IBrokers package
Manual example that works
conn <- twsConnect()
stock_dat = reqHistoricalData(conn, twsSTK("XEC"), endDateTime = "20191223 16:00:00", duration = "1 D", barSize = '5 mins')
the "XEC" and "20191223 16:00:00" are as.character and returns an object xts object for that day.
My sample data for this example would be a data frame called PoDHist and has 2 columns with 4 observations
Stock END_Date_Time
SYY 20191206 16:00:00
JNJ 20191209 16:00:00
OMC 20191210 16:00:00
MOS 20191213 16:00:00
both columns are as.characters
I am trying to use a pmap function from purrr
bar_downloader <- function(Stock,endDateTime)
{
n=PoDHist2 %>%
five_min_bar_data = reqHistoricalData(conn, twsEquity=PoDHist2$Stock, endDateTime = PoDHist2$END_Date_Time, duration = "1 D", barSize = '5 mins') %>%
print(Stock)
}
pmap(list(PoDHist2$Stock),list(PoDHist2$END_Date_Time),bar_downloader)
I am getting an error
Error in 1:nrow(PoDHist2$Stock) : argument of length 0
I am also seeing some inconsistence in the input format for endDateTime. The IBrokers r package specifies as format as ’CCYYMMDD HH:MM:SS TZ’. However I was manually able to get this to work in YYMMDD HH:MM:SS format as a character and not a time object.
Any suggestions would be helpful
I am working with a dateframe (INPUT) that contains number the of transaction of a product per calendar quarter. The first column (DATE) contains the calendar quarter in this format "2016 Q2". I would like to transform this date into the a financial quarter format such as "2016/17 Q1". The financial year start in the 1st April.
I came up with the following code which does the job, but I was wondering if there is a formula or a neater code that I could use.
INPUT$FY_Date=character(nrow(INPUT))
for (i in 1:nrow(INPUT)) {
INPUT$FY_Date[i]= if(substr(INPUT$DATE[i],7,7)==1) paste(as.numeric(substr(INPUT$DATE[i],1,4))-1,"/",substr(INPUT$DATE[i],3,4)," Q4",sep="") else
paste(substr(INPUT$DATE[i],1,4),"/", formatC(as.numeric(substr(INPUT$DATE[i],3,4))+1,width=2,format="d",flag=0)," Q",as.numeric(substr(INPUT$DATE[i],7,7))-1,sep="")
}
I could not find any previous related posts so I would appreciate any guidance.
Using the "yearqtr" class defined in zoo we can do it in two lines of code.
Convert to "yearqtr". The "yearqtr" class uses an internal representation of year + (qtr-1)/4 where qtr is 1, 2, 3 or 4 so adding 3/4 will shift it to the year-end year and fiscal quarter. Then in the final line of code as.integer will extract the year-end year. format function can be used to get the rest where %y means 2 digit year and %q means quarter.
library(zoo)
# test input
yq <- c("2016 Q2", "2016 Q3", "2016 Q4", "2017 Q1")
fyq <- as.yearqtr(yq, format = "%Y Q%q") + 3/4
paste0(as.integer(fyq) - 1, format(fyq, "/%y Q%q"))
giving:
[1] "2016/17 Q1" "2016/17 Q2" "2016/17 Q3" "2016/17 Q4"
Note that if you don't need the specific format shown in the question you could just use format(fyq) in place of the last line or maybe format(fyq, "%Y Q%q").
Update: Minor code improvements.
Forgive me for this basic question. I have loaded a set of data as timeSeries in R.
> class(Return)
[1] "timeSeries"
attr(,"package")
[1] "timeSeries"
> head(Return[,1])
GMT
Overall
2005-09-21 1.8714
2005-09-22 0.2049
2005-09-23 -1.5924
2005-09-26 -4.3111
2005-09-27 -0.2416
2005-09-28 -1.1924
When I plot this time series data, it gives me a figure with date as the label of x-axis with format "2006-01-01", "2007-01-01". How can I customise it as "2006-01" or "2006" or "2006 Jan" and how can I modify the frequency? For example I'd like to have a tick every half year instead of every year?
Any suggestion? Thank you!
For the label format you can use the format parameter (for info about the format options have a look at this page):
plot(Ts,format="%Y-%m") # 2006-01
plot(Ts,format="%Y-%b") # 2006-Jan
plot(Ts,format="%Y") # 2006
While for the labels, you can set custom labels by using the at parameter, e.g. :
# compute the desired dates to show:
minDate <- timeCalendar(y=as.integer(format(min(time(Ts)),'%Y')),m=1,d=1)
maxDate <- max(time(Ts))
datesToShow = timeSequence(from=minDate,to=maxDate,by="1 year")
plot(Ts,format="%Y-%m",at=datesToShow)
For more info about the plot parameters for timeSeries objects, just type:
?timeSeries::plot
I have a set of one year finacial data. The data is collected in working days.
Is there any way in R to assign to each data point a date given that the first data point was collected for eaxmple on juanari the 3th.
You need to take two steps to get to a solution:
Create a sequence of dates using seq.Date
Use wday to calculate the day of the week and remove all days with value 1 (Sunday) and 7 (Saturday)
The code and results:
startdate <- as.Date("2011-01-03")
dates <- seq(startdate, by="1 day", length.out=15)
dates[wday(dates) != 1 & wday(dates) != 7]
[1] "2011-01-03" "2011-01-04" "2011-01-05" "2011-01-06" "2011-01-07"
[6] "2011-01-10" "2011-01-11" "2011-01-12" "2011-01-13" "2011-01-14"
[11] "2011-01-17"
PS. You will have two keep a separate lists of holidays in your region and remove these from the list.
The timeDate package offers functions to extract business days in whatever financial center you happen to favor (there are almost 500 such financialcenters in their classification).