/* 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
 *  -------------------------------------------------------------------
 *  RM       Robert Meier
 *  MF       Mario Fortier
 *
 * Change history:
 *
 *  MMDDYY BY     Description
 *  -------------------------------------------------------------------
 *  120307 RM     Initial Version
 *  120907 MF     Handling of a few limit cases
 */

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

TA_LIB_API TA_RetCode TA_ACCBANDS( int    startIdx,
                                   int    endIdx,
                                   const double inHigh[],
                                   const double inLow[],
                                   const double inClose[],
                                   int optInTimePeriod,
                                   int          *outBegIdx,
                                   int          *outNBElement,
                                   double        outRealUpperBand[],
                                   double        outRealMiddleBand[],
                                   double        outRealLowerBand[] )
{
   TA_RetCode retCode;
   double *tempBuffer1;
   double *tempBuffer2;
   int outBegIdxDummy;
   int outNbElementDummy;
   int i;
   int j;
   int outputSize;
   int bufferSize;
   int lookbackTotal;
   double tempReal;

   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 = 20;
   else if( (int)optInTimePeriod < 2 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( !outRealUpperBand )
      return TA_BAD_PARAM;
   if( !outRealMiddleBand )
      return TA_BAD_PARAM;
   if( !outRealLowerBand )
      return TA_BAD_PARAM;

   /* Identify the minimum number of price bar needed
    * to calculate at least one output.
    */
   lookbackTotal = TA_SMA_Lookback(optInTimePeriod);
   /* 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;
   }
   /* Buffer will contains also the lookback required for SMA
    * to satisfy the caller requested startIdx/endIdx.
    */
   outputSize = endIdx - startIdx + 1;
   bufferSize = outputSize + lookbackTotal;
   tempBuffer1 = malloc(bufferSize * sizeof(double));
   if( !tempBuffer1 )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_ALLOC_ERR;
   }
   tempBuffer2 = malloc(bufferSize * sizeof(double));
   if( !tempBuffer2 )
   {
      free(tempBuffer1);
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_ALLOC_ERR;
   }
   /* Calculate the upper/lower band at the same time (no SMA yet).
    * Must start calculation back enough to cover the lookback
    * required later for the SMA.
    */
   for( j = 0, i = startIdx - lookbackTotal; i <= endIdx; i += 1, j += 1 )
   {
      tempReal = inHigh[i] + inLow[i];
      if( !TA_IS_ZERO(tempReal) )
      {
         tempReal = 4 * (inHigh[i] - inLow[i]) / tempReal;
         tempBuffer1[j] = inHigh[i] * (1 + tempReal);
         tempBuffer2[j] = inLow[i] * (1 - tempReal);
      } else 
      {
         tempBuffer1[j] = inHigh[i];
         tempBuffer2[j] = inLow[i];
      }
   }
   /* Calculate the middle band, which is a moving average of the close. */
   retCode = TA_SMA_Unguarded(startIdx,endIdx,inClose,optInTimePeriod,&outBegIdxDummy,&outNbElementDummy,outRealMiddleBand);
   if( retCode != TA_SUCCESS || (int)outNbElementDummy != outputSize )
   {
      free(tempBuffer1);
      free(tempBuffer2);
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   /* Now let's take the SMA for the upper band. */
   retCode = TA_SMA_Unguarded(0,bufferSize - 1,tempBuffer1,optInTimePeriod,&outBegIdxDummy,&outNbElementDummy,outRealUpperBand);
   if( retCode != TA_SUCCESS || (int)outNbElementDummy != outputSize )
   {
      free(tempBuffer1);
      free(tempBuffer2);
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   /* Now let's take the SMA for the lower band. */
   retCode = TA_SMA_Unguarded(0,bufferSize - 1,tempBuffer2,optInTimePeriod,&outBegIdxDummy,&outNbElementDummy,outRealLowerBand);
   free(tempBuffer1);
   free(tempBuffer2);
   if( retCode != TA_SUCCESS || (int)outNbElementDummy != outputSize )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   *outBegIdx= startIdx;
   *outNBElement= outputSize;
   return TA_SUCCESS;
}

TA_LIB_API TA_RetCode TA_ACCBANDS_Unguarded( int    startIdx,
                                             int    endIdx,
                                             const double inHigh[],
                                             const double inLow[],
                                             const double inClose[],
                                             int optInTimePeriod,
                                             int          *outBegIdx,
                                             int          *outNBElement,
                                             double        outRealUpperBand[],
                                             double        outRealMiddleBand[],
                                             double        outRealLowerBand[] )
{
   TA_RetCode retCode;
   double *tempBuffer1;
   double *tempBuffer2;
   int outBegIdxDummy;
   int outNbElementDummy;
   int i;
   int j;
   int outputSize;
   int bufferSize;
   int lookbackTotal;
   double tempReal;

   lookbackTotal = TA_SMA_Lookback(optInTimePeriod);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   outputSize = endIdx - startIdx + 1;
   bufferSize = outputSize + lookbackTotal;
   tempBuffer1 = malloc(bufferSize * sizeof(double));
   if( !tempBuffer1 )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_ALLOC_ERR;
   }
   tempBuffer2 = malloc(bufferSize * sizeof(double));
   if( !tempBuffer2 )
   {
      free(tempBuffer1);
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_ALLOC_ERR;
   }
   for( j = 0, i = startIdx - lookbackTotal; i <= endIdx; i += 1, j += 1 )
   {
      tempReal = inHigh[i] + inLow[i];
      if( !TA_IS_ZERO(tempReal) )
      {
         tempReal = 4 * (inHigh[i] - inLow[i]) / tempReal;
         tempBuffer1[j] = inHigh[i] * (1 + tempReal);
         tempBuffer2[j] = inLow[i] * (1 - tempReal);
      } else 
      {
         tempBuffer1[j] = inHigh[i];
         tempBuffer2[j] = inLow[i];
      }
   }
   retCode = TA_SMA_Unguarded(startIdx,endIdx,inClose,optInTimePeriod,&outBegIdxDummy,&outNbElementDummy,outRealMiddleBand);
   if( retCode != TA_SUCCESS || (int)outNbElementDummy != outputSize )
   {
      free(tempBuffer1);
      free(tempBuffer2);
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   retCode = TA_SMA_Unguarded(0,bufferSize - 1,tempBuffer1,optInTimePeriod,&outBegIdxDummy,&outNbElementDummy,outRealUpperBand);
   if( retCode != TA_SUCCESS || (int)outNbElementDummy != outputSize )
   {
      free(tempBuffer1);
      free(tempBuffer2);
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   retCode = TA_SMA_Unguarded(0,bufferSize - 1,tempBuffer2,optInTimePeriod,&outBegIdxDummy,&outNbElementDummy,outRealLowerBand);
   free(tempBuffer1);
   free(tempBuffer2);
   if( retCode != TA_SUCCESS || (int)outNbElementDummy != outputSize )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   *outBegIdx= startIdx;
   *outNBElement= outputSize;
   return TA_SUCCESS;
}

TA_RetCode TA_S_ACCBANDS( int    startIdx,
                          int    endIdx,
                          const float inHigh[],
                          const float inLow[],
                          const float inClose[],
                          int optInTimePeriod,
                          int          *outBegIdx,
                          int          *outNBElement,
                          double        outRealUpperBand[],
                          double        outRealMiddleBand[],
                          double        outRealLowerBand[] )
{
   TA_RetCode retCode;
   double *tempBuffer1;
   double *tempBuffer2;
   int outBegIdxDummy;
   int outNbElementDummy;
   int i;
   int j;
   int outputSize;
   int bufferSize;
   int lookbackTotal;
   double tempReal;

   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 = 20;
   else if( (int)optInTimePeriod < 2 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( !outRealUpperBand )
      return TA_BAD_PARAM;
   if( !outRealMiddleBand )
      return TA_BAD_PARAM;
   if( !outRealLowerBand )
      return TA_BAD_PARAM;

   lookbackTotal = TA_SMA_Lookback(optInTimePeriod);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   outputSize = endIdx - startIdx + 1;
   bufferSize = outputSize + lookbackTotal;
   tempBuffer1 = malloc(bufferSize * sizeof(double));
   if( !tempBuffer1 )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_ALLOC_ERR;
   }
   tempBuffer2 = malloc(bufferSize * sizeof(double));
   if( !tempBuffer2 )
   {
      free(tempBuffer1);
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_ALLOC_ERR;
   }
   for( j = 0, i = startIdx - lookbackTotal; i <= endIdx; i += 1, j += 1 )
   {
      tempReal = inHigh[i] + inLow[i];
      if( !TA_IS_ZERO(tempReal) )
      {
         tempReal = 4 * (inHigh[i] - inLow[i]) / tempReal;
         tempBuffer1[j] = inHigh[i] * (1 + tempReal);
         tempBuffer2[j] = inLow[i] * (1 - tempReal);
      } else 
      {
         tempBuffer1[j] = inHigh[i];
         tempBuffer2[j] = inLow[i];
      }
   }
   retCode = TA_S_SMA_Unguarded(startIdx,endIdx,inClose,optInTimePeriod,&outBegIdxDummy,&outNbElementDummy,outRealMiddleBand);
   if( retCode != TA_SUCCESS || (int)outNbElementDummy != outputSize )
   {
      free(tempBuffer1);
      free(tempBuffer2);
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   retCode = TA_SMA_Unguarded(0,bufferSize - 1,tempBuffer1,optInTimePeriod,&outBegIdxDummy,&outNbElementDummy,outRealUpperBand);
   if( retCode != TA_SUCCESS || (int)outNbElementDummy != outputSize )
   {
      free(tempBuffer1);
      free(tempBuffer2);
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   retCode = TA_SMA_Unguarded(0,bufferSize - 1,tempBuffer2,optInTimePeriod,&outBegIdxDummy,&outNbElementDummy,outRealLowerBand);
   free(tempBuffer1);
   free(tempBuffer2);
   if( retCode != TA_SUCCESS || (int)outNbElementDummy != outputSize )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   *outBegIdx= startIdx;
   *outNBElement= outputSize;
   return TA_SUCCESS;
}

TA_RetCode TA_S_ACCBANDS_Unguarded( int    startIdx,
                                    int    endIdx,
                                    const float inHigh[],
                                    const float inLow[],
                                    const float inClose[],
                                    int optInTimePeriod,
                                    int          *outBegIdx,
                                    int          *outNBElement,
                                    double        outRealUpperBand[],
                                    double        outRealMiddleBand[],
                                    double        outRealLowerBand[] )
{
   TA_RetCode retCode;
   double *tempBuffer1;
   double *tempBuffer2;
   int outBegIdxDummy;
   int outNbElementDummy;
   int i;
   int j;
   int outputSize;
   int bufferSize;
   int lookbackTotal;
   double tempReal;

   lookbackTotal = TA_SMA_Lookback(optInTimePeriod);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_SUCCESS;
   }
   outputSize = endIdx - startIdx + 1;
   bufferSize = outputSize + lookbackTotal;
   tempBuffer1 = malloc(bufferSize * sizeof(double));
   if( !tempBuffer1 )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_ALLOC_ERR;
   }
   tempBuffer2 = malloc(bufferSize * sizeof(double));
   if( !tempBuffer2 )
   {
      free(tempBuffer1);
      *outBegIdx= 0;
      *outNBElement= 0;
      return TA_ALLOC_ERR;
   }
   for( j = 0, i = startIdx - lookbackTotal; i <= endIdx; i += 1, j += 1 )
   {
      tempReal = inHigh[i] + inLow[i];
      if( !TA_IS_ZERO(tempReal) )
      {
         tempReal = 4 * (inHigh[i] - inLow[i]) / tempReal;
         tempBuffer1[j] = inHigh[i] * (1 + tempReal);
         tempBuffer2[j] = inLow[i] * (1 - tempReal);
      } else 
      {
         tempBuffer1[j] = inHigh[i];
         tempBuffer2[j] = inLow[i];
      }
   }
   retCode = TA_S_SMA_Unguarded(startIdx,endIdx,inClose,optInTimePeriod,&outBegIdxDummy,&outNbElementDummy,outRealMiddleBand);
   if( retCode != TA_SUCCESS || (int)outNbElementDummy != outputSize )
   {
      free(tempBuffer1);
      free(tempBuffer2);
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   retCode = TA_SMA_Unguarded(0,bufferSize - 1,tempBuffer1,optInTimePeriod,&outBegIdxDummy,&outNbElementDummy,outRealUpperBand);
   if( retCode != TA_SUCCESS || (int)outNbElementDummy != outputSize )
   {
      free(tempBuffer1);
      free(tempBuffer2);
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   retCode = TA_SMA_Unguarded(0,bufferSize - 1,tempBuffer2,optInTimePeriod,&outBegIdxDummy,&outNbElementDummy,outRealLowerBand);
   free(tempBuffer1);
   free(tempBuffer2);
   if( retCode != TA_SUCCESS || (int)outNbElementDummy != outputSize )
   {
      *outBegIdx= 0;
      *outNBElement= 0;
      return retCode;
   }
   *outBegIdx= startIdx;
   *outNBElement= outputSize;
   return TA_SUCCESS;
}

