I want to plot a PNL graph for a set of options in a data frame in r. I have found a similar question on this, /questions/55602743/how-to-plot-an-option-payoff-graph-r, however in this they were looking to plot a specific strategy. I do not want to plot just a specific strategy, but a combination of multiple options. I want to input just a sequence for the spot price and input a value for the volatility and it will create a PNL graph using those inputs and the information in the data frame. Please help.
Here is the data frame and code that I currently have, I’m sure there is a much better way to do this an would appreciate any help.
library(derivmkts)
library(ggplot2)
library(dplyr)
options_data = structure(list(Type = c("Call", "Call", "Call", "Call", "Put", "Put", "Put", "Put"),
Strike = c(114, 125, 130, 140, 120, 124,125, 130),
Size = c(10, 50, 190, 80, 80, 30, 150, 40),
Expo = structure(c(19895,19895, 19895, 19895, 19895, 19895, 19895, 19895), class = "Date"),
AvgPx = c(6.2, 7.05, 5.19, 3.145, 3.95, 6.2, 7.18, 9.95)), row.names = c(NA, -8L), class = "data.frame")
stock_prices <- seq(80, 170, by = 1)
volatility <- 0.60
payoffs <- sapply(stock_prices, function(S) {
call_payoff <- with(options_data[options_data$Type == 'Call',],
sum(pmax(0, S - Strike) * Size) - sum(AvgPx * Size))
put_payoff <- with(options_data[options_data$Type == 'Put',],
sum(pmax(0, Strike - S) * Size) - sum(AvgPx * Size))
total_payoff <- call_payoff + put_payoff
return(total_payoff)
})
bsopt(100, 100, 0.5, .05, 10/365, 0)
plot_data = data.frame(StockPrice = stock_prices, Payoff = payoffs)
ggplot(plot_data, aes(x = StockPrice, y = Payoff)) +
geom_line() +
labs(title = "Combined Payoff Graph", x = "Stock Price", y = "Payoff") +
theme_minimal()
I am looking for something more along the lines of a thinkorswim PNL graph such as this. Can no longer use thinkorswim due to schwab acquiring them so looking for an alternative way to view my positions. Here is an example.
Would appreciate any help!! Thanks a lot.