|
|
|
|
|
|
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 |
|