-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoption-strategies.R
65 lines (56 loc) · 2.4 KB
/
option-strategies.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
longCallOptionValue <- function(strike, priceAtMaturity) {
max((priceAtMaturity - strike), 0)
}
longPutOptionValue <- function(strike, priceAtMaturity) {
max((strike - priceAtMaturity), 0)
}
shortCallOptionValue <- function(strike, priceAtMaturity) {
# Remember this option is sold so the buyer will only exercise it
# if the price has risen - cost to us is at best 0
min(strike - priceAtMaturity, 0)
}
shortPutOptionValue <- function(strike, priceAtMaturity) {
# Remember this option is sold so the buyer will only exercise it
# if the price has risen - cost to us is at best 0
min(priceAtMaturity - strike, 0)
}
longCallOptionProfits <- function(strike, maturityPrices, premium) {
profits <- c(length=length(maturityPrices))
for (i in 1:length(maturityPrices)) {
profits[i] <- longCallOptionValue(strike, maturityPrices[i])-premium
}
return(profits)
}
shortCallOptionProfits <- function(strike, maturityPrices, premium) {
profits <- c(length=length(maturityPrices))
for (i in 1:length(maturityPrices)) {
profits[i] <- shortCallOptionValue(strike, maturityPrices[i])+premium
}
return(profits)
}
optionGraph <- function(maturityPrices) {
scale<-range(maturityPrices)
plot(scale, c(-max(scale)/2, max(scale)/2), type="n", xlab="Price at Maturity", ylab="Profit $")
lines(maturityPrices, rep(0,length(maturityPrices)), type="l", col="black")
}
plotLongCallOption <- function(strike, maturityPrices, premium, colour) {
data <- longCallOptionProfits(strike, maturityPrices, premium)
lines(maturityPrices, data, type="l", col=colour)
}
plotShortCallOption <- function(strike, maturityPrices, premium, colour) {
data <- shortCallOptionProfits(strike, maturityPrices, premium)
lines(maturityPrices, data, type="l", col=colour)
}
plotLongCallButterfly <- function(strike, offset, maturityPrices, premium) {
optionGraph(maturityPrices)
points(x=strike, y=0, type="o", pch = 21, bg = "orange")
text(x=strike, y=3, "Strike")
points(x=strike-offset, y=0, type="o", pch = 21, bg = "red")
text(x=strike-offset, y=3, "-a")
points(x=strike+offset, y=0, type="o", pch = 21, bg = "green")
text(x=strike+offset, y=3, "+a")
plotLongCallOption(strike-offset, maturityPrices, premium, "blue")
plotShortCallOption(strike, maturityPrices, premium, "red")
plotShortCallOption(strike, maturityPrices, premium, "green")
plotLongCallOption(strike+offset, maturityPrices, premium, "purple")
}