Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:技术分析年度结果_R_Time Series_Xts - Fatal编程技术网

R:技术分析年度结果

R:技术分析年度结果,r,time-series,xts,R,Time Series,Xts,亲爱的名单:, 我正在尝试使用TTR、quantmod和zoo包进行技术分析 我有黄金的每日价格,数据如下: > library(quantmod) > library(timeSeries) > gold <- read.csv("gold.csv") > g <- as.xts(gold, dateFormat = "Date") > g.c<-Cl(g) > head(g) Open High Low Cl

亲爱的名单:, 我正在尝试使用TTR、quantmod和zoo包进行技术分析 我有黄金的每日价格,数据如下:

> 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
鉴于

> 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"
>j年
[1] "1999" "2000" "2001" "2002" "2003" "2004" "2005" "2006" "2007" "2008" "2009" "2010"
任何关于智能环路的想法都是有价值的(注:有必要 维护xts类,以确保包装中的指示器正常工作 (TTR)


Alex

您可以使用
apply.yearly
来执行此操作,但是所有要按时段分割的数据都需要在一个对象中,因为
apply.yearly
只分割
x
,而不分割
信号
(或通过
..
传递的任何其他数据)


请提供一个独立的、可复制的示例。即,通过
getSymbols(“GLD”)
使用GLD ETF中的数据,而不是您自己的数据,并定义您使用的所有变量(
sig.dc
未定义)。此外,“我尝试了函数XYZ,但它不起作用”也没有帮助。请告诉我们为什么它对你不起作用。
> 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"
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
> 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 ...