/* 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
 *  -------------------------------------------------------------------
 *  120802 MF     Template creation.
 *  052603 MF     Adapt code to compile with .NET Managed C++
 *  062704 MF     Fix limit case to avoid divid by zero (or by
 *                a value close to zero induce by the imprecision
 *                of floating points).
 *  070226 MF,CC  Allow period of 1: output is a copy of the input,
 *                consistent with TA_MA (issues #48, #59). The natural
 *                KAMA math at period=1 would be a fixed-alpha EMA
 *                (efficiency ratio is always 1), which would disagree
 *                with TA_MA's period-1 copy, so identity is explicit.
 */

TA_LIB_API int TA_KAMA_Lookback( int optInTimePeriod )
{
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 30;
   else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 )
      return -1;
   if( optInTimePeriod == 1 )
   {
      return TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_KAMA,Kama);
   }
   return optInTimePeriod + TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_KAMA,Kama);
}

TA_LIB_API TA_RetCode TA_KAMA( int    startIdx,
                               int    endIdx,
                               const double inReal[],
                               int optInTimePeriod,
                               int          *outBegIdx,
                               int          *outNBElement,
                               double        outReal[] )
{
   double constMax;
   double constDiff;
   double tempReal;
   double tempReal2;
   double sumROC1;
   double periodROC;
   double prevKAMA;
   int i;
   int today;
   int outIdx;
   int lookbackTotal;
   int trailingIdx;
   double trailingValue;

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

   if( !inReal )
      return TA_BAD_PARAM;
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 30;
   else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( !outReal )
      return TA_BAD_PARAM;

   constMax = 2.0 / (30.0 + 1.0);
   constDiff = 2.0 / (2.0 + 1.0) - constMax;
   /* Default return values */
   *outBegIdx= 0;
   *outNBElement= 0;
   /* No smoothing at period of 1: the output is a copy of the input
    * (same convention as TA_MA for every MAType). The unstable period
    * still delays the first output for API consistency.
    */
   if( optInTimePeriod == 1 )
   {
      lookbackTotal = TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_KAMA,Kama);
      if( startIdx < lookbackTotal )
      {
         startIdx = lookbackTotal;
      }
      if( startIdx > endIdx )
      {
         return TA_SUCCESS;
      }
      *outBegIdx= startIdx;
      outIdx = 0;
      today = startIdx;
      while( today <= endIdx )
      {
         outReal[outIdx++] = inReal[today++];
      }
      *outNBElement= outIdx;
      return TA_SUCCESS;
   }
   /* Identify the minimum number of price bar needed
    * to calculate at least one output.
    */
   lookbackTotal = optInTimePeriod + TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_KAMA,Kama);
   /* 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;
   }
   /* Initialize the variables by going through
    * the lookback period.
    */
   sumROC1 = 0.0;
   today = startIdx - lookbackTotal;
   trailingIdx = today;
   i = optInTimePeriod;
   while( i-- > 0 )
   {
      tempReal = inReal[today++];
      tempReal -= inReal[today];
      sumROC1 += fabs(tempReal);
   }
   /* At this point sumROC1 represent the
    * summation of the 1-day price difference
    * over the (optInTimePeriod-1)
    */
   /* Calculate the first KAMA */
   /* The yesterday price is used here as the previous KAMA. */
   prevKAMA = inReal[today - 1];
   tempReal = inReal[today];
   tempReal2 = inReal[trailingIdx++];
   periodROC = tempReal - tempReal2;
   /* Save the trailing value. Do this because inReal
    * and outReal can be pointers to the same buffer.
    */
   trailingValue = tempReal2;
   /* Calculate the efficiency ratio */
   if( sumROC1 <= periodROC || TA_IS_ZERO(sumROC1) )
   {
      tempReal = 1.0;
   } else 
   {
      tempReal = fabs(periodROC / sumROC1);
   }
   /* Calculate the smoothing constant */
   tempReal = tempReal * constDiff + constMax;
   tempReal *= tempReal;
   /* Calculate the KAMA like an EMA, using the
    * smoothing constant as the adaptive factor.
    */
   prevKAMA = (inReal[today++] - prevKAMA) * tempReal + prevKAMA;
   /* 'today' keep track of where the processing is within the
    * input.
    */
   /* Skip the unstable period. Do the whole processing
    * needed for KAMA, but do not write it in the output.
    */
   while( today <= startIdx )
   {
      tempReal = inReal[today];
      tempReal2 = inReal[trailingIdx++];
      periodROC = tempReal - tempReal2;
      /* Adjust sumROC1:
       *  - Remove trailing ROC1
       *  - Add new ROC1
       */
      sumROC1 -= fabs(trailingValue - tempReal2);
      sumROC1 += fabs(tempReal - inReal[today - 1]);
      /* Save the trailing value. Do this because inReal
       * and outReal can be pointers to the same buffer.
       */
      trailingValue = tempReal2;
      /* Calculate the efficiency ratio */
      if( sumROC1 <= periodROC || TA_IS_ZERO(sumROC1) )
      {
         tempReal = 1.0;
      } else 
      {
         tempReal = fabs(periodROC / sumROC1);
      }
      /* Calculate the smoothing constant */
      tempReal = tempReal * constDiff + constMax;
      tempReal *= tempReal;
      /* Calculate the KAMA like an EMA, using the
       * smoothing constant as the adaptive factor.
       */
      prevKAMA = (inReal[today++] - prevKAMA) * tempReal + prevKAMA;
   }
   /* Write the first value. */
   outReal[0] = prevKAMA;
   outIdx = 1;
   *outBegIdx= today - 1;
   /* Do the KAMA calculation for the requested range. */
   while( today <= endIdx )
   {
      tempReal = inReal[today];
      tempReal2 = inReal[trailingIdx++];
      periodROC = tempReal - tempReal2;
      /* Adjust sumROC1:
       *  - Remove trailing ROC1
       *  - Add new ROC1
       */
      sumROC1 -= fabs(trailingValue - tempReal2);
      sumROC1 += fabs(tempReal - inReal[today - 1]);
      /* Save the trailing value. Do this because inReal
       * and outReal can be pointers to the same buffer.
       */
      trailingValue = tempReal2;
      /* Calculate the efficiency ratio */
      if( sumROC1 <= periodROC || TA_IS_ZERO(sumROC1) )
      {
         tempReal = 1.0;
      } else 
      {
         tempReal = fabs(periodROC / sumROC1);
      }
      /* Calculate the smoothing constant */
      tempReal = tempReal * constDiff + constMax;
      tempReal *= tempReal;
      /* Calculate the KAMA like an EMA, using the
       * smoothing constant as the adaptive factor.
       */
      prevKAMA = (inReal[today++] - prevKAMA) * tempReal + prevKAMA;
      outReal[outIdx++] = prevKAMA;
   }
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

