Home > Software design >  Calculate difference between last stock price and option strikes in percentage
Calculate difference between last stock price and option strikes in percentage

Time:11-12

I'd like to add a new column to my Options list. The new column would calculate the difference between the last stock price at close and the Option Strike in percentage.

Here's my code

#Getting Options data and creating an list containing only Puts

library(quantmod)
Symbols<-c  ("AA","AAL","AAOI","ABBV","ABC","ABNB")
Options.20221111 <- lapply(Symbols, getOptionChain)
names(Options.20221111) <- Symbols
only_puts_list <- lapply(Options.20221111, function(x) x$puts)

#Getting Stock data

start_date=as.Date("2022-06-01")
getSymbols(Symbols,from=start_date)

Next, I'd like to add a new column to only_puts_list called "%Strike-StockPrice", that would calculate the difference between the last Stock(Close.price) price calculated in getSymbols minus the Strike values (all of them in the only_put_list) in percentage.

For example, if the last Close price of a stock is 100 and a Strike price is 90, there is a 10% difference. The new column would make the calculation for all Strikes of the only_put_list.

Thanks for any input.

CodePudding user response:

This should do it :

#Getting Options data and creating an list containing only Puts
library(quantmod)
Symbols<-c  ("AA","AAL","AAOI","ABBV","ABC","ABNB")
Options.20221111 <- lapply(Symbols, getOptionChain)
names(Options.20221111) <- Symbols
only_puts_list <- lapply(Options.20221111, function(x) x$puts)

#Getting Stock data
start_date=as.Date("2022-06-01")
getSymbols(Symbols,from=start_date)

#Add the new column to only_puts_list
for(i in 1:length(only_puts_list)){
  
  close <- eval(parse(text=paste0("last(",names(only_puts_list)[i],"$",names(only_puts_list)[i],".Close)")))
  close <- as.numeric(close)
  only_puts_list[[i]]$`%Strike-StockPrice` <- (close-only_puts_list[[i]]$Strike)/close*100
  
}

CodePudding user response:

One way of doing it is also to get the stock prices in a list. Then use Map to loop over both lists and create the outcome you want. This does assume that the options list and the stock prices list are of the same length and in the same order. Otherwise you will have to work with a loop that checks the names of the option list and the name of the stock list to make sure you are calculating the correct difference.

library(quantmod)
Symbols<-c  ("AA","AAL")
Options.20221111 <- lapply(Symbols, getOptionChain)
names(Options.20221111) <- Symbols
only_puts_list <- lapply(Options.20221111, function(x) x$puts)


start_date=as.Date("2022-06-01")
# get the data from quantmod in a list
stocklist <- lapply(Symbols, getSymbols ,from=start_date, auto.assign = FALSE)
names(stocklist) <- Symbols


# anonymous function(x,y) has multiple lines, hence the ; in the code.
only_puts_list <- Map(function(x, y) {
  close <- as.numeric(last(Cl(x))); 
  y$diff <- (close - y$Strike) / close; 
  y}, last_prices, only_puts_list)
  • Related