Writing library files

All functions written in an expression window can only be accessed within the workspace. To create functions accessible to all workspaces you can write library files.

Creating library functions

In the Editor, select View | Library. This will open a sub window with corresponding to the paths in the ini-file which loads the library files. Right click in the library tree and use menu to add, select, delete and change any library files. A new folder can be created for library files at this point. This folder will be added to the path if it is not already there.

Write the following code in the expression window corresponding to the library file:

number my_lib_function(number x)
  option (category: 'Test')
{
    return x * x;
}

The keyword option is in this case used for defining the category for the function. This means that a folder called Test will appear in the Function browser, containing the function my_lib_function.

Choose File | and any compile option, which will compile your library file together with all other library files. This can be done with or without saving the file to disk. Now you can open the Function browser and inspect your function.

Tip

If you do not wish to compile but rather just check the code for syntax errors, Ctrl + F7 can be used instead. This would not result in the loss of attached functions in the Workspace in case the compile fails, which is otherwise the case.

Writing overloaded functions

It is possible to define overloaded functions, i.e., functions with the same name as another function but having other parameter definitions. For example, you can define several functions with the same name and return type but with different types of a parameter. Or you can define several functions with different number of parameters. Of course, all overloaded functions must have the same return type.

Adding member functions to object classes

In library files you can write functions that are treated as member functions to existing Quantlab objects. This means that you can for example hook on your own valuation methods or you can add methods giving real-time or database data. We illustrate this with two examples.

Adding a valuation method to an instrument

In this example we will show how to add a function giving a number output to an instrument object. A member function is created by using the dot-notation that also is used when calling the function:

number instrument.my_price(instrument i, number param)
{
   number answer;

   // Some clever calculations...

   return answer;
}

The first argument to the member function must be the object itself. After you have compiled the library file this function will appear in the member function list of the instrument object.

Adding a quote field method to an instrument

In this example we will show how to add a member function to an instrument that returns a turnover volume for that particular instrument.

First, you have to define the appropriate quote field in the database. See the manual for DatabaseTool for further information on this subject. Start by defining a new quote field by entering a new row in the table QuoteDef.

QUOTE_NAME

QUOTE_TYPE

QUOTE_COLUMN_NAME

FID_COLUMN_NAME

REAL_QUOTE

volume

number

volume

volume

0

The volume is of the basic Qlang type number and refers to a column in the Quote table that has to be defined, and which in this case is called volume. If you want to have real time data, you also have to define a new column in the table RealtimeLink called volume. There you write the FID number for the volume for each instrument. The last column is set to zero which means that this quote is not a “real quote”, i.e., it cannot be used as a quote_side when pricing instruments.

Now you have to define a member function that retrieves this data. This is simple as you only have to call the member function get_quote_num which gives any numeric quote, given the name in the QuoteDef table:

number instrument.volume(instrument i)
{
    return i.get_quote_num('volume');
}

After you have compiled the library file this function will appear in the member function list of the instrument object.

The string parameter of the get_quote functions is not limited to the type quote_side.

If you try to access a quote_side that is not defined for an instrument, this will give a runtime error of the type ‘database’.