static ErrorNumber do_test( const TA_History *history, const TA_Test *test ) { TA_RetCode retCode; ErrorNumber errNb; TA_Integer outBegIdx; TA_Integer outNbElement; TA_RangeTestParam testParam; retCode = TA_SetUnstablePeriod( TA_FUNC_UNST_EMA, 0 ); if( retCode != TA_SUCCESS ) return TA_TEST_TFRR_SETUNSTABLE_PERIOD_FAIL; TA_SetCompatibility( test->compatibility ); /* Set to NAN all the elements of the gBuffers. */ clearAllBuffers(); /* Build the input. */ setInputBuffer( 0, history->close, history->nbBars ); setInputBuffer( 1, history->close, history->nbBars ); setInputBuffer( 2, history->close, history->nbBars ); setInputBuffer( 3, history->close, history->nbBars ); CLEAR_EXPECTED_VALUE(0); CLEAR_EXPECTED_VALUE(1); CLEAR_EXPECTED_VALUE(2); /* Make a simple first call. */ switch( test->testId ) { case TA_MACDFIX_TEST: retCode = TA_MACDFIX( test->startIdx, test->endIdx, gBuffer[0].in, test->optInSignalPeriod_2, &outBegIdx, &outNbElement, gBuffer[0].out0, gBuffer[0].out1, gBuffer[0].out2 ); break; case TA_MACD_TEST: retCode = TA_MACD(test->startIdx, test->endIdx, gBuffer[0].in, test->optInFastPeriod, test->optInSlowPeriod, test->optInSignalPeriod_2, &outBegIdx, &outNbElement, gBuffer[0].out0, gBuffer[0].out1, gBuffer[0].out2 ); break; case TA_MACDEXT_TEST: retCode = TA_MACDEXT( test->startIdx, test->endIdx, gBuffer[0].in, test->optInFastPeriod, TA_MAType_EMA, test->optInSlowPeriod, TA_MAType_EMA, test->optInSignalPeriod_2, TA_MAType_EMA, &outBegIdx, &outNbElement, gBuffer[0].out0, gBuffer[0].out1, gBuffer[0].out2 ); break; } errNb = checkDataSame( gBuffer[0].in, history->close, history->nbBars ); if( errNb != TA_TEST_PASS ) return errNb; CHECK_EXPECTED_VALUE( gBuffer[0].out0, 0 ); CHECK_EXPECTED_VALUE( gBuffer[0].out1, 1 ); CHECK_EXPECTED_VALUE( gBuffer[0].out2, 2 ); outBegIdx = outNbElement = 0; /* Make another call where the input and the output are the * same buffer. */ switch( test->testId ) { case TA_MACDFIX_TEST: retCode = TA_MACDFIX( test->startIdx, test->endIdx, gBuffer[1].in, test->optInSignalPeriod_2, &outBegIdx, &outNbElement, gBuffer[1].in, gBuffer[1].out1, gBuffer[1].out2 ); break; case TA_MACD_TEST: retCode = TA_MACD(test->startIdx, test->endIdx, gBuffer[1].in, test->optInFastPeriod, test->optInSlowPeriod, test->optInSignalPeriod_2, &outBegIdx, &outNbElement, gBuffer[1].in, gBuffer[1].out1, gBuffer[1].out2 ); break; case TA_MACDEXT_TEST: retCode = TA_MACDEXT( test->startIdx, test->endIdx, gBuffer[1].in, test->optInFastPeriod, TA_MAType_EMA, test->optInSlowPeriod, TA_MAType_EMA, test->optInSignalPeriod_2, TA_MAType_EMA, &outBegIdx, &outNbElement, gBuffer[1].in, gBuffer[1].out1, gBuffer[1].out2 ); break; } /* 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 ); CHECK_EXPECTED_VALUE( gBuffer[1].out1, 1 ); CHECK_EXPECTED_VALUE( gBuffer[1].out2, 2 ); outBegIdx = outNbElement = 0; CLEAR_EXPECTED_VALUE(0); CLEAR_EXPECTED_VALUE(1); CLEAR_EXPECTED_VALUE(2); /* Make another call where the input and the output are the * same buffer. */ switch( test->testId ) { case TA_MACDFIX_TEST: retCode = TA_MACDFIX( test->startIdx, test->endIdx, gBuffer[2].in, test->optInSignalPeriod_2, &outBegIdx, &outNbElement, gBuffer[2].out1, gBuffer[2].in, gBuffer[2].out2 ); break; case TA_MACD_TEST: retCode = TA_MACD( test->startIdx, test->endIdx, gBuffer[2].in, test->optInFastPeriod, test->optInSlowPeriod, test->optInSignalPeriod_2, &outBegIdx, &outNbElement, gBuffer[2].out1, gBuffer[2].in, gBuffer[2].out2 ); break; case TA_MACDEXT_TEST: retCode = TA_MACDEXT( test->startIdx, test->endIdx, gBuffer[2].in, test->optInFastPeriod, TA_MAType_EMA, test->optInSlowPeriod, TA_MAType_EMA, test->optInSignalPeriod_2, TA_MAType_EMA, &outBegIdx, &outNbElement, gBuffer[2].out1, gBuffer[2].in, gBuffer[2].out2 ); break; } /* 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[1].out1, gBuffer[2].in ); if( errNb != TA_TEST_PASS ) return errNb; CHECK_EXPECTED_VALUE( gBuffer[2].out1, 0 ); CHECK_EXPECTED_VALUE( gBuffer[2].in, 1 ); CHECK_EXPECTED_VALUE( gBuffer[2].out2, 2 ); outBegIdx = outNbElement = 0; CLEAR_EXPECTED_VALUE(0); CLEAR_EXPECTED_VALUE(1); CLEAR_EXPECTED_VALUE(2); /* Make another call where the input and the output are the * same buffer. */ switch( test->testId ) { case TA_MACDFIX_TEST: retCode = TA_MACDFIX( test->startIdx, test->endIdx, gBuffer[3].in, test->optInSignalPeriod_2, &outBegIdx, &outNbElement, gBuffer[3].out1, gBuffer[3].out2, gBuffer[3].in ); break; case TA_MACD_TEST: retCode = TA_MACD(test->startIdx, test->endIdx, gBuffer[3].in, test->optInFastPeriod, test->optInSlowPeriod, test->optInSignalPeriod_2, &outBegIdx, &outNbElement, gBuffer[3].out1, gBuffer[3].out2, gBuffer[3].in ); break; case TA_MACDEXT_TEST: retCode = TA_MACDEXT( test->startIdx, test->endIdx, gBuffer[3].in, test->optInFastPeriod, TA_MAType_EMA, test->optInSlowPeriod, TA_MAType_EMA, test->optInSignalPeriod_2, TA_MAType_EMA, &outBegIdx, &outNbElement, gBuffer[3].out1, gBuffer[3].out2, gBuffer[3].in ); break; } /* 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[2].out2, gBuffer[3].in ); if( errNb != TA_TEST_PASS ) return errNb; CHECK_EXPECTED_VALUE( gBuffer[3].out1, 0 ); CHECK_EXPECTED_VALUE( gBuffer[3].out2, 1 ); CHECK_EXPECTED_VALUE( gBuffer[3].in, 2 ); /* Do a systematic test of most of the * possible startIdx/endIdx range. */ testParam.test = test; testParam.close = history->close; if( test->doRangeTestFlag ) { errNb = doRangeTest( rangeTestFunction, TA_FUNC_UNST_EMA, (void *)&testParam, 3, 0 ); if( errNb != TA_TEST_PASS ) return errNb; } return TA_TEST_PASS; }
/* 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 optInFastPeriod; double optInFastMA; int optInSlowPeriod; double optInSlowMA; int optInSignalPeriod; double optInSignalMA; /* output variables */ int outBegIdx; int outNbElement; double* outMACD; double* outMACDSignal; double* outMACDHist; /* input dimentions */ int inSeriesRows; int inSeriesCols; /* error handling */ TA_RetCode retCode; /* ----------------- input/output count ----------------- */ /* Check for proper number of arguments. */ if (nrhs < 1 || nrhs > 7) mexErrMsgTxt("#7 inputs possible #6 optional."); if (nlhs != 3) mexErrMsgTxt("#3 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 optInFastPeriod must be a scalar."); /* Get the scalar input optInTimePeriod. */ optInFastPeriod = (int) mxGetScalar(prhs[1]); } else { optInFastPeriod = 12; } if (nrhs >= 2+1) { if (!mxIsDouble(prhs[2]) || mxIsComplex(prhs[2]) || mxGetN(prhs[2])*mxGetM(prhs[2]) != 1) mexErrMsgTxt("Input optInFastMA must be a scalar."); /* Get the scalar input optInTimePeriod. */ optInFastMA = mxGetScalar(prhs[2]); } else { optInFastMA = 0; } if (nrhs >= 3+1) { if (!mxIsDouble(prhs[3]) || mxIsComplex(prhs[3]) || mxGetN(prhs[3])*mxGetM(prhs[3]) != 1) mexErrMsgTxt("Input optInSlowPeriod must be a scalar."); /* Get the scalar input optInTimePeriod. */ optInSlowPeriod = (int) mxGetScalar(prhs[3]); } else { optInSlowPeriod = 26; } if (nrhs >= 4+1) { if (!mxIsDouble(prhs[4]) || mxIsComplex(prhs[4]) || mxGetN(prhs[4])*mxGetM(prhs[4]) != 1) mexErrMsgTxt("Input optInSlowMA must be a scalar."); /* Get the scalar input optInTimePeriod. */ optInSlowMA = mxGetScalar(prhs[4]); } else { optInSlowMA = 0; } if (nrhs >= 5+1) { if (!mxIsDouble(prhs[5]) || mxIsComplex(prhs[5]) || mxGetN(prhs[5])*mxGetM(prhs[5]) != 1) mexErrMsgTxt("Input optInSignalPeriod must be a scalar."); /* Get the scalar input optInTimePeriod. */ optInSignalPeriod = (int) mxGetScalar(prhs[5]); } else { optInSignalPeriod = 9; } if (nrhs >= 6+1) { if (!mxIsDouble(prhs[6]) || mxIsComplex(prhs[6]) || mxGetN(prhs[6])*mxGetM(prhs[6]) != 1) mexErrMsgTxt("Input optInSignalMA must be a scalar."); /* Get the scalar input optInTimePeriod. */ optInSignalMA = mxGetScalar(prhs[6]); } else { optInSignalMA = 0; } /* ----------------- OUTPUT ----------------- */ outMACD = mxCalloc(inSeriesRows, sizeof(double)); outMACDSignal = mxCalloc(inSeriesRows, sizeof(double)); outMACDHist = mxCalloc(inSeriesRows, sizeof(double)); /* -------------- Invocation ---------------- */ retCode = TA_MACDEXT( startIdx, endIdx, inReal, optInFastPeriod, optInFastMA, optInSlowPeriod, optInSlowMA, optInSignalPeriod, optInSignalMA, &outBegIdx, &outNbElement, outMACD, outMACDSignal, outMACDHist); /* -------------- Errors ---------------- */ if (retCode) { mxFree(outMACD); mxFree(outMACDSignal); mxFree(outMACDHist); mexPrintf("%s%i","Return code=",retCode); mexErrMsgTxt(" Error!"); } // Populate Output plhs[0] = mxCreateDoubleMatrix(outBegIdx+outNbElement,1, mxREAL); memcpy(((double *) mxGetData(plhs[0]))+outBegIdx, outMACD, outNbElement*mxGetElementSize(plhs[0])); mxFree(outMACD); plhs[1] = mxCreateDoubleMatrix(outBegIdx+outNbElement,1, mxREAL); memcpy(((double *) mxGetData(plhs[1]))+outBegIdx, outMACDSignal, outNbElement*mxGetElementSize(plhs[1])); mxFree(outMACDSignal); plhs[2] = mxCreateDoubleMatrix(outBegIdx+outNbElement,1, mxREAL); memcpy(((double *) mxGetData(plhs[2]))+outBegIdx, outMACDHist, outNbElement*mxGetElementSize(plhs[2])); mxFree(outMACDHist); } /* END mexFunction */
/**** Local functions definitions. ****/ static TA_RetCode rangeTestFunction( TA_Integer startIdx, TA_Integer endIdx, TA_Real *outputBuffer, TA_Integer *outputBufferInt, TA_Integer *outBegIdx, TA_Integer *outNbElement, TA_Integer *lookback, void *opaqueData, unsigned int outputNb, unsigned int *isOutputInteger ) { TA_RetCode retCode; TA_RangeTestParam *testParam; TA_Real *dummyBuffer1, *dummyBuffer2; TA_Real *out1, *out2, *out3; (void)outputBufferInt; *isOutputInteger = 0; testParam = (TA_RangeTestParam *)opaqueData; dummyBuffer1 = TA_Malloc( ((endIdx-startIdx)+1)*sizeof(TA_Real)); if( !dummyBuffer1 ) return TA_ALLOC_ERR; dummyBuffer2 = TA_Malloc( ((endIdx-startIdx)+1)*sizeof(TA_Real)); if( !dummyBuffer2 ) { TA_Free( dummyBuffer1 ); return TA_ALLOC_ERR; } switch( outputNb ) { case 0: out1 = outputBuffer; out2 = dummyBuffer1; out3 = dummyBuffer2; break; case 1: out2 = outputBuffer; out1 = dummyBuffer1; out3 = dummyBuffer2; break; case 2: out3 = outputBuffer; out2 = dummyBuffer1; out1 = dummyBuffer2; break; default: TA_Free( dummyBuffer1 ); TA_Free( dummyBuffer2 ); return TA_BAD_PARAM; } switch( testParam->test->testId ) { case TA_MACDFIX_TEST: retCode = TA_MACDFIX( startIdx, endIdx, testParam->close, testParam->test->optInSignalPeriod_2, outBegIdx, outNbElement, out1, out2, out3 ); *lookback = TA_MACDFIX_Lookback( testParam->test->optInSignalPeriod_2 ); break; case TA_MACD_TEST: retCode = TA_MACD( startIdx, endIdx, testParam->close, testParam->test->optInFastPeriod, testParam->test->optInSlowPeriod, testParam->test->optInSignalPeriod_2, outBegIdx, outNbElement, out1, out2, out3 ); *lookback = TA_MACD_Lookback( testParam->test->optInFastPeriod, testParam->test->optInSlowPeriod, testParam->test->optInSignalPeriod_2 ); break; case TA_MACDEXT_TEST: retCode = TA_MACDEXT( startIdx, endIdx, testParam->close, testParam->test->optInFastPeriod, TA_MAType_EMA, testParam->test->optInSlowPeriod, TA_MAType_EMA, testParam->test->optInSignalPeriod_2, TA_MAType_EMA, outBegIdx, outNbElement, out1, out2, out3 ); *lookback = TA_MACDEXT_Lookback( testParam->test->optInFastPeriod, TA_MAType_EMA, testParam->test->optInSlowPeriod, TA_MAType_EMA, testParam->test->optInSignalPeriod_2, TA_MAType_EMA ); break; default: retCode = TA_BAD_PARAM; } TA_Free( dummyBuffer1 ); TA_Free( dummyBuffer2 ); return retCode; }