这几天我在看Data Mining with R-Learning with Case Studies 这本书的第二章关于股价预测的实例。按照书第一步分代码定义预测T指标我总结成了脚本(附后)。运行时发现addT.ind和addAvgPrice这两个newTA的函数实例似乎没有显示结果。但是当把那两行函数调用的代码考到命令行里重新执行时却正确显示了。请教各位同学如何解决?

---------------------------

library(DMwR)

library(quantmod)

data(GSPC)

T.ind <- function(quotes, tgt.margin = 0.025, n.days = 10) {

v <- apply(HLC(quotes), 1, mean)

r <- matrix(NA, ncol = n.days, nrow = NROW(quotes))

for (x in 1:n.days) r[, x] <- Next(Delt(v, k = x), x)

x <- apply(r, 1, function(x) sum(x[x > tgt.margin | x <

-tgt.margin]))

if (is.xts(quotes))

xts(x, time(quotes))

else x

}

candleChart(last(GSPC, "6 months"), theme = "black", TA = NULL)

avgPrice <- function(p) apply(HLC(p), 1, mean)

addAvgPrice <- newTA(FUN = avgPrice, col = 'white', legend = "AvgPrice")

addT.ind <- newTA(FUN = T.ind, col = "red", legend = "tgtRet")

addAvgPrice(on = 1) // 此行以及下一行在脚本运行时不显示结果。

addT.ind()

-----------------------------------------

系统信息:

platform x86_64-pc-linux-gnu

arch x86_64

os linux-gnu

system x86_64, linux-gnu

status

major 3

minor 0.2

year 2013

month 09

day 25

svn rev 63987

language R

version.string R version 3.0.2 (2013-09-25)

nickname Frisbee Sailing

os Ubuntu 12.04.3