New to R coding

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #445
    tynashy
    Member

    I am new to R coding and I am trying to model the returns on the ftse100 since 1990. I have got a vector with all the closing values on each trading day. however, instead of using the difference in the closing values of two consecutive days, (ie dx=diff(x) where x is the vector containing the closing values), i wanted to use the quotient of the two closing values. I have tried the following without any luck

    > for (i in 2:n) {
    + dx[i]=(d[i])/(d[i-1])
    + delta=dx[i]
    + }

    where n=length(x).
    the vecotr x contains about 5900 values.
    how do i do this in R please.

    #447
    bryan
    Participant

    One of the most straightforward ways is to create a lagged version of your vector of closing prices and use that in the computation:

    x.lag <- c(NA, x[1:(length(x)-1)]) quotient <- x/x.lag

    Where 'x' is your vector of prices and your data is sorted by ascending date/time. There are other ways to do this, but this is fairly generalizable code that is easy to change to fit your needs.

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Scroll to top
Privacy Policy