R 中的 Quantmod 与日内外汇汇率的错误

发布于 2024-12-27 02:31:22 字数 2544 浏览 1 评论 0 原文

我在处理每小时的汇率数据时遇到问题。我通过以下方式从 csv 文件中读取:

csv 文件如下:

 Date,Open,High,Low,Close,Volume

 2011-08-11 03:00:00,1.41758,1.42205,1.41625,1.42174,8974 

 ...

 2011-08-12 04:00:00,1.42175,1.42413,1.42067,1.42172,7229

 ...

2011-12-30 05:00:00,1.42173,1.42341,1.42062,1.42171,6703

... 


raw<- read.delim2("~/R/Data/EURUSD60.csv",header=TRUE,sep=",") 

 stripday<-strptime(raw$DATE,format="%Y%m%d") 
 fxdata<-data.frame(stripday,raw) 

 write.table(fxdata,"~/R/Data/EURUSD60.csv",quote=FALSE,sep=",",row.names=FALSE) 

 EURUSD<-as.xts(read.zoo("~/R/Data/EURUSD60.csv",sep=",",format="%Y-%m-%d %R",tz="GMT",header=T))

然后我设置了几个指标和以下模型:

 myATR <- function(x) ATR(HLC(x))[,'atr'] 
 mySMI <- function(x) SMI(HLC(x))[,'SMI'] 
 myADX <- function(x) ADX(HLC(x))[,'ADX'] 
 myAroon <- function(x) aroon(x[,c('High','Low')])$oscillator 
 myBB <- function(x) BBands(HLC(x))[,'pctB'] 
 myChaikinVol<-function(x)Delt(chaikinVolatility(x[,c("High","Low")]))[,1] 
 myCLV <- function(x) EMA(CLV(HLC(x)))[,1] 
 myMACD <- function(x) MACD(Cl(x))[,2] 
 mySAR <- function(x) SAR(x[,c('High','Close')]) [,1] 
 myVolat <- function(x) volatility(OHLC(x),calc="garman")[,1] 
 myEMA10 <- function(x) EMA(Cl(x),n=10)[,1] 
 myEMA20 <- function(x) EMA(Cl(x),n=20)[,1] 
 myEMA30 <- function(x) EMA(Cl(x),n=30)[,1] 
 myEMA50 <- function(x) EMA(Cl(x),n=50)[,1] 
 myEMA60 <- function(x) EMA(Cl(x),n=60)[,1] 

 data.model <- specifyModel(Delt(Cl(EURUSD)) ~ 
 myATR(EURUSD) + mySMI(EURUSD) + myADX(EURUSD) + myAroon(EURUSD) + 
 myBB(EURUSD) + myChaikinVol(EURUSD) + myCLV(EURUSD) +myEMA10(EURUSD) +myEMA20(EURUSD)      +myEMA30(EURUSD) +myEMA50(EURUSD) + myEMA60(EURUSD) +
 CMO(Cl(EURUSD)) + EMA(Delt(Cl(EURUSD))) + 
 myVolat(EURUSD) + myMACD(EURUSD) + RSI(Cl(EURUSD)) + 
 mySAR(EURUSD) + runMean(Cl(EURUSD)) + runSD(Cl(EURUSD))) 

然后我尝试执行以下操作:

 Tdata.train <- as.data.frame(modelData(data.model, 
 data.window=c('2011-08-03','2011-12-30'))) 

这给了我以下错误:

 Warnings: 
 1: In which(index(model.data) >= as.Date(data.window[1], origin = "1970-01-01")) :
 incopatible methods ("Ops.POSIXt", "Ops.Date") für ">=" 
 2: In which(index(model.data) <= as.Date(data.window[2], origin = "1970-01-01")) :
 incopatible methods ("Ops.POSIXt", "Ops.Date") für "<=" 
 3: In max(i) : kein nicht-fehlendes Argument für max; gebe -Inf zurück 

有人可以吗告诉我我做错了什么?我想这很简单,我只是在这里很愚蠢。非常感谢你们!!!

I have a problem while working with hourly data of fx rates. I've read from a csv file, the following way:

csv-file like:

 Date,Open,High,Low,Close,Volume

 2011-08-11 03:00:00,1.41758,1.42205,1.41625,1.42174,8974 

 ...

 2011-08-12 04:00:00,1.42175,1.42413,1.42067,1.42172,7229

 ...

2011-12-30 05:00:00,1.42173,1.42341,1.42062,1.42171,6703

... 


raw<- read.delim2("~/R/Data/EURUSD60.csv",header=TRUE,sep=",") 

 stripday<-strptime(raw$DATE,format="%Y%m%d") 
 fxdata<-data.frame(stripday,raw) 

 write.table(fxdata,"~/R/Data/EURUSD60.csv",quote=FALSE,sep=",",row.names=FALSE) 

 EURUSD<-as.xts(read.zoo("~/R/Data/EURUSD60.csv",sep=",",format="%Y-%m-%d %R",tz="GMT",header=T))

I then set up a couple of indicators and the following model:

 myATR <- function(x) ATR(HLC(x))[,'atr'] 
 mySMI <- function(x) SMI(HLC(x))[,'SMI'] 
 myADX <- function(x) ADX(HLC(x))[,'ADX'] 
 myAroon <- function(x) aroon(x[,c('High','Low')])$oscillator 
 myBB <- function(x) BBands(HLC(x))[,'pctB'] 
 myChaikinVol<-function(x)Delt(chaikinVolatility(x[,c("High","Low")]))[,1] 
 myCLV <- function(x) EMA(CLV(HLC(x)))[,1] 
 myMACD <- function(x) MACD(Cl(x))[,2] 
 mySAR <- function(x) SAR(x[,c('High','Close')]) [,1] 
 myVolat <- function(x) volatility(OHLC(x),calc="garman")[,1] 
 myEMA10 <- function(x) EMA(Cl(x),n=10)[,1] 
 myEMA20 <- function(x) EMA(Cl(x),n=20)[,1] 
 myEMA30 <- function(x) EMA(Cl(x),n=30)[,1] 
 myEMA50 <- function(x) EMA(Cl(x),n=50)[,1] 
 myEMA60 <- function(x) EMA(Cl(x),n=60)[,1] 

 data.model <- specifyModel(Delt(Cl(EURUSD)) ~ 
 myATR(EURUSD) + mySMI(EURUSD) + myADX(EURUSD) + myAroon(EURUSD) + 
 myBB(EURUSD) + myChaikinVol(EURUSD) + myCLV(EURUSD) +myEMA10(EURUSD) +myEMA20(EURUSD)      +myEMA30(EURUSD) +myEMA50(EURUSD) + myEMA60(EURUSD) +
 CMO(Cl(EURUSD)) + EMA(Delt(Cl(EURUSD))) + 
 myVolat(EURUSD) + myMACD(EURUSD) + RSI(Cl(EURUSD)) + 
 mySAR(EURUSD) + runMean(Cl(EURUSD)) + runSD(Cl(EURUSD))) 

I then tried to do the following:

 Tdata.train <- as.data.frame(modelData(data.model, 
 data.window=c('2011-08-03','2011-12-30'))) 

This gave me the following error:

 Warnings: 
 1: In which(index(model.data) >= as.Date(data.window[1], origin = "1970-01-01")) :
 incopatible methods ("Ops.POSIXt", "Ops.Date") für ">=" 
 2: In which(index(model.data) <= as.Date(data.window[2], origin = "1970-01-01")) :
 incopatible methods ("Ops.POSIXt", "Ops.Date") für "<=" 
 3: In max(i) : kein nicht-fehlendes Argument für max; gebe -Inf zurück 

Can someone please please tell me what I am doing wrong? I guess it is something very simple and I'm just stupid here. Thank you guys very much!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

守护在此方 2025-01-03 02:31:22

问题出在 model.data 函数上,请尝试以下操作:

f3 <- function(...) as.POSIXct(paste(...), format = "%Y-%m-%d %H:%M")


    My_modelData <- function(x, data.window = NULL, exclude.training = FALSE)
{
  model.data <- [email protected]
  if (!is.null(data.window)) {
    if (length(data.window) > 2) {
      model.data <- model.data[index(model.data) %in% data.window]
    }
    else {
      start.date.index <- index(model.data[which(index(model.data) >=f3(data.window[1]))])
      end.date.index <- index(model.data[which(index(model.data) <= f3(data.window[2]))])
      date.range <- intersect(start.date.index, end.date.index)
      model.data <- model.data[as.POSIXct(date.range, origin="1970-01-01")]
    }
  }
  if (exclude.training == TRUE) {
    model.data <- model.data[!index(model.data) %in% [email protected]]
  }
  return(model.data)
} 

代码在这里找到:http://r.789695.n4.nabble.com/Quantmod-modelData-with-datetime-format-td3860416.html。我稍微修改了一下。

The problem lies in model.data function, please try this:

f3 <- function(...) as.POSIXct(paste(...), format = "%Y-%m-%d %H:%M")


    My_modelData <- function(x, data.window = NULL, exclude.training = FALSE)
{
  model.data <- [email protected]
  if (!is.null(data.window)) {
    if (length(data.window) > 2) {
      model.data <- model.data[index(model.data) %in% data.window]
    }
    else {
      start.date.index <- index(model.data[which(index(model.data) >=f3(data.window[1]))])
      end.date.index <- index(model.data[which(index(model.data) <= f3(data.window[2]))])
      date.range <- intersect(start.date.index, end.date.index)
      model.data <- model.data[as.POSIXct(date.range, origin="1970-01-01")]
    }
  }
  if (exclude.training == TRUE) {
    model.data <- model.data[!index(model.data) %in% [email protected]]
  }
  return(model.data)
} 

The code was found here: http://r.789695.n4.nabble.com/Quantmod-modelData-with-datetime-format-td3860416.html. I have modified it a little bit.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文