Data load

gas<-read.csv("c:/R/SBS4.csv",header=TRUE,sep="")
gas<-gas[order(gas$DATE, decreasing = FALSE), ]
r=log(1+0.05) #riskless interest rate
M<-1000000 #10,000-1,000,000
S0<-gas[which(gas$DATE=="2017-01-02"),3] #현재주가
K<-gas[which(gas$DATE=="2016-12-29"),2] #행사가격(Exercise Price) 
sigma<-0.2646 #volatility 
Z<-rnorm(M,0,1) #generate normal random number

(1)

#function that returns only positive
Pos<-function(x){
  box<-c()
  for (i in 1:length(x)){
    if(x[i]>0){
      box[i]<-x[i]
    } else {
      box[i]<-0
    }
  }
  return(box)
}

###Method 1###
#generate c,p function #Call/Put option
call.op1<-function(t){
  St<-S0*exp((r-(sigma^2)/2)*t+sigma*sqrt(t)*Z)
  exp(-r*t)*sum(Pos(St-K)/M)
}
put.op1<-function(t){
  St<-S0*exp((r-(sigma^2)/2)*t+sigma*sqrt(t)*Z)
  exp(-r*t)*sum(Pos((K-St)/M))
}

###Method 2###
call.op2<-function(t){
  d1 = (log(S0/K)+(r+(sigma^2)/2)*t)/(sigma*sqrt(t))
  d2 = d1-sigma*sqrt(t)
  exp(-r*t)*(S0*exp(r*t)*pnorm(d1)-K*pnorm(d2))
}
put.op2<-function(t){
  d1 = (log(S0/K)+(r+(sigma^2)/2)*t)/(sigma*sqrt(t))
  d2 = d1-sigma*sqrt(t)
  exp(-r*t)*(-S0*exp(r*t)*pnorm(-d1)+K*pnorm(-d2))
}
#Compare
call.op1(1/12);call.op2(1/12)
## [1] 1392.281
## [1] 1392.282
call.op1(3/12);call.op2(3/12)
## [1] 2651.009
## [1] 2651.068
call.op1(6/12);call.op2(6/12)
## [1] 3978.1
## [1] 3978.283
call.op1(9/12);call.op2(9/12)
## [1] 5054.493
## [1] 5054.834
call.op1(12/12);call.op2(12/12)
## [1] 5998.34
## [1] 5998.871
put.op1(1/12);put.op2(1/12)
## [1] 1545.193
## [1] 1545.691
put.op1(3/12);put.op2(3/12)
## [1] 2412.902
## [1] 2413.687
put.op1(6/12);put.op2(6/12)
## [1] 3159.629
## [1] 3160.642
put.op1(9/12);put.op2(9/12)
## [1] 3662.807
## [1] 3663.967
put.op1(12/12);put.op2(12/12)
## [1] 4040.454
## [1] 4041.728

(2)

gas<-gas[order(gas$DATE, decreasing = FALSE), ]
St1<-gas[which(substr(gas$DATE,1,7) %in% c("2017-01")),3]
St1<-St1[length(St1)]
St3<-gas[which(substr(gas$DATE,1,7) %in% c("2017-02","2017-03")),3]
St3<-St3[length(St3)]
St6<-gas[which(substr(gas$DATE,1,7) %in% c("2017-04","2017-05","2017-06")),3]
St6<-St6[length(St6)]
St9<-gas[which(substr(gas$DATE,1,7) %in% c("2017-07","2017-07","2017-09")),3]
St9<-St9[length(St9)]
St12<-gas[which(substr(gas$DATE,1,7) %in% c("2017-10","2017-11","2017-12")),3]
St12<-St12[length(St12)]

q2_a<-data.frame("Call_option_150"=c(150*call.op1(1/12),150*call.op1(3/12),
                                      150*call.op1(6/12),150*call.op1(9/12),
                                      150*call.op1(12/12)),
                 "Put_option_50"=c(50*put.op1(1/12),50*put.op1(3/12),
                                      50*put.op1(6/12),50*put.op1(9/12),
                                      50*put.op1(12/12)),
                 "Actual_150"=c(Pos(St1-K)*150,Pos(St3-K)*150,Pos(St6-K)*150,
                                 Pos(St9-K)*150,Pos(St12-K)*150),
                 "Actual_50"=c(Pos(K-St1)*50,Pos(K-St3)*50,Pos(K-St6)*50,
                                Pos(K-St9)*50,Pos(K-St12)*50))
