Calculate Returns over Period of Time

前端 未结 6 623
[愿得一人]
[愿得一人] 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:37

    You can use the ROC function in the TTR package, or you can just create your own function.

    > library(quantmod)  # loads TTR
    > getSymbols("SPY")
    > tail(ROC(Cl(SPY),20))
                SPY.Close
    2010-12-09 0.01350383
    2010-12-10 0.02307920
    2010-12-13 0.03563051
    2010-12-14 0.03792853
    2010-12-15 0.04904805
    2010-12-16 0.05432540
    > tail(log(Cl(SPY)/lag(Cl(SPY),20)))
                SPY.Close
    2010-12-09 0.01350383
    2010-12-10 0.02307920
    2010-12-13 0.03563051
    2010-12-14 0.03792853
    2010-12-15 0.04904805
    2010-12-16 0.05432540
    

提交回复
热议问题