Wątek przeniesiony 2021-01-28 13:30 z Inne języki programowania przez cerrato.

VBA Run-time error 13 Type mismath

0

Hej nie rozumiem czemu wyrzuca mi ten blad. Oto moj kod, wszystkie zmienne sa chyba dobrze zainicjowane. Dla mniejszych wartosci N i num_of_sim dziala bez problemu

Function payoff(S_T, K, CallPut As String)
If CallPut = "call" Then
    omega = 1
Else: omega = -1
End If

payoff = maks(omega * (S_T - K), 0)

End Function

Function BS_trajektoria(S_0 As Double, T As Double, r As Double, q As Double, sigma As Double, N As Long) As Double()

Randomize
Dim S() As Double
Dim delta_t As Double
Dim i As Long
ReDim S(N)
S(0) = S_0
delta_t = T / N

For i = 1 To N
    S(i) = S(i - 1) * Exp((r - q - 0.5 * sigma ^ 2) * delta_t + sigma * delta_t ^ 0.5 * Application.NormSInv(Rnd))
Next i

BS_trajektoria = S
End Function

Function barrier_MC(S_0 As Double, K As Double, T As Double, r As Double, q As Double, sigma As Double, _
                        B As Double, N As Long, num_of_sim As Long, CallPut As String, BarType As String) As Double

Randomize
Dim max_value As Double
Dim suma_wyplat As Double
Dim wyplata As Double
Dim i As Long
Dim S() As Double
suma_wyplat = 0

If (BarType = "DO" Or BarType = "DI") And B > S_0 Then
    MsgBox "Za wysoka bariera dla opcji DOWN!"
    Exit Function
ElseIf (BarType = "UO" Or BarType = "UI") And B < S_0 Then
    MsgBox "Za niska bariera dla opcji UP!"
    Exit Function
End If

With WorksheetFunction
For i = 1 To num_of_sim
    S = BS_trajektoria(S_0, T, r, q, sigma, N)
    max_value = .Max(S)
    If max_value >= B Then
        wyplata = 0
    Else
        wyplata = payoff(S(N), K, CallPut)
    End If
    suma_wyplat = suma_wyplat + wyplata
Next i
End With
barrier_MC = Exp(-r * T) * suma_wyplat / num_of_sim
End Function

Sub test3()
MsgBox barrier_MC(100#, 100#, 1#, 0.05, 0.02, 0.2, 120#, 1000, 1000000, "call", "UO")
End Sub
0

Nie można sprawdzić. Nie podałeś kodu funkcji maks uzytej w payoff.

0

@Marcin.Miga: Mój błąd, zamiast maks powinno być WorksheetFunction.Max

1
Function payoff(S_T, K, CallPut As String)
On Error GoTo er
10: If CallPut = "call" Then
20:    omega = 1
30: Else
40:    omega = -1
50: End If

60: payoff = WorksheetFunction.Max(omega * (S_T - K), 0)

Exit Function
er:
MsgBox " błąd w linii: " & Erl & vbNewLine & Err.Description

End Function

Function BS_trajektoria(S_0 As Double, T As Double, r As Double, q As Double, sigma As Double, N As Long) As Double()
On Error GoTo er

Randomize
Dim S() As Double
Dim delta_t As Double
Dim i As Long
Dim rn As Single
70: ReDim S(N)
80: S(0) = S_0
90: delta_t = T / N

100: For i = 1 To N
110:    Do
120:        rn = Rnd
130:    Loop While rn <= 0
140:    S(i) = S(i - 1) * Exp((r - q - 0.5 * sigma ^ 2) * delta_t + sigma * delta_t ^ 0.5 * Application.NormSInv(rn))
150: Next i

160: BS_trajektoria = S

Exit Function
er:
MsgBox " błąd w linii: " & Erl & vbNewLine & Err.Description & vbNewLine & "I:=" & i
End Function

Function barrier_MC(S_0 As Double, K As Double, T As Double, r As Double, q As Double, sigma As Double, _
                        B As Double, N As Long, num_of_sim As Long, CallPut As String, BarType As String) As Double
On Error GoTo er

Randomize
Dim max_value As Double
Dim suma_wyplat As Double
Dim wyplata As Double
Dim i As Long
Dim S() As Double
170: suma_wyplat = 0

180: If (BarType = "DO" Or BarType = "DI") And B > S_0 Then
190:    MsgBox "Za wysoka bariera dla opcji DOWN!"
200:    Exit Function
210: ElseIf (BarType = "UO" Or BarType = "UI") And B < S_0 Then
220:    MsgBox "Za niska bariera dla opcji UP!"
230:    Exit Function
240: End If

250: With WorksheetFunction
260: For i = 1 To num_of_sim
270:    S = BS_trajektoria(S_0, T, r, q, sigma, N)
280:    max_value = .Max(S)
290:    If max_value >= B Then
300:        wyplata = 0
310:    Else
320:        wyplata = payoff(S(N), K, CallPut)
330:    End If
340:    suma_wyplat = suma_wyplat + wyplata
350: Next i
360: End With
370: barrier_MC = Exp(-r * T) * suma_wyplat / num_of_sim

Exit Function
er:
MsgBox " błąd w linii: " & Erl & vbNewLine & Err.Description & vbNewLine & "I:=" & i
End Function

Sub test3()
MsgBox barrier_MC(100#, 100#, 1#, 0.05, 0.02, 0.2, 120#, 1000, 1000000, "call", "UO")
End Sub

1 użytkowników online, w tym zalogowanych: 0, gości: 1