library(dplyr)
## Warning: package 'dplyr' was built under R version 3.5.1
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
q2_a<-q2_a %>% mutate("net_profit"=(Actual_150+Actual_50)-(Call_option_150+Put_option_50)) %>%
  mutate("수익률(%)"=100*(net_profit/(Call_option_150+Put_option_50)))
q2_a
##   Call_option_150 Put_option_50 Actual_150 Actual_50 net_profit  수익률(%)
## 1        208842.1      77259.67          0    157500 -128601.75 -44.949656
## 2        397651.4     120645.08          0    162500 -355796.43 -68.647286
## 3        596715.0     157981.46     787500         0   32803.52   4.346585
## 4        758174.0     183140.37          0    332500 -608814.33 -64.677049
## 5        899751.1     202022.71          0    335000 -766773.78 -69.594484
q2_b<-data.frame("Call_option_100"=c(100*call.op1(1/12),100*call.op1(3/12),
                                   100*call.op1(6/12),100*call.op1(9/12),
                                   100*call.op1(12/12)),
               "Put_option_100"=c(100*put.op1(1/12),100*put.op1(3/12),
                                 100*put.op1(6/12),100*put.op1(9/12),
                                 100*put.op1(12/12)),
               "Actual_100"=c(Pos(St1-K)*100,Pos(St3-K)*100,Pos(St6-K)*100,
                              Pos(St9-K)*100,Pos(St12-K)*100),
               "Actual_100"=c(Pos(K-St1)*100,Pos(K-St3)*100,Pos(K-St6)*100,
                             Pos(K-St9)*100,Pos(K-St12)*100))

q2_b<-q2_b %>% mutate("net_profit"=(Actual_100+Actual_100)-(Call_option_100+Put_option_100)) %>%
  mutate("수익률(%)"=100*(net_profit/(Call_option_100+Put_option_100)))
q2_b
##   Call_option_100 Put_option_100 Actual_100 Actual_100.1 net_profit
## 1        139228.1       154519.3          0       315000  -293747.4
## 2        265100.9       241290.2          0       325000  -506391.1
## 3        397810.0       315962.9     525000            0   336227.1
## 4        505449.3       366280.7          0       665000  -871730.1
## 5        599834.0       404045.4          0       670000 -1003879.5
##    수익률(%)
## 1 -100.00000
## 2 -100.00000
## 3   47.10561
## 4 -100.00000
## 5 -100.00000
q2_c<-data.frame("Call_option_50"=c(50*call.op1(1/12),50*call.op1(3/12),
                                     50*call.op1(6/12),50*call.op1(9/12),
                                     50*call.op1(12/12)),
                 "Put_option_150"=c(150*put.op1(1/12),150*put.op1(3/12),
                                    150*put.op1(6/12),150*put.op1(9/12),
                                    150*put.op1(12/12)),
                 "Actual_50"=c(Pos(St1-K)*50,Pos(St3-K)*50,Pos(St6-K)*50,
                                Pos(St9-K)*50,Pos(St12-K)*50),
                 "Actual_150"=c(Pos(K-St1)*150,Pos(K-St3)*150,Pos(K-St6)*150,
                                Pos(K-St9)*150,Pos(K-St12)*150))

q2_c<-q2_c %>% mutate("net_profit"=(Actual_50+Actual_150)-(Call_option_50+Put_option_150)) %>%
  mutate("수익률(%)"=100*(net_profit/(Call_option_50+Put_option_150)))
q2_c
##   Call_option_50 Put_option_150 Actual_50 Actual_150  net_profit
## 1       69614.03       231779.0         0     472500  171106.953
## 2      132550.45       361935.2         0     487500   -6985.679
## 3      198905.00       473944.4    262500          0 -410349.392
## 4      252724.65       549421.1         0     997500  195354.226
## 5      299917.02       606068.1         0    1005000   99014.855
##    수익률(%)
## 1  56.772031
## 2  -1.412716
## 3 -60.986812
## 4  24.353956
## 5  10.928971