2011-01-31 78 views
3

親愛的名單, 我試圖在R的技術分析,使用包TTR,quantmod,動物園 我有黃金日價格數據的模樣:R:技術分析年度業績

> 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 

我通過隨機 指示器已經定義由日收益和信號產生的信號(在這種情況下唐契安通道)

命中率則

> 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 

這給了我整個期間的結果,但是我想看到命中 比率爲每年和特定時期(可以說100天) 我已經嘗試quantmod的功能apply.yearly(),但它沒有奏效。 此外,我也嘗試過

> years <- unique(substr(g[,"Date"],1,4)) 
Error in dimnames(cd) <- list(as.character(index(x)), colnames(x)) : 
    'dimnames' applied to non-array 

> 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" 

智能迴路中的任何想法將是有價值的(注:有必要 維持秩序XTS類來封裝的指標適當工作 TTR)

亞歷

+3

請提供一個自包含的,可重複的例子。即,通過`getSymbols(「GLD」)`而不是自己的數據使用來自GLD ETF的數據,並定義您使用的所有變量(`sig.dc`未定義)。另外,「我嘗試了XYZ函數,但它不起作用」並沒有幫助。請告訴我們*爲什麼*它不適合你。 – 2011-01-31 14:12:31

回答

2

你可以用做到這一點,但所有要按期間分割的數據都需要位於一個對象中,因爲apply.yearly僅分割了x而不是signal(或通過...傳遞的任何其他內容)。

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 

此外,TTR需要xts對象的「註釋」不正確。 TTR函數在內部使用xt,這允許他們處理大多數時間序列類(xt,zoo,timeSeries,chron,its,irts,fts等)以及data.frame,matrix和numeric/integer向量。如果對象強制爲xt,則TTR函數將返回給予它們的相同類的對象。

例如:

> 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

感謝您的幫助,明白了。 – Alex 2011-02-22 16:28:53