简体   繁体   English

R-编程-get.current.chob()中的错误:图形设备设置不正确或丢失

[英]R- programming- Error in get.current.chob() : improperly set or missing graphics device

library(quantmod)
getSymbols("LT.NS")
plot(LT.NS["2013-12-01::2014-12-01"])
close<-Cl(LT.NS["2013-12-01::2014-12-01"])
open<-Op(LT.NS["2013-12-01::2014-12-01"])
close<-as.matrix(close)
open<-as.matrix(open)
bbands<-addBBands(n=20,sd=2)
values_bbands<-bbands@TA.values
values_bbands[is.na(values_bbands)]<-0
bbands<-as.matrix(values_bbands)
up<-bbands[,1]
up<-as.matrix(up)
down<-bbands[,3]
down<-as.matrix(down)
data<-read.table("c:\\temp\\dates.txt")
attach(data)
head(data)
stock<-as.matrix(data)

for(i in 131:261)
{
    if(close[i]>down[i])
    {
    print("the selling date is:")
    print(i)
    big.red.dot <- xts(open[i], as.Date(stock[i,1]))
    points(big.red.dot, col="red", pch=19, cex=0.5  )
    }
    if(close[i]<up[i])
    {
        print("the buying date is:")
        print(i)
        big.green.dot <- xts(open[i], as.Date(stock[i,1]))
        points(big.green.dot, col="green", pch=19, cex=0.5  )
    }

}

When I run this code in R , I get "****Error in get.current.chob() : improperly set or missing graphics device"****. 当我在R中运行此代码时,我得到“ **** get.current.chob()中的错误:设置不正确或缺少图形设备” ****。 2-3 times I could get an output with the proper graph with buy and sell signals indicated on the graph but now when I run the code this error gets displayed I tried it on a different version of R-1.3 too still the error appears. 我可以得到2-3次具有正确图形的输出,并在图形上指示了买卖信号,但是现在当我运行代码时,显示此错误,因此我在另一版本的R-1.3上尝试了此错误,但仍然出现错误。 In my above code is bbands<-addBBands(n=20,sd=2) appropriate? 在我上面的代码中bbands <-addBBands(n = 20,sd = 2)是否合适? Because when i run my code in individual lines the same error gets displayed for this line too. 因为当我在单独的行中运行我的代码时,也会为该行显示相同的错误。 I want final output to be a graph with buy and sell points indicated at respective points. 我希望最终输出是一个图表,其中在各个点处指示了买入和卖出点。

No, bbands <- addBBands(n=20,sd=2) is not appropriate. 不, bbands <- addBBands(n=20,sd=2)不适合。

addBBands should be called to add Bollinger Bands to an already-existing graphics device created by chartSeries . 应该调用addBBands将布林带添加到由chartSeries创建的现有图形设备中。 You can also include it directly in your chartSeries call: 您还可以将其直接包含在chartSeries调用中:

library(quantmod)
getSymbols("LT.NS")
chartSeries(LT.NS, TA="addBBands(n=20)", subset="2013-12-01::2014-12-01")

If you just want to calculate Bollinger Bands, just call TTR::BBands (which is what addBBands does). 如果只想计算布林带,只需调用TTR::BBandsaddBBands就是addBBands做的)。

bbands <- BBands(HLC(LT.NS), n=20, sd=2)

All the other stuff you're doing can be done with a couple calls to addPoints , after constructing the necessary objects to plot. 在构造必要的绘图对象之后,可以通过调用addPoints来完成您正在执行的所有其他操作。

# sells
sell <- Op(LT.NS)
is.na(sell) <- which(!Cl(LT.NS) > bbands$dn)
addPoints(1:nrow(sell), sell, col='red', pch=19, cex=0.5)
# buys
buy <- Op(LT.NS)
is.na(buy) <- which(!Cl(LT.NS) < bbands$up)
addPoints(1:nrow(buy), buy, col='green', pch=19, cex=0.5)

But note that your buys and sells are not mutually exclusive. 但是请注意,您的买卖并非互相排斥。

> head(cbind(buy,sell))
           LT.NS.Open LT.NS.Open.1
2007-01-01     1445.9       1445.9
2007-01-02     1447.0       1447.0
2007-01-03     1458.0       1458.0
2007-01-04     1489.7       1489.7
2007-01-05     1500.0       1500.0
2007-01-08     1471.0       1471.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM