R/butterfly.R

#' Visualization of Butterfly Spread Strategy
#' @description The butterfly() function generates a graph showing the profit and loss of butterfly spread trading strategies using call or put options. The reason why butterfly spreads are constructed is that the stock price is expected to change only slightly in the future.
#' @note If there is a mutual occlusion problem in the image, you can run the dev.new() command first. If there is still occlusion problem, you can directly run bull command to call out the source code of the function, and eliminate the occlusion problem by modifying the corresponding graphic parameters.
#' @param K1 Execution Price of Option 1
#' @param opt1 Price of option 1
#' @param K2 Execution Price of Option 2
#' @param opt2 Price of Option 2
#' @param K3 Execution Price of Option 3
#' @param opt3 Price of Option 3
#' @param type Specifies whether to use call option or put option to carry bear market spread arbitrage. If it is a call option, type = "call" is used to indicate that the strategy is to buy a call option with low execution price and a call option with high execution price and sell two call options with intermediate execution price at the same time; if it is a put option, type = "put" is used to indicate that the strategy is to buy a put option with low execution price and a put option with high execution price at the same time. Put options with two intermediate execution prices. The default value is "call".
#' @examples
#' butterfly(K1 = 45, opt1 = 5, K2 = 50, opt2 = 3, K3 = 55, opt3 = 1)
#' butterfly(K1 = 45, opt1 = 1, K2 = 50, opt2 = 3, K3 = 55, opt3 = 5, type = "put")
#' @references
#' OTS package, WangXu <seniorwangxu@sina.com>
#' @seealso
#' \code{\link{bear}}, \code{\link{bull}}
#' @export
butterfly <- function (K1 = 45, opt1 = 5, K2 = 50, opt2 = 3, K3 = 55, opt3 = 1, type = "call"){
  if (((K1 < K2) & (K2 < K3)) != 1)
    stop("K1, K2, K3 must be in the order of small to large")
  x = seq(K1 - K1/4, K3 + K3/4, 0.01)
  if (type == "call") {
    y1 = ifelse(x >= K1, x - K1 - opt1, -opt1)
    y3 = ifelse(x >= K3, x - K3 - opt3, -opt3)
    y2 = 2 * ifelse(x >= K2, K2 - x + opt2, opt2)
  }
  else if (type == "put") {
    y1 = ifelse(x <= K1, K1 - x - opt1, -opt1)
    y3 = ifelse(x <= K3, K3 - x - opt3, -opt3)
    y2 = 2 * ifelse(x <= K2, x - K2 + opt2, opt2)
  }
  y = y1 + y2 + y3
  plot(x, y, type = "l", ann = FALSE, ylim = c(-max(y1, y2, y3), max(y1, y2, y3)), col = 2,lty = 1, lwd = 2)
  lines(x, y1, type = "l", col = 3, lty = 1, lwd = 1)
  lines(x, y2, type = "l", col = 4, lty = 1, lwd = 1)
  lines(x, y3, type = "l", col = 5, lty = 1, lwd = 1)
  abline(h = 0, lty = 2)
  if (type == "call"){
    title(main = "Butterfly spread strategy using call options", xlab = "Stock price", ylab = "Payoff", cex.lab = 1)
    legend("bottomleft", c("Portfolio payoff", "Payoff from option with lower strike price", "Payoff from option with higher strike price"), col = 2:4, lty = 1, cex = 0.9)
  }
  else if (type == "put"){
    title(main = "Butterfly spread strategy using put options", xlab = "Stock price", ylab = "Payoff", cex.lab = 1)
    legend("bottomright", c("Portfolio payoff", "Payoff from option with lower strike price", "Payoff from option with higher strike price"), col = 2:5, lty = 1, cex = 0.75)
  }
}
czxa/FMFE documentation built on Nov. 6, 2019, 4:58 a.m.