R 使用 httr 请求数据
httr 封装了 curl,使用比较简单
body <- list(method='get_k_data', jsonrpc='2.0', params=list(code='000651'), id=0) response <- POST("localhost:4000", add_headers('Content-Type'='application/json'), body=body, encode = "json", verbose())
查看 http 信息
headers(response) cookies(response) content(response)
转换数据为时间序列:
alldata <- as.xts(data[, -1], order.by = as.Date(data$date, format = '%Y-%m-%d'))
通过 RPC 获取数据并画图:
#! /usr/bin/env Rscript library('getopt') #get options, using the spec as defined by the enclosed list. #we read the options from the default: commandArgs(TRUE). spec = matrix(c( 'code', 'c', 1, "character", 'from', 'f', 1, "character", 'to', 't', 1, "character", 'help', 'h', 0, "logical" ), byrow=TRUE, ncol=4) opt = getopt(spec) # if help was asked for print a friendly message # and exit with a non-zero error code if ( !is.null(opt$help) ) { cat(getopt(spec, usage=TRUE)) q(status=1) } library("xts") library("zoo") library("TTR") library("quantmod") library("jsonlite") library('httr') getDataFromRPC <- function(code, start, end) { body <- list(method='get_k_data', jsonrpc='2.0', params=list(code=code, start=start, end=end), id=0) response <- POST("localhost:4000", add_headers('Content-Type'='application/json'), body=body, encode = "json") data <- fromJSON(content(response)$result) result <- as.xts(data[, -1], order.by=as.Date(data$date, format='%Y-%m-%d')) return(result) } png(paste('./', opt$code, opt$from, opt$to, '.png'), width = 2300, height=1060) # data = getSymbols(opt$code, from=opt$from, to=opt$to, auto.assign=FALSE) data <- getDataFromRPC(opt$code, opt$from, opt$to) chartSeries(data) addBBands() addMACD() addRSI() dev.off()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论