📄 ta_correl.c
字号:
lookbackTotal = optInTimePeriod-1; if( startIdx < lookbackTotal ) startIdx = lookbackTotal; /* Make sure there is still something to evaluate. */ if( startIdx > endIdx ) { VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx); VALUE_HANDLE_DEREF_TO_ZERO(outNBElement); return ENUM_VALUE(RetCode,TA_SUCCESS,Success); } VALUE_HANDLE_DEREF(outBegIdx) = startIdx; trailingIdx = startIdx - lookbackTotal; /* Calculate the initial values. */ sumXY = sumX = sumY = sumX2 = sumY2 = 0.0; for( today=trailingIdx; today <= startIdx; today++ ) { x = inReal0[today]; sumX += x; sumX2 += x*x; y = inReal1[today]; sumXY += x*y; sumY += y; sumY2 += y*y; } /* Write the first output. * Save first the trailing values since the input * and output might be the same array, */ trailingX = inReal0[trailingIdx]; trailingY = inReal1[trailingIdx++]; tempReal = (sumX2-((sumX*sumX)/optInTimePeriod)) * (sumY2-((sumY*sumY)/optInTimePeriod)); if( !TA_IS_ZERO_OR_NEG(tempReal) ) outReal[0] = (sumXY-((sumX*sumY)/optInTimePeriod)) / std_sqrt(tempReal); else outReal[0] = 0.0; /* Tight loop to do subsequent values. */ outIdx = 1; while( today <= endIdx ) { /* Remove trailing values */ sumX -= trailingX; sumX2 -= trailingX*trailingX; sumXY -= trailingX*trailingY; sumY -= trailingY; sumY2 -= trailingY*trailingY; /* Add new values */ x = inReal0[today]; sumX += x; sumX2 += x*x; y = inReal1[today++]; sumXY += x*y; sumY += y; sumY2 += y*y; /* Output new coefficient. * Save first the trailing values since the input * and output might be the same array, */ trailingX = inReal0[trailingIdx]; trailingY = inReal1[trailingIdx++]; tempReal = (sumX2-((sumX*sumX)/optInTimePeriod)) * (sumY2-((sumY*sumY)/optInTimePeriod)); if( !TA_IS_ZERO_OR_NEG(tempReal) ) outReal[outIdx++] = (sumXY-((sumX*sumY)/optInTimePeriod)) / std_sqrt(tempReal); else outReal[outIdx++] = 0.0; } VALUE_HANDLE_DEREF(outNBElement) = outIdx; return ENUM_VALUE(RetCode,TA_SUCCESS,Success);}/**** START GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****//* Generated */ /* Generated */ #define USE_SINGLE_PRECISION_INPUT/* Generated */ #if !defined( _MANAGED ) && !defined( _JAVA )/* Generated */ #undef TA_PREFIX/* Generated */ #define TA_PREFIX(x) TA_S_##x/* Generated */ #endif/* Generated */ #undef INPUT_TYPE/* Generated */ #define INPUT_TYPE float/* Generated */ #if defined( _MANAGED )/* Generated */ enum class Core::RetCode Core::Correl( int startIdx,/* Generated */ int endIdx,/* Generated */ cli::array<float>^ inReal0,/* Generated */ cli::array<float>^ inReal1,/* Generated */ int optInTimePeriod, /* From 1 to 100000 *//* Generated */ [Out]int% outBegIdx,/* Generated */ [Out]int% outNBElement,/* Generated */ cli::array<double>^ outReal )/* Generated */ #elif defined( _JAVA )/* Generated */ public RetCode correl( int startIdx,/* Generated */ int endIdx,/* Generated */ float inReal0[],/* Generated */ float inReal1[],/* Generated */ int optInTimePeriod, /* From 1 to 100000 *//* Generated */ MInteger outBegIdx,/* Generated */ MInteger outNBElement,/* Generated */ double outReal[] )/* Generated */ #else/* Generated */ TA_RetCode TA_S_CORREL( int startIdx,/* Generated */ int endIdx,/* Generated */ const float inReal0[],/* Generated */ const float inReal1[],/* Generated */ int optInTimePeriod, /* From 1 to 100000 *//* Generated */ int *outBegIdx,/* Generated */ int *outNBElement,/* Generated */ double outReal[] )/* Generated */ #endif/* Generated */ {/* Generated */ double sumXY, sumX, sumY, sumX2, sumY2, x, y, trailingX, trailingY;/* Generated */ double tempReal;/* Generated */ int lookbackTotal, today, trailingIdx, outIdx;/* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK/* Generated */ if( startIdx < 0 )/* Generated */ return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);/* Generated */ if( (endIdx < 0) || (endIdx < startIdx))/* Generated */ return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);/* Generated */ #if !defined(_JAVA)/* Generated */ if( !inReal0 ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);/* Generated */ if( !inReal1 ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);/* Generated */ #endif /* Generated */ if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )/* Generated */ optInTimePeriod = 30;/* Generated */ else if( ((int)optInTimePeriod < 1) || ((int)optInTimePeriod > 100000) )/* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);/* Generated */ #if !defined(_JAVA)/* Generated */ if( !outReal )/* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);/* Generated */ #endif /* Generated */ #endif /* Generated */ lookbackTotal = optInTimePeriod-1;/* Generated */ if( startIdx < lookbackTotal )/* Generated */ startIdx = lookbackTotal;/* Generated */ if( startIdx > endIdx )/* Generated */ {/* Generated */ VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);/* Generated */ VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);/* Generated */ return ENUM_VALUE(RetCode,TA_SUCCESS,Success);/* Generated */ }/* Generated */ VALUE_HANDLE_DEREF(outBegIdx) = startIdx;/* Generated */ trailingIdx = startIdx - lookbackTotal;/* Generated */ sumXY = sumX = sumY = sumX2 = sumY2 = 0.0;/* Generated */ for( today=trailingIdx; today <= startIdx; today++ )/* Generated */ {/* Generated */ x = inReal0[today];/* Generated */ sumX += x;/* Generated */ sumX2 += x*x;/* Generated */ y = inReal1[today];/* Generated */ sumXY += x*y;/* Generated */ sumY += y;/* Generated */ sumY2 += y*y;/* Generated */ }/* Generated */ trailingX = inReal0[trailingIdx];/* Generated */ trailingY = inReal1[trailingIdx++];/* Generated */ tempReal = (sumX2-((sumX*sumX)/optInTimePeriod)) * (sumY2-((sumY*sumY)/optInTimePeriod));/* Generated */ if( !TA_IS_ZERO_OR_NEG(tempReal) )/* Generated */ outReal[0] = (sumXY-((sumX*sumY)/optInTimePeriod)) / std_sqrt(tempReal);/* Generated */ else /* Generated */ outReal[0] = 0.0;/* Generated */ outIdx = 1;/* Generated */ while( today <= endIdx )/* Generated */ {/* Generated */ sumX -= trailingX;/* Generated */ sumX2 -= trailingX*trailingX;/* Generated */ sumXY -= trailingX*trailingY;/* Generated */ sumY -= trailingY;/* Generated */ sumY2 -= trailingY*trailingY;/* Generated */ x = inReal0[today];/* Generated */ sumX += x;/* Generated */ sumX2 += x*x;/* Generated */ y = inReal1[today++];/* Generated */ sumXY += x*y;/* Generated */ sumY += y;/* Generated */ sumY2 += y*y;/* Generated */ trailingX = inReal0[trailingIdx];/* Generated */ trailingY = inReal1[trailingIdx++];/* Generated */ tempReal = (sumX2-((sumX*sumX)/optInTimePeriod)) * (sumY2-((sumY*sumY)/optInTimePeriod));/* Generated */ if( !TA_IS_ZERO_OR_NEG(tempReal) )/* Generated */ outReal[outIdx++] = (sumXY-((sumX*sumY)/optInTimePeriod)) / std_sqrt(tempReal);/* Generated */ else/* Generated */ outReal[outIdx++] = 0.0;/* Generated */ } /* Generated */ VALUE_HANDLE_DEREF(outNBElement) = outIdx;/* Generated */ return ENUM_VALUE(RetCode,TA_SUCCESS,Success);/* Generated */ }/* Generated */ /* Generated */ #if defined( _MANAGED )/* Generated */ }}} // Close namespace TicTacTec.TA.Lib/* Generated */ #endif/**** END GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -