R:技术分析年度业绩
亲爱的名单, 我正在尝试使用 R 包进行技术分析 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
我定义了每日收益生成的信号和随机生成的信号 指标(在本例中为唐奇安通道)
命中率是
> 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)
亚历克斯
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
apply.yearly
来完成此操作,但要按时间段拆分的所有数据都需要位于一个对象中,因为apply.yearly
只拆分x 而不是
signal
(或通过...
传递的任何其他内容)。另外,您的 TTR 需要 xts 对象的“注释”是不正确的。 TTR 函数在内部使用 xts,这使得它们能够处理大多数时间序列类(xts、zoo、timeSeries、chron、its、irts、fts 等)以及 data.frame、矩阵和数字/整数向量。如果该对象可强制 xts,TTR 函数将返回给定它们的同一类的对象。
例如:
You can do this with
apply.yearly
, but all the data to be split by period needs to be in one object becauseapply.yearly
only splitsx
and notsignal
(or anything else passed via...
).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: