Skip to content
Alexander Trufanov edited this page Oct 13, 2021 · 1 revision

Debugging TA-Lib with Qt Creator IDE (Linux)

  1. Get the Qt Creator IDE (it comes with gdb debugger)
  2. Get the TA-Lib sourcecode.
    It may be original sources (wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz)
    or TA-Lib RT sources (git clone https://github.com/trufanov-nok/ta-lib-rt.git).
  3. Configure buildchain with debug mode, build the sources:
./configure --enable-debug
make

Note --enable-debug argument - it will instruct compiler to not optimize the code.

  1. Open Qt Creator, Create a new project, chose Import Project/ Import existing Project.

Type in path to the sources (folder with configure script) and project title (like ta-lib-library). Leave File Selection settings as is, except for tools subfolder - you may unmark it.
Finish the new project wizard - now we have a new project.
Make sure it's buildable from IDE (In fact QtCreator is just invoking make).

  1. Create a client application that loads TA-Lib library Create a new project, let it be console Qt application ta-client.
    Add linking to TA-lib or TA-lib RT in it's .pro file:
# You may use /usr/include/ta-lib/ or /usr/local/include/ta-lib/ if ta-lib is installed
# INCLUDEPATH += /usr/local/include/ta-lib/
# LIBS += -L/usr/local/lib/ -lta_lib
 INCLUDEPATH += /path_to_ta_lib_sources/include/ta-lib/
 LIBS += -L/path_to_ta_lib_sources/src/.libs/ -lta_lib

For Ta-Lib RT replace /ta-lib with /ta-lib-rt and -lta_lib with -lta-lib-rt

In main.cpp write some code that uses TA-Lib indicator you need. For ex., STOCH:

#include "ta_func.h"

int main(int /*argc*/, char * /*argv*/[])
{
    if ( TA_Initialize() != TA_SUCCESS)
        return -1;

    // some data
    double data_H[50] = {105.68, 93.74, 92.72, 90.52, 95.22, 100.35, 97.92, 98.83, 95.33, 93.4, 95.89, 96.68, 98.78, 98.66, 104.21, 107.48, 108.18, 109.36, 106.94, 107.73, 103.13, 114.92, 112.71, 113.05, 114.06, 117.63, 116.6, 113.72, 108.84, 108.43, 110.06, 111.79, 109.9, 113.95, 115.97, 116.52, 115.82, 117.91, 119.04, 120, 121.95, 129.08, 132.12, 135.72, 136.66, 139.78, 139.14, 139.99, 140.64, 143.66};
    double data_L[50];
    double data_C[50];
    for (int i = 0; i < 50; i++) {
        data_L[i] = data_H[i] /10;
        data_C[i] = data_H[i] /5;
    }

    const int data_size = sizeof(data_H) / sizeof(double);

    QVector<double> outSlowK;
    QVector<double> outSlowD;
    int outBegIdx= 0;
    int outNbElement = 0;

    int sz = data_size - TA_STOCH_Lookback(14, 1, TA_MAType_SMA, 3, TA_MAType_SMA);


    Q_ASSERT(sz != 0);

    outSlowK.resize(sz);
    outSlowD.resize(sz);

    // get TA results
    TA_RetCode res = TA_STOCH(0, data_size-1, data_H, data_L, data_C, 14, 1, TA_MAType_SMA, 3, TA_MAType_SMA, &outBegIdx, &outNbElement,
                               outSlowK.data(), outSlowD.data());


    if (res != TA_SUCCESS) {
        qDebug() << "Error: " << res;
        return -1;
    }
    else {
        for (int i = 0; i < outNbElement; i++)
            qDebug() << "day #" << outBegIdx+i
                        << " outSlowK: " << outSlowK[i]
                           << "outSlowD: " << outSlowD[i];
    }


    return TA_Shutdown();
}

Make sure your project is buildable

If TA-Lib library is not installed to the system (I prefer to debug locally built library to be able modify it, build and load via client without installation), then we must instruct client app to load proper library binary. Go to Projects (buttons on the right bar), select Run and in Run Settings display Build Environment details. Here edit or create new environment variable: LD_LIBRARY_PATH. Set it to the /path_to_ta_lib_sources/src/.libs/ folder (here are libta_lib.so.0.0.0 binary is stored after TA-Lib compilation). If LD_LIBRARY_PATH had a non-empty value, prepend it with /path_to_ta_lib_sources/src/.libs/:. Note : at the end - it's a paths separator.

Now you shall be able to build and launch your app. And it will load a proper TA-Lib library binary from /path_to_ta_lib_sources/src/.libs/.

  1. Debugging

Both library and client projects must be opened in Qt Creator. Client project must be active. Set a breakpoint in main.cpp. Start debugging and press F11, for example, at line with TA_Initialize(). You will fall into /path_to_ta_lib_sources/src/ta_common/ta_global.c and will be able to browse and debug the TA-Lib code step by step.