Search code examples
rpercentagequantmod

Calculate difference between last stock price and option strikes in percentage


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.


Solution

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