CustomFunction





http://www.QuantTools.com
CapeTools Utils function list

Welcome | Documentation format | QuantTools Groups | QuantTools Categories | Licence

Key TAGs | Excel Index | API Index



Creates a custom function object with one variable and up to 15 constant arguments.

'x' represents the variable, 'a' through 'n' the constants.

The constant 'e' is predefined as 2.718281828459045235360287, the constant 'pi' is predefined as 3.141592653589793238462643. Thus as an example, you can input the string 'x^2-a+(d*c*x)-b' (not including the single quotes) for the 'Formula' parameter.

This string has x as the variable (which is true in all cases) and has defined 4 constant parameters (a, b, c and d).

The value of these 4 constant parameters must be provided via the 'Const_a', 'Const_b', 'Const_c' and 'Const_d' parameters.

If for example, values of 2, 5, 1.2 and 2 are used respectively, you have essentially defined the function 'x^2-a+(d*c*x)-b' => 'x^2-2+(2*1.2*x)-5' => 'x^2+2.4*x-7'. You may use the regular c++ math functions (min, max, exp, log, sqrt, sin, cos, log10).

We have also implemented an if(condition, true, false) function which can be used in the following manner.

'if(a>=c,5,a+b)*5.2'. This object can be pass to the integration functions (contained within this category of functions) in order to integrate the expression between limits.

You can also evaluate the expression via the EvaluateFunction() function at a particular point.

Internally the string-function is converted into bytecode so that repeated execution of the expression avoids the parsing and validation phase.



This function creates an object and returns a string-key value to represent this created object.
The TAG value of the string-key returned (second part of the key) is : "CUSTFUNC"



Note: Within Excel, the function is named - CT.FUNC.CustomFunction




High level graphic of CustomFunction() function with parameters. Blue square node is the actual function with the parameters ordered.



Parameter Description


  1. Key parameter

    Key value to use as a handle for the created object
  2. Reload parameter

    When creating this object for the first time, set this parameter to a positive value. Within Excel, when re-computing a worksheet where you do not wish to recreate the object, set this parameter to zero (0).
  3. Formula parameter

    Formula of interest [ie - min(max(x-a,b),max(d-a,e))+exp(c)-log(-g*h)+if(d>=f,5,6) ] (Do not include quotes around the formula).
  4. Const_a parameter

    Constant numeric value for this parameter.
  5. Const_b parameter

    Constant numeric value for this parameter.
  6. Const_c parameter

    Constant numeric value for this parameter.
  7. Const_d parameter

    Constant numeric value for this parameter.
  8. Const_f parameter

    Constant numeric value for this parameter.
  9. Const_g parameter

    Constant numeric value for this parameter.
  10. Const_h parameter

    Constant numeric value for this parameter.
  11. Const_i parameter

    Constant numeric value for this parameter.
  12. Const_j parameter

    Constant numeric value for this parameter.
  13. Const_k parameter

    Constant numeric value for this parameter.
  14. Const_l parameter

    Constant numeric value for this parameter.
  15. Const_m parameter

    Constant numeric value for this parameter.
  16. Const_n parameter

    Constant numeric value for this parameter.


Extended information

Function Syntax

VB Syntax


String CTUtils.CustomFunction( _
String Key, _
Long Reload, _
String Formula, _
Double Const_a, _
Double Const_b, _
Double Const_c, _
Double Const_d, _
Double Const_f, _
Double Const_g, _
Double Const_h, _
Double Const_i, _
Double Const_j, _
Double Const_k, _
Double Const_l, _
Double Const_m, _
Double Const_n)


Excel Spreadsheet Syntax


