This is a sample code that uses a similar data set:
library(portfolio)
p <- new("portfolioBasic", instant = as.Date("2004-12-31"), id.var = "symbol",
in.var = "price", sides = "long",
ret.var = "month.ret", data = dow.jan.2005)
a <- performance(p)@ret
b <- log(1 + a)
sum <- b + sum #the sum variable should accumulate all returns
The example above is very similar to the code I'm working on, except that it uses the dow.jan.2005 that's included in the portfolio
library.
I want to create a loop in R that calculates a certain function p <- new(... , data = "data20xx")
and this data20xx
that is used in the function should go from 2007 to 2017.
There are also two other functions that follow p
. The performance function performance(p)
calculates a percentage which then needs to be logarithmized and stored in a separate variable b
. The sum
variables keeps track of the cumulative log returns.
Here's the description of the performance
function:
Formal class 'performance' [package "portfolio"] with 6 slots
..@ ret : num -0.366
..@ profit : num 0
..@ missing.price : num NA
..@ missing.return: int 0
If i use performance(p)@ret
I get a number, but I can't use the logarithm on it.
How can I create this specific loop?