/* * call-seq: opt_real(index, value) * * Set option parameter (real) for the given parameter index. */ static VALUE ta_func_setup_opt_in_real(VALUE self, VALUE param_index, VALUE val) { TA_RetCode ret_code; ParamHolder *param_holder; Data_Get_Struct(self, ParamHolder, param_holder); ret_code = TA_SetOptInputParamReal( param_holder->p, FIX2INT(param_index), NUM2DBL(val)); if ( ret_code != TA_SUCCESS ) rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetOptInputParamRealPtr"); }
PlotLine * TALIB::calculateCustom (QString &p, QPtrList<PlotLine> &d) { // format: METHOD, ..., ...., ..... etc (first parm must be the method) QStringList l = QStringList::split(",", p, FALSE); if (! l.count()) { qDebug("TALIB::calculateCustom: no method parm"); return 0; } TA_Integer start = 0; TA_Integer end = data->count() - 1; if (d.count()) end = d.at(0)->getSize() - 1; TA_Integer outstart; TA_Integer count; PlotLine *line = new PlotLine; // sometimes are not enough data available // to calc anything if(end < 0) return line; // open a TALIB handle const TA_FuncHandle *handle; TA_RetCode retCode = TA_GetFuncHandle(l[0], &handle); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:can't open handle"); return 0; } // get info on the function const TA_FuncInfo *theInfo; retCode = TA_GetFuncInfo(handle, &theInfo); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:can't get function info"); return 0; } // create parm holder TA_ParamHolder *parmHolder; retCode = TA_ParamHolderAlloc(handle, &parmHolder); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:can't create parm holder"); return 0; } // create and input arrays int loop = data->count(); TA_Real open[loop]; TA_Real high[loop]; TA_Real low[loop]; TA_Real close[loop]; TA_Real volume[loop]; TA_Real oi[loop]; TA_Real treal[loop]; int sparmIndex = 1; // setup the input arrays const TA_InputParameterInfo *inputParms; for (loop = 0; loop < (int) theInfo->nbInput; loop++ ) { TA_GetInputParameterInfo(theInfo->handle, loop, &inputParms); if (inputParms->type == TA_Input_Price) { // populate the input arrays int loop2; for (loop2 = 0; loop2 < data->count(); loop2++) { open[loop2] = (TA_Real) data->getOpen(loop2); high[loop2] = (TA_Real) data->getHigh(loop2); low[loop2] = (TA_Real) data->getLow(loop2); close[loop2] = (TA_Real) data->getClose(loop2); volume[loop2] = (TA_Real) data->getVolume(loop2); oi[loop2] = (TA_Real) data->getOI(loop2); } retCode = TA_SetInputParamPricePtr(parmHolder, loop, &open[0], &high[0], &low[0], &close[0], &volume[0], &oi[0]); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:cannot set input prices"); return 0; } } if (inputParms->type == TA_Input_Real) { if (! d.count()) { qDebug("TALIB::calculateCustom: no input"); return 0; } if (sparmIndex >= (int) l.count()) { qDebug("TALIB::calculateCustom: input invalid number of parms"); return 0; } PlotLine *line = d.at(0); int loop2; for (loop2 = 0; loop2 < line->getSize(); loop2++) treal[loop2] = (TA_Real) line->getData(loop2); retCode = TA_SetInputParamRealPtr(parmHolder, loop, &treal[0]); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:cannot set real price"); return 0; } sparmIndex++; } } if (sparmIndex < (int) l.count()) { QStringList mal; getMATypes(mal); mal.remove("Wilder"); int t = 0; // setup the optinput parms const TA_OptInputParameterInfo *optInfo; for (loop = 0; loop < (int) theInfo->nbOptInput; loop++ ) { TA_GetOptInputParameterInfo(theInfo->handle, loop, &optInfo); switch (optInfo->type) { case TA_OptInput_RealRange: if (sparmIndex >= (int) l.count()) { qDebug("TALIB::calculateCustom: optinput real invalid number of parms"); return 0; } retCode = TA_SetOptInputParamReal(parmHolder, loop, (TA_Real) l[sparmIndex].toDouble()); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:cannot set inputopt real"); return 0; } sparmIndex++; break; case TA_OptInput_IntegerRange: if (sparmIndex >= (int) l.count()) { qDebug("TALIB::calculateCustom: optinput integer invalid number of parms"); return 0; } retCode = TA_SetOptInputParamInteger(parmHolder, loop, (TA_Integer) l[sparmIndex].toInt()); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:cannot set inputopt integer"); return 0; } sparmIndex++; break; case TA_OptInput_IntegerList: if (sparmIndex >= (int) l.count()) { qDebug("TALIB::calculateCustom: optinput integerList invalid number of parms"); return 0; } t = mal.findIndex(l[sparmIndex]); if (t == -1) t = 0; retCode = TA_SetOptInputParamInteger(parmHolder, loop, (TA_Integer) t); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:cannot set inputopt integer"); return 0; } sparmIndex++; break; default: break; } } } // setup the output arrays TA_Real out1[data->count()]; TA_Real out2[data->count()]; TA_Real out3[data->count()]; TA_Integer out4[data->count()]; const TA_OutputParameterInfo *outInfo; for (loop = 0; loop < (int) theInfo->nbOutput; loop++) { retCode = TA_GetOutputParameterInfo(handle, loop, &outInfo); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:cannot get output info"); return 0; } switch (loop) { case 0: if (outInfo->type == TA_Output_Integer) { retCode = TA_SetOutputParamIntegerPtr(parmHolder, loop, &out4[0]); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:cannot set output4"); return 0; } } else { retCode = TA_SetOutputParamRealPtr(parmHolder, loop, &out1[0]); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:cannot set output1"); return 0; } } break; case 1: retCode = TA_SetOutputParamRealPtr(parmHolder, loop, &out2[0]); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:cannot set output2"); return 0; } break; case 2: retCode = TA_SetOutputParamRealPtr(parmHolder, loop, &out3[0]); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:cannot set output3"); return 0; } break; default: break; } } // call the function /* TA_Integer start = 0; TA_Integer end = data->count() - 1; if (d.count()) end = d.at(0)->getSize() - 1; TA_Integer outstart; TA_Integer count; PlotLine *line = new PlotLine; */ retCode = TA_CallFunc(parmHolder, start, end, &outstart, &count); if (retCode != TA_SUCCESS) { printError(QString("TALIB::calculateCustom:TA_CallFunc"), retCode); qDebug("p=%s start=%d end=%d",p.ascii(), start, end); } else { // create the plotlines int loop2; retCode = TA_GetOutputParameterInfo(handle, 0, &outInfo); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculateCustom:cannot get output info"); return 0; } if (outInfo->type == TA_Output_Integer) { for (loop2 = 0; loop2 < count; loop2++) line->append((double) out4[loop2]); } else { if (theInfo->nbOutput > 1) { bool ok; l[l.count() - 1].toInt(&ok); if (! ok) { qDebug("TALIB::calculateCustom: parm #%i invalid, not an INTEGER", loop + 1); return 0; } switch (l[l.count() - 1].toInt(&ok)) { case 2: for (loop2 = 0; loop2 < count; loop2++) line->append((double) out2[loop2]); break; case 3: for (loop2 = 0; loop2 < count; loop2++) line->append((double) out3[loop2]); break; default: for (loop2 = 0; loop2 < count; loop2++) line->append((double) out1[loop2]); break; } } else { for (loop2 = 0; loop2 < count; loop2++) line->append((double) out1[loop2]); } } } retCode = TA_ParamHolderFree(parmHolder); if (retCode != TA_SUCCESS) qDebug("TALIB::calculateCustom:can't delete parm holder"); return line; }
Indicator * TALIB::calculate () { Indicator *output = new Indicator; output->setDateFlag(dateFlag); output->setLogScale(logScale); // open a TALIB handle const TA_FuncHandle *handle; QString ts = "method"; QString ts2; parms.getData(ts, ts2); TA_RetCode retCode = TA_GetFuncHandle(ts2, &handle); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculate:can't open handle"); return output; } // get info on the function const TA_FuncInfo *theInfo; retCode = TA_GetFuncInfo(handle, &theInfo); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculate:can't get function info"); return output; } // create parm holder TA_ParamHolder *parmHolder; retCode = TA_ParamHolderAlloc(handle, &parmHolder); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculate:can't create parm holder"); return output; } // create and populate the input arrays int loop = data->count(); TA_Real open[loop]; TA_Real high[loop]; TA_Real low[loop]; TA_Real close[loop]; TA_Real volume[loop]; TA_Real oi[loop]; for (loop = 0; loop < data->count(); loop++) { open[loop] = (TA_Real) data->getOpen(loop); high[loop] = (TA_Real) data->getHigh(loop); low[loop] = (TA_Real) data->getLow(loop); close[loop] = (TA_Real) data->getClose(loop); volume[loop] = (TA_Real) data->getVolume(loop); oi[loop] = (TA_Real) data->getOI(loop); } // setup the input arrays const TA_InputParameterInfo *inputParms; for (loop = 0; loop < (int) theInfo->nbInput; loop++ ) { TA_GetInputParameterInfo(theInfo->handle, loop, &inputParms); QString s; int tint = 3; switch (inputParms->type) { case TA_Input_Price: retCode = TA_SetInputParamPricePtr(parmHolder, loop, &open[0], &high[0], &low[0], &close[0], &volume[0], &oi[0]); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set input prices"); break; case TA_Input_Real: s = QObject::tr("input") + QString::number(loop + 1); tint = parms.getInt(s); switch (tint) { case 0: retCode = TA_SetInputParamRealPtr(parmHolder, loop, &open[0]); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set real price"); break; case 1: retCode = TA_SetInputParamRealPtr(parmHolder, loop, &high[0]); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set real price"); break; case 2: retCode = TA_SetInputParamRealPtr(parmHolder, loop, &low[0]); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set real price"); break; case 3: retCode = TA_SetInputParamRealPtr(parmHolder, loop, &close[0]); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set real price"); break; case 4: retCode = TA_SetInputParamRealPtr(parmHolder, loop, &volume[0]); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set integer price"); break; case 5: retCode = TA_SetInputParamRealPtr(parmHolder, loop, &oi[0]); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set integer price"); break; default: break; } break; case TA_Input_Integer: break; default: break; } } // setup the optinput parms const TA_OptInputParameterInfo *optInfo; for (loop = 0; loop < (int) theInfo->nbOptInput; loop++ ) { TA_GetOptInputParameterInfo(theInfo->handle, loop, &optInfo); QString s = optInfo->displayName; switch (optInfo->type) { case TA_OptInput_RealRange: retCode = TA_SetOptInputParamReal(parmHolder, loop, (TA_Real) parms.getDouble(s)); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set inputopt real"); break; case TA_OptInput_IntegerRange: retCode = TA_SetOptInputParamInteger(parmHolder, loop, (TA_Integer) parms.getInt(s)); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set inputopt integer"); break; case TA_OptInput_IntegerList: retCode = TA_SetOptInputParamInteger(parmHolder, loop, (TA_Integer) parms.getInt(s)); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set inputopt integerlist"); break; default: break; } } // setup the output arrays TA_Real out1[data->count()]; TA_Real out2[data->count()]; TA_Real out3[data->count()]; TA_Integer out4[data->count()]; const TA_OutputParameterInfo *outInfo; for (loop = 0; loop < (int) theInfo->nbOutput; loop++) { retCode = TA_GetOutputParameterInfo(handle, loop, &outInfo); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculate:cannot get output info"); continue; } switch (loop) { case 0: if (outInfo->type == TA_Output_Integer) { retCode = TA_SetOutputParamIntegerPtr(parmHolder, loop, &out4[0]); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set output4"); } else { retCode = TA_SetOutputParamRealPtr(parmHolder, loop, &out1[0]); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set output1"); } break; case 1: retCode = TA_SetOutputParamRealPtr(parmHolder, loop, &out2[0]); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set output2"); break; case 2: retCode = TA_SetOutputParamRealPtr(parmHolder, loop, &out3[0]); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:cannot set output3"); break; default: break; } } // call the function TA_Integer start = 0; TA_Integer end = data->count() - 1; TA_Integer outstart; TA_Integer count; retCode = TA_CallFunc(parmHolder, start, end, &outstart, &count); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:call function failed"); else { // create the plotlines const TA_OutputParameterInfo *outInfo; for (loop = 0; loop < (int) theInfo->nbOutput; loop++ ) { TA_GetOutputParameterInfo(theInfo->handle, loop, &outInfo); QString base = outInfo->paramName; base = base.right(base.length() - 3); if (! base.left(4).compare("Real")) base = base.right(base.length() - 4); if (! base.left(7).compare("Integer")) base = base.right(base.length() - 7); if (! base.length()) base = QObject::tr("Plot"); PlotLine *line = new PlotLine; QString s = base + " " + QObject::tr("Color"); parms.getData(s, ts); QColor color(ts); line->setColor(color); s = base + " " + QObject::tr("Label"); parms.getData(s, ts); line->setLabel(ts); s = base + " " + QObject::tr("Line Type"); line->setType((PlotLine::LineType)parms.getInt(s)); retCode = TA_GetOutputParameterInfo(handle, loop, &outInfo); if (retCode != TA_SUCCESS) { qDebug("TALIB::calculate:cannot get output info"); delete line; continue; } int loop2; switch (loop) { case 0: if (outInfo->type == TA_Output_Integer) { for (loop2 = 0; loop2 < count; loop2++) line->append((double) out4[loop2]); } else { for (loop2 = 0; loop2 < count; loop2++) line->append((double) out1[loop2]); } break; case 1: for (loop2 = 0; loop2 < count; loop2++) line->append((double) out2[loop2]); break; case 2: for (loop2 = 0; loop2 < count; loop2++) line->append((double) out3[loop2]); break; default: break; } if (line->getType() == PlotLine::Histogram || line->getType() == PlotLine::HistogramBar) output->prependLine(line); else output->addLine(line); } } retCode = TA_ParamHolderFree(parmHolder); if (retCode != TA_SUCCESS) qDebug("TALIB::calculate:can't delete parm holder"); return output; }