=CT.FUNC.CustomFunction(
Excel String Cell Key,
Excel Numeric Cell Reload,
Excel String Cell Formula,
Excel Numeric Cell Const_a,
Excel Numeric Cell Const_b,
Excel Numeric Cell Const_c,
Excel Numeric Cell Const_d,
Excel Numeric Cell Const_f,
Excel Numeric Cell Const_g,
Excel Numeric Cell Const_h,
Excel Numeric Cell Const_i,
Excel Numeric Cell Const_j,
Excel Numeric Cell Const_k,
Excel Numeric Cell Const_l,
Excel Numeric Cell Const_m,
Excel Numeric Cell Const_n)


C++ Syntax


static std::string CustomFunction(
std::string Key,
long Reload,
std::string Formula,
double Const_a,
double Const_b,
double Const_c,
double Const_d,
double Const_f,
double Const_g,
double Const_h,
double Const_i,
double Const_j,
double Const_k,
double Const_l,
double Const_m,
double Const_n);


DotNET Syntax


System.String CTUtilsSA.CustomFunction(
System.String Key,
System.Int32 Reload,
System.String Formula,
System.Double Const_a,
System.Double Const_b,
System.Double Const_c,
System.Double Const_d,
System.Double Const_f,
System.Double Const_g,
System.Double Const_h,
System.Double Const_i,
System.Double Const_j,
System.Double Const_k,
System.Double Const_l,
System.Double Const_m,
System.Double Const_n);

Parameter data types

ArgNameArgTypeIsKey
KeyStringFALSE
ReloadLongFALSE
FormulaStringFALSE
Const_aDoubleFALSE
Const_bDoubleFALSE
Const_cDoubleFALSE
Const_dDoubleFALSE
Const_fDoubleFALSE
Const_gDoubleFALSE
Const_hDoubleFALSE
Const_iDoubleFALSE
Const_jDoubleFALSE
Const_kDoubleFALSE
Const_lDoubleFALSE
Const_mDoubleFALSE
Const_nDoubleFALSE


Example Inputs

The first column represents the name of the parameters. The second column specifies whether the parameters are optional or not. Finally the last column provides some sample input data.
Function call input string-keys are always in the format : "NAME.EXTTAG.TICKER" The "EXTTAG.TICKER" part is determined from the output of other, capetools, object creation functions.


ArgNameIsOptional (Excel only)Example
KeyFALSEMyCustomFunction
ReloadFALSE1
FormulaFALSEx^2-a+(d*c*x)-b
Const_aTRUE2
Const_bTRUE5
Const_cTRUE1.2
Const_dTRUE2
Const_fTRUE0.0
Const_gTRUE0.0
Const_hTRUE0.0
Const_iTRUE0.0
Const_jTRUE0.0
Const_kTRUE0.0
Const_lTRUE0.0
Const_mTRUE0.0
Const_nTRUE0.0


Example function usage


The C# example below contains all the sub-function calls leading up to this function call. As a result, the example can contain a lot of code.

The VB.NET, J#, C++.NET, Java, Excel VBA, Visual Basic 6 (via COM) and C++ examples below contain function code stubs for the calls leading up to this function call. However, the function call for this function is displayed.
You can easily reproduce the stub functions code from the C# example.


If you are accessing this functrion via the MiniXL libraries, this function is present within the CT.QL.Utils20 MiniXL Excel Addin.

Within our Excel Example Addin Generator, we have used the following QuantTools sub-functions in order to prepare the arguments needed to call the CustomFunction() function. If you are executing this function via the MiniXL libraries, the module addin name, (in brackets, to the right of the sub-functions listed below), indicates the MiniXL library in which the sub-function is held. You will need to load this library into your Excel session (along with any other libraries that the sub-function call within the addin requires (ie - CT.QT.Utils20 addin in almost all cases) in order for the example to compute successfully.


The following four examples demostrate calling this function within a Microsoft .NET environment

The following four examples demostrate calling this function within a non .NET environment

The following is a sample output from executing the CustomFunction() function call


MyCustomFunction_23.CUSTFUNC.0

Copyright (c) 2003-2007 CapeTools - All Rights Reserved.