为此,我使用此脚本通过API {https://api.coindesk.com/v1/bpi/currentprice/USD.json”动态获取财务数据
library("rjson")
json_file <- "https://api.coindesk.com/v1/bpi/currentprice/USD.json"
numOfTimes <- 2L # how many times to run in total
sleepTime <- 60L # time to wait between iterations (in seconds)
iteration <- 0L
while (iteration < numOfTimes) {
# gather data
json_data <- fromJSON(paste(readLines(json_file), collapse=""))
# get json content as data.frame
x = data.frame(json_data$time$updated,json_data$time$updatedISO,json_data$time$updateduk,json_data$bpi$USD)
# create file to save in 'C:/Myfolder'
# alternatively, create just one .csv file and update it in each iteration
nameToSave <- nameToSave <- paste('C:/Myfolder/',
gsub('\\D','',format(Sys.time(),'%F%T')),
'json_data.csv', sep = '_')
# save the file
write.csv(x, nameToSave)
# update counter and wait
iteration <- iteration + 1L
Sys.sleep(sleepTime)
}
此脚本由Win8.1中的调度程序每分钟由cmd文件运行
"C:\Program Files\R\R-3.5.1\bin\x64\Rcmd.exe" BATCH "E:\Rdev\bitcoin.R"
带有csv的文件夹因此由新的csv动态更新。
假设我创建了GARCH-ARMA模型
require(rugarch)
#We can then compute the ARMA(1,1)-GARCH(1,1) model as an example:
spec <- ugarchspec(variance.model = list(model = "sGARCH",
garchOrder = c(1, 1),
submodel = NULL,
external.regressors = NULL,
variance.targeting = FALSE),
mean.model = list(armaOrder = c(1, 1),
external.regressors = NULL,
distribution.model = "norm",
start.pars = list(),
fixed.pars = list()))
garch <- ugarchfit(spec = spec, data = df$rate_float, solver.control = list(trace=0))
现在我这样做
predict(garch, n.ahead = 5)
我得到了错误
Error in UseMethod("predict") :
no applicable method for 'predict' applied to an object of class "c('uGARCHfit', 'GARCHfit', 'rGARCH')"
答案 0 :(得分:2)
似乎您只是在使用错误的功能。试试吧
ugarchforecast(garch, n.ahead = 5)