Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3951368
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T01:43:22+00:00 2026-05-20T01:43:22+00:00

Dear List, I am trying for technical analysis with R, using packages TTR, quantmod,

  • 0

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:

> 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 have defined signals generated by daily returns and signals by random
indicator (in this case donchian channels)

The hit ratio is then

> 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 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 apply.yearly(), but it did not work.
Moreover, I have 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 smart loop would be valuable (Note: It is necessary to
maintain xts class in order to proper work of indicators from package
TTR )

Alex

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T01:43:23+00:00Added an answer on May 20, 2026 at 1:43 am

    You can do this with apply.yearly, but all the data to be split by period needs to be in one object because apply.yearly only splits x and not signal (or anything else passed via ...).

    library(quantmod)
    getSymbols("GLD", from="2007-01-03", to="2011-01-28")
    
    set.seed(21)
    sig <- sign(runif(NROW(GLD)))
    
    hit.rate <- function(returnSignal) {
      N <- NROW(na.omit(returnSignal))
      HitRate <- sum(returnSignal[,1]*returnSignal[,2]>0, na.rm=TRUE)/N
      cbind(N,HitRate)
    }
    
    hit.rate(merge(ROC(Cl(GLD)),sig))
    #         N  HitRate
    # [1,] 1026 0.539961
    apply.yearly(merge(ROC(Cl(GLD)),sig), hit.rate)
    #            GLD.Close       sig
    # 2007-12-31       250 0.5560000
    # 2008-12-31       253 0.5256917
    # 2009-12-31       252 0.5277778
    # 2010-12-31       252 0.5634921
    # 2011-01-28        19 0.3684211
    

    Also, your “note” that TTR requires xts objects is incorrect. TTR functions use xts internally, which allows them to handle most time-series classes (xts, zoo, timeSeries, chron, its, irts, fts, etc.) as well as data.frame, matrix, and numeric/integer vectors. If the object is coercible to xts, TTR functions will return an object of the same class given to them.

    For example:

    > str(ROC(Cl(GLD)))
    An ‘xts’ object from 2007-01-03 to 2011-01-28 containing:
      Data: num [1:1027, 1] NA -0.01017 -0.0243 0.00514 0.0061 ...
     - attr(*, "dimnames")=List of 2
      ..$ : NULL
      ..$ : chr "GLD.Close"
      Indexed by objects of class: [Date] TZ: 
      xts Attributes:  
    List of 2
     $ src    : chr "yahoo"
     $ updated: POSIXct[1:1], format: "2011-01-31 08:41:53"
    > str(ROC(as.zoo(Cl(GLD))))
    ‘zoo’ series from 2007-01-03 to 2011-01-28
      Data: Named num [1:1027] NA -0.01017 -0.0243 0.00514 0.0061 ...
     - attr(*, "names")= chr [1:1027] "2007-01-03" "2007-01-04" "2007-01-05" "2007-01-08" ...
      Index: Class 'Date'  num [1:1027] 13516 13517 13518 13521 13522 ...
    > str(ROC(as.timeSeries(Cl(GLD))))
    Time Series:          
     Name:               object
    Data Matrix:        
     Dimension:          1027 1
     Column Names:       GLD.Close
     Row Names:          2007-01-03  ...  2011-01-28
    Positions:          
     Start:              2007-01-03
     End:                2011-01-28
    With:               
     Format:             %Y-%m-%d
     FinCenter:          GMT
     Units:              GLD.Close
     Title:              Time Series Object
     Documentation:      Mon Jan 31 08:48:35 2011
    > str(ROC(as.ts(Cl(GLD))))
     Time-Series [1:1027] from 1 to 1027: NA -0.01017 -0.0243 0.00514 0.0061 ...
    > str(ROC(as.data.frame(Cl(GLD))))
    'data.frame':   1027 obs. of  1 variable:
     $ GLD.Close: num  NA -0.01017 -0.0243 0.00514 0.0061 ...
    > str(ROC(as.matrix(Cl(GLD))))
     num [1:1027, 1] NA -0.01017 -0.0243 0.00514 0.0061 ...
     - attr(*, "dimnames")=List of 2
      ..$ : chr [1:1027] "2007-01-03" "2007-01-04" "2007-01-05" "2007-01-08" ...
      ..$ : chr "GLD.Close"
    > str(ROC(as.numeric(Cl(GLD))))
     num [1:1027] NA -0.01017 -0.0243 0.00514 0.0061 ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Dear all I have database of website link, it list out in main file
Dear experts, I was trying to dynamically generate DOM elements using JS. I read
dear all..i've a textfield, it using barcode scanner for input data..after scan it shows
I have two views of some data: a list view (a ListBox now, but
I have a list of customers that I send emails using an ActionMailer mailer.
I have a list of data, let's say client information (Name, Email, Amount Owing
dear all i want to know how to submit asp.net form data using jquery
Dear all, I have a question about Facebook Page: ( NOT user profile page,
Dear all,Now i have this question in my java program,I think it should be
Dear All, please help me since I'm newbie in SQL Server. I have a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.