• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA Error

Satyaprakash

New Member
I have the following code and it is giving me a compile error: user-defined type not defined.
Could someone help me.
Code:
Function set_cNum(rPart, iPart) As cNum
set_cNum.rP = rPart
set_cNum.iP = iPart
End Function
Function cNumProd(cNum1 As cNum, cNum2 As cNum) As cNum
cNumProd.rP = (cNum1.rP * cNum2.rP) - (cNum1.iP * cNum2.iP)
cNumProd.iP = (cNum1.rP * cNum2.iP) + (cNum1.iP * cNum2.rP)
End Function
Function cNumAdd(cNum1 As cNum, cNum2 As cNum) As cNum
cNumAdd.rP = (cNum1.rP + cNum2.rP)
cNumAdd.iP = (cNum1.iP + cNum2.iP)
End Function
Function cNumSub(cNum1 As cNum, cNum2 As cNum) As cNum
cNumSub.rP = (cNum1.rP - cNum2.rP)
cNumSub.iP = (cNum1.iP - cNum2.iP)
End Function
Function cNumDiv(cNum1 As cNum, cNum2 As cNum) As cNum
cNumProd.rP = ((cNum1.rP * cNum2.rP) + (cNum1.iP * cNum2.iP)) / (cNum2.rP ^ 2 + cNum2.iP ^ 2)
cNumProd.iP = ((cNum1.iP * cNum2.rP) - (cNum1.rP * cNum2.iP)) / (cNum2.rP ^ 2 + cNum2.iP ^ 2)
End Function
Function cNumSqrt(cNum1 As cNum) As cNum
r = Sqr(cNum1.rP ^ 2 + cNum1.iP ^ 2)
y = Atn(cNum1.iP / cNum1.rP)
cNumSqrt.rP = Sqr(r) * Cos(y / 2)
cNumSqrt.iP = Sqr(r) * Sin(y / 2)
End Function
Function cNumExp(cNum1 As cNum) As cNum
cNumExp.rP = Exp(cNum1.rP) * Cos(cNum1.iP)
cNumExp.iP = Exp(cNum1.rP) * Sin(cNum1.iP)
End Function
Function cNumLn(cNum1 As cNum) As cNum
r = (cNum1.rP ^ 2 + cNum1.iP ^ 2) ^ 0.5
theta = Atn(cNum1.iP / cNum1.rP)
cNumLn.rP = Application.Ln(r)
cNumLn.iP = theta
End Function
Function HestonP1(phi, kappa, theta, lambda, rho, sigma, tau, K, S, r, v)
mu1 = 0.5
b1 = set_cNum(kappa + lambda - rho * sigma, 0)
d1 = cNumSqrt(cNumSub(cNumSq(cNumSub(set_cNum(0, rho * sigma * phi), b1)), cNumSub(set_cNum(0, sigma ^ 2 * 2 * mu1 * phi), set_cNum(sigma ^ 2 * phi ^ 2, 0))))
g1 = cNumDiv(cNumAdd(cNumSub(b1, set_cNum(0, rho * sigma * phi)), d1), cNumSub(cNumSub(b1, set_cNum(0, rho * sigma * phi)), d1))
DD1_1 = cNumDiv(cNumAdd(cNumSub(b1, set_cNum(0, rho * sigma * phi)), d1), set_cNum(sigma ^ 2, 0))
DD1_2 = cNumSub(set_cNum(1, 0), cNumExp(cNumProd(d1, set_cNum(tau, 0))))
DD1_3 = cNumSub(set_cNum(1, 0), cNumProd(g1, cNumExp(cNumProd(d1, set_cNum(tau, 0)))))
DD1 = cNumProd(DD1_1, cNumDiv(DD1_2, DD1_3))
CC1_1 = set_cNum(0, r * phi * tau)
CC1_2 = set_cNum((kappa * theta) / (sigma ^ 2), 0)
CC1_3 = cNumProd(cNumAdd(cNumSub(b1, set_cNum(0, rho * sigma * phi)), d1), set_cNum(tau, 0))
CC1_4 = cNumProd(set_cNum(2, 0), cNumLn(cNumDiv(cNumSub(set_cNum(1, 0), cNumProd(g1, cNumExp(cNumProd(d1, set_cNum(tau, 0))))), cNumSub(set_cNum(1, 0), g1))))
cc1 = cNumAdd(CC1_1, cNumProd(CC1_2, cNumSub(CC1_3, CC1_4)))
f1 = cNumExp(cNumAdd(cNumAdd(cc1, cNumProd(DD1, set_cNum(v, 0))), set_cNum(0, phi * Application.Ln(S))))
HestonP1 = cNumReal(cNumDiv(cNumProd(cNumExp(set_cNum(0, -phi * Application.Ln(K))), f1), set_cNum(0, phi)))
End Function

