Quantcast

lapply over list that has multiple xts objects

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

lapply over list that has multiple xts objects

algotr8der
I've been going through various examples on how to use lapply when the list in question has multiple parts but I can't seem to get things working.

I have a function that computes open_close volatility as follows:

close_open <- function (OHLC, n = 10, N = 252, ...)
{
    Cl1 <- lag(OHLC[, 4])
    OHLC <- try.xts(OHLC, error = as.matrix)
    s2o <- 18.59 * runSD(log(OHLC[, 1]/Cl1), n)
    reclass(s2o, OHLC)
}

I have a list object stocks that has 909 xts OHLC objects:

> class(stocks)
[1] "list"

> length(stocks)
[1] 909

I want to compute the open_close volatility for every component in the list stock by using lapply. I have tinkered with various variants of

lapply(stocks, function(x) open_close(x, n=10, N=252, ...))

I get errors like so:

Error in FUN(X[[1L]], ...) : '...' used in an incorrect context

Any guidance on how I can achieve my goal would be greatly appreciated. THank you.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lapply over list that has multiple xts objects

Jeffrey Ryan
Don't add the ... into your call.

open_close(x, n=10, N=252) not open_close(x, n=10, N=252, ...))

HTH
Jeff

Jeffrey Ryan    |    Founder    |    [hidden email]

www.lemnica.com

On Dec 28, 2011, at 5:29 PM, algotr8der <[hidden email]> wrote:

> I've been going through various examples on how to use lapply when the list
> in question has multiple parts but I can't seem to get things working.
>
> I have a function that computes open_close volatility as follows:
>
> close_open <- function (OHLC, n = 10, N = 252, ...)
> {
>    Cl1 <- lag(OHLC[, 4])
>    OHLC <- try.xts(OHLC, error = as.matrix)
>    s2o <- 18.59 * runSD(log(OHLC[, 1]/Cl1), n)
>    reclass(s2o, OHLC)
> }
>
> I have a list object stocks that has 909 xts OHLC objects:
>
>> class(stocks)
> [1] "list"
>
>> length(stocks)
> [1] 909
>
> I want to compute the open_close volatility for every component in the list
> stock by using lapply. I have tinkered with various variants of
>
> lapply(stocks, function(x) open_close(x, n=10, N=252, ...))
>
> I get errors like so:
>
> Error in FUN(X[[1L]], ...) : '...' used in an incorrect context
>
> Any guidance on how I can achieve my goal would be greatly appreciated.
> THank you.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/lapply-over-list-that-has-multiple-xts-objects-tp4241306p4241306.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> [hidden email] 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.

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lapply over list that has multiple xts objects

algotr8der
On 12/28/11 10:06 PM, Jeff Ryan wrote:
> Don't add the ... into your call.
>
> open_close(x, n=10, N=252) not open_close(x, n=10, N=252, ...))
>
> HTH
> Jeff
>
Thanks Jeff. That seemed to have fixed that issue. However, now I get
the following:

> lapply(stock, function(x) open_close(x, n=10, N=252))
debugging in: dimnames.xts(x)
debug: {
    .Call("dimnames_zoo", x)
}
Browse[2]>
debug: .Call("dimnames_zoo", x)
Browse[2]> c
Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) :
  'dimnames' applied to non-array

I tried to debug this but it seems internal to the lapply call. Any
idea? Appreciate the help.


> Jeffrey Ryan    |    Founder    |    [hidden email]
>
> www.lemnica.com
>
> On Dec 28, 2011, at 5:29 PM, algotr8der <[hidden email]> wrote:
>
>> I've been going through various examples on how to use lapply when the list
>> in question has multiple parts but I can't seem to get things working.
>>
>> I have a function that computes open_close volatility as follows:
>>
>> close_open <- function (OHLC, n = 10, N = 252, ...)
>> {
>>    Cl1 <- lag(OHLC[, 4])
>>    OHLC <- try.xts(OHLC, error = as.matrix)
>>    s2o <- 18.59 * runSD(log(OHLC[, 1]/Cl1), n)
>>    reclass(s2o, OHLC)
>> }
>>
>> I have a list object stocks that has 909 xts OHLC objects:
>>
>>> class(stocks)
>> [1] "list"
>>
>>> length(stocks)
>> [1] 909
>>
>> I want to compute the open_close volatility for every component in the list
>> stock by using lapply. I have tinkered with various variants of
>>
>> lapply(stocks, function(x) open_close(x, n=10, N=252, ...))
>>
>> I get errors like so:
>>
>> Error in FUN(X[[1L]], ...) : '...' used in an incorrect context
>>
>> Any guidance on how I can achieve my goal would be greatly appreciated.
>> THank you.
>>
>> --
>> View this message in context: http://r.789695.n4.nabble.com/lapply-over-list-that-has-multiple-xts-objects-tp4241306p4241306.html
>> Sent from the Rmetrics mailing list archive at Nabble.com.
>>
>> _______________________________________________
>> [hidden email] 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.

_______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lapply over list that has multiple xts objects

algotr8der
Seemed like there was a problem with my data in the list. Some of the list components were empty - seems like bloomberg populated some of the tickers with no data. I need to put in error checking to make sure the data is populated correctly.

Thank you Jeff for all your help.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: lapply over list that has multiple xts objects

gsee
These are the types of things that the list would have been much
better at helping you with if you had provided a reproducible example.
 Of course, in the process of trying to make the reproducible example,
you probably would have found the answer yourself. ;-)

On Fri, Dec 30, 2011 at 11:10 AM, algotr8der <[hidden email]> wrote:

> Seemed like there was a problem with my data in the list. Some of the list
> components were empty - seems like bloomberg populated some of the tickers
> with no data. I need to put in error checking to make sure the data is
> populated correctly.
>
> Thank you Jeff for all your help.
>
> --
> View this message in context: http://r.789695.n4.nabble.com/lapply-over-list-that-has-multiple-xts-objects-tp4241306p4246236.html
> Sent from the Rmetrics mailing list archive at Nabble.com.
>
> _______________________________________________
> [hidden email] 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.

_______________________________________________
[hidden email] 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.
Loading...