performanceanalytics - R: using apply.fromstart to calculate returns and standard deviation -
i find total return has made on period standard deviation of returns. have found function, apply.fromstart
in performanceanalytics
package, looks promising. am, however, having difficulty implementing it.
here have: dataframe containing various data, including return per period:
hourlydata
time position 2014-08-01 01:00:00 1.01 2014-08-01 02:00:00 0.99 2014-08-01 03:00:00 1.01 2014-08-01 04:00:00 1.02
i find total return in each period, follows:
period totalreturn 2014-08-01 01:00:00 1.01 2014-08-01 02:00:00 1.01*0.99 2014-08-01 03:00:00 1.01*.099*1.01 2014-08-01 04:00:00 1.01*.099*1.01*1.02
my code reads:
apply.fromstart(hourlydata[,2,drop = false],fun="*",width=1)
i find standard deviation of returns. code part reads follows:
apply.fromstart(hourlydata[,2,drop = false],fun="sd",width=1)
the data type of hourlydata$position
"zoo"
i getting following error: in zoo(na, order.by = as.date(time(r))) : methods “zoo” objects not work if index entries in ‘order.by’ not unique
i have checked, , there no duplicate in row names
here result running dput(hourlydata):
> structure(list(period = structure(c(1406844000, 1406847600, > 1406851200, 1406854800, 1406858400, 1406862000), class = c("posixct", > "posixt" )), login = c(173908l, 173908l, 173908l, 173908l, 173908l, > 173908l ), netexposureusd = c(2188640, 2188730, 2189230, 2189000, > 2188310, 2187710), equityusd = c(9303.51, 9237.82, 8582.18, 9074.76, > 9929.96, > 10743.57), unrealizedprofitusd = c(-31.64, -97.33, -752.97, -260.39, > 594.81, 1408.42), depositwithdrawal = c(0, 0, 0, 0, 0, 0), laggedequity = structure(c(0, > 9303.51, 9237.82, 8582.18, 9074.76, 9929.96), index = 1:6, class = "zoo"), > return = structure(c(0, -0.00706077598669755, -0.0709734547761268, > 0.0573956733603816, 0.0942394068823857, 0.0819348718423841 > ), index = 1:6, class = "zoo"), position = structure(c(1, > 0.992939224013302, 0.929026545223873, 1.05739567336038, 1.09423940688239, > 1.08193487184238), index = 1:6, class = "zoo"), sd = c(na, > na, 0.0390979887599392, 0.0641847560185966, 0.0867288719859795, > 0.0187573743033249)), .names = c("period", "login", "netexposureusd", "equityusd", "unrealizedprofitusd", > "depositwithdrawal", "laggedequity", "return", "position", "sd"), > row.names = c("2014-08-01 01:00:00", "2014-08-01 02:00:00", > "2014-08-01 03:00:00", "2014-08-01 04:00:00", "2014-08-01 05:00:00", > "2014-08-01 06:00:00"), class = "data.frame") > period login netexposureusd equityusd unrealizedprofitusd depositwithdrawal laggedequity > return position sd 2014-08-01 01:00:00 2014-08-01 01:00:00 > 173908 2188640 9303.51 -31.64 0 > 0.00 0.000000000 1.0000000 na 2014-08-01 02:00:00 2014-08-01 02:00:00 173908 2188730 9237.82 -97.33 > 0 9303.51 -0.007060776 0.9929392 na 2014-08-01 03:00:00 > 2014-08-01 03:00:00 173908 2189230 8582.18 > -752.97 0 9237.82 -0.070973455 0.9290265 0.03909799 2014-08-01 04:00:00 2014-08-01 04:00:00 173908 2189000 9074.76 -260.39 0 8582.18 > 0.057395673 1.0573957 0.06418476 2014-08-01 05:00:00 2014-08-01 05:00:00 173908 2188310 9929.96 594.81 > 0 9074.76 0.094239407 1.0942394 0.08672887 2014-08-01 06:00:00 > 2014-08-01 06:00:00 173908 2187710 10743.57 > 1408.42 0 9929.96 0.081934872 1.0819349 0.01875737
use efficient vectorized base r function cumprod
first desired result. while second result achieved (less efficiently) using simple *apply
loop
if want keep zoo
class,
cumprod(hourlydata$position) # 1 2 3 4 5 6 # 1.0000000 0.9929392 0.9224669 0.9754125 1.0673348 1.1547867
otherwise
cumprod(as.numeric(hourlydata$position)) ## [1] 1.0000000 0.9929392 0.9224669 0.9754125 1.0673348 1.1547867
for sd
(as proposed @akrun) (used vapply
instead of sapply
in order "squeeze" maximum performance out of it)
vapply(seq_len(nrow(hourlydata)), function(i) sd(hourlydata$position[1:i]), fun.value = double(1)) # [1] na 0.004992723 0.039097989 0.052519398 0.063598345 0.063156702
Comments
Post a Comment