Calculate Returns over Period of Time

前端 未结 6 709
[愿得一人]
[愿得一人] 2021-02-04 09:51

I\'m trying to get a time series of returns for holding a certain asset for a specific time.

My dataframe looks like this:

Date          Price
1998-01-01         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-04 10:26

    Alternatively, if you are using the package xts, then life is made incredibly simple. This is a straight copy-paste of a function I wrote myself a while ago:

    ret<-function(x,k=1){
      return(diff(log(x),k))
    }
    

提交回复
热议问题