由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 问个关于R的低级问题
相关主题
matlab取行数Shell script 问题
c++读写多个大文件的问题C++ string类输入数据的问题
ask a c question什么editor在windows底下比较好(visual studio除外)
这个怎么allocate memory?how to sed from grep output in c shell? (转载)
R似乎根本就没有认真考虑过global variable的改写问题请教一个perl的问题 (转载)
Usage of Grep???help!!!how to count the times a function is used
what's wrong with this scripts?variable passing?how to grep one of the strings?
How to fix this? /bin/ls: Argument list too longin unix
相关话题的讨论汇总
话题: tc话题: raw话题: grep话题: gsub话题: file
进入Programming版参与讨论
1 (共1页)
s********y
发帖数: 64
1
以下程序在执行opt <- yahoo.getAllOptions("IBM")后可以把IBM的option数据下载到
temporary file, 请问如何把temporary file的数据导出到一个TXT文件?
谢谢!
------------------------------------------------------------------
require(fCalendar)
require(fredImport)
## workaround for R 2.1.1:
Sys.timezone <- function ()
as.vector(Sys.getenv("TZ"))
yahoo.getOption <- function(ticker="QQQQ",maturity="2005-12",file="
tempfile01",method="internal",get.short.rate=TRUE) {
############################################################################
#####################
## get quotes for stock options from Yahoo ###
############################################################################
#####################
## ticker .. Ticker symbol for underlying stock
## maturity .. maturity date of option in format YYYY-MM
## file .. name of temporary file used to store data
## method .. used by 'download.file'
############################################################################
#####################
source <- "http://finance.yahoo.com/q/op?"
url <- paste(source,"s=",ticker,"&m=",maturity,sep="")
download.file(url = url, destfile = file, method = method)

raw <- scan(file,what="character",sep=">",quote="");

## throw away file

unlink(file)
start <- grep(" raw <- raw[start:length(raw)]

table.starts <- grep(" table.ends <- grep("
heads <- grep(" subtit <- grep(" expire <- grep("Expire at close",raw)

if(!all(length(heads) == 16,length(subtit) == 1,length(expire) == 2)
) stop("Unexpected data format")

info.range <- c(subtit,min(table.ends[table.ends > subtit]))
call.range <- c(heads[8]+2, min(table.ends[table.ends > heads[8]]))
put.range <- c(heads[16]+2, min(table.ends[table.ends > heads[16]]))

info.raw <- raw[info.range[1]:info.range[2]]
call.raw <- raw[call.range[1]:call.range[2]]
put.raw <- raw[put.range[1]:put.range[2]]

b.pos <- grep(" sm.pos <- grep("
stock.name <- gsub(" Stock.price <- as.double(gsub(" Quote.date <- gsub("On |:","",info.raw[sm.pos[1]+1])
Strike.date <- gsub("Expire at close *|

strike.timeDate <- timeDate(strptime(Strike.date,format="%a, %b %d,
%Y"), zone="NewYork")
# Quote.date.timeDate <- timeDate(Quote.date,format="%b %d",
FinCenter="NewYork")
Quote.date.timeDate <- timeDate(strptime(date(),format="%a %b %d %H:
%M:%S %Y"))

TTM <- ceiling(as.double(difftime(strike.timeDate,Quote.date.
timeDate,units="day")))

## remove HTML junk and commas

call.raw <- gsub("<.*$|,","",call.raw)
put.raw <- gsub("<.*$|,","",put.raw)

## discard blank and empty elements

call.raw <- call.raw[-grep("[[:blank:]]|^$",call.raw)]
put.raw <- put.raw[-grep("[[:blank:]]|^$",put.raw)]

## reshape to matrix

call.mat <- matrix(call.raw,ncol=8,byrow=TRUE)
put.mat <- matrix(put.raw,ncol=8,byrow=TRUE)
if(nrow(call.mat) == 0 || nrow(put.mat) == 0) {
warning(paste("No data available for ticker",ticker,", maturity"
, maturity))
return(NULL)
}

tc <- function(x) type.convert(x,na.string=c("N/A",""))

## convert types, make data frames

call.df <- data.frame(Strike = tc(call.mat[-1,1]), Symbol = tc(call.
mat[-1,2]), Last = tc(call.mat[-1,3]), Chg = tc(call.mat[-1,4]),
Bid = tc(call.mat[-1,5]), Ask = tc(call.mat[
-1,6]), Vol = tc(call.mat[-1,7]), Open.Int = tc(call.mat[-1,8]))
put.df <- data.frame(Strike = tc(put.mat[-1,1]), Symbol = tc(put.mat
[-1,2]), Last = tc(put.mat[-1,3]), Chg = tc(put.mat[-1,4]),
Bid = tc(put.mat[-1,5]), Ask = tc(put.mat[-1
,6]), Vol = tc(put.mat[-1,7]), Open.Int = tc(put.mat[-1,8]))


if(get.short.rate) {
Short.rate <- get.short.rate(TTM)
} else {
Short.rate <- NA
}


return(list(call=call.df,put=put.df,Stock.ticker=ticker,Quote.date=
Quote.date.timeDate,Strike.date=strike.timeDate,
Stock.name=stock.name,Stock.price=Stock.price,TTM=TTM,Short.rate=
Short.rate))

}
b***i
发帖数: 3043
2
temporary file 是什么?

【在 s********y 的大作中提到】
: 以下程序在执行opt <- yahoo.getAllOptions("IBM")后可以把IBM的option数据下载到
: temporary file, 请问如何把temporary file的数据导出到一个TXT文件?
: 谢谢!
: ------------------------------------------------------------------
: require(fCalendar)
: require(fredImport)
: ## workaround for R 2.1.1:
: Sys.timezone <- function ()
: as.vector(Sys.getenv("TZ"))
: yahoo.getOption <- function(ticker="QQQQ",maturity="2005-12",file="

c*******y
发帖数: 1630
3
先去找找有没有更好的程序,这个程序还在用QQQQ,不是QQQ,太古老了。

【在 s********y 的大作中提到】
: 以下程序在执行opt <- yahoo.getAllOptions("IBM")后可以把IBM的option数据下载到
: temporary file, 请问如何把temporary file的数据导出到一个TXT文件?
: 谢谢!
: ------------------------------------------------------------------
: require(fCalendar)
: require(fredImport)
: ## workaround for R 2.1.1:
: Sys.timezone <- function ()
: as.vector(Sys.getenv("TZ"))
: yahoo.getOption <- function(ticker="QQQQ",maturity="2005-12",file="

c***z
发帖数: 6348
4
this helps?
t=data.frame(rep("a",5), rep("b",5))
write.table(t,"z:\\try.txt",row.names=F,col.names=F,sep="\t", quotes=FALSE)
link:
https://stat.ethz.ch/pipermail/r-help/2009-April/194326.html
1 (共1页)
进入Programming版参与讨论
相关主题
in unixR似乎根本就没有认真考虑过global variable的改写问题
[合集] 统一的regex (grep, sed, awk, python,..)Usage of Grep???help!!!
如何用’sed‘ comment out 某一行。what's wrong with this scripts?variable passing?
请教一个模式匹配问题How to fix this? /bin/ls: Argument list too long
matlab取行数Shell script 问题
c++读写多个大文件的问题C++ string类输入数据的问题
ask a c question什么editor在windows底下比较好(visual studio除外)
这个怎么allocate memory?how to sed from grep output in c shell? (转载)
相关话题的讨论汇总
话题: tc话题: raw话题: grep话题: gsub话题: file