[R-SIG-Finance] xts, period.apply question
msalese
massimo.salese at gmail.com
Tue May 10 16:14:34 CEST 2011
Hi, I'm new to R and I'm learning Rmetrics library but here I wrote some code
using quantmod/xts.
#############################################################
R.version
_
platform x86_64-pc-linux-gnu
arch x86_64
os linux-gnu
system x86_64, linux-gnu
status
major 2
minor 13.0
year 2011
month 04
day 13
svn rev 55427
language R
version.string R version 2.13.0 (2011-04-13)
######################################################
> sessionInfo()
R version 2.13.0 (2011-04-13)
Platform: x86_64-pc-linux-gnu (64-bit)
locale:
[1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C
LC_TIME=en_US.UTF-8
[4] LC_COLLATE=en_US.UTF-8 LC_MONETARY=C
LC_MESSAGES=en_US.UTF-8
[7] LC_PAPER=en_US.UTF-8 LC_NAME=C LC_ADDRESS=C
[10] LC_TELEPHONE=C LC_MEASUREMENT=en_US.UTF-8
LC_IDENTIFICATION=C
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] quantmod_0.3-15 TTR_0.20-2 xts_0.8-0 zoo_1.6-5
[5] Defaults_1.1-1 fOptions_2110.78 fBasics_2110.79 MASS_7.3-13
[9] timeSeries_2130.92 timeDate_2130.93
loaded via a namespace (and not attached):
[1] grid_2.13.0 lattice_0.19-26 tools_2.13.0
#######################################################
running on :
######################################################
Linux plutok 2.6.32-31-generic #61-Ubuntu SMP Fri Apr 8 18:25:51 UTC 2011
x86_64 GNU/Linux
Distributor ID: Ubuntu
Description: Ubuntu 10.04.2 LTS
Release: 10.04
Codename: lucid
#######################################################
Here the code that to me semes to work fine:
######################################################àà
#Load quantmod library ---> call xts/zoo too
library(quantmod)
#download data from yahoo (Generali Assurance - Italian Company )
getSymbols(Symbols="G.MI",src="yahoo")
#define function to compute max relative drawdown
relMxDD<-function(x){
rdd<-cummax(log(x))-log(x)
rdd<-1-(1/exp(rdd))
max(rdd)
}
# apply relMxDD monthly
maxRelDD.sample<-period.apply(x=Cl(G.MI),endpoints(x=Cl(G.MI),on="months"),FUN=relMxDD)
#the same if you use
#apply.monthly(x=Cl(G.MI),FUN=relMxDD))
plot(maxRelDD.sample,type='h')
hist(x=maxRelDD.sample)
--
View this message in context: http://r.789695.n4.nabble.com/xts-period-apply-question-tp3511933p3512019.html
Sent from the Rmetrics mailing list archive at Nabble.com.
More information about the R-SIG-Finance
mailing list