static VALUE ta_func_lookback(VALUE self) { TA_RetCode ret_code; ParamHolder *param_holder; TA_Integer out_lookback; Data_Get_Struct(self, ParamHolder, param_holder); ret_code = TA_GetLookback(param_holder->p, &out_lookback); if ( ret_code != TA_SUCCESS ) rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetLookback"); return INT2FIX(out_lookback); }
/* Abstract call for all candlestick functions. * * Call the function by 'name'. * * Optional inputs are pass as an array of double. * Elements will be converted to integer as needed. * * All outputs are returned in the remaining parameters. * * 'lookback' is the return value of the corresponding Lookback function. * taFuncRetCode is the return code from the call of the TA function. * */ static ErrorNumber callCandlestick( TA_ParamHolder **paramHolderPtr, const char *name, int startIdx, int endIdx, const double *inOpen, const double *inHigh, const double *inLow, const double *inClose, const double optInArray[], int *outBegIdx, int *outNbElement, int outInteger[], int *lookback, TA_RetCode *taFuncRetCode ) { /* Use the abstract interface to call the function by name. */ TA_ParamHolder *paramHolder; const TA_FuncHandle *handle; const TA_FuncInfo *funcInfo; const TA_InputParameterInfo *inputInfo; const TA_OutputParameterInfo *outputInfo; TA_RetCode retCode; (void)optInArray; /* Speed optimization if paramHolder is already initialized. */ paramHolder = *paramHolderPtr; if( !paramHolder ) { retCode = TA_GetFuncHandle( name, &handle ); if( retCode != TA_SUCCESS ) { printf( "Can't get the function handle [%d]\n", retCode ); return TA_TSTCDL_GETFUNCHANDLE_FAIL; } retCode = TA_ParamHolderAlloc( handle, ¶mHolder ); if( retCode != TA_SUCCESS ) { printf( "Can't allocate the param holder [%d]\n", retCode ); return TA_TSTCDL_PARAMHOLDERALLOC_FAIL; } *paramHolderPtr = paramHolder; TA_GetFuncInfo( handle, &funcInfo ); /* Verify that the input are only OHLC. */ if( funcInfo->nbInput != 1 ) { printf( "Candlestick are expected to use only OHLC as input.\n" ); return TA_TSTCDL_NBINPUT_WRONG; } TA_GetInputParameterInfo( handle, 0, &inputInfo ); if( inputInfo->type != TA_Input_Price ) { printf( "Candlestick are expected to use only OHLC as input.\n" ); return TA_TSTCDL_INPUT_TYPE_WRONG; } if( inputInfo->flags != (TA_IN_PRICE_OPEN | TA_IN_PRICE_HIGH | TA_IN_PRICE_LOW | TA_IN_PRICE_CLOSE) ) { printf( "Candlestick are expected to use only OHLC as input.\n" ); return TA_TSTCDL_INPUT_FLAG_WRONG; } /* Set the optional inputs. */ /* Verify that there is only one output. */ if( funcInfo->nbOutput != 1 ) { printf( "Candlestick are expected to have only one output array.\n" ); return TA_TSTCDL_NBOUTPUT_WRONG; } TA_GetOutputParameterInfo( handle, 0, &outputInfo ); if( outputInfo->type != TA_Output_Integer ) { printf( "Candlestick are expected to have only one output array of type integer.\n" ); return TA_TSTCDL_OUTPUT_TYPE_WRONG; } /* !!!!!!!!!!!!! TO BE DONE !!!!!!!!!!!!!!!!!! * For now all candlestick functions will be called with default optional parameter. */ } /* Set the input buffers. */ TA_SetInputParamPricePtr( paramHolder, 0, inOpen, inHigh, inLow, inClose, NULL, NULL ); TA_SetOutputParamIntegerPtr(paramHolder,0,outInteger); /* Do the function call. */ *taFuncRetCode = TA_CallFunc(paramHolder,startIdx,endIdx,outBegIdx,outNbElement); if( *taFuncRetCode != TA_SUCCESS ) { printf( "TA_CallFunc() failed [%d]\n", *taFuncRetCode ); TA_ParamHolderFree( paramHolder ); return TA_TSTCDL_CALLFUNC_FAIL; } /* Do the lookback function call. */ retCode = TA_GetLookback( paramHolder, lookback ); if( retCode != TA_SUCCESS ) { printf( "TA_GetLookback() failed [%d]\n", retCode ); TA_ParamHolderFree( paramHolder ); return TA_TSTCDL_GETLOOKBACK_FAIL; } return TA_TEST_PASS; }
static ErrorNumber callWithDefaults( const char *funcName, const double *input, const int *input_int, int size ) { TA_ParamHolder *paramHolder; const TA_FuncHandle *handle; const TA_FuncInfo *funcInfo; const TA_InputParameterInfo *inputInfo; const TA_OutputParameterInfo *outputInfo; TA_RetCode retCode; unsigned int i; int j; int outBegIdx, outNbElement, lookback; retCode = TA_GetFuncHandle( funcName, &handle ); if( retCode != TA_SUCCESS ) { printf( "Can't get the function handle [%d]\n", retCode ); return TA_ABS_TST_FAIL_GETFUNCHANDLE; } retCode = TA_ParamHolderAlloc( handle, ¶mHolder ); if( retCode != TA_SUCCESS ) { printf( "Can't allocate the param holder [%d]\n", retCode ); return TA_ABS_TST_FAIL_PARAMHOLDERALLOC; } TA_GetFuncInfo( handle, &funcInfo ); for( i=0; i < funcInfo->nbInput; i++ ) { TA_GetInputParameterInfo( handle, i, &inputInfo ); switch(inputInfo->type) { case TA_Input_Price: TA_SetInputParamPricePtr( paramHolder, i, inputInfo->flags&TA_IN_PRICE_OPEN?input:NULL, inputInfo->flags&TA_IN_PRICE_HIGH?input:NULL, inputInfo->flags&TA_IN_PRICE_LOW?input:NULL, inputInfo->flags&TA_IN_PRICE_CLOSE?input:NULL, inputInfo->flags&TA_IN_PRICE_VOLUME?input_int:NULL, NULL ); break; case TA_Input_Real: TA_SetInputParamRealPtr( paramHolder, i, input ); break; case TA_Input_Integer: TA_SetInputParamIntegerPtr( paramHolder, i, input_int ); break; } } for( i=0; i < funcInfo->nbOutput; i++ ) { TA_GetOutputParameterInfo( handle, i, &outputInfo ); switch(outputInfo->type) { case TA_Output_Real: TA_SetOutputParamRealPtr(paramHolder,i,&output[i][0]); for( j=0; j < 2000; j++ ) output[i][j] = TA_REAL_MIN; break; case TA_Output_Integer: TA_SetOutputParamIntegerPtr(paramHolder,i,&output_int[i][0]); for( j=0; j < 2000; j++ ) output_int[i][j] = TA_INTEGER_MIN; break; } } /* Do the function call. */ retCode = TA_CallFunc(paramHolder,0,size-1,&outBegIdx,&outNbElement); if( retCode != TA_SUCCESS ) { printf( "TA_CallFunc() failed zero data test [%d]\n", retCode ); TA_ParamHolderFree( paramHolder ); return TA_ABS_TST_FAIL_CALLFUNC_1; } /* Verify consistency with Lookback */ retCode = TA_GetLookback( paramHolder, &lookback ); if( retCode != TA_SUCCESS ) { printf( "TA_GetLookback() failed zero data test [%d]\n", retCode ); TA_ParamHolderFree( paramHolder ); return TA_ABS_TST_FAIL_CALLFUNC_2; } if( outBegIdx != lookback ) { printf( "TA_GetLookback() != outBegIdx [%d != %d]\n", lookback, outBegIdx ); TA_ParamHolderFree( paramHolder ); return TA_ABS_TST_FAIL_CALLFUNC_3; } for( i=0; i < funcInfo->nbOutput; i++ ) { switch(outputInfo->type) { case TA_Output_Real: for( j=0; j < outNbElement; j++ ) { if( trio_isnan(output[i][j]) || trio_isinf(output[i][j])) { printf( "Failed for output[%d][%d] = %e\n", i, j, output[i][j] ); return TA_ABS_TST_FAIL_INVALID_OUTPUT; } } break; case TA_Output_Integer: break; } } /* Do another function call where startIdx == endIdx == 0. * In that case, outBegIdx should ALWAYS be zero. */ retCode = TA_CallFunc(paramHolder,0,0,&outBegIdx,&outNbElement); if( retCode != TA_SUCCESS ) { printf( "TA_CallFunc() failed data test 4 [%d]\n", retCode ); TA_ParamHolderFree( paramHolder ); return TA_ABS_TST_FAIL_CALLFUNC_4; } if( outBegIdx != 0 ) { printf( "failed outBegIdx=%d when startIdx==endIdx==0\n", outBegIdx ); TA_ParamHolderFree( paramHolder ); return TA_ABS_TST_FAIL_STARTEND_ZERO; } retCode = TA_ParamHolderFree( paramHolder ); if( retCode != TA_SUCCESS ) { printf( "TA_ParamHolderFree failed [%d]\n", retCode ); return TA_ABS_TST_FAIL_PARAMHOLDERFREE; } return TA_TEST_PASS; }
/**** Local functions definitions. ****/ static ErrorNumber testLookback( TA_ParamHolder *paramHolder ) { TA_RetCode retCode; int lookback; /* Change the parameters of STOCH and verify that TA_GetLookback respond correctly. */ retCode = TA_SetOptInputParamInteger( paramHolder, 0, 3 ); if( retCode != TA_SUCCESS ) { printf( "TA_SetOptInputParamInteger call failed [%d]\n", retCode ); return TA_ABS_TST_FAIL_OPTINPUTPARAMINTEGER; } retCode = TA_SetOptInputParamInteger( paramHolder, 1, 4 ); if( retCode != TA_SUCCESS ) { printf( "TA_SetOptInputParamInteger call failed [%d]\n", retCode ); return TA_ABS_TST_FAIL_OPTINPUTPARAMINTEGER; } retCode = TA_SetOptInputParamInteger( paramHolder, 2, (TA_Integer)TA_MAType_SMA ); if( retCode != TA_SUCCESS ) { printf( "TA_SetOptInputParamInteger call failed [%d]\n", retCode ); return TA_ABS_TST_FAIL_OPTINPUTPARAMINTEGER; } retCode = TA_SetOptInputParamInteger( paramHolder, 3, 4 ); if( retCode != TA_SUCCESS ) { printf( "TA_SetOptInputParamInteger call failed [%d]\n", retCode ); return TA_ABS_TST_FAIL_OPTINPUTPARAMINTEGER; } retCode = TA_SetOptInputParamInteger( paramHolder, 4, (TA_Integer)TA_MAType_SMA ); if( retCode != TA_SUCCESS ) { printf( "TA_SetOptInputParamInteger call failed [%d]\n", retCode ); return TA_ABS_TST_FAIL_OPTINPUTPARAMINTEGER; } retCode = TA_GetLookback(paramHolder,&lookback); if( retCode != TA_SUCCESS ) { printf( "TA_GetLookback failed [%d]\n", retCode ); return TA_ABS_TST_FAIL_GETLOOKBACK_CALL_1; } if( lookback != 8 ) { printf( "TA_GetLookback failed [%d != 8]\n", lookback ); return TA_ABS_TST_FAIL_GETLOOKBACK_1; } /* Change one parameter and check again. */ retCode = TA_SetOptInputParamInteger( paramHolder, 3, 3 ); if( retCode != TA_SUCCESS ) { printf( "TA_SetOptInputParamInteger call failed [%d]\n", retCode ); return TA_ABS_TST_FAIL_OPTINPUTPARAMINTEGER; } retCode = TA_GetLookback(paramHolder,&lookback); if( retCode != TA_SUCCESS ) { printf( "TA_GetLookback failed [%d]\n", retCode ); return TA_ABS_TST_FAIL_GETLOOKBACK_CALL_2; } if( lookback != 7 ) { printf( "TA_GetLookback failed [%d != 7]\n", lookback ); return TA_ABS_TST_FAIL_GETLOOKBACK_2; } return TA_TEST_PASS; }