-
Notifications
You must be signed in to change notification settings - Fork 5
/
getYHOC_Ex.R
81 lines (77 loc) · 3.41 KB
/
getYHOC_Ex.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
require("jsonlite");require("plyr");require("data.table")
getYHOOOC = function(symbol,x)
{
url <- paste0('https://query2.finance.yahoo.com/v7/finance/options/',symbol,
'?&date=',x)
chain <- fromJSON(url)
chain <- as.vector(chain)
# CALLS
CALLS <- as.data.frame(rbindlist(chain$optionChain$result[[1]]$options[[1]]$calls, use.names = TRUE,fill = TRUE))
# FIX THE TIMESTAMPS
CALLS$lastTradeDate <- as.Date(as.POSIXct(as.numeric(as.character(CALLS$lastTradeDate)),
origin = "1970-01-01",tz="EST"))
CALLS$expiration <- as.Date(as.POSIXct(as.numeric(as.character(CALLS$expiration)),
origin = "1970-01-01",tz="EST"))
# CALL/PUT
NOMS <- c(names(CALLS),"type")
CALLS <- cbind(CALLS,as.data.frame(rep("c",nrow(CALLS))))
colnames(CALLS) <- NOMS
# PUTS
PUTS <- as.data.frame(rbindlist(chain$optionChain$result[[1]]$options[[1]]$puts,use.names = TRUE,fill=TRUE))
# FIX THE TIMESTAMPS
PUTS$lastTradeDate <- as.Date(as.POSIXct(as.numeric(as.character(PUTS$lastTradeDate)),
origin = "1970-01-01",tz="EST"))
PUTS$expiration <- as.Date(as.POSIXct(as.numeric(as.character(PUTS$expiration)),
origin = "1970-01-01",tz="EST"))
# CALL/PUT
NOMS <- c(names(PUTS),"type")
PUTS <- cbind(PUTS,as.data.frame(rep("p",nrow(PUTS))))
colnames(PUTS) <- NOMS
# CALLS/PUTS COMBINED
OC <- rbind(CALLS,PUTS)
OC
}
getOC = function(x)
{
symbol <- as.character(x)
# TEST
url <- paste0('https://query2.finance.yahoo.com/v7/finance/options/',symbol,
'?&date=1471564800')
chain <- try(fromJSON(url))
if(!inherits(chain,'try-error'))
{
# ALL EXPIRATIONS DATES
chain <- as.vector(chain)
EXP <- chain$optionChain$result[[1]]$expirationDates
# apply a function
all <- lapply(as.list(EXP), function(x){
tmp <- try(getYHOOOC(symbol=symbol, x))
if(!inherits(tmp,'try-error'))
tmp
})
# COMBINE ALL EXPIRATIONS
ALL <- rbindlist(all,use.names = TRUE, fill = TRUE)
# GET ALL THE METADATA
LIST1 <- chain$optionChain$result[[1]]$quote
METADATA <- as.data.frame(do.call(cbind,LIST1))
# WANT <- names(METADATA)
WANT <- c("regularMarketChangePercent","regularMarketPreviousClose","bid","ask",
"bidSize","askSize","averageDailyVolume3Month","averageDailyVolume10Day",
"fiftyTwoWeekLowChange","fiftyTwoWeekLowChangePercent","fiftyTwoWeekHighChange",
"fiftyTwoWeekHighChangePercent","fiftyTwoWeekLow","fiftyTwoWeekHigh",
"dividendDate","earningsTimestamp","trailingAnnualDividendRate","trailingPE",
"epsTrailingTwelveMonths","epsForward","sharesOutstanding",
"twoHundredDayAverageChangePercent","bookValue","fiftyDayAverage",
"fiftyDayAverageChange","fiftyDayAverageChangePercent","twoHundredDayAverage",
"twoHundredDayAverageChange","marketCap","forwardPE","priceToBook",
"preMarketChange","preMarketChangePercent","preMarketTime","preMarketPrice",
"regularMarketPrice","regularMarketTime","regularMarketChange",
"regularMarketOpen","regularMarketDayHigh","regularMarketDayLow",
"regularMarketVolume","symbol"
)
METADATA <- METADATA[,names(METADATA) %in% WANT]
ALL <- rbind.fill(cbind(ALL,METADATA))
ALL
}
}
OC <- getOC("FB")