2

I am trying to do some technical analysis of this data using ta-lib library in C++. The problem with ta-lib is that there is very few(most probably none except documentation) tutorials regarding their usage in C++. I converted the open values (third/C-th column) in the spreadsheet to a vector double vec of size 124. I want to use this vector for the calculation of EMA and RSI of 10 day period. This is

    //headers used
    #include <vector>
    #include <ta-lib/ta_libc.h>
    std::vector <double> vec;

    //Technical analysis part of the code
    int n=vec.size();  //size of the vector
    std::cout <<"size "<< n  << ' ';
    TA_RetCode retCode;
    retCode = TA_Initialize( );
    if( retCode != TA_SUCCESS )
        std::cout<<"Cannot initialize TA-Lib !\n"<< retCode <"\n";
    else
    {
        std::cout<<"TA-Lib correctly initialized.\n" ;

        /* ... other TA-Lib functions can be used here. */
        double ma=TA_MA(0,n,vec,10,TA_MAType_EMA);
        double rsi=TA_RSI(0,n,vec,10);
        std::cout <<"EMA "<< ma <<"\n";
        std::cout <<"RSI "<< rsi <<"\n";
        TA_Shutdown();
    }

The error is

error: cannot convert ‘std::vector’ to ‘const double*’ for argument ‘3’ to ‘TA_RetCode TA_MA(int, int, const double*, int, TA_MAType, int*, int*, double*)

too honest for this site
  • 12,050
  • 4
  • 30
  • 52
Eka
  • 14,170
  • 38
  • 128
  • 212

1 Answers1

2

Well, your code is completely wrong.

  1. If you want to stick with std::vector you need to pass vec.data() to TA-Lib's functions instead of vec.

  2. Why do you think TA_MA() returns MA value? They all return TA_RetCode with TA_SUCCESS or error code. It's stated in error message you provide and in documentation. And there is a good sample of how to call such function with static arrays.

  3. Why you think you can ignore last 3 parameters of TA_MA which is a pointer to array where results should be stored (result is array of moving MAs and its indexes)? If you want stick to std::vector<double> you must declare one for results and prefill it with enough number of values (to allocate memory for storage). Then you can use std::vector<double>::data() again.
truf
  • 2,843
  • 26
  • 39