R/bear.R

#' Visualization of Bear Market Spread Strategy
#' @description The bear() function generates a graph showing the gains and losses of bear market spread trading strategies using call or put options. The reason why bear market spreads are constructed is to look short on the market.
#' @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 bear 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 type Specifies whether to use call option or put option to carry bear market spread arbitrage. If it is a call option, use type = "call" to indicate that the strategy is to buy a call option with a high execution price and sell a call option with a low execution price; if it is a put option, use type = "put", indicating that the strategy is to buy a put option with a high execution price and sell a put option with a low execution price. The default value is "call".
#' @examples
#' bear(K1 = 40, opt1 = 6, K2 = 60, opt2 = 2)
#' bear(K1 = 50, opt1 = 2, K2 = 60, opt2 = 6, type = "put")
#' @references
#' OTS package, WangXu <seniorwangxu@@sina.com>
#' @seealso
#' \code{\link{bull}}, \code{\link{butterfly}}
#' @export
bear <- function (K1 = 40, opt1 = 6, K2 = 60, opt2 = 2, type = "call"){
  if (K1 >= K2)
    stop("K1 must be smaller than K2")
  x = seq(K1 - K1/4, K2 + K2/4, 0.01)
  if (type == "call") {
    y1 = ifelse(x >= K1, K1 - x + opt1, opt1)
    y2 = ifelse(x >= K2, x - K2 - opt2, -opt2)
  }
  else if (type == "put") {
    y1 = ifelse(x <= K1, x - K1 + opt1, opt1)
    y2 = ifelse(x <= K2, K2 - x - opt2, -opt2)
  }
  y = y1 + y2
  plot(x, y, type = "l", ann = FALSE, ylim = c(min(y1, y2), max(y1, y2)), 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)
  abline(h = 0, lty = 2)
  if (type == "call"){
    title(main = "Bear 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 = "Bear spread strategy using put options", xlab = "Stock price", ylab = "Payoff", cex.lab = 1)
    legend("topright", c("Portfolio payoff", "Payoff from option with lower strike price", "Payoff from option with higher strike price"), col = 2:4, lty = 1, cex = 0.75)
  }
}
czxa/FMFE documentation built on Nov. 6, 2019, 4:58 a.m.