Skip to content

Create a Monday-Friday time series?

8 messages · Murali.Menon at avivainvestors.com, Gabor Grothendieck, Mark Knecht

#
Hi,
   I think I'm going to have a couple of questions but I figured I'd
start with (I hope) a very simple one. I've not worked with time
series objects before so I'm trying to learn them. I've been looking
at things like ?xts, ?zoo as well as Phil Proctor's book but so far no
luck.

   Starting from this little code stub I'd like to create a Monday
through Friday time series into which I can eventually place trading
system results.

   Here's the stub:

TStoDate = function (TSDate) {
	X = strptime(TSDate + 19e6L, "%Y%m%d")
	return(as.Date(X))
}

# TradeStation date format
FirstDate = 1100401
LastDate = 1100601

#Dates
StartDate = TStoDate(FirstDate)
EndDate = TStoDate(LastDate)

StartDate
EndDate

   How can I do this, and (if possible and easy) is there a way to
exclude U.S. holidays?

   I did discover

R1 <- as.zoo(StartDate:EndDate )

does create an object, but the values are numbers which I don't
understand but suspect are some internal R code for a date, and it
appears to have enough entries to include weekends.

   Anyway, hopefully this is clear enough to describe what I'm trying
to do and where I'm stuck.

Thanks,
Mark
#
Hi,

For US holidays, the timeDate package in Rmetrics has a function holidayNYSE() to which you pass a numeric range for years, e.g. holidayNYSE(1980:1990).

Cheers,
Murali

-----Original Message-----
From: r-sig-finance-bounces at r-project.org [mailto:r-sig-finance-bounces at r-project.org] On Behalf Of Mark Knecht
Sent: 06 January 2011 17:19
To: R-Finance
Subject: [R-SIG-Finance] Create a Monday-Friday time series?

Hi,
   I think I'm going to have a couple of questions but I figured I'd
start with (I hope) a very simple one. I've not worked with time
series objects before so I'm trying to learn them. I've been looking
at things like ?xts, ?zoo as well as Phil Proctor's book but so far no
luck.

   Starting from this little code stub I'd like to create a Monday
through Friday time series into which I can eventually place trading
system results.

   Here's the stub:

TStoDate = function (TSDate) {
	X = strptime(TSDate + 19e6L, "%Y%m%d")
	return(as.Date(X))
}

# TradeStation date format
FirstDate = 1100401
LastDate = 1100601

#Dates
StartDate = TStoDate(FirstDate)
EndDate = TStoDate(LastDate)

StartDate
EndDate

   How can I do this, and (if possible and easy) is there a way to
exclude U.S. holidays?

   I did discover

R1 <- as.zoo(StartDate:EndDate )

does create an object, but the values are numbers which I don't
understand but suspect are some internal R code for a date, and it
appears to have enough entries to include weekends.

   Anyway, hopefully this is clear enough to describe what I'm trying
to do and where I'm stuck.

Thanks,
Mark

_______________________________________________
R-SIG-Finance at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-sig-finance
-- Subscriber-posting only. If you want to post, subscribe first.
-- Also note that this is not the r-help list where general R questions should go.
#
On Thu, Jan 6, 2011 at 9:29 AM, <Murali.Menon at avivainvestors.com> wrote:
Thanks Murali. I'd not check out Rmetrics before so the install
procedure threw me a little bit, but I got it installed and it looks
helpful on the holiday part of my problem.

Cheers,
Mark
#
On Thu, Jan 6, 2011 at 12:29 PM, <Murali.Menon at avivainvestors.com> wrote:
To add to this try:

library(timeDate) # holidayNYSE
library(chron) # .Holidays / is.holiday / is.weekend

# this will be used by is.holiday
.Holidays <- chron(as.Date(holidayNYSE(year = 2010)))

StartDate <- as.Date("2010-01-01")
EndDate <- as.Date("2010-04-01")

dd <- seq(StartDate, EndDate, by = "day")

dd[!is.weekend(dd) & !is.holiday(dd)]
#
Thanks Gabor. That seems to work nicely.

What is chron trying to tell me about the origin? Is that specific to
my machine or did you see it also?

I am using 2.12.1.

Also, what's the command to package up some real data from a
data.frame so that it survives being emailed? When I respond back
later I want to include a few real trades so you can recreate whatever
I'm seeing.

Cheers,
Mark

On Thu, Jan 6, 2011 at 9:55 AM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
<SNIP>
<SNIP>
Error in convert.dates(dates., format = fmt, origin. = origin.) :
  object dates. must be numeric or character
[1] "2010-01-01" "2010-01-04" "2010-01-05" "2010-01-06" "2010-01-07"
 [6] "2010-01-08" "2010-01-11" "2010-01-12" "2010-01-13" "2010-01-14"
[11] "2010-01-15" "2010-01-18" "2010-01-19" "2010-01-20" "2010-01-21"
[16] "2010-01-22" "2010-01-25" "2010-01-26" "2010-01-27" "2010-01-28"
[21] "2010-01-29" "2010-02-01" "2010-02-02" "2010-02-03" "2010-02-04"
[26] "2010-02-05" "2010-02-08" "2010-02-09" "2010-02-10" "2010-02-11"
[31] "2010-02-12" "2010-02-15" "2010-02-16" "2010-02-17" "2010-02-18"
[36] "2010-02-19" "2010-02-22" "2010-02-23" "2010-02-24" "2010-02-25"
[41] "2010-02-26" "2010-03-01" "2010-03-02" "2010-03-03" "2010-03-04"
[46] "2010-03-05" "2010-03-08" "2010-03-09" "2010-03-10" "2010-03-11"
[51] "2010-03-12" "2010-03-15" "2010-03-16" "2010-03-17" "2010-03-18"
[56] "2010-03-19" "2010-03-22" "2010-03-23" "2010-03-24" "2010-03-25"
[61] "2010-03-26" "2010-03-29" "2010-03-30" "2010-03-31" "2010-04-01"
#
On Thu, Jan 6, 2011 at 12:55 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
That should as.chron, rather than chron, i.e.

.Holidays <- as.chron(as.Date(holidayNYSE(year = 2010)))

  
    
#
On Thu, Jan 6, 2011 at 1:33 PM, Gabor Grothendieck
<ggrothendieck at gmail.com> wrote:
<SNIP>
Thanks.

- Mark
#
On Thu, Jan 6, 2011 at 2:51 PM, Mark Knecht <markknecht at gmail.com> wrote:
See ?dput