-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCode.gs
224 lines (195 loc) · 8.17 KB
/
Code.gs
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
/**
* Calculates an Option's Delta using the Black-Scholes Model.
*
* @param {number} price - The spot price of the underlying asset.
* @param {number} strike - The strike price of the option.
* @param {number} volatility - The volatility of returns of the underlying asset.
* @param {number} interest - The input the risk-free interest rate.
* @param {number} dividend - The dividend rate as a percentage.
* @param {number} days - The time to maturity in days.
* @param {string} optiontype - The the type of option, Call or Put.
* @return the Black-Scholes calculation for an option's Delta.
* @customfunction
*/
function OPTIONDELTA(price, strike, volatility, interest, dividend, days, optiontype) {
var eqt = Math.exp(-dividend *(days/365));
var nd1 = NORMDIST_(D1_(price, strike, volatility, interest, dividend, days));
if (optiontype == "Put")
{
nd1 = nd1 - 1;
}
return eqt * nd1;
}
/**
* Calculates an Option's Gamma using the Black-Scholes Model.
*
* @param {number} price - The spot price of the underlying asset.
* @param {number} strike - The strike price of the option.
* @param {number} volatility - The volatility of returns of the underlying asset.
* @param {number} interest - The input the risk-free interest rate.
* @param {number} dividend - The dividend rate as a percentage.
* @param {number} days - The time to maturity in days.
* @return the Black-Scholes calculation for an option's Gamma.
* @customfunction
*/
function OPTIONGAMMA(price, strike, volatility, interest, dividend, days) {
var d1 = D1_(price, strike, volatility, interest, dividend, days);
var time = days/365;
var eqt = Math.exp(-dividend * time);
var asqrtT = volatility * Math.sqrt(time);
return Math.exp(-1 * Math.pow(d1, 2)/2)/Math.sqrt(2*Math.PI)*eqt/(price*asqrtT);
}
/**
* Calculates an Option's Theta using the Black-Scholes Model.
*
* @param {number} price - The spot price of the underlying asset.
* @param {number} strike - The strike price of the option.
* @param {number} volatility - The volatility of returns of the underlying asset.
* @param {number} interest - The input the risk-free interest rate.
* @param {number} dividend - The dividend rate as a percentage.
* @param {number} days - The time to maturity in days.
* @param {string} optiontype - The the type of option, Call or Put.
* @return the Black-Scholes calculation for an option's Theta.
* @customfunction
*/
function OPTIONTHETA(price, strike, volatility, interest, dividend, days, optiontype) {
var d1 = D1_(price, strike, volatility, interest, dividend, days);
var time = days/365;
var eqt = Math.exp(-dividend * time);
var xert = Math.exp(-interest * time) * strike;
var nd1 = NORMDIST_(D1_(price, strike, volatility, interest, dividend, days));
if (optiontype == "Put")
{
return (-(price*Math.exp(-1*Math.pow(d1,2)/2)/Math.sqrt(2*Math.PI)*volatility*eqt/(2*Math.sqrt(time)))+(interest*xert*nd1)-(dividend*price*nd1*eqt))/365;
}
return (-(price*Math.exp(-1*Math.pow(d1,2)/2)/Math.sqrt(2*Math.PI)*volatility*eqt/(2*Math.sqrt(time)))-(interest*xert*nd1)+(dividend*price*nd1*eqt))/365;
}
/**
* Calculates an Option's Vega using the Black-Scholes Model.
*
* @param {number} price - The spot price of the underlying asset.
* @param {number} strike - The strike price of the option.
* @param {number} volatility - The volatility of returns of the underlying asset.
* @param {number} interest - The input the risk-free interest rate.
* @param {number} dividend - The dividend rate as a percentage.
* @param {number} days - The time to maturity in days.
* @param {string} optiontype - The the type of option, Call or Put.
* @return the Black-Scholes calculation for an option's Vega.
* @customfunction
*/
function OPTIONVEGA(price, strike, volatility, interest, dividend, days) {
var d1 = D1_(price, strike, volatility, interest, dividend, days);
var time = days/365;
var eqt = Math.exp(-dividend * time);
return Math.exp(-1*Math.pow(d1,2)/2)/Math.sqrt(2*Math.PI)*eqt*price*Math.sqrt(time)/100;
}
/**
* Calculates an Option's Rho using the Black-Scholes Model.
*
* @param {number} price - The spot price of the underlying asset.
* @param {number} strike - The strike price of the option.
* @param {number} volatility - The volatility of returns of the underlying asset.
* @param {number} interest - The input the risk-free interest rate.
* @param {number} dividend - The dividend rate as a percentage.
* @param {number} days - The time to maturity in days.
* @return the Black-Scholes calculation for an option's Rho.
* @customfunction
*/
function OPTIONRHO(price, strike, volatility, interest, dividend, days, optiontype) {
var time = days/365;
var ert = Math.exp(-interest * time);
if (optiontype == "Put")
{
var nNegD2 = NORMDIST_(-D2_(price, strike, volatility, interest, dividend, days));
return -strike * time * ert * nNegD2/100;
}
var nNegD1 = NORMDIST_(D2_(price, strike, volatility, interest, dividend, days));
return strike * time * ert * nNegD1/100;
}
/**
* Calculates Option Price using the Black-Scholes Model.
*
* @param {number} price - The spot price of the underlying asset.
* @param {number} strike - The strike price of the option.
* @param {number} volatility - The volatility of returns of the underlying asset.
* @param {number} interest - The input the risk-free interest rate.
* @param {number} dividend - The dividend rate as a percentage.
* @param {number} days - The time to maturity in days.
* @param {string} optiontype - The the type of option, Call or Put.
* @return the price of an Option.
* @customfunction
*/
function OPTIONPRICE(price, strike, volatility, interest, dividend, days, optiontype) {
var time = days/365;
var xert = Math.exp(-interest * time) * strike;
var seqt = Math.exp(-dividend * time) * price;
if (optiontype == "Put")
{
var nNegD1 = NORMDIST_(-D1_(price, strike, volatility, interest, dividend, days));
var nNegD2 = NORMDIST_(-D2_(price, strike, volatility, interest, dividend, days));
return xert * nNegD2 - seqt * nNegD1;
}
var nD1 = NORMDIST_(D1_(price, strike, volatility, interest, dividend, days));
var nD2 = NORMDIST_(D2_(price, strike, volatility, interest, dividend, days));
return seqt * nD1 - xert * nD2;
}
/**
* Calculates D1 using the Black-Scholes Model.
*
* @param {number} price - The spot price of the underlying asset.
* @param {number} strike - The strike price of the option.
* @param {number} volatility - The volatility of returns of the underlying asset.
* @param {number} interest - The input the risk-free interest rate.
* @param {number} dividend - The dividend rate as a percentage.
* @param {number} days - The time to maturity in days.
* @return the value of D1.
* @customfunction
*/
function D1_(price, strike, volatility, interest, dividend, days) {
var time = days/365;
var lnsx = Math.log(price/strike);
var trqa = (interest - dividend + (Math.pow(volatility, 2))/2)*time;
var asqrtT = volatility * Math.sqrt(time);
return (lnsx + trqa)/asqrtT;
}
/**
* Calculates D2 using the Black-Scholes Model.
*
* @param {number} price - The spot price of the underlying asset.
* @param {number} strike - The strike price of the option.
* @param {number} volatility - The volatility of returns of the underlying asset.
* @param {number} interest - The input the risk-free interest rate.
* @param {number} dividend - The dividend rate as a percentage.
* @param {number} days - The time to maturity in days.
* @return the value of D2.
* @customfunction
*/
function D2_(price, strike, volatility, interest, dividend, days) {
var time = days/365;
var d1 = D1_(price, strike, volatility, interest, dividend, days);
var asqrtT = volatility * Math.sqrt(time);
return d1 - asqrtT;
}
/**
* Calculates an estimation of the normal distribution of a value.
*
* @param {number} d - The d value.
* @return the value of the normal distribution of d.
* @customfunction
*/
function NORMDIST_(d) {
var z = (d)/Math.sqrt(2);
var t = 1/(1+0.3275911*Math.abs(z));
var a1 = 0.254829592;
var a2 = -0.284496736;
var a3 = 1.421413741;
var a4 = -1.453152027;
var a5 = 1.061405429;
var erf = 1-(((((a5*t + a4)*t) + a3)*t + a2)*t + a1)*t*Math.exp(-z*z);
var sign = 1;
if(z < 0)
{
sign = -1;
}
return (1/2)*(1+sign*erf);
}