TA_LIB_API TA_RetCode TA_KAMA_Unguarded( int    startIdx,
                                         int    endIdx,
                                         const double inReal[],
                                         int optInTimePeriod,
                                         int          *outBegIdx,
                                         int          *outNBElement,
                                         double        outReal[] )
{
   double constMax;
   double constDiff;
   double tempReal;
   double tempReal2;
   double sumROC1;
   double periodROC;
   double prevKAMA;
   int i;
   int today;
   int outIdx;
   int lookbackTotal;
   int trailingIdx;
   double trailingValue;

   constMax = 2.0 / (30.0 + 1.0);
   constDiff = 2.0 / (2.0 + 1.0) - constMax;
   *outBegIdx= 0;
   *outNBElement= 0;
   if( optInTimePeriod == 1 )
   {
      lookbackTotal = TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_KAMA,Kama);
      if( startIdx < lookbackTotal )
      {
         startIdx = lookbackTotal;
      }
      if( startIdx > endIdx )
      {
         return TA_SUCCESS;
      }
      *outBegIdx= startIdx;
      outIdx = 0;
      today = startIdx;
      while( today <= endIdx )
      {
         outReal[outIdx++] = inReal[today++];
      }
      *outNBElement= outIdx;
      return TA_SUCCESS;
   }
   lookbackTotal = optInTimePeriod + TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_KAMA,Kama);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   sumROC1 = 0.0;
   today = startIdx - lookbackTotal;
   trailingIdx = today;
   i = optInTimePeriod;
   while( i-- > 0 )
   {
      tempReal = inReal[today++];
      tempReal -= inReal[today];
      sumROC1 += fabs(tempReal);
   }
   prevKAMA = inReal[today - 1];
   tempReal = inReal[today];
   tempReal2 = inReal[trailingIdx++];
   periodROC = tempReal - tempReal2;
   trailingValue = tempReal2;
   if( sumROC1 <= periodROC || TA_IS_ZERO(sumROC1) )
   {
      tempReal = 1.0;
   } else 
   {
      tempReal = fabs(periodROC / sumROC1);
   }
   tempReal = tempReal * constDiff + constMax;
   tempReal *= tempReal;
   prevKAMA = (inReal[today++] - prevKAMA) * tempReal + prevKAMA;
   while( today <= startIdx )
   {
      tempReal = inReal[today];
      tempReal2 = inReal[trailingIdx++];
      periodROC = tempReal - tempReal2;
      sumROC1 -= fabs(trailingValue - tempReal2);
      sumROC1 += fabs(tempReal - inReal[today - 1]);
      trailingValue = tempReal2;
      if( sumROC1 <= periodROC || TA_IS_ZERO(sumROC1) )
      {
         tempReal = 1.0;
      } else 
      {
         tempReal = fabs(periodROC / sumROC1);
      }
      tempReal = tempReal * constDiff + constMax;
      tempReal *= tempReal;
      prevKAMA = (inReal[today++] - prevKAMA) * tempReal + prevKAMA;
   }
   outReal[0] = prevKAMA;
   outIdx = 1;
   *outBegIdx= today - 1;
   while( today <= endIdx )
   {
      tempReal = inReal[today];
      tempReal2 = inReal[trailingIdx++];
      periodROC = tempReal - tempReal2;
      sumROC1 -= fabs(trailingValue - tempReal2);
      sumROC1 += fabs(tempReal - inReal[today - 1]);
      trailingValue = tempReal2;
      if( sumROC1 <= periodROC || TA_IS_ZERO(sumROC1) )
      {
         tempReal = 1.0;
      } else 
      {
         tempReal = fabs(periodROC / sumROC1);
      }
      tempReal = tempReal * constDiff + constMax;
      tempReal *= tempReal;
      prevKAMA = (inReal[today++] - prevKAMA) * tempReal + prevKAMA;
      outReal[outIdx++] = prevKAMA;
   }
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

