Professional Documents
Culture Documents
Excel VBA
Excel VBA
- () -
Oct. 2008
(contents)
1. (1)
19.
2. (2)
20. : vs
3. Macro : (1)
5. VBA
6. Sheet (Layout)
23. : -(Newton-Rapson)
7. KOSPI200
8. Exotic option delta surface
30.
31.
32.
33.
34.
35.
36.
37.
38.
24. : -(Newton-Rapson)
1. (1)
EXCEL
VBA
(Spreadsheet
(SpreadsheetModeling)
Modeling)
Use
only
simple
arithmetic
Use only simple arithmetic
Organize
Organizespreadsheet
spreadsheetfor
forbetter
betterpresentation
presentation
(built-in
(built-infuctions)
fuctions)
Make
excel
work
more
efficient
Make excel work more efficient
Powerful
Powerfulexcel
excelbuilt-in
built-infunctions
functions
VBA
VBAI:I:
writing
writingthe
theBlack-Scholes
Black-Scholesfunction
function
pros
prosand
andcons
consof
ofdifferent
differentimplementation
implementation
VBA
VBAII:
II:
Make
Makeexcel
excelwork
workhard,
hard,and
andfancy
fancyusers
usersinput
input
be
beaabetter
betterprogrammer
programmer
2. (2)
(Solver)
(Solver)
(Portfolio optimization)
(Portfolio optimization)
- (Interpolation), ,
- (Interpolation), ,
--(Tree,
(Tree,FDM),
FDM),
(Randon
(RandonVariables)
Variables)
(Monte
(MonteCarlo
CarloSimulation)
Simulation)
Pricing multiple stock options
Pricing multiple stock options
(Risk
(RiskManagement)
Management)
Garch
GarchVolatility
VolatilityModeling
Modeling
Value
at
Risk
Value at Risk
(Interest
(InterestRate
RateDerivatives)
Derivatives)
Modeling interest rate derivatives in Excel
Modeling interest rate derivatives in Excel
C
C
Why need C?
Why need C?
Excel
Exceland
andCC
3. Macro : (1)
?
?
VBA
VBA??Visual
VisualBasic
Basicfor
forApplication
Application,
,vb
vb
: :
.
.
.
.
: :
: :
,
,
.
.
5. VBA
VBA
(Excel)
xy
x*y
x*y
x^2
x^2
LN(x)
Log(x)
EXP(x)
Exp(x)
SQRT(x)
Sqr(x)
3!
FACT(3)
Application.Fact(3)
NORMSDIST(x)
Application.NormSDist(x)
RAND()
Rnd
x2
loge x
exp(x)
EXP(x)
EXP(x)
,
,exp(x)
exp(x).
.
LN(x)
x
.
LN(x) x .
SQRT(x)
SQRT(x)x
x
.
.
FACT(numbers)
1*2*3**numbers
FACT(numbers) 1*2*3**numbers
NORMDIST
.
NORMDIST .
: :
: :
,
,
()
:
() :
: :
[1] VBA
F0 = S 0 e ( r q )T
(S : , r : , q : , T: )
//VBA Code,
Public Function Futures (S As Double, Tau As Double, R As Double, Q As Double)
S= (KOSPI200 )
Tau = ()
Futures = S * Exp((R-Q) * Tau )
End Function
7. KOSPI200
.
. (OTM)
(OTM)
0
0 ,
,
(ITM)
(ITM)
,
, (ATM)
(ATM)
60
50
40
30
20
10
150
140
130
120
110
100
90
80
70
60
0
50
0.1
0.2
0.3
0.4
0.5
0.6
T-t
0.7
0.8
0.9
-1.0000
-1.5000
8
37
-2.0000
65
-0.5000
-2.5000
94
123
-3.0000
151
118.72
115.76
112.80
109.84
106.88
103.92
98.00
100.96
95.04
92.08
86.15
83.19
180
89.12
-3.5000
Autocall
Autocall Structure
Structure
10
9
8
7
6
5
, ATM
990
1095
780
675
465
360
255
150
-1
62 68 76
84 92 100
108
45
570
885
c = SN ( d 1 ) Xe r ( T t ) N ( d 2 )
p = Xe r ( T t ) N ( d 2 ) SN ( d 1 )
Public
PublicFunction
FunctionBlackSholes(CallPutFlag
BlackSholes(CallPutFlagAs
AsString,
String,SSAs
AsDouble,
Double,XXAs
AsDouble,
Double,
r rAs
AsDouble,
Double,TTAs
AsDouble,
Double,vvAs
AsDouble)
Double) As
AsDouble
Double
Dim
Dimd1
d1As
AsDouble,
Double,d2
d2As
AsDouble
Double
d1
d1==(Log(S/X)+(r+v^2/2)*T)/(v*Sqr(T))
(Log(S/X)+(r+v^2/2)*T)/(v*Sqr(T))
d2=d1-v*Sqr(T)
d2=d1-v*Sqr(T)
IfIfCallPutFlag
CallPutFlag==c
cThen
Then
BlackSholes
=
S*Application.NormSDist(d1)-X*Exp(-r*T)*Application.NormSDist(d2)
BlackSholes = S*Application.NormSDist(d1)-X*Exp(-r*T)*Application.NormSDist(d2)
ElseIf
ElseIfCallPutFlag
CallPutFlag==p
pThen
Then
BlackSholes
=
X*Exp(-r*T)*Application.NormSDist(-d2)-S*Application.NormSDist(-d1)
BlackSholes = X*Exp(-r*T)*Application.NormSDist(-d2)-S*Application.NormSDist(-d1)
End
EndIfIf
End
EndFunction
Function
VBA
VBACODE
CODE
1.1.
(
(BlackSholes)
BlackSholes)
2.2.
BlackSholes
BlackSholes
: :String,
String,Double
Double
3.3.Dim
Dim
d1
d1d2
d2
4.4.
If
:
If-Then-ElsIf
If : If-Then-ElsIf
5.5.
(End
(EndFunction)
Function)
C
= N (d1 )
S
()
c
= e( br )T N (d1 ) > 0
S
p
put =
= e ( b r )T [ N (d1 ) 1] < 0
S
call =
//VBA
//VBACode,
Code,(Delta)
(Delta): :()
()
Public
Function
Delta(CallPutFlag
Public Function Delta(CallPutFlagAs
AsString,
String,SSAs
AsDouble,
Double,XXAs
AsDouble,
Double,TTAs
AsDouble,
Double,_r_rAs
As
Double,
v
As
Double)
As
Double
Double, v As Double) As Double
Dim
Dimd1
d1As
AsDouble
Double
d1
=
(Log(S
d1 = (Log(S/ /X)
X)++(r(r++vv^^22/ /2)2)* *T)
T)/ /(v(v* *Sqr(T))
Sqr(T))
IfIfCallPutFlag
=
"c"
Then
CallPutFlag = "c" Then
Delta
Delta==Exp((-r)
Exp((-r)* *T)
T)* *Application.NormSDist(d1)
Application.NormSDist(d1)
ElseIf
CallPutFlag
=
"p"
ElseIf CallPutFlag = "p"Then
Then
Delta
=
Exp((-r)
*
T)
*
(Application.NormSDist(d1)
Delta = Exp((-r) * T) * (Application.NormSDist(d1)- -1)1)
End
EndIfIf
End
EndFunction
Function
== Delta
Deltaof
ofHedge
HedgePortfolio
Portfolio
== Gamma
of
Hedge
Gamma of HedgePortfolio
Portfolio
== Theta
of
Hedge
Portfolio
Theta of Hedge Portfolio
== Rho
Rhoof
ofHedge
HedgePortfolio
Portfolio
== Vega
Vegaof
ofHedge
HedgePortfolio
Portfolio
( )
Delta
Deltaof
ofOption
Option
== Delta
Deltaof
ofHedge
HedgePortfolio
Portfolio(
(
)
)
c.f
c.f: :
0
Gamma of Large Portfolio 0
Vega
Vegaof
ofLarge
LargePortfolio
Portfolio 00
Black-Scholes
Black-Scholes
Black-Scholes
Black-Scholes
(
(
)
)
!
!
B-S
B-S
:
:
1
1++
Delta:
Delta:(
(
)/(
)/()
)
Option
price
Slope =
B
Stock price
call , put
2 c 2 p n(d1)e ( b r )T
= 2 =
=
>0
S
S 2
S T
//VBA
//VBACode,
Code,(Gamma)
(Gamma): :()
()
Public
PublicFunction
FunctionGamma(S
Gamma(SAs
AsDouble,
Double,XXAs
AsDouble,
Double,TTAs
AsDouble,
Double,r rAs
AsDouble,
Double,vvAs
AsDouble)
Double)As
AsDouble
Double
Dim
Dimd1
d1As
AsDouble
Double
d1
d1==(Log(S
(Log(S/ /X)
X)++(r(r++vv^^22/ /2)2)* *T)
T)/ /(v(v* *Sqr(T))
Sqr(T))
Gamma
Gamma==Exp((-r)
Exp((-r)* *T)
T)* *ND(d1)
ND(d1)/ /(S
(S* *vv* *Sqr(T))
Sqr(T))
End
EndFunction
Function
, (implied volatility) ,
.
, .
, ,
.
Vegacall . put =
c
p
= Se (b r )T n(d1) T > 0
=
//VBA
//VBACode,
Code,(Vega)
(Vega): :()
()
Public
PublicFunction
FunctionVega(S
Vega(SAs
AsDouble,
Double,XXAs
AsDouble,
Double,TTAs
AsDouble,
Double,r rAs
AsDouble,
Double,vvAs
AsDouble)
Double)As
AsDouble
Double
Dim
d1
As
Double
Dim d1 As Double
d1
d1==(Log(S
(Log(S/ /X)
X)++(r(r++vv^^22/ /2)2)* *T)
T)/ /(v(v* *Sqr(T))
Sqr(T))
Vega
=
S
*
Exp((-r)
*
T)
*
ND(d1)
*
Sqr(T)
Vega = S * Exp((-r) * T) * ND(d1) * Sqr(T)
End
EndFunction
Function
Se (b r )T n(d1)
c
=
=
(b r ) Se ( b r )T N ( d1 ) rXe rT N (d 2 ) 0
T
2 T
put
p
Se (b r )T n(d1)
= =
+ (b r ) Se (b r )T N (d1 ) + rXe rT N (d 2 ) 0
T
2 T
//VBA
//VBACode,
Code,(Vega)
(Vega): :()
()
Public
Function
Theta(CallPutFlag
Public Function Theta(CallPutFlagAs
AsString,
String,SSAs
AsDouble,
Double,XXAs
AsDouble,
Double,TTAs
AsDouble,
Double,
r rAs
Double,
v
As
Double)
As
Double
As Double, v As Double) As Double
Dim
Dimd1
d1As
AsDouble,
Double,d2
d2As
AsDouble
Double
d1
=
(Log(S
/
X)
+
(r
+
v
^
2
d1 = (Log(S / X) + (r + v ^ 2/ /2)2)* *T)
T)/ /(v(v* *Sqr(T))
Sqr(T))
d2
=
d1
v
*
Sqr(T)
d2 = d1 - v * Sqr(T)
IfIfCallPutFlag
CallPutFlag=="c"
"c"Then
Then
Theta
=
-S
*
Exp((-r)
Theta = -S * Exp((-r)* *T)
T)* *Application.NormSDist(d1)
Application.NormSDist(d1)* *vv/ /(2(2* *Sqr(T))
Sqr(T))- -(-r)
(-r)* *SS* *Exp((-r)
Exp((-r)* *T)
T)
* *Application.NormSDist(d1)
Application.NormSDist(d1)
- -r r* *XX* *Exp(-r
Exp(-r* *T)
T)* *Application.NormSDist(d2)
Application.NormSDist(d2)
ElseIf
CallPutFlag
=
"p"
ElseIf CallPutFlag = "p"Then
Then
Theta
=
-S
*
Exp((-r)
*
T)
Theta = -S * Exp((-r) * T)* *Application.NormSDist(d1)
Application.NormSDist(d1)* *vv/ /(2(2* *Sqr(T))
Sqr(T))++(-r)
(-r)* *SS* *Exp((-r)
Exp((-r)* *T)
T)
* *Application.NormSDist(-d1)
Application.NormSDist(-d1)
++r r* *XX* *Exp(-r
Exp(-r* *T)
T)* *Application.NormSDist(-d2)
Application.NormSDist(-d2)
End
EndIfIf
End
EndFunction
Function
//VBA
//VBACode,
Code,(Rho)
(Rho): :()
()
Public
PublicFunction
FunctionRho(CallPutFlag
Rho(CallPutFlagAs
AsString,
String,SSAs
AsDouble,
Double,XXAs
AsDouble,
Double,TTAs
AsDouble,
Double,r rAs
As
Double,
v
As
Double)
As
Double
Double, v As Double) As Double
Dim
Dimd1
d1As
AsDouble,
Double,d2
d2As
AsDouble
Double
d1
=
(Log(S
/
X)
+
(r
+
v
^
2
d1 = (Log(S / X) + (r + v ^ 2/ /2)2)* *T)
T)/ /(v(v* *Sqr(T))
Sqr(T))
d2
=
d1
v
*
Sqr(T)
d2 = d1 - v * Sqr(T)
IfIfCallPutFlag
CallPutFlag=="c"
"c"Then
Then
Rho
=
X
*
T
*
Exp(-r
Rho = X * T * Exp(-r* *T)
T)* *Application.NormSDist(d2)
Application.NormSDist(d2)
ElseIf
CallPutFlag
=
"p"
Then
ElseIf CallPutFlag = "p" Then
Rho
Rho==-X
-X* *TT* *Exp(-r
Exp(-r* *T)
T)* *Application.NormSDist(-d2)
Application.NormSDist(-d2)
End
If
End If
End
EndFunction
Function
[4]
.
Check
, , , kospi200,/,
, ,
, /, , ,,,
, /
, , ,
10
, ,
, , Check ?
Worksheet ?
Worksheet ?
(closed
(closedform
formsolution)
solution)
,
,
, ,
.
.,
,
.
.
,
,
(Binomial
(Binomial) )
(Trinomial
(TrinomialTree),
Tree),
(FDM)
(FDM)
.
.
(Tree)
(FDM)
20. : vs
.
.
.
.
.
.
(argument)
(argument)
. ,
.
, .
,
,=f(,,,,)
=f(,,,,),
,=g(,,
=g(,,
,
,
)
, , ) . .
.
x0
f(x)
x1
11
x0) f(x0)*f(x1) < 0
::
[x
[x0,0,xx1]1] (,
(, xx1<
1< x0) f(x0)*f(x1) < 0
2
2 ,
,
[x
[x0,0,xx1]
1]
x 0 + x1
22
x2 = 2
::
:
|
x
x
|
>
33
4
4
xx2
: | x2 2 - x0 0| >
2
44
f(x
f(x0)*f(x
)*f(x2)) << 00
xx1
xx2
,
,
xx0
xx2
::
0
vHigh
=
2
vHigh = 2
Epsilon
=
0.000001
Epsilon = 0.000001
cLow
cLow==BS(CallPutFlag,
BS(CallPutFlag,S,S,X,X,T,T,r,r,vLow)
vLow)
cHigh
=
BS(CallPutFlag,
S,
X,T,
r,
vHigh)
cHigh = BS(CallPutFlag, S, X,T, r, vHigh)
ViVi==vLow
vLow++(price
(pricecLow)*(vHigh-vLow)
cLow)*(vHigh-vLow)/ /(cHigh
(cHighcLow)
cLow)
Do
DoWhile
WhileAbs(priceAbs(price-BS(CallPutFlag,
BS(CallPutFlag,S,S,X,X,T,T,r,r,vi))
vi))>>Epsilon
Epsilon
IfIf BS(CallPutFlag,
S,
X,
T,
r,
vi)
<
Price
Then
BS(CallPutFlag, S, X, T, r, vi) < Price Then
vLow
vLow==vivi
Else
Else
vHihg
vHihg==vivi
cLow
cLow==BS(CallPutFlag,
BS(CallPutFlag,S,S,X,X,T,T,r,r,vLow)
vLow)
cHigh
=
BS(CallPutFlag,
S,
X,
T,
r,
vHihg)
cHigh = BS(CallPutFlag, S, X, T, r, vHihg)
vivi==vLow
vLow++(price
(pricecLow)
cLow)* *(vHihg
(vHihgvLow
vLow) )/ /(cHihg
(cHihgcLow)
cLow)
Loop
Loop
Imvol
Imvol==vivi
End
EndFunction
Function
f ( x i)
f(x
i+ 1
[x
i,
( x i )]
i+ 2
i+ 1
-
-
.
.
.
.
Xi
Xi
f(Xi)
f(Xi)
g(X)
g(X)
g(x)=0
g(x)=0
Xi+1
Xi+1 .
.
,
,
.
.
MarketPrice
TempError = TempPrice MarketPrice
IfIfTempError
TempError<=
<=Error
ErrorThen
Then
ImpliedVolatility
InitialValue
ImpliedVolatility InitialValue
Exit
ExitFunction
Function
Else
Else
InitialValue
InitialValue==InitialValue
InitialValueTempError
TempError/ /__
BlackSchole(Spot,
BlackSchole(Spot,Strike,
Strike,RiskFreeRate,
RiskFreeRate,0,0,Tau,
Tau,InitialValue,
InitialValue,PutCall
PutCall&&vega)
vega)
End
If
End If
Next
NextI I
ImpliedVolatility
ImpliedVolatility==00
End
EndFunction
Function
1979,
1979,John
JohnCox,
Cox,Stephen
StephenRoss,
Ross,Mark
MarkRubinstein
Rubinstein
Risk-Neutral
Risk-NeutralValuation
Valuation
Real
RealWorld
WorldRisk
RiskNeutral
NeutralWorld
World
Path-Dependent
Path-Dependent
Early
EarlyExercise
Exercise
Local
LocalVolatility
Volatility
Yield
YieldCurve
Curve
=>
=>Trinomial
TrinomialModel,
Model,Equal
EqualProbability
ProbabilityModel
Model
S, f, r
u, d, , d < r < u
Su, Sd
fu, fd
1
S0u
fu
S
f
S0d
fd
: , : 1
: Su - fu
: Sd fd
: S - f
Su - fu = Sd fd
//
= (fu fd)/(Su Sd) //
S - f = e-rT (Su - fu ) //
f = S (1 u e-rT) + fu e-rT
= e-rT[ p fu + (1-p)fd]
, p = (e-rT d) / ( u d)
, 0 < p < 1
, p Real World Risk
Neutral World . (f)
/ =>
100
0.2
u
v
Risk-neutral p
1.02
0.98
0.5125
Interest rate
0.05
Timestep
Discount factor
0.01
0.9995
Strike
100
One timestep
before expiry
Expiry
102
2
100
0.5
1.024488
98
Color coding
Stock
Delta
Option
The tree
Asset
Volatility
100
0.2
u
v
Risk-neutral p
1.02
0.98
0.5125
Interest rate
0.05
Timestep
Discount factor
0.01
0.9995
Strike
100
1.0005
Color coding
Three timesteps
before expiry
Two timesteps
before expiry
One timestep
before expiry
Expiry
106.1208
6.1208
104.04
1
4.089975
102
0.756467
2.584069
100
0.517497
1.574163
101.9592
1.9592
99.96
0.489996
1.003588
98
0.256017
0.514082
Stock
Delta
Option
97.9608
0
96.04
0
0
94.1192
0
s(0) = Asset
For n = 1 To NoSteps
For j = n To 1 Step -1
s(j) = u * s(j - 1)
Next j
s(0) = d * s(0)
Next n
s(0, 0) = Asset
For n = 1 To NoSteps
For j = n To 1 Step -1
s(j, n) = u * s(j - 1, n - 1)
Next j
s(0, n) = d * s(0, n - 1)
Next n
For j = 0 To NoSteps
V(j) = Payoff(s(j), Strike)
Next j
For j = 0 To NoSteps
V(j, NoSteps) = Payoff(s(j, NoSteps), Strike)
Next j
Function Payoff(s, e)
If s < e Then Payoff = e - s
End Function
31. MC Simulation
PayOff
PayOff
Log-normal
S(t+1) = S(t) * Exp((r-d-.5*Vol^2)*dt + *Vol*sqr(dt))
()
: NormsInv(Rand())
Box-Muller Algorithm
32. MC Simulation
Function BoxMuller()
Randomize
Do
X = 2 * Rnd() - 1
Y = 2 * Rnd() - 1
dist = X * X + Y * Y
Loop Until dist < 1
BoxMuller = X * Sqr(-2 * Log(dist) / dist)
End Function
33.
Cholesky Factorization
Correlated Random Variable
() (MMT )
= M Correlated Random Variable Vector
E( i j) = ij
= M
MMT =
T = M T MT
E( T) = ME[ T ] MT = MMT =
, : correlation matrix
Matrix LU
=> Correlation Matrix
,
Matrix M
0.67082
0.044721
0.193793
0.089443
0.134164
Product of M
and M^T
0.45
0.03
0.13
0.06
0.09
0.03
0.34
0
-0.09
0.12
0.13
0
0.28
0.001
-0.13
0.06
-0.09
0.001
0.41
0.03
0.09
0.12
-0.13
0.03
0.32
0
0
0
0
0.581378
0
0
0
-0.014907 0.492161
0
0
-0.161685 -0.038084 0.611889
0
0.196086 -0.31103 0.061872 0.403711
0.03
0.34
0
-0.09
0.12
0.13
0
0.28
0.001
-0.13
0.06
-0.09
0.001
0.41
0.03
0.09
0.12
-0.13
0.03
0.32
36. Performance
Issue
MC Simulations (Sqrt() )
Antithetic Variables
Control Variate Method
( )
Drift
, ( )
( , closed form solution)
,
, (?)
V1 = V1 V2 + V2
, V1: V1 , V2: V2 , V2: V2
End