R/callbt.R

Defines functions callbt

Documented in callbt

#' Binary Tree Pricing of Call Options (Continuous Compound Interest)
#' @description The function uses a binary tree to price European options, and finally draws a binary tree and the price of return options.
#' @param S Current stock price, default value is 41
#' @param K Execution price, default value is 40
#' @param sigma Volatility of stock price, default value is 0.3
#' @param r Risk-free interest rate, default value is 0.2
#' @param T Option maturity, default value is 1
#' @param dividend Compound dividend rate, default value is 0, that is, no dividend.
#' @param l The default length of each period in a binary tree is 1/3, that is, the default duration of a binary tree is 3 periods.
#' @examples
#' callbt(S = 41, K = 40, sigma = 0.3, r = 0.2, T = 1, dividend = 0, l = 1/3)
#' callbt(S = 50, K = 40, sigma = 0.3, r = 0.2, T = 1, dividend = 0, l = 1/3)
#' @references
#' OTS package, WangXu <seniorwangxu@@sina.com>
#' @seealso
#' \code{\link{dcallbt}}
#' @export
callbt <- function(S = 41, K = 40, sigma = 0.3, r = 0.2, T = 1, dividend = 0, l = 1/3)
{
  u = exp(sigma*sqrt(l))
  d = exp(-sigma*sqrt(l))
  n = T / l
  BT <- matrix(ncol = n + 1, nrow = n + 1)
  plot(0, 0, axes = FALSE, xlab = "", ylab = "", xlim = c(-1, 2 * n + 2), ylim = c(-n - 1, n + 2))
  for(i in 1:n){
    for(j in 1:i){
      BT[i, j] = S * d^(j - 1) * u^(i - j)
      points(2 * i - 2, i + 1 - 2 * j)
      arrows(2 * i - 2, i + 1 - 2 * j, 2 * i, i + 2 - 2 * j, length = 0.1, angle = 10, code = 2)
      arrows(2 * i - 2, i + 1 - 2 * j, 2 * i, i - 2 * j, length = 0.1, angle = 10, code = 2)
      text(2 * i - 2, i + 1 - 2 * j, round(BT[i, j], 3), col = 2, adj = 0, pos = 3, offset = 0.5, cex = 0.75)
    }
  }
  for(j in 1:(n+1)){
    BT[n + 1, j] = S * d^(j - 1) * u^(n + 1 - j)
    points(2 * n, n + 2 - 2 * j)
    text(2 * n, n + 2 - 2 * j, round(BT[n + 1, j], 3), col = 2, adj = 0, pos = 3, offset = 0.5, cex = 0.75)
  }
  pstar = (exp((r - dividend) * l) - d)/(u - d)
  CT <- matrix(ncol = n + 1, nrow = n + 1)
  CT[n + 1, ] = (BT[n + 1, ] - K) * ((BT[n + 1, ] - K) >= 0)
  for(j in 1:(n + 1)){
    text(2 * n, n + 2 - 2 * j, round(CT[n + 1, j], 3), col = 3, adj = 0, pos = 1, offset = 0.5, cex = 0.75)
  }
  for(i in n:1){
    for(j in 1:i){
      CT[i, j] = exp(-r * l) * (pstar * CT[i + 1, j] + (1 - pstar) * CT[i + 1, j + 1])
      text(2 * i - 2, i + 1 - 2 * j, round(CT[i, j], 3), col = 3, adj = 0, pos = 1, offset = 0.5, cex = 0.75)
    }
  }
  mtext("Binomial tree pricing for European call", side = 1, cex = 1.25)
  return(round(CT[1, 1], 3))
}
czxa/FMFE documentation built on Nov. 6, 2019, 4:58 a.m.