/* 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
 *  -------------------------------------------------------------------
 *  112400 MF   Template creation.
 *  052603 MF   Adapt code to compile with .NET Managed C++
 */

TA_LIB_API int TA_ATR_Lookback( int optInTimePeriod )
{
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 14;
   else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 )
      return -1;
   /* The ATR lookback is the sum of:
    *    1 + (optInTimePeriod - 1)
    *
    * Where 1 is for the True Range, and
    * (optInTimePeriod-1) is for the simple
    * moving average.
    */
   return optInTimePeriod + TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_ATR,Atr);
}

TA_LIB_API TA_RetCode TA_ATR( int    startIdx,
                              int    endIdx,
                              const double inHigh[],
                              const double inLow[],
                              const double inClose[],
                              int optInTimePeriod,
                              int          *outBegIdx,
                              int          *outNBElement,
                              double        outReal[] )
{
   TA_RetCode retCode;
   int outIdx;
   int today;
   int lookbackTotal;
   int nbATR;
   int outBegIdx1;
   int outNbElement1;
   double prevATR;
   double *tempBuffer;

   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 < 1 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( !outReal )
      return TA_BAD_PARAM;

   /* Average True Range is the greatest of the following:
    *
    *  val1 = distance from today's high to today's low.
    *  val2 = distance from yesterday's close to today's high.
    *  val3 = distance from yesterday's close to today's low.
    *
    * These value are averaged for the specified period using
    * Wilder method. This method have an unstable period comparable
    * to and Exponential Moving Average (EMA).
    */
   *outBegIdx= 0;
   *outNBElement= 0;
   /* Adjust startIdx to account for the lookback period. */
   lookbackTotal = TA_ATR_Lookback(optInTimePeriod);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   /* Make sure there is still something to evaluate. */
   if( startIdx > endIdx )
   {
      return TA_SUCCESS;
   }
   /* Trap the case where no smoothing is needed. */
   if( optInTimePeriod <= 1 )
   {
      /* No smoothing needed. Just do a TRANGE. */
      return TA_TRANGE_Unguarded(startIdx,endIdx,inHigh,inLow,inClose,outBegIdx,outNBElement,outReal);
   }
   /* Allocate an intermediate buffer for TRANGE. */
   tempBuffer = malloc((lookbackTotal + (endIdx - startIdx) + 1) * sizeof(double));
   /* Do TRANGE in the intermediate buffer. */
   retCode = TA_TRANGE_Unguarded(startIdx - lookbackTotal + 1,endIdx,inHigh,inLow,inClose,&outBegIdx1,&outNbElement1,tempBuffer);
   if( retCode != TA_SUCCESS )
   {
      free(tempBuffer);
      return retCode;
   }
   /* First value of the ATR is a simple Average of
    * the TRANGE output for the specified period.
    */
   retCode = TA_SMA_Unguarded(optInTimePeriod - 1,optInTimePeriod - 1,tempBuffer,optInTimePeriod,&outBegIdx1,&outNbElement1,&prevATR);
   if( retCode != TA_SUCCESS )
   {
      free(tempBuffer);
      return retCode;
   }
   /* Subsequent value are smoothed using the
    * previous ATR value (Wilder's approach).
    *  1) Multiply the previous ATR by 'period-1'.
    *  2) Add today TR value.
    *  3) Divide by 'period'.
    */
   today = optInTimePeriod;
   outIdx = TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_ATR,Atr);
   /* Skip the unstable period. */
   while( outIdx != 0 )
   {
      prevATR *= optInTimePeriod - 1;
      prevATR += tempBuffer[today++];
      prevATR /= optInTimePeriod;
      outIdx -= 1;
   }
   /* Now start to write the final ATR in the caller
    * provided outReal.
    */
   outIdx = 1;
   outReal[0] = prevATR;
   /* Now do the number of requested ATR. */
   nbATR = endIdx - startIdx + 1;
   while( --nbATR != 0 )
   {
      prevATR *= optInTimePeriod - 1;
      prevATR += tempBuffer[today++];
      prevATR /= optInTimePeriod;
      outReal[outIdx++] = prevATR;
   }
   *outBegIdx= startIdx;
   *outNBElement= outIdx;
   free(tempBuffer);
   return retCode;
}

TA_LIB_API TA_RetCode TA_ATR_Unguarded( int    startIdx,
                                        int    endIdx,
                                        const double inHigh[],
                                        const double inLow[],
                                        const double inClose[],
                                        int optInTimePeriod,
                                        int          *outBegIdx,
                                        int          *outNBElement,
                                        double        outReal[] )
{
   TA_RetCode retCode;
   int outIdx;
   int today;
   int lookbackTotal;
   int nbATR;
   int outBegIdx1;
   int outNbElement1;
   double prevATR;
   double *tempBuffer;

   *outBegIdx= 0;
   *outNBElement= 0;
   lookbackTotal = TA_ATR_Lookback(optInTimePeriod);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      return TA_SUCCESS;
   }
   if( optInTimePeriod <= 1 )
   {
      return TA_TRANGE_Unguarded(startIdx,endIdx,inHigh,inLow,inClose,outBegIdx,outNBElement,outReal);
   }
   tempBuffer = malloc((lookbackTotal + (endIdx - startIdx) + 1) * sizeof(double));
   retCode = TA_TRANGE_Unguarded(startIdx - lookbackTotal + 1,endIdx,inHigh,inLow,inClose,&outBegIdx1,&outNbElement1,tempBuffer);
   if( retCode != TA_SUCCESS )
   {
      free(tempBuffer);
      return retCode;
   }
   retCode = TA_SMA_Unguarded(optInTimePeriod - 1,optInTimePeriod - 1,tempBuffer,optInTimePeriod,&outBegIdx1,&outNbElement1,&prevATR);
   if( retCode != TA_SUCCESS )
   {
      free(tempBuffer);
      return retCode;
   }
   today = optInTimePeriod;
   outIdx = TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_ATR,Atr);
   while( outIdx != 0 )
   {
      prevATR *= optInTimePeriod - 1;
      prevATR += tempBuffer[today++];
      prevATR /= optInTimePeriod;
      outIdx -= 1;
   }
   outIdx = 1;
   outReal[0] = prevATR;
   nbATR = endIdx - startIdx + 1;
   while( --nbATR != 0 )
   {
      prevATR *= optInTimePeriod - 1;
      prevATR += tempBuffer[today++];
      prevATR /= optInTimePeriod;
      outReal[outIdx++] = prevATR;
   }
   *outBegIdx= startIdx;
   *outNBElement= outIdx;
   free(tempBuffer);
   return retCode;
}

