/* TA-LIB Copyright (c) 1999-2026, Mario Fortier * All rights reserved. * * Redistribution and use in source and binary forms, with or * without modification, are permitted provided that the following * conditions are met: * * - Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * * - Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. * * - Neither name of author nor the names of its contributors * may be used to endorse or promote products derived from this * software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ /* AUTO-GENERATED by ta_codegen — DO NOT EDIT. * Source of truth: ta_codegen/input// (regenerate: cd ta_codegen/generator && cargo run -- generate) */ #include #include #include "ta_func.h" #include "ta_utility.h" #include "ta_memory.h" #include "ta_func_stream_private.h" /* List of contributors: * * Initial Name/description * ------------------------------------------------------------------- * AC Angelo Ciceri * MF Mario Fortier * CC Claude Code (AI assistant) * * * Change history: * * MMDDYY BY Description * ------------------------------------------------------------------- * 071804 AC Creation * 100426 MF,CC Remove the outInteger comment, which misstated the values */ #if defined(__GNUC__) && !defined(__clang__) #pragma GCC optimize("no-split-paths") #endif TA_LIB_API int TA_CDLSPINNINGTOP_Lookback( void ) { int BodyShort_avgPeriod = TA_Globals->candleSettings[TA_BodyShort].avgPeriod; return BodyShort_avgPeriod; } TA_LIB_API int TA_CDLSPINNINGTOP_DisplayShift( int outputIdx ) { if( outputIdx < 0 || outputIdx >= 1 ) return INT_MIN; return 0; } TA_LIB_API TA_RetCode TA_CDLSPINNINGTOP( int startIdx, int endIdx, const double inOpen[], const double inHigh[], const double inLow[], const double inClose[], int *outBegIdx, int *outNBElement, int outInteger[] ) { double BodyPeriodTotal; int i; int outIdx; int BodyTrailingIdx; int lookbackTotal; int BodyShort_rangeType = TA_Globals->candleSettings[TA_BodyShort].rangeType; int BodyShort_avgPeriod = TA_Globals->candleSettings[TA_BodyShort].avgPeriod; double BodyShort_factor = TA_Globals->candleSettings[TA_BodyShort].factor; if( (startIdx < 0) || (startIdx > TA_INDEX_MAX) ) return TA_OUT_OF_RANGE_START_INDEX; if( (endIdx < 0) || (endIdx > TA_INDEX_MAX) || (endIdx < startIdx) ) return TA_OUT_OF_RANGE_END_INDEX; if( !inOpen ) return TA_BAD_PARAM; if( !inHigh ) return TA_BAD_PARAM; if( !inLow ) return TA_BAD_PARAM; if( !inClose ) return TA_BAD_PARAM; if( !outBegIdx || !outNBElement ) return TA_BAD_PARAM; if( !outInteger ) return TA_BAD_PARAM; /* Identify the minimum number of price bar needed * to calculate at least one output. */ lookbackTotal = TA_CDLSPINNINGTOP_Lookback(); /* 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 ) { *outBegIdx= 0; *outNBElement= 0; return TA_SUCCESS; } if( BodyShort_rangeType == TA_RangeType_RealBody ) { /* Do the calculation using tight loops. */ /* Add-up the initial period, except for the last value. */ BodyPeriodTotal = 0; BodyTrailingIdx = startIdx - BodyShort_avgPeriod; i = BodyTrailingIdx; while( i < startIdx ) { BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_RealBody,i); i += 1; } /* Proceed with the calculation for the requested range. * Must have: * - small real body * - shadows longer than the real body * The meaning of "short" is specified with TA_SetCandleSettings */ outIdx = 0; do { outInteger[outIdx++] = ((((inClose[i] >= inOpen[i]) ? 1 : -1) * 100) & -(int)((((inHigh[i] - TA_COLOR_MAX(inClose[i], inOpen[i])) > fabs(inClose[i] - inOpen[i])) & ((TA_COLOR_MIN(inClose[i], inOpen[i]) - inLow[i]) > fabs(inClose[i] - inOpen[i])) & (fabs(inClose[i] - inOpen[i]) < TA_CANDLEAVERAGE_OF(TA_RangeType_RealBody,BodyShort_avgPeriod,BodyShort_factor,BodyPeriodTotal,i))))); /* add the current range and subtract the first range: this is done after the pattern recognition * when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle) */ BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_RealBody,i) - TA_CANDLERANGE_OF(TA_RangeType_RealBody,BodyTrailingIdx); i += 1; BodyTrailingIdx += 1; } while( i <= endIdx ); /* All done. Indicate the output limits and return. */ *outNBElement= outIdx; *outBegIdx= startIdx; return TA_SUCCESS; } else if( BodyShort_rangeType == TA_RangeType_HighLow ) { /* Do the calculation using tight loops. */ /* Add-up the initial period, except for the last value. */ BodyPeriodTotal = 0; BodyTrailingIdx = startIdx - BodyShort_avgPeriod; i = BodyTrailingIdx; while( i < startIdx ) { BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_HighLow,i); i += 1; } /* Proceed with the calculation for the requested range. * Must have: * - small real body * - shadows longer than the real body * The meaning of "short" is specified with TA_SetCandleSettings */ outIdx = 0; do { outInteger[outIdx++] = ((((inClose[i] >= inOpen[i]) ? 1 : -1) * 100) & -(int)((((inHigh[i] - TA_COLOR_MAX(inClose[i], inOpen[i])) > fabs(inClose[i] - inOpen[i])) & ((TA_COLOR_MIN(inClose[i], inOpen[i]) - inLow[i]) > fabs(inClose[i] - inOpen[i])) & (fabs(inClose[i] - inOpen[i]) < TA_CANDLEAVERAGE_OF(TA_RangeType_HighLow,BodyShort_avgPeriod,BodyShort_factor,BodyPeriodTotal,i))))); /* add the current range and subtract the first range: this is done after the pattern recognition * when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle) */ BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_HighLow,i) - TA_CANDLERANGE_OF(TA_RangeType_HighLow,BodyTrailingIdx); i += 1; BodyTrailingIdx += 1; } while( i <= endIdx ); /* All done. Indicate the output limits and return. */ *outNBElement= outIdx; *outBegIdx= startIdx; return TA_SUCCESS; } else if( BodyShort_rangeType == TA_RangeType_Shadows ) { /* Do the calculation using tight loops. */ /* Add-up the initial period, except for the last value. */ BodyPeriodTotal = 0; BodyTrailingIdx = startIdx - BodyShort_avgPeriod; i = BodyTrailingIdx; while( i < startIdx ) { BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_Shadows,i); i += 1; } /* Proceed with the calculation for the requested range. * Must have: * - small real body * - shadows longer than the real body * The meaning of "short" is specified with TA_SetCandleSettings */ outIdx = 0; do { outInteger[outIdx++] = ((((inClose[i] >= inOpen[i]) ? 1 : -1) * 100) & -(int)((((inHigh[i] - TA_COLOR_MAX(inClose[i], inOpen[i])) > fabs(inClose[i] - inOpen[i])) & ((TA_COLOR_MIN(inClose[i], inOpen[i]) - inLow[i]) > fabs(inClose[i] - inOpen[i])) & (fabs(inClose[i] - inOpen[i]) < TA_CANDLEAVERAGE_OF(TA_RangeType_Shadows,BodyShort_avgPeriod,BodyShort_factor,BodyPeriodTotal,i))))); /* add the current range and subtract the first range: this is done after the pattern recognition * when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle) */ BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_Shadows,i) - TA_CANDLERANGE_OF(TA_RangeType_Shadows,BodyTrailingIdx); i += 1; BodyTrailingIdx += 1; } while( i <= endIdx ); /* All done. Indicate the output limits and return. */ *outNBElement= outIdx; *outBegIdx= startIdx; return TA_SUCCESS; } else { /* Do the calculation using tight loops. */ /* Add-up the initial period, except for the last value. */ BodyPeriodTotal = 0; BodyTrailingIdx = startIdx - BodyShort_avgPeriod; i = BodyTrailingIdx; while( i < startIdx ) { BodyPeriodTotal += TA_CANDLERANGE_OF(BodyShort_rangeType,i); i += 1; } /* Proceed with the calculation for the requested range. * Must have: * - small real body * - shadows longer than the real body * The meaning of "short" is specified with TA_SetCandleSettings */ outIdx = 0; do { outInteger[outIdx++] = ((((inClose[i] >= inOpen[i]) ? 1 : -1) * 100) & -(int)((((inHigh[i] - TA_COLOR_MAX(inClose[i], inOpen[i])) > fabs(inClose[i] - inOpen[i])) & ((TA_COLOR_MIN(inClose[i], inOpen[i]) - inLow[i]) > fabs(inClose[i] - inOpen[i])) & (fabs(inClose[i] - inOpen[i]) < TA_CANDLEAVERAGE_OF(BodyShort_rangeType,BodyShort_avgPeriod,BodyShort_factor,BodyPeriodTotal,i))))); /* add the current range and subtract the first range: this is done after the pattern recognition * when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle) */ BodyPeriodTotal += TA_CANDLERANGE_OF(BodyShort_rangeType,i) - TA_CANDLERANGE_OF(BodyShort_rangeType,BodyTrailingIdx); i += 1; BodyTrailingIdx += 1; } while( i <= endIdx ); /* All done. Indicate the output limits and return. */ *outNBElement= outIdx; *outBegIdx= startIdx; return TA_SUCCESS; } } TA_RetCode TA_S_CDLSPINNINGTOP( int startIdx, int endIdx, const float inOpen[], const float inHigh[], const float inLow[], const float inClose[], int *outBegIdx, int *outNBElement, int outInteger[] ) { double BodyPeriodTotal; int i; int outIdx; int BodyTrailingIdx; int lookbackTotal; int BodyShort_rangeType = TA_Globals->candleSettings[TA_BodyShort].rangeType; int BodyShort_avgPeriod = TA_Globals->candleSettings[TA_BodyShort].avgPeriod; double BodyShort_factor = TA_Globals->candleSettings[TA_BodyShort].factor; if( (startIdx < 0) || (startIdx > TA_INDEX_MAX) ) return TA_OUT_OF_RANGE_START_INDEX; if( (endIdx < 0) || (endIdx > TA_INDEX_MAX) || (endIdx < startIdx) ) return TA_OUT_OF_RANGE_END_INDEX; if( !inOpen ) return TA_BAD_PARAM; if( !inHigh ) return TA_BAD_PARAM; if( !inLow ) return TA_BAD_PARAM; if( !inClose ) return TA_BAD_PARAM; if( !outBegIdx || !outNBElement ) return TA_BAD_PARAM; if( !outInteger ) return TA_BAD_PARAM; lookbackTotal = TA_CDLSPINNINGTOP_Lookback(); if( startIdx < lookbackTotal ) { startIdx = lookbackTotal; } if( startIdx > endIdx ) { *outBegIdx= 0; *outNBElement= 0; return TA_SUCCESS; } if( BodyShort_rangeType == TA_RangeType_RealBody ) { BodyPeriodTotal = 0; BodyTrailingIdx = startIdx - BodyShort_avgPeriod; i = BodyTrailingIdx; while( i < startIdx ) { BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_RealBody,i); i += 1; } outIdx = 0; do { outInteger[outIdx++] = (((((double)inClose[i] >= (double)inOpen[i]) ? 1 : -1) * 100) & -(int)(((((double)inHigh[i] - TA_COLOR_MAX((double)inClose[i], (double)inOpen[i])) > fabs((double)inClose[i] - (double)inOpen[i])) & ((TA_COLOR_MIN((double)inClose[i], (double)inOpen[i]) - (double)inLow[i]) > fabs((double)inClose[i] - (double)inOpen[i])) & (fabs((double)inClose[i] - (double)inOpen[i]) < TA_CANDLEAVERAGE_OF(TA_RangeType_RealBody,BodyShort_avgPeriod,BodyShort_factor,BodyPeriodTotal,i))))); BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_RealBody,i) - TA_CANDLERANGE_OF(TA_RangeType_RealBody,BodyTrailingIdx); i += 1; BodyTrailingIdx += 1; } while( i <= endIdx ); *outNBElement= outIdx; *outBegIdx= startIdx; return TA_SUCCESS; } else if( BodyShort_rangeType == TA_RangeType_HighLow ) { BodyPeriodTotal = 0; BodyTrailingIdx = startIdx - BodyShort_avgPeriod; i = BodyTrailingIdx; while( i < startIdx ) { BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_HighLow,i); i += 1; } outIdx = 0; do { outInteger[outIdx++] = (((((double)inClose[i] >= (double)inOpen[i]) ? 1 : -1) * 100) & -(int)(((((double)inHigh[i] - TA_COLOR_MAX((double)inClose[i], (double)inOpen[i])) > fabs((double)inClose[i] - (double)inOpen[i])) & ((TA_COLOR_MIN((double)inClose[i], (double)inOpen[i]) - (double)inLow[i]) > fabs((double)inClose[i] - (double)inOpen[i])) & (fabs((double)inClose[i] - (double)inOpen[i]) < TA_CANDLEAVERAGE_OF(TA_RangeType_HighLow,BodyShort_avgPeriod,BodyShort_factor,BodyPeriodTotal,i))))); BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_HighLow,i) - TA_CANDLERANGE_OF(TA_RangeType_HighLow,BodyTrailingIdx); i += 1; BodyTrailingIdx += 1; } while( i <= endIdx ); *outNBElement= outIdx; *outBegIdx= startIdx; return TA_SUCCESS; } else if( BodyShort_rangeType == TA_RangeType_Shadows ) { BodyPeriodTotal = 0; BodyTrailingIdx = startIdx - BodyShort_avgPeriod; i = BodyTrailingIdx; while( i < startIdx ) { BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_Shadows,i); i += 1; } outIdx = 0; do { outInteger[outIdx++] = (((((double)inClose[i] >= (double)inOpen[i]) ? 1 : -1) * 100) & -(int)(((((double)inHigh[i] - TA_COLOR_MAX((double)inClose[i], (double)inOpen[i])) > fabs((double)inClose[i] - (double)inOpen[i])) & ((TA_COLOR_MIN((double)inClose[i], (double)inOpen[i]) - (double)inLow[i]) > fabs((double)inClose[i] - (double)inOpen[i])) & (fabs((double)inClose[i] - (double)inOpen[i]) < TA_CANDLEAVERAGE_OF(TA_RangeType_Shadows,BodyShort_avgPeriod,BodyShort_factor,BodyPeriodTotal,i))))); BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_Shadows,i) - TA_CANDLERANGE_OF(TA_RangeType_Shadows,BodyTrailingIdx); i += 1; BodyTrailingIdx += 1; } while( i <= endIdx ); *outNBElement= outIdx; *outBegIdx= startIdx; return TA_SUCCESS; } else { BodyPeriodTotal = 0; BodyTrailingIdx = startIdx - BodyShort_avgPeriod; i = BodyTrailingIdx; while( i < startIdx ) { BodyPeriodTotal += TA_CANDLERANGE_OF(BodyShort_rangeType,i); i += 1; } outIdx = 0; do { outInteger[outIdx++] = (((((double)inClose[i] >= (double)inOpen[i]) ? 1 : -1) * 100) & -(int)(((((double)inHigh[i] - TA_COLOR_MAX((double)inClose[i], (double)inOpen[i])) > fabs((double)inClose[i] - (double)inOpen[i])) & ((TA_COLOR_MIN((double)inClose[i], (double)inOpen[i]) - (double)inLow[i]) > fabs((double)inClose[i] - (double)inOpen[i])) & (fabs((double)inClose[i] - (double)inOpen[i]) < TA_CANDLEAVERAGE_OF(BodyShort_rangeType,BodyShort_avgPeriod,BodyShort_factor,BodyPeriodTotal,i))))); BodyPeriodTotal += TA_CANDLERANGE_OF(BodyShort_rangeType,i) - TA_CANDLERANGE_OF(BodyShort_rangeType,BodyTrailingIdx); i += 1; BodyTrailingIdx += 1; } while( i <= endIdx ); *outNBElement= outIdx; *outBegIdx= startIdx; return TA_SUCCESS; } } /**** Streaming API *****/ struct TA_CDLSPINNINGTOP_Stream { /* The bars this handle has an output for (see TA_CDLSPINNINGTOP_OutRange). */ int outRangeBegIdx; int outRangeCount; /* The value(s) at the last bar the stream counted (see TA_CDLSPINNINGTOP_Value). */ int cur_outInteger; double BodyPeriodTotal; int ringPos_BodyTrailingIdx; int ringCap_BodyTrailingIdx; double *ring_BodyTrailingIdx_derived; }; /* Private function, not in public API. */ static void TA_CDLSPINNINGTOP_ReleaseImpl( struct TA_CDLSPINNINGTOP_Stream *sp ) { if( !sp ) return; if( sp->ring_BodyTrailingIdx_derived ) TA_Free( sp->ring_BodyTrailingIdx_derived ); TA_Free( sp ); } /* Private function, not in public API. */ static void TA_CDLSPINNINGTOP_StepImpl( struct TA_CDLSPINNINGTOP_Stream *sp, double inOpen, double inHigh, double inLow, double inClose, int *outInteger ) { int ringPosL_BodyTrailingIdx; if( sp->ringCap_BodyTrailingIdx == 0 ) { sp->ring_BodyTrailingIdx_derived[0] = TA_STREAM_CANDLERANGE(BodyShort,inOpen,inHigh,inLow,inClose); } if( (inHigh - TA_COLOR_MAX(inClose, inOpen)) > fabs(inClose - inOpen) && (TA_COLOR_MIN(inClose, inOpen) - inLow) > fabs(inClose - inOpen) && fabs(inClose - inOpen) < TA_STREAM_CANDLEAVERAGE(BodyShort,sp->BodyPeriodTotal,inOpen,inHigh,inLow,inClose) ) { *outInteger= ((inClose >= inOpen) ? 1 : -1) * 100; } else { *outInteger= 0; } /* add the current range and subtract the first range: this is done after the pattern recognition * when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle) */ sp->BodyPeriodTotal += TA_STREAM_CANDLERANGE(BodyShort,inOpen,inHigh,inLow,inClose) - sp->ring_BodyTrailingIdx_derived[sp->ringPos_BodyTrailingIdx]; sp->cur_outInteger = *outInteger; ringPosL_BodyTrailingIdx = sp->ringPos_BodyTrailingIdx; sp->ring_BodyTrailingIdx_derived[ringPosL_BodyTrailingIdx] = TA_STREAM_CANDLERANGE(BodyShort,inOpen,inHigh,inLow,inClose); ringPosL_BodyTrailingIdx = ringPosL_BodyTrailingIdx + 1; if( ringPosL_BodyTrailingIdx >= sp->ringCap_BodyTrailingIdx ) { ringPosL_BodyTrailingIdx = 0; } sp->ringPos_BodyTrailingIdx = ringPosL_BodyTrailingIdx; } static TA_OPEN_INLINE TA_RetCode TA_CDLSPINNINGTOP_OpenBody( struct TA_CDLSPINNINGTOP_Stream **stream, const double inOpen[], const double inHigh[], const double inLow[], const double inClose[], int startIdx, int historyLen, int *outBegIdx, int *outNBElement, int outInteger[], int outStride ) { struct TA_CDLSPINNINGTOP_Stream *sp; int endIdx; endIdx = historyLen - 1; { int BodyShort_rangeType = TA_Globals->candleSettings[TA_BodyShort].rangeType; int BodyShort_avgPeriod = TA_Globals->candleSettings[TA_BodyShort].avgPeriod; double BodyShort_factor = TA_Globals->candleSettings[TA_BodyShort].factor; double BodyPeriodTotal = 0.0; int i; int outIdx; int BodyTrailingIdx; int lookbackTotal; /* Identify the minimum number of price bar needed * to calculate at least one output. */ lookbackTotal = TA_CDLSPINNINGTOP_Lookback(); /* 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 ) { *outBegIdx= 0; *outNBElement= 0; return TA_INSUFFICIENT_HISTORY; } if( BodyShort_rangeType == TA_RangeType_RealBody ) { /* Do the calculation using tight loops. */ /* Add-up the initial period, except for the last value. */ BodyPeriodTotal = 0; BodyTrailingIdx = startIdx - BodyShort_avgPeriod; i = BodyTrailingIdx; while( i < startIdx ) { BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_RealBody,i); i += 1; } /* Proceed with the calculation for the requested range. * Must have: * - small real body * - shadows longer than the real body * The meaning of "short" is specified with TA_SetCandleSettings */ outIdx = 0; do { outInteger[outIdx++ * outStride] = ((((inClose[i] >= inOpen[i]) ? 1 : -1) * 100) & -(int)((((inHigh[i] - TA_COLOR_MAX(inClose[i], inOpen[i])) > fabs(inClose[i] - inOpen[i])) & ((TA_COLOR_MIN(inClose[i], inOpen[i]) - inLow[i]) > fabs(inClose[i] - inOpen[i])) & (fabs(inClose[i] - inOpen[i]) < TA_CANDLEAVERAGE_OF(TA_RangeType_RealBody,BodyShort_avgPeriod,BodyShort_factor,BodyPeriodTotal,i))))); /* add the current range and subtract the first range: this is done after the pattern recognition * when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle) */ BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_RealBody,i) - TA_CANDLERANGE_OF(TA_RangeType_RealBody,BodyTrailingIdx); i += 1; BodyTrailingIdx += 1; } while( i <= endIdx ); /* All done. Indicate the output limits and return. */ *outNBElement= outIdx; *outBegIdx= startIdx; /* Capture the live batch state into the handle. */ sp = (struct TA_CDLSPINNINGTOP_Stream *)TA_Malloc( sizeof(*sp) ); if( !sp ) { return TA_ALLOC_ERR; } memset( sp, 0, sizeof(*sp) ); sp->BodyPeriodTotal = BodyPeriodTotal; sp->ringCap_BodyTrailingIdx = (int)(i - BodyTrailingIdx); if( sp->ringCap_BodyTrailingIdx < 0 || sp->ringCap_BodyTrailingIdx > historyLen ) { TA_CDLSPINNINGTOP_ReleaseImpl( sp ); return TA_INTERNAL_ERROR(297); } { size_t allocN = (size_t)(sp->ringCap_BodyTrailingIdx > 0 ? sp->ringCap_BodyTrailingIdx : 1); sp->ring_BodyTrailingIdx_derived = (double *)TA_Malloc( sizeof(double) * allocN ); if( !sp->ring_BodyTrailingIdx_derived ) { TA_CDLSPINNINGTOP_ReleaseImpl( sp ); return TA_ALLOC_ERR; } { int fillJ; for( fillJ = historyLen - sp->ringCap_BodyTrailingIdx; fillJ < historyLen; fillJ++ ) sp->ring_BodyTrailingIdx_derived[fillJ - (historyLen - sp->ringCap_BodyTrailingIdx)] = TA_STREAM_CANDLERANGE(BodyShort,inOpen[fillJ],inHigh[fillJ],inLow[fillJ],inClose[fillJ]); } } sp->ringPos_BodyTrailingIdx = 0; sp->outRangeBegIdx = *outBegIdx; sp->outRangeCount = *outNBElement; sp->cur_outInteger = outInteger[(*outNBElement - 1) * outStride]; *stream = sp; return TA_SUCCESS; } else if( BodyShort_rangeType == TA_RangeType_HighLow ) { /* Do the calculation using tight loops. */ /* Add-up the initial period, except for the last value. */ BodyPeriodTotal = 0; BodyTrailingIdx = startIdx - BodyShort_avgPeriod; i = BodyTrailingIdx; while( i < startIdx ) { BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_HighLow,i); i += 1; } /* Proceed with the calculation for the requested range. * Must have: * - small real body * - shadows longer than the real body * The meaning of "short" is specified with TA_SetCandleSettings */ outIdx = 0; do { outInteger[outIdx++ * outStride] = ((((inClose[i] >= inOpen[i]) ? 1 : -1) * 100) & -(int)((((inHigh[i] - TA_COLOR_MAX(inClose[i], inOpen[i])) > fabs(inClose[i] - inOpen[i])) & ((TA_COLOR_MIN(inClose[i], inOpen[i]) - inLow[i]) > fabs(inClose[i] - inOpen[i])) & (fabs(inClose[i] - inOpen[i]) < TA_CANDLEAVERAGE_OF(TA_RangeType_HighLow,BodyShort_avgPeriod,BodyShort_factor,BodyPeriodTotal,i))))); /* add the current range and subtract the first range: this is done after the pattern recognition * when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle) */ BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_HighLow,i) - TA_CANDLERANGE_OF(TA_RangeType_HighLow,BodyTrailingIdx); i += 1; BodyTrailingIdx += 1; } while( i <= endIdx ); /* All done. Indicate the output limits and return. */ *outNBElement= outIdx; *outBegIdx= startIdx; /* Capture the live batch state into the handle. */ sp = (struct TA_CDLSPINNINGTOP_Stream *)TA_Malloc( sizeof(*sp) ); if( !sp ) { return TA_ALLOC_ERR; } memset( sp, 0, sizeof(*sp) ); sp->BodyPeriodTotal = BodyPeriodTotal; sp->ringCap_BodyTrailingIdx = (int)(i - BodyTrailingIdx); if( sp->ringCap_BodyTrailingIdx < 0 || sp->ringCap_BodyTrailingIdx > historyLen ) { TA_CDLSPINNINGTOP_ReleaseImpl( sp ); return TA_INTERNAL_ERROR(297); } { size_t allocN = (size_t)(sp->ringCap_BodyTrailingIdx > 0 ? sp->ringCap_BodyTrailingIdx : 1); sp->ring_BodyTrailingIdx_derived = (double *)TA_Malloc( sizeof(double) * allocN ); if( !sp->ring_BodyTrailingIdx_derived ) { TA_CDLSPINNINGTOP_ReleaseImpl( sp ); return TA_ALLOC_ERR; } { int fillJ; for( fillJ = historyLen - sp->ringCap_BodyTrailingIdx; fillJ < historyLen; fillJ++ ) sp->ring_BodyTrailingIdx_derived[fillJ - (historyLen - sp->ringCap_BodyTrailingIdx)] = TA_STREAM_CANDLERANGE(BodyShort,inOpen[fillJ],inHigh[fillJ],inLow[fillJ],inClose[fillJ]); } } sp->ringPos_BodyTrailingIdx = 0; sp->outRangeBegIdx = *outBegIdx; sp->outRangeCount = *outNBElement; sp->cur_outInteger = outInteger[(*outNBElement - 1) * outStride]; *stream = sp; return TA_SUCCESS; } else if( BodyShort_rangeType == TA_RangeType_Shadows ) { /* Do the calculation using tight loops. */ /* Add-up the initial period, except for the last value. */ BodyPeriodTotal = 0; BodyTrailingIdx = startIdx - BodyShort_avgPeriod; i = BodyTrailingIdx; while( i < startIdx ) { BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_Shadows,i); i += 1; } /* Proceed with the calculation for the requested range. * Must have: * - small real body * - shadows longer than the real body * The meaning of "short" is specified with TA_SetCandleSettings */ outIdx = 0; do { outInteger[outIdx++ * outStride] = ((((inClose[i] >= inOpen[i]) ? 1 : -1) * 100) & -(int)((((inHigh[i] - TA_COLOR_MAX(inClose[i], inOpen[i])) > fabs(inClose[i] - inOpen[i])) & ((TA_COLOR_MIN(inClose[i], inOpen[i]) - inLow[i]) > fabs(inClose[i] - inOpen[i])) & (fabs(inClose[i] - inOpen[i]) < TA_CANDLEAVERAGE_OF(TA_RangeType_Shadows,BodyShort_avgPeriod,BodyShort_factor,BodyPeriodTotal,i))))); /* add the current range and subtract the first range: this is done after the pattern recognition * when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle) */ BodyPeriodTotal += TA_CANDLERANGE_OF(TA_RangeType_Shadows,i) - TA_CANDLERANGE_OF(TA_RangeType_Shadows,BodyTrailingIdx); i += 1; BodyTrailingIdx += 1; } while( i <= endIdx ); /* All done. Indicate the output limits and return. */ *outNBElement= outIdx; *outBegIdx= startIdx; /* Capture the live batch state into the handle. */ sp = (struct TA_CDLSPINNINGTOP_Stream *)TA_Malloc( sizeof(*sp) ); if( !sp ) { return TA_ALLOC_ERR; } memset( sp, 0, sizeof(*sp) ); sp->BodyPeriodTotal = BodyPeriodTotal; sp->ringCap_BodyTrailingIdx = (int)(i - BodyTrailingIdx); if( sp->ringCap_BodyTrailingIdx < 0 || sp->ringCap_BodyTrailingIdx > historyLen ) { TA_CDLSPINNINGTOP_ReleaseImpl( sp ); return TA_INTERNAL_ERROR(297); } { size_t allocN = (size_t)(sp->ringCap_BodyTrailingIdx > 0 ? sp->ringCap_BodyTrailingIdx : 1); sp->ring_BodyTrailingIdx_derived = (double *)TA_Malloc( sizeof(double) * allocN ); if( !sp->ring_BodyTrailingIdx_derived ) { TA_CDLSPINNINGTOP_ReleaseImpl( sp ); return TA_ALLOC_ERR; } { int fillJ; for( fillJ = historyLen - sp->ringCap_BodyTrailingIdx; fillJ < historyLen; fillJ++ ) sp->ring_BodyTrailingIdx_derived[fillJ - (historyLen - sp->ringCap_BodyTrailingIdx)] = TA_STREAM_CANDLERANGE(BodyShort,inOpen[fillJ],inHigh[fillJ],inLow[fillJ],inClose[fillJ]); } } sp->ringPos_BodyTrailingIdx = 0; sp->outRangeBegIdx = *outBegIdx; sp->outRangeCount = *outNBElement; sp->cur_outInteger = outInteger[(*outNBElement - 1) * outStride]; *stream = sp; return TA_SUCCESS; } else { /* Do the calculation using tight loops. */ /* Add-up the initial period, except for the last value. */ BodyPeriodTotal = 0; BodyTrailingIdx = startIdx - BodyShort_avgPeriod; i = BodyTrailingIdx; while( i < startIdx ) { BodyPeriodTotal += TA_CANDLERANGE_OF(BodyShort_rangeType,i); i += 1; } /* Proceed with the calculation for the requested range. * Must have: * - small real body * - shadows longer than the real body * The meaning of "short" is specified with TA_SetCandleSettings */ outIdx = 0; do { outInteger[outIdx++ * outStride] = ((((inClose[i] >= inOpen[i]) ? 1 : -1) * 100) & -(int)((((inHigh[i] - TA_COLOR_MAX(inClose[i], inOpen[i])) > fabs(inClose[i] - inOpen[i])) & ((TA_COLOR_MIN(inClose[i], inOpen[i]) - inLow[i]) > fabs(inClose[i] - inOpen[i])) & (fabs(inClose[i] - inOpen[i]) < TA_CANDLEAVERAGE_OF(BodyShort_rangeType,BodyShort_avgPeriod,BodyShort_factor,BodyPeriodTotal,i))))); /* add the current range and subtract the first range: this is done after the pattern recognition * when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle) */ BodyPeriodTotal += TA_CANDLERANGE_OF(BodyShort_rangeType,i) - TA_CANDLERANGE_OF(BodyShort_rangeType,BodyTrailingIdx); i += 1; BodyTrailingIdx += 1; } while( i <= endIdx ); /* All done. Indicate the output limits and return. */ *outNBElement= outIdx; *outBegIdx= startIdx; /* Capture the live batch state into the handle. */ sp = (struct TA_CDLSPINNINGTOP_Stream *)TA_Malloc( sizeof(*sp) ); if( !sp ) { return TA_ALLOC_ERR; } memset( sp, 0, sizeof(*sp) ); sp->BodyPeriodTotal = BodyPeriodTotal; sp->ringCap_BodyTrailingIdx = (int)(i - BodyTrailingIdx); if( sp->ringCap_BodyTrailingIdx < 0 || sp->ringCap_BodyTrailingIdx > historyLen ) { TA_CDLSPINNINGTOP_ReleaseImpl( sp ); return TA_INTERNAL_ERROR(297); } { size_t allocN = (size_t)(sp->ringCap_BodyTrailingIdx > 0 ? sp->ringCap_BodyTrailingIdx : 1); sp->ring_BodyTrailingIdx_derived = (double *)TA_Malloc( sizeof(double) * allocN ); if( !sp->ring_BodyTrailingIdx_derived ) { TA_CDLSPINNINGTOP_ReleaseImpl( sp ); return TA_ALLOC_ERR; } { int fillJ; for( fillJ = historyLen - sp->ringCap_BodyTrailingIdx; fillJ < historyLen; fillJ++ ) sp->ring_BodyTrailingIdx_derived[fillJ - (historyLen - sp->ringCap_BodyTrailingIdx)] = TA_STREAM_CANDLERANGE(BodyShort,inOpen[fillJ],inHigh[fillJ],inLow[fillJ],inClose[fillJ]); } } sp->ringPos_BodyTrailingIdx = 0; sp->outRangeBegIdx = *outBegIdx; sp->outRangeCount = *outNBElement; sp->cur_outInteger = outInteger[(*outNBElement - 1) * outStride]; *stream = sp; return TA_SUCCESS; } } } static TA_OPEN_BODY TA_RetCode TA_CDLSPINNINGTOP_OpenFillFrame( struct TA_CDLSPINNINGTOP_Stream **stream, const double inOpen[], const double inHigh[], const double inLow[], const double inClose[], int startIdx, int historyLen, int *outBegIdx, int *outNBElement, int outInteger[] ) { return TA_CDLSPINNINGTOP_OpenBody( stream, inOpen, inHigh, inLow, inClose, startIdx, historyLen, outBegIdx, outNBElement, outInteger, 1 ); } static TA_OPEN_BODY TA_RetCode TA_CDLSPINNINGTOP_OpenSinkFrame( struct TA_CDLSPINNINGTOP_Stream **stream, const double inOpen[], const double inHigh[], const double inLow[], const double inClose[], int startIdx, int historyLen, int *outBegIdx, int *outNBElement, int outInteger[] ) { int sink_outInteger = 0; TA_RetCode retCode = TA_CDLSPINNINGTOP_OpenBody( stream, inOpen, inHigh, inLow, inClose, startIdx, historyLen, outBegIdx, outNBElement, &sink_outInteger, 0 ); if( retCode == TA_SUCCESS ) { if( outInteger ) *outInteger = sink_outInteger; } return retCode; } static TA_RetCode TA_CDLSPINNINGTOP_OpenImpl( struct TA_CDLSPINNINGTOP_Stream **stream, const double inOpen[], const double inHigh[], const double inLow[], const double inClose[], int startIdx, int historyLen, int *outBegIdx, int *outNBElement, int outInteger[], int outStride ) { if( !stream ) return TA_BAD_PARAM; *stream = NULL; if( historyLen > TA_INDEX_MAX + 1 ) return TA_OUT_OF_RANGE_END_INDEX; if( !inOpen || !inHigh || !inLow || !inClose || !outInteger ) return TA_BAD_PARAM; if( startIdx >= historyLen ) { *outBegIdx = 0; *outNBElement = 0; return TA_INSUFFICIENT_HISTORY; } return outStride ? TA_CDLSPINNINGTOP_OpenFillFrame( stream, inOpen, inHigh, inLow, inClose, startIdx, historyLen, outBegIdx, outNBElement, outInteger ) : TA_CDLSPINNINGTOP_OpenSinkFrame( stream, inOpen, inHigh, inLow, inClose, startIdx, historyLen, outBegIdx, outNBElement, outInteger ); } /* Private function, not in public API. */ TA_RetCode TA_CDLSPINNINGTOP_OpenInternal( struct TA_CDLSPINNINGTOP_Stream **stream, const double inOpen[], const double inHigh[], const double inLow[], const double inClose[], int startIdx, int historyLen, int *outInteger ) { TA_RetCode retCode; int dummyBegIdx = 0; int dummyNBElement = 0; int sink_outInteger = 0; retCode = TA_CDLSPINNINGTOP_OpenImpl( stream, inOpen, inHigh, inLow, inClose, startIdx, historyLen, &dummyBegIdx, &dummyNBElement, &sink_outInteger, 0 ); if( retCode == TA_SUCCESS ) { *outInteger = sink_outInteger; } return retCode; } TA_LIB_API TA_RetCode TA_CDLSPINNINGTOP_Open( TA_CDLSPINNINGTOP_Stream **stream, const double inOpen[], const double inHigh[], const double inLow[], const double inClose[], int historyLen, int *outInteger ) { if( !stream ) return TA_BAD_PARAM; *stream = NULL; if( historyLen > TA_INDEX_MAX + 1 ) return TA_OUT_OF_RANGE_END_INDEX; if( !inOpen || !inHigh || !inLow || !inClose || !outInteger ) return TA_BAD_PARAM; return TA_CDLSPINNINGTOP_OpenInternal( stream, inOpen, inHigh, inLow, inClose, 0, historyLen, outInteger ); } TA_LIB_API TA_RetCode TA_CDLSPINNINGTOP_OpenAndFill( TA_CDLSPINNINGTOP_Stream **stream, const double inOpen[], const double inHigh[], const double inLow[], const double inClose[], int historyLen, int *outBegIdx, int *outNBElement, int outInteger[] ) { if( !stream ) return TA_BAD_PARAM; *stream = NULL; if( historyLen > TA_INDEX_MAX + 1 ) return TA_OUT_OF_RANGE_END_INDEX; if( !inOpen || !inHigh || !inLow || !inClose || !outBegIdx || !outNBElement || !outInteger ) return TA_BAD_PARAM; if( (const void *)outInteger == (const void *)inOpen || (const void *)outInteger == (const void *)inHigh || (const void *)outInteger == (const void *)inLow || (const void *)outInteger == (const void *)inClose ) return TA_BAD_PARAM; return TA_CDLSPINNINGTOP_OpenAndFillInternal( stream, inOpen, inHigh, inLow, inClose, 0, historyLen, outBegIdx, outNBElement, outInteger ); } /* Private function, not in public API. */ TA_RetCode TA_CDLSPINNINGTOP_OpenAndFillInternal( struct TA_CDLSPINNINGTOP_Stream **stream, const double inOpen[], const double inHigh[], const double inLow[], const double inClose[], int startIdx, int historyLen, int *outBegIdx, int *outNBElement, int outInteger[] ) { return TA_CDLSPINNINGTOP_OpenImpl( stream, inOpen, inHigh, inLow, inClose, startIdx, historyLen, outBegIdx, outNBElement, outInteger, 1 ); } TA_LIB_API TA_RetCode TA_CDLSPINNINGTOP_Update( TA_CDLSPINNINGTOP_Stream *stream, double inOpen, double inHigh, double inLow, double inClose, int *outInteger ) { if( !stream ) return TA_BAD_PARAM; if( stream->outRangeBegIdx + stream->outRangeCount > TA_INDEX_MAX ) return TA_OUT_OF_RANGE_END_INDEX; if( !outInteger ) return TA_BAD_PARAM; if( !TA_IS_FINITE( inOpen ) || !TA_IS_FINITE( inHigh ) || !TA_IS_FINITE( inLow ) || !TA_IS_FINITE( inClose ) ) return TA_BAD_PARAM; TA_CDLSPINNINGTOP_StepImpl( stream, inOpen, inHigh, inLow, inClose, outInteger ); stream->outRangeCount++; return TA_SUCCESS; } TA_LIB_API TA_RetCode TA_CDLSPINNINGTOP_Peek( const TA_CDLSPINNINGTOP_Stream *stream, double inOpen, double inHigh, double inLow, double inClose, int *outInteger ) { const struct TA_CDLSPINNINGTOP_Stream *sp = stream; if( !stream || !outInteger ) return TA_BAD_PARAM; if( !TA_IS_FINITE( inOpen ) || !TA_IS_FINITE( inHigh ) || !TA_IS_FINITE( inLow ) || !TA_IS_FINITE( inClose ) ) return TA_BAD_PARAM; if( (inHigh - TA_COLOR_MAX(inClose, inOpen)) > fabs(inClose - inOpen) && (TA_COLOR_MIN(inClose, inOpen) - inLow) > fabs(inClose - inOpen) && fabs(inClose - inOpen) < TA_STREAM_CANDLEAVERAGE(BodyShort,sp->BodyPeriodTotal,inOpen,inHigh,inLow,inClose) ) { *outInteger= ((inClose >= inOpen) ? 1 : -1) * 100; } else { *outInteger= 0; } return TA_SUCCESS; } TA_LIB_API TA_RetCode TA_CDLSPINNINGTOP_Close( TA_CDLSPINNINGTOP_Stream *stream ) { TA_CDLSPINNINGTOP_ReleaseImpl( stream ); return TA_SUCCESS; } TA_LIB_API TA_RetCode TA_CDLSPINNINGTOP_Value( const TA_CDLSPINNINGTOP_Stream *stream, int *outInteger ) { if( !stream || !outInteger ) return TA_BAD_PARAM; *outInteger = stream->cur_outInteger; return TA_SUCCESS; } TA_LIB_API TA_RetCode TA_CDLSPINNINGTOP_OutRange( const TA_CDLSPINNINGTOP_Stream *stream, int *outBegIdx, int *outNBElement ) { if( !stream || !outBegIdx || !outNBElement ) return TA_BAD_PARAM; *outBegIdx = stream->outRangeBegIdx; *outNBElement = stream->outRangeCount; return TA_SUCCESS; } TA_LIB_API TA_RetCode TA_CDLSPINNINGTOP_Advance( TA_CDLSPINNINGTOP_Stream *stream ) { if( !stream ) return TA_BAD_PARAM; if( stream->outRangeBegIdx + stream->outRangeCount > TA_INDEX_MAX ) return TA_OUT_OF_RANGE_END_INDEX; stream->outRangeCount++; return TA_SUCCESS; } TA_LIB_API TA_RetCode TA_CDLSPINNINGTOP_Clone( const TA_CDLSPINNINGTOP_Stream *stream, TA_CDLSPINNINGTOP_Stream **clone ) { struct TA_CDLSPINNINGTOP_Stream *sp; if( !clone ) return TA_BAD_PARAM; *clone = NULL; if( !stream ) return TA_BAD_PARAM; sp = (struct TA_CDLSPINNINGTOP_Stream *)TA_Malloc( sizeof(*sp) ); if( !sp ) return TA_ALLOC_ERR; *sp = *stream; sp->ring_BodyTrailingIdx_derived = NULL; if( stream->ring_BodyTrailingIdx_derived ) { size_t copyN = (size_t)(sp->ringCap_BodyTrailingIdx > 0 ? sp->ringCap_BodyTrailingIdx : 1); sp->ring_BodyTrailingIdx_derived = (double *)TA_Malloc( sizeof(double) * copyN ); if( !sp->ring_BodyTrailingIdx_derived ) { TA_CDLSPINNINGTOP_Close( sp ); return TA_ALLOC_ERR; } memcpy( sp->ring_BodyTrailingIdx_derived, stream->ring_BodyTrailingIdx_derived, sizeof(double) * copyN ); } *clone = sp; return TA_SUCCESS; }