r - Calculate cumulatve growth/drawdown from local min/max -
i'm learning r (and application trading tasks via quantmod lib) , looking through community pretty regularly lot of new knowledge , tricks here. impression r in general , quantmod lib in particular - it's awesome.
at point need help of seasoned r users. i'm using timeseries downloaded via getsymbols , need calculate cumulative growth/drawdown local minimum/maximum respectively.
i can solve task using cycles can necessary modelling in ms excel, want figure out more simple solution not require cycles , more "native" in r.
example. input data:
20121121 79810 20121122 79100 20121123 80045 20121126 81020 20121127 80200 20121128 81350 20121129 81010 20121130 80550 20121203 80780 20121204 81700 20121205 83705 20121206 83350 20121207 83800 20121210 85385
result:
close cumulative gr/dd 20121121 79810 n/a 20121122 79100 0.58% 20121123 80045 1.55% 20121126 81020 2.37% 20121127 80200 -0.10% 20121128 81350 0.06% 20121129 81010 -0.76% 20121130 80550 -0.82% 20121203 80780 0.73% 20121204 81700 3.78% 20121205 83705 5.19% 20121206 83350 -1.50% 20121207 83800 1.67% 20121210 85385 2.22%
the calculation in tseries bundle function maxdrawdown
. here origin of example:
mxdrwdr> # toy illustration mxdrwdr> x <- c(1:10, 9:7, 8:14, 13:8, 9:20) mxdrwdr> mdd <- maxdrawdown(x) mxdrwdr> mdd $maxdrawdown [1] 6 $from [1] 20 $to [1] 26
turning percentages pretty easy -- @ (short) code of function itself.
r time-series xts quantmod
No comments:
Post a Comment