-1

i can use talib MACD function correctly in python, but it failed in c++.

talib is easy to use in python, i can use it as:

MACD(close, 12, 26,9) # close is a pandas series

but when i use it in c++, it confuses me about the params.

I have a function which update one close price for one time, so i save the close price sequence in a vector, like this:

std::vector<double> close_price;
void f(double new_close) {
  close_price.push_back(new_close);
  std::vector<TA_Real> a(655360, 0.0);
  std::vector<TA_Real> b(655360, 0.0);
  std::vector<TA_Real> c(655360, 0.0);
  int s, n;
  TA_RetCode retCode = TA_MACD(0, close_price.size(), close_price.data(), 3, 12, 1, &s, &n, a.data(), b.data(), c.data());
  assert(retCode == TA_SUCCESS);
}

but it always failed in assert, ERROR message is:

Error 5119(TA_INTERNAL_ERROR): Unexpected Internal Error - Contact TA-Lib.org

did i miss anything? i think i passin the parameters as the example talib provide.

xyhuang
  • 414
  • 3
  • 11

1 Answers1

2

endIdx is the last index, not the size. Hence, you need to call it as:

TA_MACD(..., close_price.size() - 1, ...);
Maxim Egorushkin
  • 131,725
  • 17
  • 180
  • 271
  • @xyhuang Well, my answer fixes one error present in your question. – Maxim Egorushkin Dec 14 '20 at 12:05
  • 1
    @xyhuang The code is working and return `TA_SUCCESS` on my machine. Probably you didn't add something in your example that really reflects the processing. Also, I would insist on calling in `int main(...) { if ( TA_Initialize() != TA_SUCCESS) return -1; <... app code...> return TA_Shutdown(); }` This library initialiser is crucial for come candle indicators and must be called always on library load/unload. – truf Dec 15 '20 at 02:05
  • @xyhuang Apart from that if you are looking into indicator calculation each time new data value arrives (`f(double new_close)`) you may better to check out [TA-Lib RT](https://github.com/trufanov-nok/ta-lib-rt) - a fork designed for that purpose. – truf Dec 15 '20 at 02:05