/* TA-LIB Copyright (c) 1999-2025, 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/<name>/  (regenerate: cd ta_codegen/generator && cargo run -- generate)
*/

#include <string.h>
#include <math.h>
#include "ta_func.h"
#include "ta_utility.h"
#include "ta_memory.h"
#include "ta_func_unguarded.h"

/* List of contributors:
 *
 *  Initial  Name/description
 *  -------------------------------------------------------------------
 *  MF       Mario Fortier
 *
 *
 * Change history:
 *
 *  MMDDYY BY   Description
 *  -------------------------------------------------------------------
 *  031202 MF   Template creation.
 *  052603 MF   Port to managed C++. Change to use CIRCBUF macros.
 *  061704 MF   Lower limit for period to 2, and correct algorithm
 *              to avoid cummulative error when value are close to
 *              the floating point epsilon.
 */

TA_LIB_API int TA_CCI_Lookback( int optInTimePeriod )
{
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 14;
   else if( (int)optInTimePeriod < 2 || (int)optInTimePeriod > 100000 )
      return -1;
   return optInTimePeriod - 1;
}

TA_LIB_API TA_RetCode TA_CCI( int    startIdx,
                              int    endIdx,
                              const double inHigh[],
                              const double inLow[],
                              const double inClose[],
                              int optInTimePeriod,
                              int          *outBegIdx,
                              int          *outNBElement,
                              double        outReal[] )
{
   double tempReal;
   double tempReal2;
   double theAverage;
   double lastValue;
   int i;
   int j;
   int outIdx;
   int lookbackTotal;
   double local_circBuffer[30];
   double *circBuffer;
   int circBuffer_Idx;
   int maxIdx_circBuffer;

   if( startIdx < 0 )
      return TA_OUT_OF_RANGE_START_INDEX;
   if( (endIdx < 0) || (endIdx < startIdx) )
      return TA_OUT_OF_RANGE_END_INDEX;

   if( !inHigh )
      return TA_BAD_PARAM;
   if( !inLow )
      return TA_BAD_PARAM;
   if( !inClose )
      return TA_BAD_PARAM;
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 14;
   else if( (int)optInTimePeriod < 2 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( !outReal )
      return TA_BAD_PARAM;

   /* This ptr will points on a circular buffer of
    * at least "optInTimePeriod" element.
    */
   /* Identify the minimum number of price bar needed
    * to calculate at least one output.
    */
   lookbackTotal = optInTimePeriod - 1;
   /* 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;
   }
   /* Allocate a circular buffer equal to the requested
    * period.
    */
   if( optInTimePeriod < 1 ) return TA_INTERNAL_ERROR(137);
   if( (int)optInTimePeriod > (int)(sizeof(local_circBuffer)/sizeof(double)) )
   {
      circBuffer = TA_Malloc( sizeof(double)*optInTimePeriod );
      if( !circBuffer )
      {
         return TA_ALLOC_ERR;
      }
   }
   else
   {
      circBuffer = &local_circBuffer[0];
   }
   maxIdx_circBuffer = (optInTimePeriod-1);
   circBuffer_Idx = 0;
   /* Do the MA calculation using tight loops. */
   /* Add-up the initial period, except for the last value.
    * Fill up the circular buffer at the same time.
    */
   i = startIdx - lookbackTotal;
   if( optInTimePeriod > 1 )
   {
      while( i < startIdx )
      {
         circBuffer[circBuffer_Idx] = (inHigh[i] + inLow[i] + inClose[i]) / 3;
         i += 1;
         circBuffer_Idx++;
         if( circBuffer_Idx > maxIdx_circBuffer ) circBuffer_Idx = 0;
      }
   }
   /* Proceed with the calculation for the requested range.
    * Note that this algorithm allows the inReal and
    * outReal to be the same buffer.
    */
   outIdx = 0;
   do
   {
      lastValue = (inHigh[i] + inLow[i] + inClose[i]) / 3;
      circBuffer[circBuffer_Idx] = lastValue;
      /* Calculate the average for the whole period. */
      theAverage = 0;
      for( j = 0; j < optInTimePeriod; j += 1 )
      {
         theAverage += circBuffer[j];
      }
      theAverage /= optInTimePeriod;
      /* Do the summation of the ABS(TypePrice-average)
       * for the whole period.
       */
      tempReal2 = 0;
      for( j = 0; j < optInTimePeriod; j += 1 )
      {
         tempReal2 += fabs(circBuffer[j] - theAverage);
      }
      /* And finally, the CCI... */
      tempReal = lastValue - theAverage;
      if( tempReal != 0.0 && tempReal2 != 0.0 )
      {
         outReal[outIdx++] = tempReal / (0.015 * (tempReal2 / optInTimePeriod));
      } else 
      {
         outReal[outIdx++] = 0.0;
      }
      /* Move forward the circular buffer indexes. */
      circBuffer_Idx++;
      if( circBuffer_Idx > maxIdx_circBuffer ) circBuffer_Idx = 0;
      i += 1;
   } while( i <= endIdx );
   /* All done. Indicate the output limits and return. */
   *outNBElement= outIdx;
   *outBegIdx= startIdx;
   /* Free the circular buffer if it was dynamically allocated. */
   if( circBuffer != &local_circBuffer[0] ) TA_Free( circBuffer );
   return TA_SUCCESS;
}

TA_LIB_API TA_RetCode TA_CCI_Unguarded( int    startIdx,
                                        int    endIdx,
                                        const double inHigh[],
                                        const double inLow[],
                                        const double inClose[],
                                        int optInTimePeriod,
                                        int          *outBegIdx,
                                        int          *outNBElement,
                                        double        outReal[] )
{
   double tempReal;
   double tempReal2;
   double theAverage;
   double lastValue;
   int i;
   int j;
   int outIdx;
   int lookbackTotal;
   double local_circBuffer[30];
   double *circBuffer;
   int circBuffer_Idx;
   int maxIdx_circBuffer;

   lookbackTotal = optInTimePeriod - 1;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   if( optInTimePeriod < 1 ) return TA_INTERNAL_ERROR(137);
   if( (int)optInTimePeriod > (int)(sizeof(local_circBuffer)/sizeof(double)) )
   {
      circBuffer = TA_Malloc( sizeof(double)*optInTimePeriod );
      if( !circBuffer )
      {
         return TA_ALLOC_ERR;
      }
   }
   else
   {
      circBuffer = &local_circBuffer[0];
   }
   maxIdx_circBuffer = (optInTimePeriod-1);
   circBuffer_Idx = 0;
   i = startIdx - lookbackTotal;
   if( optInTimePeriod > 1 )
   {
      while( i < startIdx )
      {
         circBuffer[circBuffer_Idx] = (inHigh[i] + inLow[i] + inClose[i]) / 3;
         i += 1;
         circBuffer_Idx++;
         if( circBuffer_Idx > maxIdx_circBuffer ) circBuffer_Idx = 0;
      }
   }
   outIdx = 0;
   do
   {
      lastValue = (inHigh[i] + inLow[i] + inClose[i]) / 3;
      circBuffer[circBuffer_Idx] = lastValue;
      theAverage = 0;
      for( j = 0; j < optInTimePeriod; j += 1 )
      {
         theAverage += circBuffer[j];
      }
      theAverage /= optInTimePeriod;
      tempReal2 = 0;
      for( j = 0; j < optInTimePeriod; j += 1 )
      {
         tempReal2 += fabs(circBuffer[j] - theAverage);
      }
      tempReal = lastValue - theAverage;
      if( tempReal != 0.0 && tempReal2 != 0.0 )
      {
         outReal[outIdx++] = tempReal / (0.015 * (tempReal2 / optInTimePeriod));
      } else 
      {
         outReal[outIdx++] = 0.0;
      }
      circBuffer_Idx++;
      if( circBuffer_Idx > maxIdx_circBuffer ) circBuffer_Idx = 0;
      i += 1;
   } while( i <= endIdx );
   *outNBElement= outIdx;
   *outBegIdx= startIdx;
   if( circBuffer != &local_circBuffer[0] ) TA_Free( circBuffer );
   return TA_SUCCESS;
}

TA_RetCode TA_S_CCI( int    startIdx,
                     int    endIdx,
                     const float inHigh[],
                     const float inLow[],
                     const float inClose[],
                     int optInTimePeriod,
                     int          *outBegIdx,
                     int          *outNBElement,
                     double        outReal[] )
{
   double tempReal;
   double tempReal2;
   double theAverage;
   double lastValue;
   int i;
   int j;
   int outIdx;
   int lookbackTotal;
   double local_circBuffer[30];
   double *circBuffer;
   int circBuffer_Idx;
   int maxIdx_circBuffer;

   if( startIdx < 0 )
      return TA_OUT_OF_RANGE_START_INDEX;
   if( (endIdx < 0) || (endIdx < startIdx) )
      return TA_OUT_OF_RANGE_END_INDEX;

   if( !inHigh )
      return TA_BAD_PARAM;
   if( !inLow )
      return TA_BAD_PARAM;
   if( !inClose )
      return TA_BAD_PARAM;
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 14;
   else if( (int)optInTimePeriod < 2 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( !outReal )
      return TA_BAD_PARAM;

   lookbackTotal = optInTimePeriod - 1;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   if( optInTimePeriod < 1 ) return TA_INTERNAL_ERROR(137);
   if( (int)optInTimePeriod > (int)(sizeof(local_circBuffer)/sizeof(double)) )
   {
      circBuffer = TA_Malloc( sizeof(double)*optInTimePeriod );
      if( !circBuffer )
      {
         return TA_ALLOC_ERR;
      }
   }
   else
   {
      circBuffer = &local_circBuffer[0];
   }
   maxIdx_circBuffer = (optInTimePeriod-1);
   circBuffer_Idx = 0;
   i = startIdx - lookbackTotal;
   if( optInTimePeriod > 1 )
   {
      while( i < startIdx )
      {
         circBuffer[circBuffer_Idx] = (inHigh[i] + inLow[i] + inClose[i]) / 3;
         i += 1;
         circBuffer_Idx++;
         if( circBuffer_Idx > maxIdx_circBuffer ) circBuffer_Idx = 0;
      }
   }
   outIdx = 0;
   do
   {
      lastValue = (inHigh[i] + inLow[i] + inClose[i]) / 3;
      circBuffer[circBuffer_Idx] = lastValue;
      theAverage = 0;
      for( j = 0; j < optInTimePeriod; j += 1 )
      {
         theAverage += circBuffer[j];
      }
      theAverage /= optInTimePeriod;
      tempReal2 = 0;
      for( j = 0; j < optInTimePeriod; j += 1 )
      {
         tempReal2 += fabs(circBuffer[j] - theAverage);
      }
      tempReal = lastValue - theAverage;
      if( tempReal != 0.0 && tempReal2 != 0.0 )
      {
         outReal[outIdx++] = tempReal / (0.015 * (tempReal2 / optInTimePeriod));
      } else 
      {
         outReal[outIdx++] = 0.0;
      }
      circBuffer_Idx++;
      if( circBuffer_Idx > maxIdx_circBuffer ) circBuffer_Idx = 0;
      i += 1;
   } while( i <= endIdx );
   *outNBElement= outIdx;
   *outBegIdx= startIdx;
   if( circBuffer != &local_circBuffer[0] ) TA_Free( circBuffer );
   return TA_SUCCESS;
}

TA_RetCode TA_S_CCI_Unguarded( int    startIdx,
                               int    endIdx,
                               const float inHigh[],
                               const float inLow[],
                               const float inClose[],
                               int optInTimePeriod,
                               int          *outBegIdx,
                               int          *outNBElement,
                               double        outReal[] )
{
   double tempReal;
   double tempReal2;
   double theAverage;
   double lastValue;
   int i;
   int j;
   int outIdx;
   int lookbackTotal;
   double local_circBuffer[30];
   double *circBuffer;
   int circBuffer_Idx;
   int maxIdx_circBuffer;

   lookbackTotal = optInTimePeriod - 1;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   if( optInTimePeriod < 1 ) return TA_INTERNAL_ERROR(137);
   if( (int)optInTimePeriod > (int)(sizeof(local_circBuffer)/sizeof(double)) )
   {
      circBuffer = TA_Malloc( sizeof(double)*optInTimePeriod );
      if( !circBuffer )
      {
         return TA_ALLOC_ERR;
      }
   }
   else
   {
      circBuffer = &local_circBuffer[0];
   }
   maxIdx_circBuffer = (optInTimePeriod-1);
   circBuffer_Idx = 0;
   i = startIdx - lookbackTotal;
   if( optInTimePeriod > 1 )
   {
      while( i < startIdx )
      {
         circBuffer[circBuffer_Idx] = (inHigh[i] + inLow[i] + inClose[i]) / 3;
         i += 1;
         circBuffer_Idx++;
         if( circBuffer_Idx > maxIdx_circBuffer ) circBuffer_Idx = 0;
      }
   }
   outIdx = 0;
   do
   {
      lastValue = (inHigh[i] + inLow[i] + inClose[i]) / 3;
      circBuffer[circBuffer_Idx] = lastValue;
      theAverage = 0;
      for( j = 0; j < optInTimePeriod; j += 1 )
      {
         theAverage += circBuffer[j];
      }
      theAverage /= optInTimePeriod;
      tempReal2 = 0;
      for( j = 0; j < optInTimePeriod; j += 1 )
      {
         tempReal2 += fabs(circBuffer[j] - theAverage);
      }
      tempReal = lastValue - theAverage;
      if( tempReal != 0.0 && tempReal2 != 0.0 )
      {
         outReal[outIdx++] = tempReal / (0.015 * (tempReal2 / optInTimePeriod));
      } else 
      {
         outReal[outIdx++] = 0.0;
      }
      circBuffer_Idx++;
      if( circBuffer_Idx > maxIdx_circBuffer ) circBuffer_Idx = 0;
      i += 1;
   } while( i <= endIdx );
   *outNBElement= outIdx;
   *outBegIdx= startIdx;
   if( circBuffer != &local_circBuffer[0] ) TA_Free( circBuffer );
   return TA_SUCCESS;
}

