Advertisement
mjaniec

Plotting option strategies with R

Feb 15th, 2014
663
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
R 4.12 KB | None | 0 0
  1. ## description available at: http://reakkt.com
  2.  
  3. rm(list = ls()) # clear all
  4.  
  5. ### sample strategies
  6.  
  7. # ## Put Hedging
  8. #
  9. # assetsN <- 2
  10. #
  11. # current.price <- 40
  12. #
  13. # assets.mat <- matrix(NA,assetsN,5)
  14. # colnames(assets.mat) <- c("trans","type","strike","price","amount")
  15. #
  16. # assets.mat[1,] <- c("buy", "base", 40, 0, 1) # buy underlying
  17. # assets.mat[2,] <- c("buy", "put",  35, 2, 1) # hedge with a OTM put
  18. #
  19. # price.range <- c(10,80)
  20. # price.step  <- 1
  21.  
  22. ## Sell short base
  23.  
  24. assetsN <- 2
  25.  
  26. current.price <- 40
  27.  
  28. assets.mat <- matrix(NA,assetsN,5)
  29. colnames(assets.mat) <- c("trans","type","strike","price","amount")
  30.  
  31. assets.mat[1,] <- c("sell", "base", 40, 0, 1) # sell underlying
  32. assets.mat[2,] <- c("buy", "call",  35, 2, 1) # hedge with a ITM call
  33.  
  34. price.range <- c(10,80)
  35. price.step  <- 1
  36.  
  37. # ## Bull Call Spread
  38. # # http://www.theoptionsguide.com/bull-call-spread.aspx
  39. #
  40. # assetsN <- 2
  41. #
  42. # current.price <- 42
  43. #
  44. # assets.mat <- matrix(NA,assetsN,5)
  45. # colnames(assets.mat) <- c("trans","type","strike","price","amount")
  46. #
  47. # assets.mat[1,] <- c("buy",  "call",  40, 3, 1) # ITM call
  48. # assets.mat[2,] <- c("sell", "call",  45, 1, 1) # OTM call
  49. #
  50. # price.range <- c(35,50)
  51. # price.step  <- 1
  52.  
  53. # ## Call Backspread
  54. # #  http://www.theoptionsguide.com/call-backspread.aspx
  55. #
  56. # assetsN <- 2
  57. #
  58. # current.price <- 43
  59. #
  60. # assets.mat <- matrix(NA,assetsN,5)
  61. # colnames(assets.mat) <- c("trans","type","strike","price","amount")
  62. #
  63. # assets.mat[1,] <- c("sell",  "call",  40,  4, 1) # ITM call
  64. # assets.mat[2,] <- c("buy",   "call",  45,  2, 2) # OTM call
  65.  
  66. # ## Condor
  67. # # http://www.theoptionsguide.com/condor.aspx
  68. #
  69. # assetsN <- 4
  70. #
  71. # current.price <- 45
  72. #
  73. # assets.mat <- matrix(NA,assetsN,5)
  74. # colnames(assets.mat) <- c("trans","type","strike","price","amount")
  75. #
  76. # assets.mat[1,] <- c("buy",  "call",  35,  11, 1)
  77. # assets.mat[2,] <- c("buy",  "call",  55,  1, 1)
  78. # assets.mat[3,] <- c("sell", "call",  40,  7, 1)
  79. # assets.mat[4,] <- c("sell", "call",  50,  2, 1)
  80.  
  81. # ## some other strategy
  82. # assetsN <- 3
  83. #
  84. # assets.mat <- matrix(NA,assetsN,5)
  85. # colnames(assets.mat) <- c("trans","type","strike","price","amount")
  86. #
  87. # assets.mat[1,] <- c("sell",  "put",  500,  5,  1)
  88. # assets.mat[2,] <- c("sell",  "put",  700,  10, 1)
  89. # assets.mat[3,] <- c("buy",  "call", 1000,  15, 1)
  90.  
  91. ###
  92.  
  93. if (!exists("price.range")) {
  94.  
  95.   price.range <- range(as.numeric(assets.mat[,"strike"]))*c(0.8,1.2)
  96.   price.step  <- 0.1
  97.  
  98. }
  99.  
  100. x <- seq(price.range[1],price.range[2],price.step)
  101.  
  102. options.val <- matrix(NA,assetsN,length(x))
  103.  
  104. ###
  105.  
  106. OptionVal <- function(x,trans,type,strike,premium,amount=1) {
  107.  
  108.   strike  <- as.numeric(strike)
  109.   premium <- as.numeric(premium)
  110.   amount  <- as.numeric(amount)
  111.  
  112.   if (trans=="buy" && type=="base")   value <- x-strike-premium
  113.  
  114.   if (trans=="sell" && type=="base")  value <- strike-x-premium
  115.  
  116.   if (trans=="buy" && type=="call")   value <- max(x-strike,0)-premium
  117.  
  118.   if (trans=="sell" && type=="call")  value <- premium-max(x-strike,0)
  119.  
  120.   if (trans=="buy" && type=="put")    value <- max(strike-x,0)-premium
  121.  
  122.   if (trans=="sell" && type=="put")   value <- premium-max(strike-x,0)
  123.  
  124.   return( amount*value )
  125.  
  126. }
  127.  
  128. for (i in 1:assetsN)
  129.  
  130.   options.val[i,] <- sapply( x, function(x)
  131.    
  132.                                   OptionVal(x,
  133.                                             assets.mat[i,"trans"],
  134.                                             assets.mat[i,"type"],
  135.                                             assets.mat[i,"strike"],
  136.                                             assets.mat[i,"price"],
  137.                                             assets.mat[i,"amount"]) )
  138.  
  139. options.all <- colSums(options.val)
  140.  
  141. # rbind(x,options.all)
  142.  
  143. plot(options.all,
  144.      main="Payoff",ylab="profit/loss",xlab="price",
  145.      type="l",xaxt="n")
  146. if (!is.null(current.price)) abline(v=which(x==current.price),col="Green",lwd=2)
  147. abline(v=sapply(as.numeric(assets.mat[,"strike"]), function(s) which(x==s)),col="Blue",lty="dotted")
  148. abline(h=0,col="Red",lty="dashed")
  149. axis(1,seq(0,length(x),length.out=5),round(seq(min(x),max(x),length.out=5)))
  150.  
  151. assets.mat
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement