📄 ta_macd.c
字号:
/* 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. */ return FUNCTION_CALL(INT_MACD)( startIdx, endIdx, inReal, optInFastPeriod, optInSlowPeriod, optInSignalPeriod, outBegIdx, outNBElement, outMACD, outMACDSignal, outMACDHist );}#if defined( _MANAGED ) && defined( USE_SUBARRAY ) && !defined( USE_SINGLE_PRECISION_INPUT ) enum class Core::RetCode Core::TA_INT_MACD( int startIdx, int endIdx, SubArray^ inReal, int optInFastPeriod, /* 0 is fix 12 */ int optInSlowPeriod, /* 0 is fix 26 */ int optInSignalPeriod_2, [Out]int% outBegIdx, [Out]int% outNBElement, cli::array<double>^ outMACD, cli::array<double>^ outMACDSignal, cli::array<double>^ outMACDHist )#elif defined( _MANAGED ) enum class Core::RetCode Core::TA_INT_MACD( int startIdx, int endIdx, cli::array<INPUT_TYPE>^ inReal, int optInFastPeriod, /* 0 is fix 12 */ int optInSlowPeriod, /* 0 is fix 26 */ int optInSignalPeriod_2, [Out]int% outBegIdx, [Out]int% outNBElement, cli::array<double>^ outMACD, cli::array<double>^ outMACDSignal, cli::array<double>^ outMACDHist )#elif defined( _JAVA )RetCode TA_INT_MACD( int startIdx, int endIdx, INPUT_TYPE inReal[], int optInFastPeriod, /* 0 is fix 12 */ int optInSlowPeriod, /* 0 is fix 26 */ int optInSignalPeriod_2, MInteger outBegIdx, MInteger outNBElement, double outMACD[], double outMACDSignal[], double outMACDHist[] )#elseTA_RetCode TA_PREFIX(INT_MACD)( int startIdx, int endIdx, const INPUT_TYPE inReal[], int optInFastPeriod, /* 0 is fix 12 */ int optInSlowPeriod, /* 0 is fix 26 */ int optInSignalPeriod_2, int *outBegIdx, int *outNBElement, double outMACD[], double outMACDSignal[], double outMACDHist[] )#endif{ ARRAY_REF(slowEMABuffer); ARRAY_REF(fastEMABuffer); double k1, k2; ENUM_DECLARATION(RetCode) retCode; int tempInteger; VALUE_HANDLE_INT(outBegIdx1); VALUE_HANDLE_INT(outNbElement1); VALUE_HANDLE_INT(outBegIdx2); VALUE_HANDLE_INT(outNbElement2); int lookbackTotal, lookbackSignal; int i; /* !!! A lot of speed optimization could be done * !!! with this function. * !!! * !!! A better approach would be to use TA_INT_EMA * !!! just to get the seeding values for the * !!! fast and slow EMA. Then process the difference * !!! in an allocated buffer until enough data is * !!! available for the first signal value. * !!! From that point all the processing can * !!! be done in a tight loop. * !!! * !!! That approach will have the following * !!! advantage: * !!! 1) One mem allocation needed instead of two. * !!! 2) The mem allocation size will be only the * !!! signal lookback period instead of the * !!! whole range of data. * !!! 3) Processing will be done in a tight loop. * !!! allowing to avoid a lot of memory store-load * !!! operation. * !!! 4) The memcpy at the end will be eliminated! * !!! * !!! If only I had time.... */ /* Make sure slow is really slower than * the fast period! if not, swap... */ if( optInSlowPeriod < optInFastPeriod ) { /* swap */ tempInteger = optInSlowPeriod; optInSlowPeriod = optInFastPeriod; optInFastPeriod = tempInteger; } /* Catch special case for fix 26/12 MACD. */ if( optInSlowPeriod != 0 ) k1 = PER_TO_K(optInSlowPeriod); else { optInSlowPeriod = 26; k1 = (double)0.075; /* Fix 26 */ } if( optInFastPeriod != 0 ) k2 = PER_TO_K(optInFastPeriod); else { optInFastPeriod = 12; k2 = (double)0.15; /* Fix 12 */ } lookbackSignal = LOOKBACK_CALL(EMA)( optInSignalPeriod_2 ); /* Move up the start index if there is not * enough initial data. */ lookbackTotal = lookbackSignal; lookbackTotal += LOOKBACK_CALL(EMA)( optInSlowPeriod ); 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); } /* Allocate intermediate buffer for fast/slow EMA. */ tempInteger = (endIdx-startIdx)+1+lookbackSignal; ARRAY_ALLOC( fastEMABuffer, tempInteger ); #if !defined( _JAVA ) if( !fastEMABuffer ) { VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx); VALUE_HANDLE_DEREF_TO_ZERO(outNBElement); return ENUM_VALUE(RetCode,TA_ALLOC_ERR,AllocErr); } #endif ARRAY_ALLOC( slowEMABuffer, tempInteger ); #if !defined( _JAVA ) if( !slowEMABuffer ) { VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx); VALUE_HANDLE_DEREF_TO_ZERO(outNBElement); ARRAY_FREE( fastEMABuffer ); return ENUM_VALUE(RetCode,TA_ALLOC_ERR,AllocErr); } #endif /* Calculate the slow EMA. * * Move back the startIdx to get enough data * for the signal period. That way, once the * signal calculation is done, all the output * will start at the requested 'startIdx'. */ tempInteger = startIdx-lookbackSignal; retCode = FUNCTION_CALL(INT_EMA)( tempInteger, endIdx, inReal, optInSlowPeriod, k1, VALUE_HANDLE_OUT(outBegIdx1), VALUE_HANDLE_OUT(outNbElement1), slowEMABuffer ); if( retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) ) { VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx); VALUE_HANDLE_DEREF_TO_ZERO(outNBElement); ARRAY_FREE( fastEMABuffer ); ARRAY_FREE( slowEMABuffer ); return retCode; } /* Calculate the fast EMA. */ retCode = FUNCTION_CALL(INT_EMA)( tempInteger, endIdx, inReal, optInFastPeriod, k2, VALUE_HANDLE_OUT(outBegIdx2), VALUE_HANDLE_OUT(outNbElement2), fastEMABuffer ); if( retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) ) { VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx); VALUE_HANDLE_DEREF_TO_ZERO(outNBElement); ARRAY_FREE( fastEMABuffer ); ARRAY_FREE( slowEMABuffer ); return retCode; } /* Parano tests. Will be removed eventually. */ if( (VALUE_HANDLE_GET(outBegIdx1) != tempInteger) || (VALUE_HANDLE_GET(outBegIdx2) != tempInteger) || (VALUE_HANDLE_GET(outNbElement1) != VALUE_HANDLE_GET(outNbElement2)) || (VALUE_HANDLE_GET(outNbElement1) != (endIdx-startIdx)+1+lookbackSignal) ) { VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx); VALUE_HANDLE_DEREF_TO_ZERO(outNBElement); ARRAY_FREE( fastEMABuffer ); ARRAY_FREE( slowEMABuffer ); return TA_INTERNAL_ERROR(119); } /* Calculate (fast EMA) - (slow EMA). */ for( i=0; i < VALUE_HANDLE_GET(outNbElement1); i++ ) fastEMABuffer[i] = fastEMABuffer[i] - slowEMABuffer[i]; /* Copy the result into the output for the caller. */ ARRAY_MEMMOVE( outMACD, 0, fastEMABuffer, lookbackSignal, (endIdx-startIdx)+1 ); /* Calculate the signal/trigger line. */ retCode = FUNCTION_CALL_DOUBLE(INT_EMA)( 0, VALUE_HANDLE_GET(outNbElement1)-1, fastEMABuffer, optInSignalPeriod_2, PER_TO_K(optInSignalPeriod_2), VALUE_HANDLE_OUT(outBegIdx2), VALUE_HANDLE_OUT(outNbElement2), outMACDSignal ); ARRAY_FREE( fastEMABuffer ); ARRAY_FREE( slowEMABuffer ); if( retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) ) { VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx); VALUE_HANDLE_DEREF_TO_ZERO(outNBElement); return retCode; } /* Calculate the histogram. */ for( i=0; i < VALUE_HANDLE_GET(outNbElement2); i++ ) outMACDHist[i] = outMACD[i]-outMACDSignal[i]; /* All done! Indicate the output limits and return success. */ VALUE_HANDLE_DEREF(outBegIdx) = startIdx; VALUE_HANDLE_DEREF(outNBElement) = VALUE_HANDLE_GET(outNbElement2); return ENUM_VALUE(RetCode,TA_SUCCESS,Success);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -