3

This is my first c++ program and I'm not very familar with c either so please be gentle with me!

I'm trying to call the TA_MACD function from the ta_lib library and having no success. Could someone help me out please.

My code is this:

#include <iostream>
#include "ta_libc.h"

using namespace std;

void go(const TA_Real *array[], TA_Integer fastPeriod, TA_Integer slowPeriod, TA_Integer signalPeriod);

struct Struct_Macd
{
    double Macd;
    double Signal;
    double Histogram;
} MacdResult;

int main(int argc, char**argv) 
{
    TA_Real close_data[]={1.09999000,1.09995000,1.09996000,1.09995000,1.09994000,1.09993000,1.09994000,1.09996000,
                        1.09997000,1.09999000,1.10000000,1.09999000,1.10000000,1.09998000,1.09999000,1.09998000,
                        1.09999000,1.10000000,1.10001000,1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10005000,1.10006000,1.10007000,1.10008000,
                        1.10012000,1.10011000,1.10012000,1.10011000,1.10013000,1.10014000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10017000,1.10018000,1.10019000,1.10018000,1.10017000,
                        1.10018000,1.10017000,1.10018000,1.10017000,1.10016000,1.10014000,1.10013000,1.10015000,
                        1.10014000,1.10015000,1.10014000,1.10013000,1.10012000,1.10011000,1.10010000,1.10011000,
                        1.10010000,1.10011000,1.10010000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,
                        1.10008000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,1.10004000,
                        1.10003000,1.10002000,1.10007000,1.10008000,1.10007000,1.10008000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10009000,1.10008000,1.10009000,1.10008000,1.10007000,1.10008000,
                        1.10007000,1.10006000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,
                        1.10012000,1.10013000,1.10014000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10017000,
                        1.10018000,1.10019000,1.10020000,1.10019000,1.10020000,1.10019000,1.10018000,1.10019000,
                        1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10019000,
                        1.10018000,1.10019000,1.10018000,1.10019000,1.10018000,1.10019000,1.10017000,1.10018000,
                        1.10016000,1.10017000,1.10016000,1.10015000,1.10011000,1.10013000,1.10014000,1.10013000,
                        1.10014000,1.10013000,1.10015000,1.10014000,1.10015000,1.10016000,1.10015000,1.10014000,
                        1.10013000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,
                        1.10011000,1.10012000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,1.10006000,
                        1.10007000,1.10006000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,
                        1.10005000,1.10004000,1.10003000,1.10002000,1.10001000,1.10002000,1.10001000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10005000,1.10006000,1.10014000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10007000,1.10006000,1.10005000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,
                        1.10001000,1.10000000,1.09999000,1.09998000,1.09997000,1.09998000,1.09997000,1.09999000,
                        1.09998000,1.10000000,1.09999000,1.10000000,1.09999000,1.10000000,1.09999000,1.09998000,
                        1.09999000,1.09998000,1.09999000,1.10000000,1.10001000,1.10002000,1.10006000,1.10004000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10001000,1.10000000,1.09998000,1.09999000,
                        1.09998000,1.09999000,1.09997000};

    go(close_data,12,26,9);
    return 0;
}

void go(const TA_Real *array[], TA_Integer fastPeriod, TA_Integer slowPeriod, TA_Integer signalPeriod) {
    TA_Integer outBegin = 0, outElements = 0;
    TA_Real outMacd[];
    TA_Real outSignal[];
    TA_Real outHistogram[];

    TA_RetCode retCode = TA_MACD( (sizeof(array)/sizeof(*array))-1-slowPeriod, (sizeof(array)/sizeof(*array))-1, array, fastPeriod, slowPeriod, signalPeriod, &outBegin, &outElements, outMacd, outSignal, outHistogram );    

    if(retCode == TA_SUCCESS) 
    {
        int lastElement = outElements-1;
        MacdResult.Macd = outMacd[lastElement];
        MacdResult.Signal = outSignal[lastElement];
        MacdResult.Histogram = outHistogram[lastElement];

        cout << "Macd = " << MacdResult.Macd << " Signal = " << MacdResult.Signal << " Histograme = " << MacdResult.Histogram << endl;
    }
}

When I compile using MinGW I get the following errors:

"/C/MinGW/msys/1.0/bin/make.exe" -f nbproject/Makefile-Debug.mk QMAKE= SUBPROJECTS= .build-conf
make.exe[1]: Entering directory `/c/Users/ypx7647/Documents/NetBeansProjects/TA_MACD Example'
"/C/MinGW/msys/1.0/bin/make.exe"  -f nbproject/Makefile-Debug.mk dist/Debug/MinGW-Windows/ta_macd_example.exe
make.exe[2]: Entering directory `/c/Users/ypx7647/Documents/NetBeansProjects/TA_MACD Example'
mkdir -p build/Debug/MinGW-Windows
rm -f "build/Debug/MinGW-Windows/io.o.d"
g++    -c -g -I/C/ta-lib-0.4.0-msvc/ta-lib/c/include -MMD -MP -MF "build/Debug/MinGW-Windows/io.o.d" -o build/Debug/MinGW-Windows/io.o io.cpp
io.cpp: In function `int main(int, char**)':
io.cpp:51: error: cannot convert `TA_Real*' to `const TA_Real**' for argument `1' to `void go(const TA_Real**, TA_Integer, TA_Integer, TA_Integer)'
io.cpp: In function `void go(const TA_Real**, TA_Integer, TA_Integer, TA_Integer)':
io.cpp:57: error: storage size of `outMacd' isn't known
io.cpp:58: error: storage size of `outSignal' isn't known
io.cpp:59: error: storage size of `outHistogram' isn't known
io.cpp:61: error: cannot convert `const TA_Real**' to `const double*' for argument `3' to `TA_RetCode TA_MACD(int, int, const double*, int, int, int, int*, int*, double*, double*, double*)'
make.exe[2]: *** [build/Debug/MinGW-Windows/io.o] Error 1
make.exe[2]: Leaving directory `/c/Users/ypx7647/Documents/NetBeansProjects/TA_MACD Example'
make.exe[1]: *** [.build-conf] Error 2
make.exe[1]: Leaving directory `/c/Users/ypx7647/Documents/NetBeansProjects/TA_MACD Example'
make.exe": *** [.build-impl] Error 2

BUILD FAILED (exit value 2, total time: 10s)

I'll be the first to admit that I don't know what I'm doing so any help will be appreciated.

Also, how do I set the size of the out variables when I don't know how many the TA_MACD function is returning?

Thanks in advance.

After modification using the C++11 Std I'm here now, still with problems.

#include <iostream>
#include <array>
#include "ta_libc.h"

using namespace std;

struct Struct_Macd
{
    double Macd;
    double Signal;
    double Histogram;
} MacdResult;

void go(TA_Real *close, const int array_size, TA_Integer fastPeriod, TA_Integer slowPeriod, TA_Integer signalPeriod)
{
    TA_Integer outBegin = 0, outElements = 0;
    array<TA_Real, array_size-1> outMacd;
    array<TA_Real, array_size-1> outSignal;
    array<TA_Real, array_size-1> outHistogram;

    TA_RetCode retCode = TA_MACD( array_size-1-slowPeriod, array_size-1, close, fastPeriod, slowPeriod, signalPeriod, &outBegin, &outElements, outMacd, outSignal, outHistogram );

    if(retCode == TA_SUCCESS)
    {
        int lastElement = outElements-1;
        MacdResult.Macd = outMacd[lastElement];
        MacdResult.Signal = outSignal[lastElement];
        MacdResult.Histogram = outHistogram[lastElement];

        cout << "Macd = " << MacdResult.Macd << " Signal = " << MacdResult.Signal << " Histograme = " << MacdResult.Histogram << endl;
    }
}

int main(int argc, char**argv)
{
    array<TA_Real,259> close_data={1.09999000,1.09995000,1.09996000,1.09995000,1.09994000,1.09993000,1.09994000,1.09996000,
                        1.09997000,1.09999000,1.10000000,1.09999000,1.10000000,1.09998000,1.09999000,1.09998000,
                        1.09999000,1.10000000,1.10001000,1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10005000,1.10006000,1.10007000,1.10008000,
                        1.10012000,1.10011000,1.10012000,1.10011000,1.10013000,1.10014000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10017000,1.10018000,1.10019000,1.10018000,1.10017000,
                        1.10018000,1.10017000,1.10018000,1.10017000,1.10016000,1.10014000,1.10013000,1.10015000,
                        1.10014000,1.10015000,1.10014000,1.10013000,1.10012000,1.10011000,1.10010000,1.10011000,
                        1.10010000,1.10011000,1.10010000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,
                        1.10008000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,1.10004000,
                        1.10003000,1.10002000,1.10007000,1.10008000,1.10007000,1.10008000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10009000,1.10008000,1.10009000,1.10008000,1.10007000,1.10008000,
                        1.10007000,1.10006000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,
                        1.10012000,1.10013000,1.10014000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10017000,
                        1.10018000,1.10019000,1.10020000,1.10019000,1.10020000,1.10019000,1.10018000,1.10019000,
                        1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10019000,
                        1.10018000,1.10019000,1.10018000,1.10019000,1.10018000,1.10019000,1.10017000,1.10018000,
                        1.10016000,1.10017000,1.10016000,1.10015000,1.10011000,1.10013000,1.10014000,1.10013000,
                        1.10014000,1.10013000,1.10015000,1.10014000,1.10015000,1.10016000,1.10015000,1.10014000,
                        1.10013000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,
                        1.10011000,1.10012000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,1.10006000,
                        1.10007000,1.10006000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,
                        1.10005000,1.10004000,1.10003000,1.10002000,1.10001000,1.10002000,1.10001000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10005000,1.10006000,1.10014000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10007000,1.10006000,1.10005000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,
                        1.10001000,1.10000000,1.09999000,1.09998000,1.09997000,1.09998000,1.09997000,1.09999000,
                        1.09998000,1.10000000,1.09999000,1.10000000,1.09999000,1.10000000,1.09999000,1.09998000,
                        1.09999000,1.09998000,1.09999000,1.10000000,1.10001000,1.10002000,1.10006000,1.10004000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10001000,1.10000000,1.09998000,1.09999000,
                        1.09998000,1.09999000,1.09997000};

    go(close_data,close_data.size(),12,26,9);
    return 0;
}

This is the output:

||=== Build: Debug in Test (compiler: GNU GCC Compiler) ===|
C:\Users\ypx7647\Documents\C++\Test\main.cpp||In function 'void go(TA_Real*, int, TA_Integer, TA_Integer, TA_Integer)':|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|17|error: 'array_size' is not a constant expression|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|17|note: in template argument for type 'unsigned int' |
C:\Users\ypx7647\Documents\C++\Test\main.cpp|17|error: invalid type in declaration before ';' token|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|18|error: 'array_size' is not a constant expression|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|18|note: in template argument for type 'unsigned int' |
C:\Users\ypx7647\Documents\C++\Test\main.cpp|18|error: invalid type in declaration before ';' token|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|19|error: 'array_size' is not a constant expression|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|19|note: in template argument for type 'unsigned int' |
C:\Users\ypx7647\Documents\C++\Test\main.cpp|19|error: invalid type in declaration before ';' token|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|21|error: invalid conversion from 'int' to 'double*' [-fpermissive]|
..\..\..\..\..\ta-lib-0.4.0-msvc\ta-lib\c\include\ta_func.h|3242|error:   initializing argument 9 of 'TA_RetCode TA_MACD(int, int, const double*, int, int, int, int*, int*, double*, double*, double*)' [-fpermissive]|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|21|error: invalid conversion from 'int' to 'double*' [-fpermissive]|
..\..\..\..\..\ta-lib-0.4.0-msvc\ta-lib\c\include\ta_func.h|3242|error:   initializing argument 10 of 'TA_RetCode TA_MACD(int, int, const double*, int, int, int, int*, int*, double*, double*, double*)' [-fpermissive]|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|21|error: invalid conversion from 'int' to 'double*' [-fpermissive]|
..\..\..\..\..\ta-lib-0.4.0-msvc\ta-lib\c\include\ta_func.h|3242|error:   initializing argument 11 of 'TA_RetCode TA_MACD(int, int, const double*, int, int, int, int*, int*, double*, double*, double*)' [-fpermissive]|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|26|error: invalid types 'int[int]' for array subscript|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|27|error: invalid types 'int[int]' for array subscript|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|28|error: invalid types 'int[int]' for array subscript|
C:\Users\ypx7647\Documents\C++\Test\main.cpp||In function 'int main(int, char**)':|
C:\Users\ypx7647\Documents\C++\Test\main.cpp|70|error: cannot convert 'std::array<double, 259u>' to 'TA_Real* {aka double*}' for argument '1' to 'void go(TA_Real*, int, TA_Integer, TA_Integer, TA_Integer)'|
||=== Build failed: 16 error(s), 0 warning(s) (0 minute(s), 1 second(s)) ===|

Having made the modifications and adaptations suggested my code is now:

#include <iostream>
#include <vector>
#include "ta_libc.h"

using namespace std;

struct Struct_Macd
{
    double Macd;
    double Signal;
    double Histogram;
} MacdResult;

void go(const vector<TA_Real> &close, TA_Integer fastPeriod, TA_Integer slowPeriod, TA_Integer signalPeriod)
{
    TA_Integer outBegin = 0, outElements = 0;

    int beginIndx = close.size()-1-slowPeriod;
    int endIndx = close.size()-1;
    int array_size = endIndx - beginIndx + 1;
    vector<TA_Real> outMacd(array_size, 0.0);
    vector<TA_Real> outSignal(array_size, 0.0);
    vector<TA_Real> outHistogram(array_size, 0.0);

    const TA_Real* close_c_pointer = close.data();
    TA_Real* outMacd_c_pointer = outMacd.data();
    TA_Real* outSignal_c_pointer = outMacd.data();
    TA_Real* outHistogram_c_pointer = outMacd.data();

    TA_RetCode retCode = TA_MACD( beginIndx, endIndx, close_c_pointer, fastPeriod, slowPeriod, signalPeriod, &outBegin, &outElements, outMacd_c_pointer, outSignal_c_pointer, outHistogram_c_pointer );

    if(retCode == TA_SUCCESS)
    {
        int lastElement = outElements-1;
        MacdResult.Macd = outMacd[lastElement];
        MacdResult.Signal = outSignal[lastElement];
        MacdResult.Histogram = outHistogram[lastElement];

        cout << "Macd = " << MacdResult.Macd << " Signal = " << MacdResult.Signal << " Histograme = " << MacdResult.Histogram << endl;
    }
}

int main(int argc, char**argv)
{
    vector<TA_Real> close_data {1.09999000,1.09995000,1.09996000,1.09995000,1.09994000,1.09993000,1.09994000,1.09996000,
                        1.09997000,1.09999000,1.10000000,1.09999000,1.10000000,1.09998000,1.09999000,1.09998000,
                        1.09999000,1.10000000,1.10001000,1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10005000,1.10006000,1.10007000,1.10008000,
                        1.10012000,1.10011000,1.10012000,1.10011000,1.10013000,1.10014000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10017000,1.10018000,1.10019000,1.10018000,1.10017000,
                        1.10018000,1.10017000,1.10018000,1.10017000,1.10016000,1.10014000,1.10013000,1.10015000,
                        1.10014000,1.10015000,1.10014000,1.10013000,1.10012000,1.10011000,1.10010000,1.10011000,
                        1.10010000,1.10011000,1.10010000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,
                        1.10008000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,1.10004000,
                        1.10003000,1.10002000,1.10007000,1.10008000,1.10007000,1.10008000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10009000,1.10008000,1.10009000,1.10008000,1.10007000,1.10008000,
                        1.10007000,1.10006000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,
                        1.10012000,1.10013000,1.10014000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10017000,
                        1.10018000,1.10019000,1.10020000,1.10019000,1.10020000,1.10019000,1.10018000,1.10019000,
                        1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10019000,
                        1.10018000,1.10019000,1.10018000,1.10019000,1.10018000,1.10019000,1.10017000,1.10018000,
                        1.10016000,1.10017000,1.10016000,1.10015000,1.10011000,1.10013000,1.10014000,1.10013000,
                        1.10014000,1.10013000,1.10015000,1.10014000,1.10015000,1.10016000,1.10015000,1.10014000,
                        1.10013000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,
                        1.10011000,1.10012000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,1.10006000,
                        1.10007000,1.10006000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,
                        1.10005000,1.10004000,1.10003000,1.10002000,1.10001000,1.10002000,1.10001000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10005000,1.10006000,1.10014000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10007000,1.10006000,1.10005000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,
                        1.10001000,1.10000000,1.09999000,1.09998000,1.09997000,1.09998000,1.09997000,1.09999000,
                        1.09998000,1.10000000,1.09999000,1.10000000,1.09999000,1.10000000,1.09999000,1.09998000,
                        1.09999000,1.09998000,1.09999000,1.10000000,1.10001000,1.10002000,1.10006000,1.10004000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10001000,1.10000000,1.09998000,1.09999000,
                        1.09998000,1.09999000,1.09997000};

    go(close_data,12,26,9);
    return 0;
}

I believe I have a linker error now:

-------------- Build: Debug in Test (compiler: GNU GCC Compiler)---------------

mingw32-g++.exe -Wall -fexceptions -std=c++11 -g -I..\..\..\..\..\ta-lib-0.4.0-msvc\ta-lib\c\include -c C:\Users\ypx7647\Documents\C++\Test\main.cpp -o obj\Debug\main.o
mingw32-g++.exe  -o bin\Debug\Test.exe obj\Debug\main.o   
obj\Debug\main.o: In function `Z2goRSt6vectorIdSaIdEEiii':
C:/Users/ypx7647/Documents/C++/Test/main.cpp:30: undefined reference to `TA_MACD'
collect2.exe: error: ld returned 1 exit status
Process terminated with status 1 (0 minute(s), 4 second(s))
1 error(s), 0 warning(s) (0 minute(s), 4 second(s))

What on earth is function "Z2goRSt6vectorIdSaIdEEiii"????

Well progress, the program now compiles and links BUT it produces 0 for the output. Any ideas?

Here's my latest code:

#include <iostream>
#include <vector>
#include "ta_libc.h"

using namespace std;

struct Struct_Macd
{
    double Macd;
    double Signal;
    double Histogram;
} MacdResult;

void go(const vector<TA_Real> &close, TA_Integer fastPeriod, TA_Integer slowPeriod, TA_Integer signalPeriod)
{
    TA_Integer outBegin = 0, outElements = 0;

    int beginIndx = close.size()-slowPeriod-1;
    int endIndx = close.size();
    int array_size = endIndx - beginIndx + 1;
    cout << "beginIndx = " << beginIndx << endl << "endIndx = " << endIndx << endl << "array_size = " << array_size << endl;
    vector<TA_Real> outMacd(array_size, 0.0);
    vector<TA_Real> outSignal(array_size, 0.0);
    vector<TA_Real> outHistogram(array_size, 0.0);

    const TA_Real* close_c_pointer = close.data();
    TA_Real* outMacd_c_pointer = outMacd.data();
    TA_Real* outSignal_c_pointer = outMacd.data();
    TA_Real* outHistogram_c_pointer = outMacd.data();

    TA_RetCode retCode = TA_MACD( beginIndx, endIndx, close_c_pointer, fastPeriod, slowPeriod, signalPeriod, &outBegin, &outElements, outMacd_c_pointer, outSignal_c_pointer, outHistogram_c_pointer );

    cout << "retCode = " << retCode << endl;

    if(retCode == TA_SUCCESS)
    {
        cout << "outBegin = " << outBegin << " outElements = " << outElements << endl;
        int lastElement = outElements-1;
        cout << "outMacd.at(lastElement) = " << outMacd.at(lastElement) << " outSignal.at(lastElement) = " << outSignal.at(lastElement) << " outHistogram.at(lastElement) = " << outHistogram.at(lastElement) << endl;
        MacdResult.Macd = outMacd.at(lastElement);
        MacdResult.Signal = outSignal.at(lastElement);
        MacdResult.Histogram = outHistogram.at(lastElement);

        cout << "Macd = " << MacdResult.Macd << " Signal = " << MacdResult.Signal << " Histogram = " << MacdResult.Histogram << endl;
    }
}

int main(int argc, char**argv)
{
    vector<TA_Real> close_data {1.09999000,1.09995000,1.09996000,1.09995000,1.09994000,1.09993000,1.09994000,1.09996000,
                        1.09997000,1.09999000,1.10000000,1.09999000,1.10000000,1.09998000,1.09999000,1.09998000,
                        1.09999000,1.10000000,1.10001000,1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10005000,1.10006000,1.10007000,1.10008000,
                        1.10012000,1.10011000,1.10012000,1.10011000,1.10013000,1.10014000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10017000,1.10018000,1.10019000,1.10018000,1.10017000,
                        1.10018000,1.10017000,1.10018000,1.10017000,1.10016000,1.10014000,1.10013000,1.10015000,
                        1.10014000,1.10015000,1.10014000,1.10013000,1.10012000,1.10011000,1.10010000,1.10011000,
                        1.10010000,1.10011000,1.10010000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,
                        1.10008000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,1.10004000,
                        1.10003000,1.10002000,1.10007000,1.10008000,1.10007000,1.10008000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10009000,1.10008000,1.10009000,1.10008000,1.10007000,1.10008000,
                        1.10007000,1.10006000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,
                        1.10012000,1.10013000,1.10014000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,
                        1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10015000,1.10016000,1.10017000,
                        1.10018000,1.10019000,1.10020000,1.10019000,1.10020000,1.10019000,1.10018000,1.10019000,
                        1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10018000,1.10017000,1.10019000,
                        1.10018000,1.10019000,1.10018000,1.10019000,1.10018000,1.10019000,1.10017000,1.10018000,
                        1.10016000,1.10017000,1.10016000,1.10015000,1.10011000,1.10013000,1.10014000,1.10013000,
                        1.10014000,1.10013000,1.10015000,1.10014000,1.10015000,1.10016000,1.10015000,1.10014000,
                        1.10013000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,1.10011000,1.10012000,
                        1.10011000,1.10012000,1.10011000,1.10010000,1.10009000,1.10008000,1.10007000,1.10006000,
                        1.10007000,1.10006000,1.10007000,1.10008000,1.10007000,1.10008000,1.10007000,1.10006000,
                        1.10005000,1.10004000,1.10003000,1.10002000,1.10001000,1.10002000,1.10001000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,1.10003000,
                        1.10002000,1.10003000,1.10004000,1.10005000,1.10006000,1.10014000,1.10009000,1.10008000,
                        1.10009000,1.10008000,1.10007000,1.10006000,1.10005000,1.10004000,1.10003000,1.10004000,
                        1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10004000,1.10003000,1.10002000,
                        1.10001000,1.10000000,1.09999000,1.09998000,1.09997000,1.09998000,1.09997000,1.09999000,
                        1.09998000,1.10000000,1.09999000,1.10000000,1.09999000,1.10000000,1.09999000,1.09998000,
                        1.09999000,1.09998000,1.09999000,1.10000000,1.10001000,1.10002000,1.10006000,1.10004000,
                        1.10002000,1.10003000,1.10004000,1.10003000,1.10001000,1.10000000,1.09998000,1.09999000,
                        1.09998000,1.09999000,1.09997000};

    go(close_data,12,26,9);
    return 0;
}

Program output:

beginIndx = 232
endIndx = 259
array_size = 28
retCode = 0
outBegin = 232 outElements = 28
outMacd.at(lastElement) = 0 outSignal.at(lastElement) = 0 outHistogram.at(lastEl
ement) = 0
Macd = 0 Signal = 0 Histogram = 0

Process returned 0 (0x0)   execution time : 0.080 s

@truf found the stupid error with e c pointer references not pointing to individual arrays.

Here's my final code to (too long to enter so here a snippet!) help people who are having problems too:

    MacdResult.Macd = outMacd.at(lastElement);
    MacdResult.Signal = outSignal.at(lastElement);
    MacdResult.Histogram = outHistogram.at(lastElement);
Andrew
  • 626
  • 6
  • 16

1 Answers1

2

There are several issues (at least three):

  1. io.cpp:51: error: cannot convert TA_Real*' toconst TA_Real**' ...
    • use simple 'const TA_Real *array' instead of 'const TA_Real *array[]'
    • do not use 'sizeof(array)/sizeof(*array)' to determine array length (it doesn't work inside function), just pass this length in additional parameter: void go(const TA_Real *array[], TA_Integer array_size, ...) and call it: go(close_data, sizeof(close_data)/sizeof(*close_data), 12, 26, 9);
  2. io.cpp:57: error: storage size of `outMacd' isn't known
    • place size in brackets, e.g. 'TA_Real outMacd[array_size]' instead of 'TA_Real outMacd[]'
  3. io.cpp:61: error: cannot convert const TA_Real**' toconst double*'...
    • i hope this will self-solved :)

EDIT: Accordingly TA_MACD sources (http://sourceforge.net/p/ta-lib/code/HEAD/tree/trunk/ta-lib/c/src/ta_func/ta_MACD.c), the size of out variables is (endIdx-startIdx+1).

EDIT2: Uhh, C++11 make things is even worse here :) The TA_Lib is using plain C interface so you need be an accurate with conversions between C++ objects and C types.

I propose something like this:

  1. Use vector instead of array (on C++11 compatible compiler)

    #include <array>
    vector<TA_Real> close_data { 31.09999000,1.09995000,1.09996000, ... }
    
  2. So you don't need to pass array length on call

    go(close_data,12,26,9);
    
  3. Edit function (C++11 compatible compiler also required for vector::data())

    void go(const vector<TA_Real> &close, TA_Integer fastPeriod, TA_Integer slowPeriod, TA_Integer signalPeriod)
    {
        TA_Integer outBegin = 0, outElements = 0;
        vector<TA_Real> outMacd(array_size, 0.0);
        vector<TA_Real> outSignal(array_size, 0.0);
        vector<TA_Real> outHistogram(array_size, 0.0);
    
        TA_RetCode retCode = TA_MACD( array_size-1-slowPeriod, array_size-1, close.data(), fastPeriod, slowPeriod, signalPeriod, outBegin, outElements, outMacd.data(), outSignal.data(), outHistogram.data() );
    
        if(retCode == TA_SUCCESS)
        {
            int lastElement = outElements-1;
            MacdResult.Macd = outMacd[lastElement];
            MacdResult.Signal = outSignal[lastElement];
            MacdResult.Histogram = outHistogram[lastElement];
    
            cout << "Macd = " << MacdResult.Macd << " Signal = " << MacdResult.Signal << " Histograme = " << MacdResult.Histogram << endl;
        }
    }
    

