Greconomist
Greconomist

Reputation: 396

Delete entries from Friday to Sunday on an xts object

My xts object looks like this:

                            BID     OFR     PRICE
    2015-01-01 13:15:00 1.48168 1.48285 0.3935712
    2015-01-01 13:20:00 1.48013 1.48102 0.3924305
    2015-01-01 13:25:00 1.47922 1.48012 0.3918190
    2015-01-01 13:30:00 1.47947 1.47970 0.3917616
    2015-01-01 13:35:00 1.48019 1.48046 0.3922617
       .
       .
       .
       .

I set the time zone to EST. How can I delete entries from Friday 17:05 , Eastern Time (EST) until Sunday 17:00, Easter Time (EST) as well? Thank you in advance.

Upvotes: 0

Views: 97

Answers (1)

FXQuantTrader
FXQuantTrader

Reputation: 6891

Explore the documentation in xts for ?.indexwday.

You could do this, to get the data you want for your specified (forex) trading hours, assuming dat contains your xts time series:

is_fri <- .indexwday(dat) == 5 & (.indexhour(dat) >= 18 | .indexhour(dat) == 17 & .indexmin(dat) >= 5)
is_sat <- .indexwday(dat) == 6 
is_sun <- .indexwday(dat) == 0 & .indexhour(dat) <= 16

xts_data_you_want <- dat[!(is_fri | is_sat | is_sun)]

.indexwday returns 1 for Monday, 2 for Tuesday, etc, for the timestamps in the xts object, and what should be self explanatory numeric values are also returned for .indexmin, .indexhour.

As a check, you can do weekdays(index(dat)) to see .indexwday is returning what you expect for certain timestamps.

Upvotes: 2

Related Questions