/* 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
 *  AA       Andrew Atkinson
 *
 * Change history:
 *
 *  MMDDYY BY   Description
 *  -------------------------------------------------------------------
 *  112400 MF   Template creation.
 *  052603 MF   Adapt code to compile with .NET Managed C++
 *  020605 AA   Fix #1117656. NULL pointer assignement.
 */

TA_LIB_API int TA_TRIX_Lookback( int optInTimePeriod )
{
   int emaLookback;
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 30;
   else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 )
      return -1;
   emaLookback = TA_EMA_Lookback(optInTimePeriod);
   return emaLookback * 3 + TA_ROCR_Lookback(1);
}

TA_LIB_API TA_RetCode TA_TRIX( int    startIdx,
                               int    endIdx,
                               const double inReal[],
                               int optInTimePeriod,
                               int          *outBegIdx,
                               int          *outNBElement,
                               double        outReal[] )
{
   double *tempBuffer;
   int nbElement;
   int begIdx;
   int totalLookback;
   int emaLookback;
   int rocLookback;
   TA_RetCode retCode;
   int nbElementToOutput;

   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;

   /* Adjust the startIdx to account for the lookback. */
   emaLookback = TA_EMA_Lookback(optInTimePeriod);
   rocLookback = TA_ROCR_Lookback(1);
   totalLookback = emaLookback * 3 + rocLookback;
   if( startIdx < totalLookback )
   {
      startIdx = totalLookback;
   }
   /* Make sure there is still something to evaluate. */
   if( startIdx > endIdx )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      return TA_SUCCESS;
   }
   *outBegIdx= startIdx;
   nbElementToOutput = endIdx - startIdx + 1 + totalLookback;
   /* Allocate a temporary buffer for performing
    * the calculation.
    */
   tempBuffer = malloc(nbElementToOutput * sizeof(double));
   if( !tempBuffer )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      return TA_ALLOC_ERR;
   }
   /* Calculate the first EMA */
   retCode = TA_EMA_Unguarded(startIdx - totalLookback,endIdx,inReal,optInTimePeriod,&begIdx,&nbElement,tempBuffer);
   /* Verify for failure or if not enough data after
    * calculating the EMA.
    */
   if( retCode != TA_SUCCESS || nbElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      free(tempBuffer);
      return retCode;
   }
   nbElementToOutput -= 1;
   /* Make this variable zero base from now on. */
   /* Calculate the second EMA */
   nbElementToOutput -= emaLookback;
   retCode = TA_EMA_Unguarded(0,nbElementToOutput,tempBuffer,optInTimePeriod,&begIdx,&nbElement,tempBuffer);
   /* Verify for failure or if not enough data after
    * calculating the EMA.
    */
   if( retCode != TA_SUCCESS || nbElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      free(tempBuffer);
      return retCode;
   }
   /* Calculate the third EMA */
   nbElementToOutput -= emaLookback;
   retCode = TA_EMA_Unguarded(0,nbElementToOutput,tempBuffer,optInTimePeriod,&begIdx,&nbElement,tempBuffer);
   /* Verify for failure or if not enough data after
    * calculating the EMA.
    */
   if( retCode != TA_SUCCESS || nbElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      free(tempBuffer);
      return retCode;
   }
   /* Calculate the 1-day Rate-Of-Change */
   nbElementToOutput -= emaLookback;
   retCode = TA_ROC_Unguarded(0,nbElementToOutput,tempBuffer,1,&begIdx,outNBElement,outReal);
   free(tempBuffer);
   /* Verify for failure or if not enough data after
    * calculating the rate-of-change.
    */
   if( retCode != TA_SUCCESS || (int)*outNBElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      return retCode;
   }
   return TA_SUCCESS;
}

TA_LIB_API TA_RetCode TA_TRIX_Unguarded( int    startIdx,
                                         int    endIdx,
                                         const double inReal[],
                                         int optInTimePeriod,
                                         int          *outBegIdx,
                                         int          *outNBElement,
                                         double        outReal[] )
{
   double *tempBuffer;
   int nbElement;
   int begIdx;
   int totalLookback;
   int emaLookback;
   int rocLookback;
   TA_RetCode retCode;
   int nbElementToOutput;

   emaLookback = TA_EMA_Lookback(optInTimePeriod);
   rocLookback = TA_ROCR_Lookback(1);
   totalLookback = emaLookback * 3 + rocLookback;
   if( startIdx < totalLookback )
   {
      startIdx = totalLookback;
   }
   if( startIdx > endIdx )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      return TA_SUCCESS;
   }
   *outBegIdx= startIdx;
   nbElementToOutput = endIdx - startIdx + 1 + totalLookback;
   tempBuffer = malloc(nbElementToOutput * sizeof(double));
   if( !tempBuffer )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      return TA_ALLOC_ERR;
   }
   retCode = TA_EMA_Unguarded(startIdx - totalLookback,endIdx,inReal,optInTimePeriod,&begIdx,&nbElement,tempBuffer);
   if( retCode != TA_SUCCESS || nbElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      free(tempBuffer);
      return retCode;
   }
   nbElementToOutput -= 1;
   nbElementToOutput -= emaLookback;
   retCode = TA_EMA_Unguarded(0,nbElementToOutput,tempBuffer,optInTimePeriod,&begIdx,&nbElement,tempBuffer);
   if( retCode != TA_SUCCESS || nbElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      free(tempBuffer);
      return retCode;
   }
   nbElementToOutput -= emaLookback;
   retCode = TA_EMA_Unguarded(0,nbElementToOutput,tempBuffer,optInTimePeriod,&begIdx,&nbElement,tempBuffer);
   if( retCode != TA_SUCCESS || nbElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      free(tempBuffer);
      return retCode;
   }
   nbElementToOutput -= emaLookback;
   retCode = TA_ROC_Unguarded(0,nbElementToOutput,tempBuffer,1,&begIdx,outNBElement,outReal);
   free(tempBuffer);
   if( retCode != TA_SUCCESS || (int)*outNBElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      return retCode;
   }
   return TA_SUCCESS;
}

