double * getPhaseAll(CandleData * data, long numSticks) { double * csData = CandleData::getData(data, CandleData::CLOSE, numSticks); int outNbElement = 0; int outBeg = 0; double * dataOut = new double[numSticks]; memset(dataOut,0,numSticks * sizeof(double)); TA_RetCode ret = TA_HT_DCPHASE( 0, numSticks-1, csData, &outBeg, &outNbElement, dataOut ); double * retVal = new double[numSticks + outBeg]; memset(retVal,0,(numSticks + outBeg) * sizeof(double)); memcpy(&retVal[outBeg],dataOut,numSticks*sizeof(double)); delete [] csData; delete [] dataOut; return retVal; }
double getPhase(CandleData * data, long numSticks) { double retVal = 0.0; double * csData = CandleData::getData(data, CandleData::CLOSE, numSticks); int outNbElement = 0; int outBeg = 0; double * dataOut = new double[numSticks]; memset(dataOut,0,numSticks * sizeof(double)); TA_RetCode ret = TA_HT_DCPHASE( 0, numSticks-1, csData, &outBeg, &outNbElement, dataOut ); if(outNbElement != 0) { retVal = dataOut[outNbElement - 1]; } delete [] csData; delete [] dataOut; return retVal; }
Phase getPhaseEnum(CandleData * data, long numSticks) { Phase phaseVal = PhaseTwo; double * csData = CandleData::getData(data, CandleData::CLOSE, numSticks); int outNbElement = 0; int outBeg = 0; double * dataOut = new double[1]; TA_RetCode ret = TA_HT_DCPHASE( 0, numSticks -1, csData, &outBeg, &outNbElement, dataOut ); double trend = dataOut[0]; delete [] csData; delete [] dataOut; if(trend > 0.0 && trend < 90.0) { phaseVal = PhaseTwo; } else if(trend > 90.0 && trend < 180.0) { phaseVal = PhaseThree; } else if(trend > 180.0 && trend < 270.0) { phaseVal = PhaseFour; } else if(trend > 270.0 && trend < 360.0) { phaseVal = PhaseOne; } return phaseVal; }
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; const TA_Real *referenceInput; TA_Integer *intBuffer; int size, i; /* Set to NAN all the elements of the gBuffers. */ clearAllBuffers(); /* Build the input. */ setInputBuffer( 0, history->close, history->nbBars ); /* Change the input to MEDPRICE for some tests. */ switch( test->theFunction ) { case TA_HT_DCPERIOD_TEST: case TA_HT_DCPHASE_TEST: case TA_HT_TRENDLINE_TEST: case TA_HT_TRENDMODE_TEST: TA_MEDPRICE( 0, history->nbBars-1, history->high, history->low, &outBegIdx, &outNbElement, gBuffer[0].in ); /* Will be use as reference */ TA_MEDPRICE( 0, history->nbBars-1, history->high, history->low, &outBegIdx, &outNbElement, gBuffer[1].in ); referenceInput = gBuffer[1].in; break; default: referenceInput = history->close; } /* Make a simple first call. */ size = (test->endIdx-test->startIdx)+1; switch( test->theFunction ) { case TA_HT_DCPERIOD_TEST: retCode = TA_HT_DCPERIOD( test->startIdx, test->endIdx, gBuffer[0].in, &outBegIdx, &outNbElement, gBuffer[0].out0 ); break; case TA_HT_DCPHASE_TEST: retCode = TA_HT_DCPHASE( test->startIdx, test->endIdx, gBuffer[0].in, &outBegIdx, &outNbElement, gBuffer[0].out0 ); break; case TA_HT_TRENDLINE_TEST: retCode = TA_HT_TRENDLINE( test->startIdx, test->endIdx, gBuffer[0].in, &outBegIdx, &outNbElement, gBuffer[0].out0 ); break; case TA_HT_TRENDMODE_TEST: ALLOC_INT_BUFFER(size); retCode = TA_HT_TRENDMODE( test->startIdx, test->endIdx, gBuffer[0].in, &outBegIdx, &outNbElement, &intBuffer[1] ); FREE_INT_BUFFER( gBuffer[0].out0, outNbElement ); break; default: retCode = TA_INTERNAL_ERROR(133); } /* Check that the input were preserved. */ errNb = checkDataSame( gBuffer[0].in, referenceInput, 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_HT_DCPERIOD_TEST: retCode = TA_HT_DCPERIOD( test->startIdx, test->endIdx, gBuffer[0].in, &outBegIdx, &outNbElement, gBuffer[0].in ); break; case TA_HT_DCPHASE_TEST: retCode = TA_HT_DCPHASE( test->startIdx, test->endIdx, gBuffer[0].in, &outBegIdx, &outNbElement, gBuffer[0].in ); break; case TA_HT_TRENDLINE_TEST: retCode = TA_HT_TRENDLINE( test->startIdx, test->endIdx, gBuffer[0].in, &outBegIdx, &outNbElement, gBuffer[0].in ); break; case TA_HT_TRENDMODE_TEST: ALLOC_INT_BUFFER(size); retCode = TA_HT_TRENDMODE( test->startIdx, test->endIdx, gBuffer[0].in, &outBegIdx, &outNbElement, &intBuffer[1] ); FREE_INT_BUFFER( gBuffer[0].in, outNbElement ); break; default: retCode = TA_INTERNAL_ERROR(134); } /* 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[0].in ); if( errNb != TA_TEST_PASS ) return errNb; CHECK_EXPECTED_VALUE( gBuffer[0].in, 0 ); /* Do a systematic test of most of the * possible startIdx/endIdx range. */ testParam.test = test; testParam.price = referenceInput; if( test->doRangeTestFlag ) { switch( test->theFunction ) { case TA_HT_DCPERIOD_TEST: errNb = doRangeTest( rangeTestFunction, TA_FUNC_UNST_HT_DCPERIOD, (void *)&testParam, 1, 0 ); break; case TA_HT_DCPHASE_TEST: errNb = doRangeTest( rangeTestFunction, TA_FUNC_UNST_HT_DCPHASE, (void *)&testParam, 1, 360 ); break; case TA_HT_TRENDLINE_TEST: errNb = doRangeTest( rangeTestFunction, TA_FUNC_UNST_HT_TRENDLINE, (void *)&testParam, 1, 0 ); break; case TA_HT_TRENDMODE_TEST: errNb = doRangeTest( rangeTestFunction, TA_FUNC_UNST_HT_TRENDMODE, (void *)&testParam, 1, 0 ); break; default: errNb = doRangeTest( rangeTestFunction, TA_FUNC_UNST_NONE, (void *)&testParam, 1, 0 ); } if( errNb != TA_TEST_PASS ) return errNb; } return TA_TEST_PASS; }
/**** Local functions definitions. ****/ static TA_RetCode rangeTestFunction( 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; TA_Integer *intBuffer; int size, i; (void)outputNb; testParam = (TA_RangeTestParam *)opaqueData; switch( testParam->test->theFunction ) { case TA_HT_DCPERIOD_TEST: retCode = TA_HT_DCPERIOD( startIdx, endIdx, testParam->price, outBegIdx, outNbElement, outputBuffer ); *lookback = TA_HT_DCPERIOD_Lookback(); break; case TA_HT_DCPHASE_TEST: retCode = TA_HT_DCPHASE( startIdx, endIdx, testParam->price, outBegIdx, outNbElement, outputBuffer ); *lookback = TA_HT_DCPHASE_Lookback(); break; case TA_HT_TRENDLINE_TEST: retCode = TA_HT_TRENDLINE( startIdx, endIdx, testParam->price, outBegIdx, outNbElement, outputBuffer ); *lookback = TA_HT_TRENDLINE_Lookback(); break; case TA_HT_TRENDMODE_TEST: /* Trendmode returns integers, but this test * is comparing real, so a translation is done * here. */ #define PRE_SENTINEL ((TA_Integer)0xABABFEDC) #define POST_SENTINEL ((TA_Integer)0xEFABCDFF) #define ALLOC_INT_BUFFER(varSize) \ { \ intBuffer = TA_Malloc(sizeof(TA_Integer)*(varSize+2)); \ intBuffer[0] = PRE_SENTINEL; \ intBuffer[varSize+1] = POST_SENTINEL; \ } size = endIdx-startIdx+1; \ ALLOC_INT_BUFFER(size); retCode = TA_HT_TRENDMODE( startIdx, endIdx, testParam->price, outBegIdx, outNbElement, &intBuffer[1] ); *lookback = TA_HT_TRENDMODE_Lookback(); #define FREE_INT_BUFFER( destBuffer, varNbElement ) \ { \ if( intBuffer[0] != PRE_SENTINEL ) \ { \ retCode = TA_INTERNAL_ERROR(138); \ } \ else if( intBuffer[size+1] != POST_SENTINEL ) \ { \ retCode = TA_INTERNAL_ERROR(139); \ } \ else \ { \ for( i=0; i < varNbElement; i++ ) \ destBuffer[i] = (double)intBuffer[i+1]; \ } \ TA_Free( intBuffer ); \ } FREE_INT_BUFFER( outputBuffer, *outNbElement ); break; default: retCode = TA_INTERNAL_ERROR(132); } return retCode; }