TA_RetCode TA_S_KAMA( int    startIdx,
                      int    endIdx,
                      const float inReal[],
                      int optInTimePeriod,
                      int          *outBegIdx,
                      int          *outNBElement,
                      double        outReal[] )
{
   double constMax;
   double constDiff;
   double tempReal;
   double tempReal2;
   double sumROC1;
   double periodROC;
   double prevKAMA;
   int i;
   int today;
   int outIdx;
   int lookbackTotal;
   int trailingIdx;
   double trailingValue;

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

   if( !inReal )
      return TA_BAD_PARAM;
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 30;
   else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( !outReal )
      return TA_BAD_PARAM;

   constMax = 2.0 / (30.0 + 1.0);
   constDiff = 2.0 / (2.0 + 1.0) - constMax;
   *outBegIdx= 0;
   *outNBElement= 0;
   if( optInTimePeriod == 1 )
   {
      lookbackTotal = TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_KAMA,Kama);
      if( startIdx < lookbackTotal )
      {
         startIdx = lookbackTotal;
      }
      if( startIdx > endIdx )
      {
         return TA_SUCCESS;
      }
      *outBegIdx= startIdx;
      outIdx = 0;
      today = startIdx;
      while( today <= endIdx )
      {
         outReal[outIdx++] = inReal[today++];
      }
      *outNBElement= outIdx;
      return TA_SUCCESS;
   }
   lookbackTotal = optInTimePeriod + TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_KAMA,Kama);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   sumROC1 = 0.0;
   today = startIdx - lookbackTotal;
   trailingIdx = today;
   i = optInTimePeriod;
   while( i-- > 0 )
   {
      tempReal = inReal[today++];
      tempReal -= inReal[today];
      sumROC1 += fabs(tempReal);
   }
   prevKAMA = inReal[today - 1];
   tempReal = inReal[today];
   tempReal2 = inReal[trailingIdx++];
   periodROC = tempReal - tempReal2;
   trailingValue = tempReal2;
   if( sumROC1 <= periodROC || TA_IS_ZERO(sumROC1) )
   {
      tempReal = 1.0;
   } else 
   {
      tempReal = fabs(periodROC / sumROC1);
   }
   tempReal = tempReal * constDiff + constMax;
   tempReal *= tempReal;
   prevKAMA = (inReal[today++] - prevKAMA) * tempReal + prevKAMA;
   while( today <= startIdx )
   {
      tempReal = inReal[today];
      tempReal2 = inReal[trailingIdx++];
      periodROC = tempReal - tempReal2;
      sumROC1 -= fabs(trailingValue - tempReal2);
      sumROC1 += fabs(tempReal - inReal[today - 1]);
      trailingValue = tempReal2;
      if( sumROC1 <= periodROC || TA_IS_ZERO(sumROC1) )
      {
         tempReal = 1.0;
      } else 
      {
         tempReal = fabs(periodROC / sumROC1);
      }
      tempReal = tempReal * constDiff + constMax;
      tempReal *= tempReal;
      prevKAMA = (inReal[today++] - prevKAMA) * tempReal + prevKAMA;
   }
   outReal[0] = prevKAMA;
   outIdx = 1;
   *outBegIdx= today - 1;
   while( today <= endIdx )
   {
      tempReal = inReal[today];
      tempReal2 = inReal[trailingIdx++];
      periodROC = tempReal - tempReal2;
      sumROC1 -= fabs(trailingValue - tempReal2);
      sumROC1 += fabs(tempReal - inReal[today - 1]);
      trailingValue = tempReal2;
      if( sumROC1 <= periodROC || TA_IS_ZERO(sumROC1) )
      {
         tempReal = 1.0;
      } else 
      {
         tempReal = fabs(periodROC / sumROC1);
      }
      tempReal = tempReal * constDiff + constMax;
      tempReal *= tempReal;
      prevKAMA = (inReal[today++] - prevKAMA) * tempReal + prevKAMA;
      outReal[outIdx++] = prevKAMA;
   }
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

