int RSI::getRSI (QString ikey, int period, QString okey) { if (! g_symbol) return 0; TA_RetCode rc = TA_Initialize(); if (rc != TA_SUCCESS) qDebug() << "RSI::RSI: error on TA_Initialize"; QList<int> keys = g_symbol->keys(); TA_Real input[MAX_SIZE]; TA_Real out[MAX_SIZE]; TA_Integer outBeg; TA_Integer outNb; int dpos = 0; for (int kpos = 0; kpos < keys.size(); kpos++) { CBar *bar = g_symbol->bar(keys.at(kpos)); double v; if (! bar->get(ikey, v)) continue; input[dpos++] = (TA_Real) v; } rc = TA_RSI (0, dpos - 1, &input[0], period, &outBeg, &outNb, &out[0]); if (rc != TA_SUCCESS) { qDebug() << "RSI::getRSI: TA-Lib error" << rc; return 0; } int keyLoop = keys.size() - 1; int outLoop = outNb - 1; while (keyLoop > -1 && outLoop > -1) { CBar *bar = g_symbol->bar(keys.at(keyLoop)); bar->set(okey, out[outLoop]); keyLoop--; outLoop--; } return 1; }
INDICATOR(RSI)(const std::vector<Candlestick>* candlesticks, int* startIndex) { std::vector<double> indicatorData = std::vector<double>(); int outBeginIdx; int outNbElement; int optInTimePeriod = 14; unsigned int lookback = TA_RSI_Lookback(optInTimePeriod); if (candlesticks->size() <= lookback) { return indicatorData; } //Initialize all required parameters this->PrepareParameters(candlesticks); //Perform the RSI calculation TA_RSI( this->startIdx, this->endIdx, this->inClose, optInTimePeriod, &outBeginIdx, &outNbElement, this->outReal); //Put the result into indicator data vector in RsiModel for (int i = 0; i < outNbElement; i++) { indicatorData.push_back(outReal[i]); } *startIndex = outBeginIdx; //Cleanup all required parameters this->Clean(); return indicatorData; }
/* The gateway routine */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* ----------------- Variables ----------------- */ /* input variables */ /* mandatory input */ int startIdx; int endIdx; double * inReal; /* optional input */ int optInTimePeriod; /* output variables */ int outBegIdx; int outNbElement; double* outReal; /* input dimentions */ int inSeriesRows; int inSeriesCols; /* error handling */ TA_RetCode retCode; /* ----------------- input/output count ----------------- */ /* Check for proper number of arguments. */ if (nrhs < 1 || nrhs > 2) mexErrMsgTxt("#2 inputs possible #1 optional."); if (nlhs != 1) mexErrMsgTxt("#1 output required."); /* ----------------- INPUT ----------------- */ /* Create a pointer to the input matrix inReal. */ inReal = mxGetPr(prhs[0]); /* Get the dimensions of the matrix input inReal. */ inSeriesCols = mxGetN(prhs[0]); if (inSeriesCols != 1) mexErrMsgTxt("inReal only vector alowed."); inSeriesRows = mxGetM(prhs[0]); endIdx = inSeriesRows - 1; startIdx = 0; /* Process optional arguments */ if (nrhs >= 1+1) { if (!mxIsDouble(prhs[1]) || mxIsComplex(prhs[1]) || mxGetN(prhs[1])*mxGetM(prhs[1]) != 1) mexErrMsgTxt("Input optInTimePeriod must be a scalar."); /* Get the scalar input optInTimePeriod. */ optInTimePeriod = (int) mxGetScalar(prhs[1]); } else { optInTimePeriod = 14; } /* ----------------- OUTPUT ----------------- */ outReal = mxCalloc(inSeriesRows, sizeof(double)); /* -------------- Invocation ---------------- */ retCode = TA_RSI( startIdx, endIdx, inReal, optInTimePeriod, &outBegIdx, &outNbElement, outReal); /* -------------- Errors ---------------- */ if (retCode) { mxFree(outReal); mexPrintf("%s%i","Return code=",retCode); mexErrMsgTxt(" Error!"); } // Populate Output plhs[0] = mxCreateDoubleMatrix(outBegIdx+outNbElement,1, mxREAL); memcpy(((double *) mxGetData(plhs[0]))+outBegIdx, outReal, outNbElement*mxGetElementSize(plhs[0])); mxFree(outReal); } /* END mexFunction */