Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option Greeks #32

Open
rpm3o opened this issue May 11, 2022 · 8 comments
Open

Option Greeks #32

rpm3o opened this issue May 11, 2022 · 8 comments

Comments

@rpm3o
Copy link

rpm3o commented May 11, 2022

In the TWS API for Python and other languages, I've found documentation on returning greek values for options. Is it possible to do this with the R API? Thank you for the package.

@cholzer68
Copy link

This works for me.

eWrapper.data.Opt_Model <- function(n) {
eW <- eWrapper(NULL) # use basic template
eW$assign.Data("data", rep(list(structure(.xts(matrix(rep(NA_real_,8),nc=8),0),
.Dimnames=list(NULL,c('impVol','delta','TV','pvDiv','gamma','vega','theta','undPrice')))),n))

eW$tickOptionComputation <- function(curMsg, msg, timestamp, file, ...)
{
tickType = msg[3]
msg <- as.numeric(msg)
id <- msg[2] #as.numeric(msg[2])
data <- eW$get.Data("data") #[[1]] # list position of symbol (by id == msg[2])
attr(data[[id]],"index") <- as.numeric(Sys.time())
nr.data <- NROW(data[[id]])
if(tickType == .twsTickType$MODEL_OPTION) {
data[[id]][nr.data,1:8] <- msg[4:11]
}
#else
# if(tickType == .twsTickType$ASK) {
# data[[id]][nr.data,2] <- msg[4]
# }
eW$assign.Data("data", data)
c(curMsg, msg)
}

return(eW)
}

@rpm3o
Copy link
Author

rpm3o commented Aug 26, 2022

Thank you for the response. I'm having difficulty knowing what and where to input the option details. Can you please provide an example using a common stock and options contract? Again, thank you very much for your help.

@cholzer68
Copy link

reqMktData(tws,OptionContract,eventWrapper=eWrapper.data.Opt_Model(1),CALLBACK = snapShot)

This should work as long as you define an options contract.

@rpm3o
Copy link
Author

rpm3o commented Aug 26, 2022

I've tried to apply this to VIX September 21 Call at strike 20 and receive an error.

> reqMktData(tws,twsOption(local="VIX 220921C00020000"),eventWrapper=eWrapper.data.Opt_Model(1),CALLBACK = snapShot)
Error in reqMktData(tws, twsOption(local = "VIX 220921C00020000"), eventWrapper = eWrapper.data.Opt_Model(1), :
object 'snapShot' not found

Thank you.

@cholzer68
Copy link

Sorry, you have to define the Snapshot function:

snapShot <- function (twsCon, eWrapper, timestamp, file, playback = 1, ...){
if (missing(eWrapper))
eWrapper <- eWrapper()
names(eWrapper$.Data$data) <- eWrapper$.Data$symbols
con <- twsCon[[1]]
if (inherits(twsCon, "twsPlayback")) {
sys.time <- NULL
while (TRUE) {
if (!is.null(timestamp)) {
last.time <- sys.time
sys.time <- as.POSIXct(strptime(paste(readBin(con,
character(), 2),
collapse = " "), timestamp))
if (!is.null(last.time)) {
Sys.sleep((sys.time - last.time) * playback)
}
curMsg <- .Internal(readBin(con, "character",
1L, NA_integer_, TRUE, FALSE))
if (length(curMsg) < 1)
next
processMsg(curMsg, con, eWrapper, format(sys.time,
timestamp), file, ...)
}
else {
curMsg <- readBin(con, character(), 1)
if (length(curMsg) < 1)
next
processMsg(curMsg, con, eWrapper, timestamp,
file, ...)
if (curMsg == .twsIncomingMSG$REAL_TIME_BARS)
Sys.sleep(5 * playback)
}
}
}
else {
while (TRUE) {
socketSelect(list(con), FALSE, NULL)
curMsg <- .Internal(readBin(con, "character", 1L,
NA_integer_, TRUE, FALSE))
if (!is.null(timestamp)) {
processMsg(curMsg, con, eWrapper, format(Sys.time(),
timestamp), file, ...)
}
else {
processMsg(curMsg, con, eWrapper, timestamp,
file, ...)
}
if (!any(sapply(eWrapper$.Data$data, is.na)))
return(do.call(rbind, lapply(eWrapper$.Data$data,
as.data.frame)))
}
}
}

@rpm3o
Copy link
Author

rpm3o commented Aug 26, 2022

Great! Thank you!

@cholzer68
Copy link

It took me along time to figure it out! Now I am trying to get it to work in python.

@rpm3o
Copy link
Author

rpm3o commented Aug 26, 2022

In case you haven't seen this:
https://interactivebrokers.github.io/tws-api/option_computations.html

Good luck!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants