/**** Local functions definitions. ****/ static TA_RetCode rangeTestFunction( TA_Libc *libHandle, TA_Integer startIdx, TA_Integer endIdx, TA_Real *outputBuffer, TA_Integer *outBegIdx, TA_Integer *outNbElement, TA_Integer *lookback, void *opaqueData, unsigned int outputNb ) { TA_RetCode retCode; TA_RangeTestParam *testParam; (void)outputNb; testParam = (TA_RangeTestParam *)opaqueData; switch( testParam->test->theFunction ) { case TA_CCI_TEST: retCode = TA_CCI( libHandle, startIdx, endIdx, testParam->high, testParam->low, testParam->close, testParam->test->optInTimePeriod_0, outBegIdx, outNbElement, outputBuffer ); *lookback = TA_CCI_Lookback( testParam->test->optInTimePeriod_0 ); break; default: retCode = TA_INTERNAL_ERROR(132); } return retCode; }
double getCCISlope(const int unsigned cciBars, const int unsigned slopeBars, CandleData * data, long numSticks) { double slope = 0.0; double * highData = CandleData::getData(data, CandleData::HIGH, numSticks); double * lowData = CandleData::getData(data, CandleData::LOW, numSticks); double * closeData = CandleData::getData(data, CandleData::CLOSE, numSticks); int outNbElement = 0; int outBeg = 0; int arraySize = numSticks + cciBars; double * CCIDataOut = new double[arraySize]; memset(CCIDataOut,0,arraySize * sizeof(double)); TA_RetCode CCIRet = TA_CCI( 0, numSticks - 1, highData, lowData, closeData, cciBars, &outBeg, &outNbElement, CCIDataOut); int newSize = outNbElement+outBeg; double * ret = new double[newSize]; memset(ret,0,(newSize) * sizeof(double)); memcpy(&ret[outBeg],CCIDataOut,outNbElement*sizeof(double)); delete [] highData; delete [] lowData; delete [] closeData; delete [] CCIDataOut; double * slopeDataOut = new double[newSize]; memset(slopeDataOut,0,(newSize) * sizeof(double)); TA_RetCode retCodeSlope = TA_LINEARREG_SLOPE(0, newSize - 1, ret, slopeBars, &outBeg, &outNbElement, slopeDataOut); if(outNbElement == 0) { slope = 0.0; } else { slope = slopeDataOut[outNbElement - 1]; } delete [] ret; delete [] slopeDataOut; return slope; }
/* The gateway routine */ void mexFunction(int nlhs, mxArray *plhs[], int nrhs, const mxArray *prhs[]) { /* ----------------- Variables ----------------- */ /* input variables */ /* mandatory input */ int startIdx; int endIdx; double * high; double * low; double * close; /* 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 < 3 || nrhs > 4) mexErrMsgTxt("#4 inputs possible #1 optional."); if (nlhs != 1) mexErrMsgTxt("#1 output required."); /* ----------------- INPUT ----------------- */ /* Create a pointer to the input matrix high. */ high = mxGetPr(prhs[0]); /* Get the dimensions of the matrix input high. */ inSeriesCols = mxGetN(prhs[0]); if (inSeriesCols != 1) mexErrMsgTxt("high only vector alowed."); /* Create a pointer to the input matrix low. */ low = mxGetPr(prhs[1]); /* Get the dimensions of the matrix input low. */ inSeriesCols = mxGetN(prhs[1]); if (inSeriesCols != 1) mexErrMsgTxt("low only vector alowed."); /* Create a pointer to the input matrix close. */ close = mxGetPr(prhs[2]); /* Get the dimensions of the matrix input close. */ inSeriesCols = mxGetN(prhs[2]); if (inSeriesCols != 1) mexErrMsgTxt("close only vector alowed."); inSeriesRows = mxGetM(prhs[2]); endIdx = inSeriesRows - 1; startIdx = 0; /* Process optional arguments */ if (nrhs >= 3+1) { if (!mxIsDouble(prhs[3]) || mxIsComplex(prhs[3]) || mxGetN(prhs[3])*mxGetM(prhs[3]) != 1) mexErrMsgTxt("Input optInTimePeriod must be a scalar."); /* Get the scalar input optInTimePeriod. */ optInTimePeriod = (int) mxGetScalar(prhs[3]); } else { optInTimePeriod = 14; } /* ----------------- OUTPUT ----------------- */ outReal = mxCalloc(inSeriesRows, sizeof(double)); /* -------------- Invocation ---------------- */ retCode = TA_CCI( startIdx, endIdx, high, low, close, 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 */
static ErrorNumber do_test( const TA_History *history, const TA_Test *test ) { TA_RangeTestParam testParam; ErrorNumber errNb; TA_RetCode retCode; (void)test; /* Set to NAN all the elements of the gBuffers. */ clearAllBuffers(); /* Build the input. */ setInputBuffer( 0, history->open, history->nbBars ); setInputBuffer( 1, history->high, history->nbBars ); setInputBuffer( 2, history->low, history->nbBars ); setInputBuffer( 3, history->close, history->nbBars ); #if 0 /* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! */ /* Test for specific value not yet implemented */ /* Make a simple first call. */ switch( test->theFunction ) { case TA_CCI_TEST: retCode = TA_CCI( test->startIdx, test->endIdx, gBuffer[0].in, gBuffer[1].in, gBuffer[2].in, test->optInTimePeriod, &outBegIdx, &outNbElement, gBuffer[0].out0 ); break; case TA_WILLR_TEST: retCode = TA_WILLR( test->startIdx, test->endIdx, gBuffer[0].in, gBuffer[1].in, gBuffer[2].in, test->optInTimePeriod, &outBegIdx, &outNbElement, gBuffer[0].out0 ); break; default: retCode = TA_INTERNAL_ERROR(133); } /* Check that the input were preserved. */ errNb = checkDataSame( gBuffer[0].in, history->high,history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; errNb = checkDataSame( gBuffer[1].in, history->low, history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; errNb = checkDataSame( gBuffer[2].in, history->close,history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; CHECK_EXPECTED_VALUE( gBuffer[0].out0, 0 ); outBegIdx = outNbElement = 0; /* Make another call where the input and the output are the * same buffer. */ switch( test->theFunction ) { case TA_CCI_TEST: retCode = TA_CCI( test->startIdx, test->endIdx, gBuffer[0].in, gBuffer[1].in, gBuffer[2].in, test->optInTimePeriod, &outBegIdx, &outNbElement, gBuffer[0].in ); break; case TA_WILLR_TEST: retCode = TA_WILLR( test->startIdx, test->endIdx, gBuffer[0].in, gBuffer[1].in, gBuffer[2].in, test->optInTimePeriod, &outBegIdx, &outNbElement, gBuffer[0].in ); break; default: retCode = TA_INTERNAL_ERROR(134); } /* Check that the input were preserved. */ errNb = checkDataSame( gBuffer[1].in, history->low, history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; errNb = checkDataSame( gBuffer[2].in, history->close,history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; /* The previous call to TA_MA should have the same output * as this call. * * checkSameContent verify that all value different than NAN in * the first parameter is identical in the second parameter. */ errNb = checkSameContent( gBuffer[0].out0, gBuffer[0].in ); if( errNb != TA_TEST_PASS ) return errNb; CHECK_EXPECTED_VALUE( gBuffer[0].in, 0 ); setInputBuffer( 0, history->high, history->nbBars ); /* Make another call where the input and the output are the * same buffer. */ switch( test->theFunction ) { case TA_CCI_TEST: retCode = TA_CCI( test->startIdx, test->endIdx, gBuffer[0].in, gBuffer[1].in, gBuffer[2].in, test->optInTimePeriod, &outBegIdx, &outNbElement, gBuffer[1].in ); break; case TA_WILLR_TEST: retCode = TA_WILLR( test->startIdx, test->endIdx, gBuffer[0].in, gBuffer[1].in, gBuffer[2].in, test->optInTimePeriod, &outBegIdx, &outNbElement, gBuffer[1].in ); break; default: retCode = TA_INTERNAL_ERROR(135); } /* Check that the input were preserved. */ errNb = checkDataSame( gBuffer[0].in, history->high,history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; errNb = checkDataSame( gBuffer[2].in, history->close,history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; /* The previous call should have the same output as this call. * * checkSameContent verify that all value different than NAN in * the first parameter is identical in the second parameter. */ errNb = checkSameContent( gBuffer[0].out0, gBuffer[1].in ); if( errNb != TA_TEST_PASS ) return errNb; CHECK_EXPECTED_VALUE( gBuffer[1].in, 0 ); setInputBuffer( 1, history->low, history->nbBars ); /* Make another call where the input and the output are the * same buffer. */ switch( test->theFunction ) { case TA_CCI_TEST: retCode = TA_CCI( test->startIdx, test->endIdx, gBuffer[0].in, gBuffer[1].in, gBuffer[2].in, test->optInTimePeriod, &outBegIdx, &outNbElement, gBuffer[2].in ); break; case TA_WILLR_TEST: retCode = TA_WILLR( test->startIdx, test->endIdx, gBuffer[0].in, gBuffer[1].in, gBuffer[2].in, test->optInTimePeriod, &outBegIdx, &outNbElement, gBuffer[2].in ); break; default: retCode = TA_INTERNAL_ERROR(136); } /* Check that the input were preserved. */ errNb = checkDataSame( gBuffer[0].in, history->high,history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; errNb = checkDataSame( gBuffer[1].in, history->low, history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; /* The previous call to TA_MA should have the same output * as this call. * * checkSameContent verify that all value different than NAN in * the first parameter is identical in the second parameter. */ errNb = checkSameContent( gBuffer[0].out0, gBuffer[2].in ); if( errNb != TA_TEST_PASS ) return errNb; CHECK_EXPECTED_VALUE( gBuffer[2].in, 0 ); setInputBuffer( 2, history->close, history->nbBars ); #endif /* Do a systematic test of most of the * possible startIdx/endIdx range. */ testParam.test = test; testParam.open = history->open; testParam.high = history->high; testParam.low = history->low; testParam.close = history->close; testParam.paramHolder = NULL; if( test->doRangeTestFlag ) { errNb = doRangeTest( rangeTestFunction, TA_FUNC_UNST_NONE, (void *)&testParam, 1, 0 ); if( testParam.paramHolder ) { retCode = TA_ParamHolderFree( testParam.paramHolder ); if( retCode != TA_SUCCESS ) { printf( "TA_ParamHolderFree failed [%d]\n", retCode ); return TA_TSTCDL_PARAMHOLDERFREE_FAIL; } } if( errNb != TA_TEST_PASS ) return errNb; } return TA_TEST_PASS; }
static ErrorNumber do_test( TA_Libc *libHandle, const TA_History *history, const TA_Test *test ) { TA_RetCode retCode; ErrorNumber errNb; TA_Integer outBegIdx; TA_Integer outNbElement; TA_RangeTestParam testParam; /* Set to NAN all the elements of the gBuffers. */ clearAllBuffers(); /* Build the input. */ setInputBuffer( 0, history->high, history->nbBars ); setInputBuffer( 1, history->low, history->nbBars ); setInputBuffer( 2, history->close, history->nbBars ); /* Make a simple first call. */ switch( test->theFunction ) { case TA_CCI_TEST: retCode = TA_CCI( libHandle, test->startIdx, test->endIdx, gBuffer[0].in, gBuffer[1].in, gBuffer[2].in, test->optInTimePeriod_0, &outBegIdx, &outNbElement, gBuffer[0].out0 ); break; default: retCode = TA_UNKNOWN_ERR; } /* Check that the input were preserved. */ errNb = checkDataSame( gBuffer[0].in, history->high,history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; errNb = checkDataSame( gBuffer[1].in, history->low, history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; errNb = checkDataSame( gBuffer[2].in, history->close,history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; CHECK_EXPECTED_VALUE( gBuffer[0].out0, 0 ); outBegIdx = outNbElement = 0; /* Make another call where the input and the output are the * same buffer. */ switch( test->theFunction ) { case TA_CCI_TEST: retCode = TA_CCI( libHandle, test->startIdx, test->endIdx, gBuffer[0].in, gBuffer[1].in, gBuffer[2].in, test->optInTimePeriod_0, &outBegIdx, &outNbElement, gBuffer[0].in ); break; default: retCode = TA_UNKNOWN_ERR; } /* Check that the input were preserved. */ errNb = checkDataSame( gBuffer[1].in, history->low, history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; errNb = checkDataSame( gBuffer[2].in, history->close,history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; /* The previous call to TA_MA should have the same output * as this call. * * checkSameContent verify that all value different than NAN in * the first parameter is identical in the second parameter. */ errNb = checkSameContent( gBuffer[0].out0, gBuffer[0].in ); if( errNb != TA_TEST_PASS ) return errNb; CHECK_EXPECTED_VALUE( gBuffer[0].in, 0 ); setInputBuffer( 0, history->high, history->nbBars ); /* Make another call where the input and the output are the * same buffer. */ switch( test->theFunction ) { case TA_CCI_TEST: retCode = TA_CCI( libHandle, test->startIdx, test->endIdx, gBuffer[0].in, gBuffer[1].in, gBuffer[2].in, test->optInTimePeriod_0, &outBegIdx, &outNbElement, gBuffer[1].in ); break; default: retCode = TA_UNKNOWN_ERR; } /* Check that the input were preserved. */ errNb = checkDataSame( gBuffer[0].in, history->high,history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; errNb = checkDataSame( gBuffer[2].in, history->close,history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; /* The previous call to TA_MA should have the same output * as this call. * * checkSameContent verify that all value different than NAN in * the first parameter is identical in the second parameter. */ errNb = checkSameContent( gBuffer[0].out0, gBuffer[1].in ); if( errNb != TA_TEST_PASS ) return errNb; CHECK_EXPECTED_VALUE( gBuffer[1].in, 0 ); setInputBuffer( 1, history->low, history->nbBars ); /* Make another call where the input and the output are the * same buffer. */ switch( test->theFunction ) { case TA_CCI_TEST: retCode = TA_CCI( libHandle, test->startIdx, test->endIdx, gBuffer[0].in, gBuffer[1].in, gBuffer[2].in, test->optInTimePeriod_0, &outBegIdx, &outNbElement, gBuffer[2].in ); break; default: retCode = TA_UNKNOWN_ERR; } /* Check that the input were preserved. */ errNb = checkDataSame( gBuffer[0].in, history->high,history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; errNb = checkDataSame( gBuffer[1].in, history->low, history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; /* The previous call to TA_MA should have the same output * as this call. * * checkSameContent verify that all value different than NAN in * the first parameter is identical in the second parameter. */ errNb = checkSameContent( gBuffer[0].out0, gBuffer[2].in ); if( errNb != TA_TEST_PASS ) return errNb; CHECK_EXPECTED_VALUE( gBuffer[2].in, 0 ); setInputBuffer( 2, history->close, history->nbBars ); /* Do a systematic test of most of the * possible startIdx/endIdx range. */ testParam.test = test; testParam.high = history->high; testParam.low = history->low; testParam.close = history->close; if( test->doRangeTestFlag ) { errNb = doRangeTest( libHandle, rangeTestFunction, TA_FUNC_UNST_NONE, (void *)&testParam, 1 ); if( errNb != TA_TEST_PASS ) return errNb; } return TA_TEST_PASS; }