📄 ta_stoch.c
字号:
/* Generated */ if(!inHigh||!inLow||!inClose)/* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);/* Generated */ /* Generated */ #endif /* !defined(_JAVA)*//* Generated */ /* min/max are checked for optInFastK_Period. *//* Generated */ if( (int)optInFastK_Period == TA_INTEGER_DEFAULT )/* Generated */ optInFastK_Period = 5;/* Generated */ else if( ((int)optInFastK_Period < 1) || ((int)optInFastK_Period > 100000) )/* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);/* Generated */ /* Generated */ /* min/max are checked for optInSlowK_Period. *//* Generated */ if( (int)optInSlowK_Period == TA_INTEGER_DEFAULT )/* Generated */ optInSlowK_Period = 3;/* Generated */ else if( ((int)optInSlowK_Period < 1) || ((int)optInSlowK_Period > 100000) )/* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);/* Generated */ /* Generated */ #if !defined(_MANAGED) && !defined(_JAVA)/* Generated */ if( (int)optInSlowK_MAType == TA_INTEGER_DEFAULT )/* Generated */ optInSlowK_MAType = (TA_MAType)0;/* Generated */ else if( ((int)optInSlowK_MAType < 0) || ((int)optInSlowK_MAType > 8) )/* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);/* Generated */ /* Generated */ #endif /* !defined(_MANAGED) && !defined(_JAVA)*//* Generated */ /* min/max are checked for optInSlowD_Period. *//* Generated */ if( (int)optInSlowD_Period == TA_INTEGER_DEFAULT )/* Generated */ optInSlowD_Period = 3;/* Generated */ else if( ((int)optInSlowD_Period < 1) || ((int)optInSlowD_Period > 100000) )/* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);/* Generated */ /* Generated */ #if !defined(_MANAGED) && !defined(_JAVA)/* Generated */ if( (int)optInSlowD_MAType == TA_INTEGER_DEFAULT )/* Generated */ optInSlowD_MAType = (TA_MAType)0;/* Generated */ else if( ((int)optInSlowD_MAType < 0) || ((int)optInSlowD_MAType > 8) )/* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);/* Generated */ /* Generated */ #endif /* !defined(_MANAGED) && !defined(_JAVA)*//* Generated */ #if !defined(_JAVA)/* Generated */ if( !outSlowK )/* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);/* Generated */ /* Generated */ if( !outSlowD )/* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);/* Generated */ /* Generated */ #endif /* !defined(_JAVA) *//* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK *//* Generated */ /**** END GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/ /* Insert TA function code here. */ /* With stochastic, there is a total of 4 different lines that * are defined: FASTK, FASTD, SLOWK and SLOWD. * * The D is the signal line usually drawn over its * corresponding K function. * * (Today's Close - LowestLow) * FASTK(Kperiod) = --------------------------- * 100 * (HighestHigh - LowestLow) * * FASTD(FastDperiod, MA type) = MA Smoothed FASTK over FastDperiod * * SLOWK(SlowKperiod, MA type) = MA Smoothed FASTK over SlowKperiod * * SLOWD(SlowDperiod, MA Type) = MA Smoothed SLOWK over SlowDperiod * * The HighestHigh and LowestLow are the extreme values among the * last 'Kperiod'. * * SLOWK and FASTD are equivalent when using the same period. * * The following shows how these four lines are made available in TA-LIB: * * TA_STOCH : Returns the SLOWK and SLOWD * TA_STOCHF : Returns the FASTK and FASTD * * The TA_STOCH function correspond to the more widely implemented version * found in many software/charting package. The TA_STOCHF is more rarely * used because its higher volatility cause often whipsaws. */ /* Identify the lookback needed. */ lookbackK = optInFastK_Period-1; lookbackKSlow = LOOKBACK_CALL(MA)( optInSlowK_Period, optInSlowK_MAType ); lookbackDSlow = LOOKBACK_CALL(MA)( optInSlowD_Period, optInSlowD_MAType ); lookbackTotal = lookbackK + lookbackDSlow + lookbackKSlow; /* Move up the start index if there is not * enough initial data. */ if( startIdx < lookbackTotal ) startIdx = lookbackTotal; /* Make sure there is still something to evaluate. */ if( startIdx > endIdx ) { /* Succeed... but no data in the output. */ VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx); VALUE_HANDLE_DEREF_TO_ZERO(outNBElement); return ENUM_VALUE(RetCode,TA_SUCCESS,Success); } /* Do the K calculation: * * Kt = 100 x ((Ct-Lt)/(Ht-Lt)) * * Kt is today stochastic * Ct is today closing price. * Lt is the lowest price of the last K Period (including today) * Ht is the highest price of the last K Period (including today) */ /* Proceed with the calculation for the requested range. * Note that this algorithm allows the input and * output to be the same buffer. */ outIdx = 0; /* Calculate just enough K for ending up with the caller * requested range. (The range of k must consider all * the lookback involve with the smoothing). */ trailingIdx = startIdx-lookbackTotal; today = trailingIdx+lookbackK; lowestIdx = highestIdx = -1; diff = highest = lowest = 0.0; /* Allocate a temporary buffer large enough to * store the K. * * If the output is the same as the input, great * we just save ourself one memory allocation. */ #if !defined( _MANAGED ) && !defined(USE_SINGLE_PRECISION_INPUT) && !defined( _JAVA ) bufferIsAllocated = 0; #endif #if defined(USE_SINGLE_PRECISION_INPUT) || defined( USE_SUBARRAY ) /* Always alloc, since output is of different type and * its allocated size is not guarantee to be as large as * the input. */ ARRAY_ALLOC( tempBuffer, endIdx-today+1 ); #else if( (outSlowK == inHigh) || (outSlowK == inLow) || (outSlowK == inClose) ) { tempBuffer = outSlowK; } else if( (outSlowD == inHigh) || (outSlowD == inLow) || (outSlowD == inClose) ) { tempBuffer = outSlowD; } else { #if !defined( _MANAGED ) && !defined(_JAVA) bufferIsAllocated = 1; #endif ARRAY_ALLOC( tempBuffer, endIdx-today+1 ); } #endif /* Do the K calculation */ while( today <= endIdx ) { /* Set the lowest low */ tmp = inLow[today]; if( lowestIdx < trailingIdx ) { lowestIdx = trailingIdx; lowest = inLow[lowestIdx]; i = lowestIdx; while( ++i<=today ) { tmp = inLow[i]; if( tmp < lowest ) { lowestIdx = i; lowest = tmp; } } diff = (highest - lowest)/100.0; } else if( tmp <= lowest ) { lowestIdx = today; lowest = tmp; diff = (highest - lowest)/100.0; } /* Set the highest high */ tmp = inHigh[today]; if( highestIdx < trailingIdx ) { highestIdx = trailingIdx; highest = inHigh[highestIdx]; i = highestIdx; while( ++i<=today ) { tmp = inHigh[i]; if( tmp > highest ) { highestIdx = i; highest = tmp; } } diff = (highest - lowest)/100.0; } else if( tmp >= highest ) { highestIdx = today; highest = tmp; diff = (highest - lowest)/100.0; } /* Calculate stochastic. */ if( diff != 0.0 ) tempBuffer[outIdx++] = (inClose[today]-lowest)/diff; else tempBuffer[outIdx++] = 0.0; trailingIdx++; today++; } /* Un-smoothed K calculation completed. This K calculation is not returned * to the caller. It is always smoothed and then return. * Some documentation will refer to the smoothed version as being * "K-Slow", but often this end up to be shorten to "K". */ retCode = FUNCTION_CALL_DOUBLE(MA)( 0, outIdx-1, tempBuffer, optInSlowK_Period, optInSlowK_MAType, outBegIdx, outNBElement, tempBuffer ); if( (retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) ) || ((int)VALUE_HANDLE_DEREF(outNBElement) == 0) ) { #if defined(USE_SINGLE_PRECISION_INPUT) ARRAY_FREE( tempBuffer ); #else ARRAY_FREE_COND( bufferIsAllocated, tempBuffer ); #endif /* Something wrong happen? No further data? */ VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx); VALUE_HANDLE_DEREF_TO_ZERO(outNBElement); return retCode; } /* Calculate the %D which is simply a moving average of * the already smoothed %K. */ retCode = FUNCTION_CALL_DOUBLE(MA)( 0, (int)VALUE_HANDLE_DEREF(outNBElement)-1, tempBuffer, optInSlowD_Period, optInSlowD_MAType, outBegIdx, outNBElement, outSlowD ); /* Copy tempBuffer into the caller buffer. * (Calculation could not be done directly in the * caller buffer because more input data then the * requested range was needed for doing %D). */ ARRAY_MEMMOVE( outSlowK, 0, tempBuffer,lookbackDSlow,(int)VALUE_HANDLE_DEREF(outNBElement)); /* Don't need K anymore, free it if it was allocated here. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -