R/dcallbt.R

Defines functions dcallbt

Documented in dcallbt

#' Binary Tree Pricing of Call Options (Discrete 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 4
#' @param K Current stock price, default value is 4 executive price, default value is 5.
#' @param u up ratio.
#' @param d down ratio.
#' @param r Risk-free interest rate, default value is 0.25.
#' @param T Option maturity, default value is 3.
#' @param l The default length of each period in a binary tree is 1, that is, the default duration of a binary tree is 3 periods.
#' @examples
#' dcallbt(S = 4, K = 5, r = 1/4, u = 2, d = 1/2, T = 3, l = 1)
#' dcallbt(S = 41, K = 50, r = 1/4, u = 2, d = 1/2, T = 3, l = 1)
#' @references
#' OTS package, WangXu <seniorwangxu@@sina.com>
#' @seealso
#' \code{\link{callbt}}
#' @export
dcallbt <- function(S = 4, K = 5, r = 1/4, u = 2, d = 1/2, T = 3, l = 1)
{
  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)
  }
  p = (1 + r - 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] = (1 + r)^(-l) * (p * CT[i + 1, j] + (1 - p) * 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.