[R-SIG-Finance] troubles with apply.daily

R. Michael Weylandt michael.weylandt at gmail.com
Mon Jan 30 05:02:10 CET 2012


I'm not sure time() is very good for what you want to do. It's tied to
R's builtin ts class, which, and it pains me to say this about R,
really isn't very good (at least for finance-y things). I think all
your problems come from that...

Perhaps construct your new index sequence as:

seq(start(alpha), end(alpha), by = "min")

Since you didn't supply example data, let's try this (admittedly
absurd) analysis to showcase how these techniques should work:

library(quantmod)
getSymbols("AAPL")
AAPL <- Cl(AAPL)

## Force to have daily (including non-trading days) points
AAPL <- na.approx(AAPL, xout = seq(start(AAPL), end(AAPL), by = "day"))

# check that it worked
head(AAPL, 20)

# Now we aggregate to weekly
AAPL.w <- to.weekly(AAPL)

# and now we apply a function monthly
apply.monthly(AAPL.w, max)

So everything seems to be in order. Does this help?

Michael


On Sun, Jan 29, 2012 at 12:51 PM, Ted Byers <r.ted.byers at gmail.com> wrote:
> I do not understand this, either to figure out the cause, let alone the fix.
>
>
>
> Here is what I tried:
>
>
>
> myfun <- function(d) {
>
>  str(d)
>
> }
>
> apply.daily(alpha,myfun)
>
>
>
> And here are what the beginning and end of alpha (an xts object created by
> to.minute()):
>
>
>
>> head(alpha)
>
>                        Open     High      Low Close
>
> 2011-03-10 00:00:00 10350.00 10365.00 10350.00 10360
>
> 2011-03-10 00:00:01 10350.06 10364.97 10350.06 10360
>
> 2011-03-10 00:00:02 10350.11 10364.94 10350.11 10360
>
> 2011-03-10 00:00:03 10350.17 10364.92 10350.17 10360
>
> 2011-03-10 00:00:04 10350.22 10364.89 10350.22 10360
>
> 2011-03-10 00:00:05 10350.28 10364.86 10350.28 10360
>
>> tail(alpha)
>
>                    Open High  Low Close
>
> 2011-06-08 23:55:55 9430 9430 9430  9430
>
> 2011-06-08 23:55:56 9430 9430 9430  9430
>
> 2011-06-08 23:55:57 9430 9430 9430  9430
>
> 2011-06-08 23:55:58 9430 9430 9430  9430
>
> 2011-06-08 23:55:59 9430 9430 9430  9430
>
> 2011-06-08 23:56:00 9430 9430 9430  9430
>
>>
>
>
>
> There is almost three months of tick data here, converted to one minute OHLC
> data.
>
>
>
> I had apparently successfully used the following to ensure I had an even
> time series with values for every minute from start to end:
>
>
>
> tseq <- seq(start(alpha),end(alpha), by = time("00:01:00"))
>
> alpha <- na.approx(alpha, xout = tseq)
>
>
>
> But there is something weird here.  How is it that alpha appears to have
> rows for every second from the start to the end, rather than 'just' for
> every minute?
>
>
>
> Now here is what the output looks like:
>
>
>
> An 'xts' object from 2011-06-05 to 2011-06-05 23:59:59 containing:
>
>  Data: num [1:86400, 1:4] 9437 9437 9437 9437 9437 ...
>
> - attr(*, "dimnames")=List of 2
>
>  ..$ : NULL
>
>  ..$ : chr [1:4] "Open" "High" "Low" "Close"
>
>  Indexed by objects of class: [POSIXct,POSIXt] TZ:
>
>  xts Attributes:
>
>  NULL
>
> An 'xts' object from 2011-06-06 to 2011-06-06 23:59:59 containing:
>
>  Data: num [1:86400, 1:4] 9420 9420 9420 9420 9420 ...
>
> - attr(*, "dimnames")=List of 2
>
>  ..$ : NULL
>
>  ..$ : chr [1:4] "Open" "High" "Low" "Close"
>
>  Indexed by objects of class: [POSIXct,POSIXt] TZ:
>
>  xts Attributes:
>
>  NULL
>
> An 'xts' object from 2011-06-07 to 2011-06-07 23:59:59 containing:
>
>  Data: num [1:86400, 1:4] 9428 9428 9428 9428 9428 ...
>
> - attr(*, "dimnames")=List of 2
>
>  ..$ : NULL
>
>  ..$ : chr [1:4] "Open" "High" "Low" "Close"
>
>  Indexed by objects of class: [POSIXct,POSIXt] TZ:
>
>  xts Attributes:
>
>  NULL
>
> An 'xts' object from 2011-06-08 to 2011-06-08 23:56:00 containing:
>
>  Data: num [1:86161, 1:4] 9435 9435 9435 9435 9435 ...
>
> - attr(*, "dimnames")=List of 2
>
>  ..$ : NULL
>
>  ..$ : chr [1:4] "Open" "High" "Low" "Close"
>
>  Indexed by objects of class: [POSIXct,POSIXt] TZ:
>
>  xts Attributes:
>
>  NULL
>
> Error in coredata.xts(x) : currently unsupported data type
>
>
>
> Now, I do not understand what is happening here.  The data seem consistent
> throughout, so why would it crash and burn on the very last day, and only on
> that day, of the three months of data
>
>
>
> Any insight would be greatly appreciated.
>
>
>
> Thanks
>
>
>
> Ted
>
>
>        [[alternative HTML version deleted]]
>
> _______________________________________________
> 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.



More information about the R-SIG-Finance mailing list