TA_RetCode TA_S_KAMA_Unguarded( int    startIdx,
                                int    endIdx,
                                const float inReal[],
                                int optInTimePeriod,
                                int          *outBegIdx,
                                int          *outNBElement,
                                double        outReal[] )
{
   double constMax;
   double constDiff;
   double tempReal;
   double tempReal2;
   double sumROC1;
   double periodROC;
   double prevKAMA;
   int i;
   int today;
   int outIdx;
   int lookbackTotal;
   int trailingIdx;
   double trailingValue;

   constMax = 2.0 / (30.0 + 1.0);
   constDiff = 2.0 / (2.0 + 1.0) - constMax;
   *outBegIdx= 0;
   *outNBElement= 0;
   if( optInTimePeriod == 1 )
   {
      lookbackTotal = TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_KAMA,Kama);
      if( startIdx < lookbackTotal )
      {
         startIdx = lookbackTotal;
      }
      if( startIdx > endIdx )
      {
         return TA_SUCCESS;
      }
      *outBegIdx= startIdx;
      outIdx = 0;
      today = startIdx;
      while( today <= endIdx )
      {
         outReal[outIdx++] = inReal[today++];
      }
      *outNBElement= outIdx;
      return TA_SUCCESS;
   }
   lookbackTotal = optInTimePeriod + TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_KAMA,Kama);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   sumROC1 = 0.0;
   today = startIdx - lookbackTotal;
   trailingIdx = today;
   i = optInTimePeriod;
   while( i-- > 0 )
   {
      tempReal = inReal[today++];
      tempReal -= inReal[today];
      sumROC1 += fabs(tempReal);
   }
   prevKAMA = inReal[today - 1];
   tempReal = inReal[today];
   tempReal2 = inReal[trailingIdx++];
   periodROC = tempReal - tempReal2;
   trailingValue = tempReal2;
   if( sumROC1 <= periodROC || TA_IS_ZERO(sumROC1) )
   {
      tempReal = 1.0;
   } else 
   {
      tempReal = fabs(periodROC / sumROC1);
   }
   tempReal = tempReal * constDiff + constMax;
   tempReal *= tempReal;
   prevKAMA = (inReal[today++] - prevKAMA) * tempReal + prevKAMA;
   while( today <= startIdx )
   {
      tempReal = inReal[today];
      tempReal2 = inReal[trailingIdx++];
      periodROC = tempReal - tempReal2;
      sumROC1 -= fabs(trailingValue - tempReal2);
      sumROC1 += fabs(tempReal - inReal[today - 1]);
      trailingValue = tempReal2;
      if( sumROC1 <= periodROC || TA_IS_ZERO(sumROC1) )
      {
         tempReal = 1.0;
      } else 
      {
         tempReal = fabs(periodROC / sumROC1);
      }
      tempReal = tempReal * constDiff + constMax;
      tempReal *= tempReal;
      prevKAMA = (inReal[today++] - prevKAMA) * tempReal + prevKAMA;
   }
   outReal[0] = prevKAMA;
   outIdx = 1;
   *outBegIdx= today - 1;
   while( today <= endIdx )
   {
      tempReal = inReal[today];
      tempReal2 = inReal[trailingIdx++];
      periodROC = tempReal - tempReal2;
      sumROC1 -= fabs(trailingValue - tempReal2);
      sumROC1 += fabs(tempReal - inReal[today - 1]);
      trailingValue = tempReal2;
      if( sumROC1 <= periodROC || TA_IS_ZERO(sumROC1) )
      {
         tempReal = 1.0;
      } else 
      {
         tempReal = fabs(periodROC / sumROC1);
      }
      tempReal = tempReal * constDiff + constMax;
      tempReal *= tempReal;
      prevKAMA = (inReal[today++] - prevKAMA) * tempReal + prevKAMA;
      outReal[outIdx++] = prevKAMA;
   }
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

