### Visual Basic code

Sub CalculateButton_Click()

Dim Simulations As Integer

Dim Drift As Double

Dim Volatility As Double

Dim RandomNumberVolatility As Double

Dim RandomNumberPrice As Double

Dim i As Integer 'Simulations, days

Dim j As Integer 'Nodes'

Dim Gotoy As Integer

Dim Gotox As Integer

Dim StockPrice As Double

Dim Payoff As Double

Dim Stike As Double

Dim IniDailyDrift As Double

Dim ConstTheta As Double

Dim ConstK As Double

Dim FairPrice As Double

Dim RiskFreeInterestRate As Double

Sheets("sheet1").Activate

FairPrice = 0

'-----------Inputs-----------------------------'

Simulations = Range("D3").Value

Periods = Range("E3").Value

StockPrice = Range("F3").Value

Strike = Range("G3").Value

Volatility = Range("H6").Value 'initial volatility t=1

DailyDrift = Range("I6").Value ' FIXED

ExpectedDailyDrift = Range("J6").Value ' VARIABLE

ConstTheta = Range("J3").Value

ConstK = Range("K3").Value

'--Main Funtion, Begin-------------------------'

Gotoy = 10

Gotox = 12

If Simulations <= 5000 Then

For i = 1 To Simulations

For j = 1 To Periods

RandomNumberPrice = Application.WorksheetFunction.NormSInv(Rnd()) 'Zt

RandomNumberVolatility = Application.WorksheetFunction.NormSInv(Rnd()) 'Epsilont

If (Volatility < 0) Or (Volatility > 0.2) Then

Volatility = 0.01

End If

Volatility = Volatility + ConstK * (ConstTheta - Volatility) + RandomNumberVolatility * Sqr(Volatility)
Returni = ExpectedDailyDrift + Volatility * RandomNumberPrice ' Core formula logreturn

ExpectedDailyDrift = DailyDrift - 0.5 * (Volatility) ^ 2 'alpha(t)= daily drift - 0,5*(volatility(t)^2) volatility must change

StockPrice = StockPrice * Exp(Returni) 'Stock price for the next period

Cells(Gotoy, Gotox) = StockPrice

Gotox = Gotox + 1

Next j

Gotox = 12

Payoff = StockPrice - Strike ' call option payoff max{Sn-K,0}
If Payoff < 0 Then

Payoff = 0

End If

FairPrice = Payoff + FairPrice
Cells(Gotoy, 4).Value = i

Cells(Gotoy, 5).Value = RandomNumberVolatility

Cells(Gotoy, 6).Value = RandomNumberPrice

Cells(Gotoy, 7).Value = Volatility

Cells(Gotoy, 8).Value = Returni

Cells(Gotoy, 9).Value = StockPrice

Cells(Gotoy, 10).Value = Payoff

Gotoy = Gotoy + 1

'-----------Restart Inputs For the New Simulation-----------------------------'

Volatility = Range("H6").Value 'initial volatility t=1

DailyDrift = Range("I6").Value ' FIXED

ExpectedDailyDrift = Range("J6").Value ' VARIABLE

'-----------------------------------------------------------------------------'

Next i

'-----------Option Price Calculation-------------------------------------------'

FairPrice = (FairPrice / Simulations) * e ^ (-Periods * RiskFreeInterestRate)
Range("B5").Value = FairPrice

'-----------------------------------------------------------------------------'

Else

MsgBox "It will only run for 5000"
End If

End Sub

Sub ClearButton_Click()
Dim AmountRandomNumber As Double

Dim Gotoy As Integer

Dim Gotox As Integer

Simulations = Range("D3")

Periods = 12 + Range("E3").Value

Gotoy = 10

Sheets("sheet1").Activate

Gotox = 4

Range("B5") = ""

For x = 4 To Periods

For i = 1 To Simulations

Cells(Gotoy, Gotox) = ""

Gotoy = Gotoy + 1

Next i

Gotoy = 10

Gotox = Gotox + 1
Next x

End Sub

**Dostları ilə paylaş:**