Function HestonP2(phi, kappa, theta, lambda, rho, sigma, tau, K, S, r, v)
mu2 = -0.5
b2 = set_cNum(kappa + lambda, 0)
d1 = cNumSqrt(cNumSub(cNumSq(cNumSub(set_cNum(0, rho * sigma * phi), b2)), cNumSub(set_cNum(0, sigma ^ 2 * 2 * mu2 * phi), set_cNum(sigma ^ 2 * phi ^ 2, 0))))
g1 = cNumDiv(cNumAdd(cNumSub(b2, set_cNum(0, rho * sigma * phi)), d1), cNumSub(cNumSub(b2, set_cNum(0, rho * sigma * phi)), d1))
DD1_1 = cNumDiv(cNumAdd(cNumSub(b2, set_cNum(0, rho * sigma * phi)), d1), set_cNum(sigma ^ 2, 0))
DD1_2 = cNumSub(set_cNum(1, 0), cNumExp(cNumProd(d1, set_cNum(tau, 0))))
DD1_3 = cNumSub(set_cNum(1, 0), cNumProd(g1, cNumExp(cNumProd(d1, set_cNum(tau, 0)))))
DD1 = cNumProd(DD1_1, cNumDiv(DD1_2, DD1_3))
CC1_1 = set_cNum(0, r * phi * tau)
CC1_2 = set_cNum((kappa * theta) / (sigma ^ 2), 0)
CC1_3 = cNumProd(cNumAdd(cNumSub(b2, set_cNum(0, rho * sigma * phi)), d1), set_cNum(tau, 0))
CC1_4 = cNumProd(set_cNum(2, 0), cNumLn(cNumDiv(cNumSub(set_cNum(1, 0), cNumProd(g1, cNumExp(cNumProd(d1, set_cNum(tau, 0))))), cNumSub(set_cNum(1, 0), g1))))
cc1 = cNumAdd(CC1_1, cNumProd(CC1_2, cNumSub(CC1_3, CC1_4)))
f1 = cNumExp(cNumAdd(cNumAdd(cc1, cNumProd(DD1, set_cNum(v, 0))), set_cNum(0, phi * Application.Ln(S))))
HestonP1 = cNumReal(cNumDiv(cNumProd(cNumExp(set_cNum(0, -phi * Application.Ln(K))), f1), set_cNum(0, phi)))
End Function
Function Heston(PutCall As String, kappa, theta, lambda, rho, sigma, tau, K, S, r, v)
Dim P1_int(1001) As Double, P2_int(1001) As Double, phi_int(1001) As Double
Dim p1 As Double, p2 As Double, phi As Double, xg(16) As Double, wg(16) As Double
cnt = 1
For phi = 0.0001 To 100.0001 Step 0.1
phi_int(cnt) = phi
P1_int(cnt) = HestonP1(phi, kappa, theta, lambda, rho, sigma, tau, K, S, r, v)
P2_int(cnt) = HestonP2(phi, kappa, theta, lambda, rho, sigma, tau, K, S, r, v)
cnt = cnt + 1
Next phi
p1 = 0.5 + (1 / thePI) * TRAPnumint(phi_int, P1_int)
p2 = 0.5 + (1 / thePI) * TRAPnumint(phi_int, P2_int)
If p1 < 0 Then p1 = 0
If p1 > 1 Then p1 = 1
If p2 < 0 Then p2 = 0
If p2 > 1 Then p2 = 1
HestonC = S * p1 - K * Exp(-r * tau) * p2
If PutCall = "Call" Then
Heston = HestonC
ElseIf PutCall = "Put" Then
Heston = HestonC + K * Exp(-r * tau) - S
End If
End Function
 
Last edited by a moderator:
Hi Satyaprakash ,

You can make it easier for others by :

a) Which function is giving the error ?

b) Within the function , which line is generating the error ?

Narayan
 
I don't think the Definition part of each function
Function set_cNum(rPart, iPart) As cNum
is allowable as VBA doesn't have a data type cNum

I'd change them all to As Variant
eg:
Function set_cNum(rPart, iPart) As variant
 
Hi ,

I don't think replacing cNum by Variant will work ; I think cNum stands for a complex number , which has a real part and an imaginary part.

To handle such numbers , there has to be a user-defined type cNum.

Narayan
 
I am sorry it was a bit hastily posted.... Actually I am all thumbs when it comes to VBA. Narayan, for me the first function itself is giving the error. However on impulse I just posted the entire thing so that if a mistake is there anywhere else also, then someone can help me out.
P.S:Actually I didnt write the code; I compiled it from a source.
 
Hi Satyaprakash ,

If you explain why you have copied this section of code , and post the source , we can help.

It looks as if what you have posted is just a subset of a complete suite of complex number handling routines / functions. As such , you should probably download the full set rather than take bits and pieces.

Narayan
 
Thank You Debraj,
But I took the VBA from this book only... and I am getting the error.

And Narayan,
No actually I copied them all from the book Option pricing using Excel VBA by fabrice rouah.
 
Last edited:
Hi Satyaprakash ,

Never copy ; try to understand.

If you read the book , or at least the section from where you copied this code , you will see what else you are supposed to copy.

Narayan
 
Thank you Narayan. That solved my problem.
But now I have another.
Function HestonP1(phi, kappa, theta, lambda, rho, sigma, tau, K, S, r, v)
mu1 = 0.5
b1 = set_cNum(kappa + lambda - rho * sigma, 0)
In this line for b1 is giving me an error- Only user defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions. I then entered the code
Dim b1 As Class1
Then set_cNum gets highlighted with the same error. But I have already put set_cNum as a function in the code. What is happening?
 
Last edited:
Hi Satyaprakash ,

Can you please upload a sample file with all of the code in it ? It is difficult to debug segments of code , especially the one you have posted.

Narayan
 
Hi Satyaprakash ,

See your file now.

I have made quite a few changes which I thought logical ; they may not be mathematically correct , since I have no idea what the code is doing.

You need to verify that you get the correct outputs for the given inputs.

Narayan
 

Attachments

  • trial.xlsm
    28.4 KB · Views: 44
The function is an implementation of the Heston method of calculating the Black-Scholes option pricing.
 
Back
Top