Dear list, I am trying to conduct a technical analysis using R using the TTR, quantmod, zoo packages. I have daily gold prices, the data looks like this:
> library(quantmod)
> library(timeSeries)
> gold <- read.csv("gold.csv")
> g <- as.xts(gold, dateFormat = "Date")
> g.c<-Cl(g)
> head(g)
Open High Low Close
1999-01-08 292.2 293.3 291.2 292.0
1999-01-11 292.3 294.3 291.6 293.6
1999-01-12 292.2 292.5 288.0 289.3
1999-01-13 288.8 289.1 285.0 287.0
1999-01-14 287.4 287.4 285.0 287.4
1999-01-15 286.7 287.6 286.4 287.4
> first(g)
Open High Low Close
1999-01-08 292.2 293.3 291.2 292
> last(g)
Open High Low Close
2010-10-20 1332 1346.5 1330.8 1343.6
I determined the signals generated by daily income and random indicator signals (in this case, the Donchian channels)
Then hit rate
> x<-g.c
> timeseries.eval <- function(x,signal) {
+ returns <- returns(x)
+ hit.rate <- function(x,signal) {
+ rate<- length(which(signal*returns> 0))/length(x)
+ rate
+ }
+ round(data.frame(N=length(x),HitRate=hit.rate(x,signal)),3)
+ }
> timeseries.eval(x, sig.dc)
N HitRate
1 3074 0.628
This gives me results for the entire period, however I want to see the hit ratio for each year, as well as for a certain period (say, 100 days) I tried the quantmod function apply.yearly(), but it did not work. In addition, I also tried
> years <- unique(substr(g[,"Date"],1,4))
Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) :
'dimnames' applied to non-array
whereas
> j<-as.data.frame(g)
> years <- unique(substr(y,1,4))
> years
[1] "1999" "2000" "2001" "2002" "2003" "2004" "2005" "2006" "2007" "2008" "2009" "2010"
Any ideas for a smart cycle would be valuable (Note: you must maintain the xts class for the indicators from the TTR package to work properly)
Alex