Unfortunatelly, I could not re-check this code with compiler, so use it as a template and subject to change in case of errors.

EDIT3:

What on earth is function "Z2goRSt6vectorIdSaIdEEiii"????

Yeh, C++ likes such names! It is encoded, compiler-internal name of your go() function. You can see name - 'go', and argument types - 'vector', i (integer) and so on.

C:/Users/ypx7647/Documents/C++/Test/main.cpp:30: undefined reference to `TA_MACD'

Looks like TA_lib requires external lib file to be built and specified in linker command line. You can see ta-lib/README.TXT file for instructions.

  1. E.g. go to: ta-lib/c/make/csr/linux/g++/ and run 'make' there
  2. Add to your compiler command line the path to lib files and lib file:

mingw32-g++.exe [...] -Lta-lib\c\lib -lta_libc_csr.a

Also, I could not check this, so be creative in case of errors, good luck!

s.cpp
  • 91
  • 1
  • 3
  • Thanks, this was helpful. I've modified the code to use the C++11 std but still having problems. – Andrew Aug 28 '15 at 14:43
  • Thanks again for your help. Seems you were correct. However, I think I have a linking problem now! See edited answer. – Andrew Aug 28 '15 at 16:34
  • It compiles and links - thanks so much for your help, however, it doesn't produce any results!! Any ideas? I'll add the code to the edited answer. – Andrew Aug 28 '15 at 19:35
  • Thank you so much. It was a stupid mistake and I've looked at the code for a couple of days now and didn't notice it!! It's been a useful learning process so thanks very much for your help! – Andrew Aug 30 '15 at 12:19
  • @Andrew, you are welcome, I'm happy to be helpful. Looks like you will be successful with C++ :) – s.cpp Aug 30 '15 at 20:00