title: R: Technical analysis annual results
body: <p>Dear List,
  I am trying for technical analysis with R, using packages TTR, quantmod, zoo
  I have daily prices of gold, the data looks like:</p>
  
  <pre><code>&gt; library(quantmod)
  &gt; library(timeSeries)
  &gt; gold &lt;- read.csv("gold.csv")
  &gt; g &lt;- as.xts(gold, dateFormat = "Date")
  &gt; g.c&lt;-Cl(g)
  &gt; 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
  &gt; first(g)
              Open  High   Low Close
  1999-01-08 292.2 293.3 291.2   292
  &gt; last(g)
             Open   High    Low  Close
  2010-10-20 1332 1346.5 1330.8 1343.6
  </code></pre>
  
  <p>I have defined signals generated by daily returns and signals by random
  indicator (in this case donchian channels)</p>
  
  <p>The hit ratio is then</p>
  
  <pre><code>&gt; x&lt;-g.c
  &gt; timeseries.eval &lt;- function(x,signal) {
  +    returns &lt;- returns(x)
  +    hit.rate &lt;- function(x,signal) {
  +      rate&lt;- length(which(signal*returns&gt; 0))/length(x)
  +      rate
  +    }
  +  round(data.frame(N=length(x),HitRate=hit.rate(x,signal)),3)
  + }
  &gt; timeseries.eval(x, sig.dc)
       N HitRate
  1 3074   0.628
  </code></pre>
  
  <p>This gives me results for the whole period, however I want to see hit
  ratio for every year and also for specific period (lets say 100 days)
  I have tried quantmod´s function <code>apply.yearly()</code>, but it did not work.
  Moreover, I have also tried</p>
  
  <pre><code>&gt; years &lt;- unique(substr(g[,"Date"],1,4))
  Error in dimnames(cd) &lt;- list(as.character(index(x)), colnames(x)) : 
    'dimnames' applied to non-array
  </code></pre>
  
  <p>whereas </p>
  
  <pre><code>&gt; j&lt;-as.data.frame(g)
  &gt; years &lt;- unique(substr(y,1,4))
  &gt; years
  [1] "1999" "2000" "2001" "2002" "2003" "2004" "2005" "2006" "2007" "2008" "2009" "2010"
  </code></pre>
  
  <p>Any ideas for smart loop would be valuable (Note: It is necessary to
  maintain xts class in order to proper work of indicators from package
  TTR )</p>
  
  <p>Alex</p>