I have created the following code:
Option Explicit
Function EuropeanOption2(OptionType As String, Spot As Double, Strike As Double, RiskFreeInterest As Double, Volatility As Double, TimetoMaturity As Double, Dividend As Double, n As Double, Iterations As Double)
Dim m As Integer
Dim p As Integer
Dim OptionPayoff() As Double, St As Double, deltat As Double, random As Double, EuropeanOptionPrice As Double
ReDim Payoff(1 To Iterations)
deltat = TimetoMaturity / n
Randomize
For m = 1 To Iterations
St = Spot
For p = 1 To n
random = WorksheetFunction.NormSInv(Rnd())
St = St * Exp((RiskFreeInterest - Dividend - volatility ^ 2 / 2) * deltat + Volatility * Sqr(deltat) * random)
Next p
If OptionType = "Call" Then 'Call or Put
Payoff(m) = WorksheetFunction.Max(St - Strike, 0) * Exp(-RiskFreeInterest * TimetoMaturity)
ElseIf OptionType = "Put" Then
Payoff(m) = WorksheetFunction.Max(Strike - St, 0) * Exp(-RiskFreeInterest * TimetoMaturity)
End If
Next m
For m = 1 To Iterations
EuropeanOptionPrice = EuropeanOptionPrice + Payoff(m)
Next m
EuropeanOption2 = EuropeanOptionPrice / Iterations
End Function
Sub Graph2()
Dim Column As Integer
Dim StartingValue As Integer
Dim StartingCell As Integer
Dim LastCell As Integer
Dim CallorPut As String
Dim cell As Integer
Application.ScreenUpdating = False
Application.Calculation = xlManual
Column = 1
StartingValue = 1
StartingCell = 15
LastCell = 15 + Range("G8")
Range("A" & StartingCell, "A" & LastCell).Formula = "=EuropeanOption2($B$4,$B$5,$B$6,$B$9,$B$8,$B$7,$B$10,$G$7,$A$13)"
'FOR 1000 simulations Range("B" & StartingCell, "B" & LastCell).Formula = "=EuropeanOption2($B$4,$B$5,$B$6,$B$9,$B$8,$B$7,$B$10,$G$7,$B$13)"
'For 10.000 simulations Range("C" & StartingCell, "C" & LastCell).Formula = "=EuropeanOption2($B$4,$B$5,$B$6,$B$9,$B$8,$B$7,$B$10,$G$7,$C$13)"
End Sub
As it takes forever to put in formulas for 1000 & 10.000+ simulations, I wanted to ask how to store the results in an array and plot these in the excel sheet and if this could help to speed up to computing process.
WorksheetFunction.Max(St - Strike, 0)
is a lot slower thanIIf(St - Strike > 0, St - Strike, 0)
\$\endgroup\$