TA_RetCode TA_S_ATR( int    startIdx,
                     int    endIdx,
                     const float inHigh[],
                     const float inLow[],
                     const float inClose[],
                     int optInTimePeriod,
                     int          *outBegIdx,
                     int          *outNBElement,
                     double        outReal[] )
{
   TA_RetCode retCode;
   int outIdx;
   int today;
   int lookbackTotal;
   int nbATR;
   int outBegIdx1;
   int outNbElement1;
   double prevATR;
   double *tempBuffer;

   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 < 1 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( !outReal )
      return TA_BAD_PARAM;

   *outBegIdx= 0;
   *outNBElement= 0;
   lookbackTotal = TA_ATR_Lookback(optInTimePeriod);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      return TA_SUCCESS;
   }
   if( optInTimePeriod <= 1 )
   {
      return TA_S_TRANGE_Unguarded(startIdx,endIdx,inHigh,inLow,inClose,outBegIdx,outNBElement,outReal);
   }
   tempBuffer = malloc((lookbackTotal + (endIdx - startIdx) + 1) * sizeof(double));
   retCode = TA_S_TRANGE_Unguarded(startIdx - lookbackTotal + 1,endIdx,inHigh,inLow,inClose,&outBegIdx1,&outNbElement1,tempBuffer);
   if( retCode != TA_SUCCESS )
   {
      free(tempBuffer);
      return retCode;
   }
   retCode = TA_SMA_Unguarded(optInTimePeriod - 1,optInTimePeriod - 1,tempBuffer,optInTimePeriod,&outBegIdx1,&outNbElement1,&prevATR);
   if( retCode != TA_SUCCESS )
   {
      free(tempBuffer);
      return retCode;
   }
   today = optInTimePeriod;
   outIdx = TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_ATR,Atr);
   while( outIdx != 0 )
   {
      prevATR *= optInTimePeriod - 1;
      prevATR += tempBuffer[today++];
      prevATR /= optInTimePeriod;
      outIdx -= 1;
   }
   outIdx = 1;
   outReal[0] = prevATR;
   nbATR = endIdx - startIdx + 1;
   while( --nbATR != 0 )
   {
      prevATR *= optInTimePeriod - 1;
      prevATR += tempBuffer[today++];
      prevATR /= optInTimePeriod;
      outReal[outIdx++] = prevATR;
   }
   *outBegIdx= startIdx;
   *outNBElement= outIdx;
   free(tempBuffer);
   return retCode;
}

TA_RetCode TA_S_ATR_Unguarded( int    startIdx,
                               int    endIdx,
                               const float inHigh[],
                               const float inLow[],
                               const float inClose[],
                               int optInTimePeriod,
                               int          *outBegIdx,
                               int          *outNBElement,
                               double        outReal[] )
{
   TA_RetCode retCode;
   int outIdx;
   int today;
   int lookbackTotal;
   int nbATR;
   int outBegIdx1;
   int outNbElement1;
   double prevATR;
   double *tempBuffer;

   *outBegIdx= 0;
   *outNBElement= 0;
   lookbackTotal = TA_ATR_Lookback(optInTimePeriod);
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      return TA_SUCCESS;
   }
   if( optInTimePeriod <= 1 )
   {
      return TA_S_TRANGE_Unguarded(startIdx,endIdx,inHigh,inLow,inClose,outBegIdx,outNBElement,outReal);
   }
   tempBuffer = malloc((lookbackTotal + (endIdx - startIdx) + 1) * sizeof(double));
   retCode = TA_S_TRANGE_Unguarded(startIdx - lookbackTotal + 1,endIdx,inHigh,inLow,inClose,&outBegIdx1,&outNbElement1,tempBuffer);
   if( retCode != TA_SUCCESS )
   {
      free(tempBuffer);
      return retCode;
   }
   retCode = TA_SMA_Unguarded(optInTimePeriod - 1,optInTimePeriod - 1,tempBuffer,optInTimePeriod,&outBegIdx1,&outNbElement1,&prevATR);
   if( retCode != TA_SUCCESS )
   {
      free(tempBuffer);
      return retCode;
   }
   today = optInTimePeriod;
   outIdx = TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_ATR,Atr);
   while( outIdx != 0 )
   {
      prevATR *= optInTimePeriod - 1;
      prevATR += tempBuffer[today++];
      prevATR /= optInTimePeriod;
      outIdx -= 1;
   }
   outIdx = 1;
   outReal[0] = prevATR;
   nbATR = endIdx - startIdx + 1;
   while( --nbATR != 0 )
   {
      prevATR *= optInTimePeriod - 1;
      prevATR += tempBuffer[today++];
      prevATR /= optInTimePeriod;
      outReal[outIdx++] = prevATR;
   }
   *outBegIdx= startIdx;
   *outNBElement= outIdx;
   free(tempBuffer);
   return retCode;
}