TA_RetCode TA_S_TRIX( int    startIdx,
                      int    endIdx,
                      const float inReal[],
                      int optInTimePeriod,
                      int          *outBegIdx,
                      int          *outNBElement,
                      double        outReal[] )
{
   double *tempBuffer;
   int nbElement;
   int begIdx;
   int totalLookback;
   int emaLookback;
   int rocLookback;
   TA_RetCode retCode;
   int nbElementToOutput;

   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;

   emaLookback = TA_EMA_Lookback(optInTimePeriod);
   rocLookback = TA_ROCR_Lookback(1);
   totalLookback = emaLookback * 3 + rocLookback;
   if( startIdx < totalLookback )
   {
      startIdx = totalLookback;
   }
   if( startIdx > endIdx )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      return TA_SUCCESS;
   }
   *outBegIdx= startIdx;
   nbElementToOutput = endIdx - startIdx + 1 + totalLookback;
   tempBuffer = malloc(nbElementToOutput * sizeof(double));
   if( !tempBuffer )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      return TA_ALLOC_ERR;
   }
   retCode = TA_S_EMA_Unguarded(startIdx - totalLookback,endIdx,inReal,optInTimePeriod,&begIdx,&nbElement,tempBuffer);
   if( retCode != TA_SUCCESS || nbElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      free(tempBuffer);
      return retCode;
   }
   nbElementToOutput -= 1;
   nbElementToOutput -= emaLookback;
   retCode = TA_EMA_Unguarded(0,nbElementToOutput,tempBuffer,optInTimePeriod,&begIdx,&nbElement,tempBuffer);
   if( retCode != TA_SUCCESS || nbElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      free(tempBuffer);
      return retCode;
   }
   nbElementToOutput -= emaLookback;
   retCode = TA_EMA_Unguarded(0,nbElementToOutput,tempBuffer,optInTimePeriod,&begIdx,&nbElement,tempBuffer);
   if( retCode != TA_SUCCESS || nbElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      free(tempBuffer);
      return retCode;
   }
   nbElementToOutput -= emaLookback;
   retCode = TA_ROC_Unguarded(0,nbElementToOutput,tempBuffer,1,&begIdx,outNBElement,outReal);
   free(tempBuffer);
   if( retCode != TA_SUCCESS || (int)*outNBElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      return retCode;
   }
   return TA_SUCCESS;
}

TA_RetCode TA_S_TRIX_Unguarded( int    startIdx,
                                int    endIdx,
                                const float inReal[],
                                int optInTimePeriod,
                                int          *outBegIdx,
                                int          *outNBElement,
                                double        outReal[] )
{
   double *tempBuffer;
   int nbElement;
   int begIdx;
   int totalLookback;
   int emaLookback;
   int rocLookback;
   TA_RetCode retCode;
   int nbElementToOutput;

   emaLookback = TA_EMA_Lookback(optInTimePeriod);
   rocLookback = TA_ROCR_Lookback(1);
   totalLookback = emaLookback * 3 + rocLookback;
   if( startIdx < totalLookback )
   {
      startIdx = totalLookback;
   }
   if( startIdx > endIdx )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      return TA_SUCCESS;
   }
   *outBegIdx= startIdx;
   nbElementToOutput = endIdx - startIdx + 1 + totalLookback;
   tempBuffer = malloc(nbElementToOutput * sizeof(double));
   if( !tempBuffer )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      return TA_ALLOC_ERR;
   }
   retCode = TA_S_EMA_Unguarded(startIdx - totalLookback,endIdx,inReal,optInTimePeriod,&begIdx,&nbElement,tempBuffer);
   if( retCode != TA_SUCCESS || nbElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      free(tempBuffer);
      return retCode;
   }
   nbElementToOutput -= 1;
   nbElementToOutput -= emaLookback;
   retCode = TA_EMA_Unguarded(0,nbElementToOutput,tempBuffer,optInTimePeriod,&begIdx,&nbElement,tempBuffer);
   if( retCode != TA_SUCCESS || nbElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      free(tempBuffer);
      return retCode;
   }
   nbElementToOutput -= emaLookback;
   retCode = TA_EMA_Unguarded(0,nbElementToOutput,tempBuffer,optInTimePeriod,&begIdx,&nbElement,tempBuffer);
   if( retCode != TA_SUCCESS || nbElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      free(tempBuffer);
      return retCode;
   }
   nbElementToOutput -= emaLookback;
   retCode = TA_ROC_Unguarded(0,nbElementToOutput,tempBuffer,1,&begIdx,outNBElement,outReal);
   free(tempBuffer);
   if( retCode != TA_SUCCESS || (int)*outNBElement == 0 )
   {
      *outNBElement= 0;
      *outBegIdx= 0;
      return retCode;
   }
   return TA_SUCCESS;
}

