### XLW XLL

Hi,

I'm trying to create an XLL using XLW http://xlw.sourceforge.net/ . Below is the VBA code that I need to convert to C++ so I can create the XLL using XLW. I'm new to C++. Does anyone know how to create a function and then call that function in another part of the code for an XLL? Could someone please provide some direction and example code on how this could be implemented? The actual VBA code im trying to implement in XLL using C++ is below.

From what I understand in regular C++ code you would declare a function and you could then call it in the Main() function, this doesn't seem to work when i try to do this when creating an XLL using XLW... for example in regular c++...

int subtraction (int a, int b)
{
int r;
r=a-b;
return (r);
}

int main ()
{
int x=5, y=3, z;
z = subtraction (7,2);
cout << "The first result is " << z << '\n';
cout << "The second result is " << subtraction (7,2) << '\n';
cout << "The third result is " << subtraction (x,y) << '\n';
z= 4 + subtraction (x,y);
cout << "The fourth result is " << z << '\n';
return 0;
}

Excel VBA code....

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169`` ``````Global Const Pi = 3.14159265358979 '// The normal distribution function Public Function ND(X As Double) As Double ND = 1 / Sqr(2 * Pi) * Exp(-X ^ 2 / 2) End Function '// Cummulative double precision algorithm based on Hart 1968 '// Based on implementation by Graham Function CND(X As Double) As Double Dim Y As Double, Exponential As Double, SumA As Double, SumB As Double Y = Abs(X) If Y > 37 Then CND = 0 Else Exponential = Exp(-Y ^ 2 / 2) If Y < 7.07106781186547 Then SumA = 3.52624965998911E-02 * Y + 0.700383064443688 SumA = SumA * Y + 6.37396220353165 SumA = SumA * Y + 33.912866078383 SumA = SumA * Y + 112.079291497871 SumA = SumA * Y + 221.213596169931 SumA = SumA * Y + 220.206867912376 SumB = 8.83883476483184E-02 * Y + 1.75566716318264 SumB = SumB * Y + 16.064177579207 SumB = SumB * Y + 86.7807322029461 SumB = SumB * Y + 296.564248779674 SumB = SumB * Y + 637.333633378831 SumB = SumB * Y + 793.826512519948 SumB = SumB * Y + 440.413735824752 CND = Exponential * SumA / SumB Else SumA = Y + 0.65 SumA = Y + 4 / SumA SumA = Y + 3 / SumA SumA = Y + 2 / SumA SumA = Y + 1 / SumA CND = Exponential / (SumA * 2.506628274631) End If End If If X > 0 Then CND = 1 - CND End Function Public Function GImpliedVolatilityNR(CallPutFlag As String, S As Double, X _ As Double, T As Double, r As Double, B As Double, cm As Double, epsilon As Double) Dim vi As Double, ci As Double Dim vegai As Double Dim minDiff As Double 'Manaster and Koehler seed value (vi) vi = Sqr(Abs(Log(S / X) + r * T) * 2 / T) ci = GBlackScholes(CallPutFlag, S, X, T, r, B, vi) vegai = GVega(S, X, T, r, B, vi) minDiff = Abs(cm - ci) While Abs(cm - ci) >= epsilon And Abs(cm - ci) <= minDiff vi = vi - (ci - cm) / vegai ci = GBlackScholes(CallPutFlag, S, X, T, r, B, vi) vegai = GVega(S, X, T, r, B, vi) minDiff = Abs(cm - ci) Wend If Abs(cm - ci) < epsilon Then GImpliedVolatilityNR = vi Else GImpliedVolatilityNR = "NA" End Function '// The generalized Black and Scholes formula Public Function GBlackScholes(CallPutFlag As String, S As Double, X _ As Double, T As Double, r As Double, B As Double, v As Double) As Double Dim d1 As Double, d2 As Double d1 = (Log(S / X) + (B + v ^ 2 / 2) * T) / (v * Sqr(T)) d2 = d1 - v * Sqr(T) If CallPutFlag = "c" Then GBlackScholes = S * Exp((B - r) * T) * CND(d1) - X * Exp(-r * T) * CND(d2) ElseIf CallPutFlag = "p" Then GBlackScholes = X * Exp(-r * T) * CND(-d2) - S * Exp((B - r) * T) * CND(-d1) End If End Function '// Vega for the generalized Black and Scholes formula Public Function GVega(S As Double, X As Double, T As Double, r As Double, B As Double, v As Double) As Double Dim d1 As Double d1 = (Log(S / X) + (B + v ^ 2 / 2) * T) / (v * Sqr(T)) GVega = S * Exp((B - r) * T) * ND(d1) * Sqr(T) End Function Public Function SkewKurtCorradoSuModified(CallPutFlag As String, S As Double, X As Double, _ T As Double, r As Double, B As Double, v As Double, Skew As Double, Kurt As Double) As Double Dim Q3 As Double, Q4 As Double Dim d As Double, w As Double Dim CallValue As Double w = Skew / 6 * v ^ 3 * T ^ 1.5 + Kurt / 24 * v ^ 4 * T ^ 2 d = (Log(S / X) + (B + v ^ 2 / 2) * T - Log(1 + w)) / (v * Sqr(T)) Q3 = 1 / (6 * (1 + w)) * S * v * Sqr(T) * (2 * v * Sqr(T) - d) * ND(d) Q4 = 1 / (24 * 1 + w) * S * v * Sqr(T) * (d ^ 2 - 3 * d * v * Sqr(T) + 3 * v ^ 2 * T - 1) * ND(d) CallValue = GBlackScholes("c", S, X, T, r, B, v) + Skew * Q3 + (Kurt - 3) * Q4 If CallPutFlag = "c" Then SkewKurtCorradoSuModified = CallValue Else '// Use put-call parity to find put value SkewKurtCorradoSuModified = CallValue - S * Exp((B - r) * T) + X * Exp(-r * T) End If End Function Public Function ESkewKurtCorradoSuModified(OutPutFlag As String, CallPutFlag As String, S As Double, X As Double, T As Double, _ r As Double, B As Double, v As Double, Skew As Double, Kurt As Double, Optional dS) If IsMissing(dS) Then dS = 0.1 End If If OutPutFlag = "p" Then ' Value ESkewKurtCorradoSuModified = SkewKurtCorradoSuModified(CallPutFlag, S, X, T, r, B, v, Skew, Kurt) ElseIf OutPutFlag = "d" Then 'Delta ESkewKurtCorradoSuModified = (SkewKurtCorradoSuModified(CallPutFlag, S + dS, X, T, r, B, v, Skew, Kurt) - SkewKurtCorradoSuModified(CallPutFlag, S - dS, X, T, r, B, v, Skew, Kurt)) / (2 * dS) ElseIf OutPutFlag = "g" Then 'Gamma ESkewKurtCorradoSuModified = (SkewKurtCorradoSuModified(CallPutFlag, S + dS, X, T, r, B, v, Skew, Kurt) - 2 * SkewKurtCorradoSuModified(CallPutFlag, S, X, T, r, B, v, Skew, Kurt) + SkewKurtCorradoSuModified(CallPutFlag, S - dS, X, T, r, B, v, Skew, Kurt)) / dS ^ 2 ElseIf OutPutFlag = "v" Then 'Vega ESkewKurtCorradoSuModified = (SkewKurtCorradoSuModified(CallPutFlag, S, X, T, r, B, v + 0.01, Skew, Kurt) - SkewKurtCorradoSuModified(CallPutFlag, S, X, T, r, B, v - 0.01, Skew, Kurt)) / 2 ElseIf OutPutFlag = "t" Then 'Theta If T <= 1 / 365 Then ESkewKurtCorradoSuModified = SkewKurtCorradoSuModified(CallPutFlag, S, X, 0.00001, r, B, v, Skew, Kurt) - SkewKurtCorradoSuModified(CallPutFlag, S, X, T, r, B, v, Skew, Kurt) Else ESkewKurtCorradoSuModified = SkewKurtCorradoSuModified(CallPutFlag, S, X, T - 1 / 365, r, B, v, Skew, Kurt) - SkewKurtCorradoSuModified(CallPutFlag, S, X, T, r, B, v, Skew, Kurt) End If ElseIf OutPutFlag = "r" Then 'Rho ESkewKurtCorradoSuModified = (SkewKurtCorradoSuModified(CallPutFlag, S, X, T, r + 0.01, B + 0.01, v, Skew, Kurt) - SkewKurtCorradoSuModified(CallPutFlag, S, X, T, r - 0.01, B - 0.01, v, Skew, Kurt)) / (2) ElseIf OutPutFlag = "fr" Then 'Futures options rho ESkewKurtCorradoSuModified = (SkewKurtCorradoSuModified(CallPutFlag, S, X, T, r + 0.01, B, v, Skew, Kurt) - SkewKurtCorradoSuModified(CallPutFlag, S, X, T, r - 0.01, B, v, Skew, Kurt)) / (2) ElseIf OutPutFlag = "f" Then 'Rho2 ESkewKurtCorradoSuModified = (SkewKurtCorradoSuModified(CallPutFlag, S, X, T, r, B - 0.01, v, Skew, Kurt) - SkewKurtCorradoSuModified(CallPutFlag, S, X, T, r, B + 0.01, v, Skew, Kurt)) / (2) End If End Function ``````
Topic archived. No new replies allowed.