Enhancing the CQG Supplied CQG-MATLAB API Examples

This tutorial is one in a series that discusses Using CQG From Within MATLAB.

A short critique of the CQG-MATLAB API examples downloadable from the CQG web site is presented, along with some suggestions for enhancement. The examples linked off the Using CQG From Within MATLAB index page show how to incorporate the suggestions presented here when writing MATLAB applications that use CQG.

The specific topics discussed here are,

It is assumed that the reader has some understanding of both the CQG API and MATLAB, although a detailed knowledge of neither is required.

Depth and Width of Examples

Numerous examples demonstrating how to use the CQG API are available for download from the CQG web site. However, the vast majority of those examples are written in VBA, C# or C++. There are only half a dozen examples dealing specifically with the CQG-MATLAB API.

On the plus side, the MATLAB examples are typically quite good at outlining the very basics of the API. They provide good starting points for learning how to connect to CQG; request different types of data and/or execute trades; and handle the events fired by CQG to make MATLAB aware that new data is available.

However, due to the limited number of available MATLAB examples, they do not provide a good breadth of ways to tackle different problems. For those experienced in using VBA, C# and/or C++ it is often useful to look at and understand the examples written using one or more of those languages and then translate that knowledge into a MATLAB implementation.

Of course part of that translation needs to take into account the differences between those languages and MATLAB, and the Limitations of the MATLAB COM Interface. Another aspect of the examples is that they give the impression of having been written by someone not wholly familiar with best practices of MATLAB programming.

The following sections discuss perceived deficiencies in the way the examples have been written and where applicable suggest alternative programming methodologies to bring the examples more inline with customary MATLAB best practices.

The (Unnecessary) Use of Global Variables

The examples supplied by CQG make extensive use of global variables. Globals are almost universally considered a bad idea when using MATLAB.

Yes, globals are available, so why not use them? The simple answer is that there are certain very strict rules that need to be followed to use them correctly, and that it is very easy, even when being extremely careful, to use them incorrectly. And when used incorrectly (even, or perhaps especially, when done inadvertently) they can make debugging extremely difficult.

In practice there are very few places that globals would be used by an experienced MATLAB programmer, and interfacing with CQG is not one of them. In short: do not use global variables in MATLAB.

The better approach is to recognize that the CQG COM object created within MATLAB is exactly that – an object. And that the MATLAB COM interface allows custom attributes to be attached to COM objects. Hence all of the data that is being created as global in the CQG provided examples can and should be attached to the CQG object as custom attributes. In this way the data gets passed into the CQG event handlers along with the CQG object and is hence always available.

The (Over) Use of pause

The CQG provided examples make extensive use of the pause function. Although this works for the specific examples it isn't difficult to create examples where a pause causes problems.

Pause tells MATLAB to pause execution for a user specified period of time. In cases where MATLAB may be running faster than a specific CQG event then a pause is typically causing an unnecessary delay, while in cases where MATLAB is too slow the addition of a pause just forces an even greater execution mismatch.

Pause has presumably been added in the CQG provided examples to force MATLAB to pause while CQG performs a task (e.g. starts up, or initialize the acquisition of some data). However, the time it takes to perform those task will be different on each installation (due to such things as CPU speed and whether the computer is also currently performing other tasks). Hence the amount of pause required is really unknown.

Due to the asynchronous nature of the CQG events and how MATLAB handles them (as discussed in the Limitations of the MATLAB COM Interface tutorial) it is simply better to try to avoid adding these somewhat random pauses to the code.

Using Separate Functions (or Not)

As applications get larger, or if sections of code can be used across multiple applications, it makes sense for those sections of code to be placed in their own function and/or into their own file. However, the CQG provided examples are all small and self contained. There is really no reason for them to be split across many multiple files as they are.

Although this is arguably a design choice, all of the functions required by each of the examples linked off the Using CQG From Within MATLAB index page, are contained in one consolidated file (for each example).

(Unnecessarily) Using Symbol Toolbox Functions

Several of the CQG provided examples calculate The Greeks for an option on a specified futures contract. For some reason the developers chose to perform a symbolic integration as part of the calculation, meaning that the example(s) cannot be executed by anyone without a license for MATLAB's Symbolic Toolbox.

The calculation in question is contained within the BS_CND function (downloaded as part of the examples from CQG). However it can be performed using the Error Function, which is in the base MATLAB package, thus eliminating the dependence on the Symbolic Toolbox.

The specifc change is to replace

function v = BS_CND(x)

syms t;
v = single(int(exp(- t ^ 2 / 2) / sqrt(2 * pi), -inf, x));


function v = BS_CND(x)

v = single(0.5*(erf(x)-erf(-inf)));

Back To Top | Back to CQG Tutorials Index | More Tutorials