/* 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 #1117666 Lookback bug.
 */

TA_LIB_API int TA_PPO_Lookback( int optInFastPeriod, int optInSlowPeriod, TA_MAType optInMAType )
{
   if( (int)optInFastPeriod == (int)0x80000000 )
      optInFastPeriod = 12;
   else if( (int)optInFastPeriod < 2 || (int)optInFastPeriod > 100000 )
      return -1;
   if( (int)optInSlowPeriod == (int)0x80000000 )
      optInSlowPeriod = 26;
   else if( (int)optInSlowPeriod < 2 || (int)optInSlowPeriod > 100000 )
      return -1;
   if( (int)optInMAType == (int)0x80000000 )
      optInMAType = 0;
   /* Lookback is driven by the slowest MA. */
   return TA_MA_Lookback(fmax(optInSlowPeriod,optInFastPeriod),optInMAType);
}

TA_LIB_API TA_RetCode TA_PPO( int    startIdx,
                              int    endIdx,
                              const double inReal[],
                              int optInFastPeriod,
                              int optInSlowPeriod,
                              TA_MAType optInMAType,
                              int          *outBegIdx,
                              int          *outNBElement,
                              double        outReal[] )
{
   double *tempBuffer;
   TA_RetCode retCode;
   double tempReal;
   int tempInteger;
   int outBegIdx1;
   int outNbElement1;
   int outBegIdx2;
   int outNbElement2;
   int i;
   int j;

   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)optInFastPeriod == (int)0x80000000 )
      optInFastPeriod = 12;
   else if( (int)optInFastPeriod < 2 || (int)optInFastPeriod > 100000 )
      return TA_BAD_PARAM;
   if( (int)optInSlowPeriod == (int)0x80000000 )
      optInSlowPeriod = 26;
   else if( (int)optInSlowPeriod < 2 || (int)optInSlowPeriod > 100000 )
      return TA_BAD_PARAM;
   if( (int)optInMAType == (int)0x80000000 )
      optInMAType = 0;
   if( !outReal )
      return TA_BAD_PARAM;

   /* Allocate an intermediate buffer. */
   tempBuffer = malloc((endIdx - startIdx + 1) * sizeof(double));
   if( !tempBuffer )
   {
      return TA_ALLOC_ERR;
   }
   /* Make sure slow is really slower than
    * the fast period! if not, swap...
    */
   if( optInSlowPeriod < optInFastPeriod )
   {
      /* swap */
      tempInteger = optInSlowPeriod;
      optInSlowPeriod = optInFastPeriod;
      optInFastPeriod = tempInteger;
   }
   /* Calculate the fast MA into the tempBuffer. */
   retCode = TA_MA_Unguarded(startIdx,endIdx,inReal,optInFastPeriod,optInMAType,&outBegIdx2,&outNbElement2,tempBuffer);
   if( retCode == TA_SUCCESS )
   {
      /* Calculate the slow MA into the output. */
      retCode = TA_MA_Unguarded(startIdx,endIdx,inReal,optInSlowPeriod,optInMAType,&outBegIdx1,&outNbElement1,outReal);
      if( retCode == TA_SUCCESS )
      {
         tempInteger = outBegIdx1 - outBegIdx2;
         /* Calculate ((fast MA)-(slow MA))/(slow MA) in the output. */
         for( i = 0, j = tempInteger; i < outNbElement1; i += 1, j += 1 )
         {
            tempReal = outReal[i];
            if( !TA_IS_ZERO(tempReal) )
            {
               outReal[i] = (tempBuffer[j] - tempReal) / tempReal * 100.0;
            } else 
            {
               outReal[i] = 0.0;
            }
         }
         *outBegIdx= outBegIdx1;
         *outNBElement= outNbElement1;
      }
   }
   free(tempBuffer);
   return retCode;
}

TA_LIB_API TA_RetCode TA_PPO_Unguarded( int    startIdx,
                                        int    endIdx,
                                        const double inReal[],
                                        int optInFastPeriod,
                                        int optInSlowPeriod,
                                        TA_MAType optInMAType,
                                        int          *outBegIdx,
                                        int          *outNBElement,
                                        double        outReal[] )
{
   double *tempBuffer;
   TA_RetCode retCode;
   double tempReal;
   int tempInteger;
   int outBegIdx1;
   int outNbElement1;
   int outBegIdx2;
   int outNbElement2;
   int i;
   int j;

   tempBuffer = malloc((endIdx - startIdx + 1) * sizeof(double));
   if( !tempBuffer )
   {
      return TA_ALLOC_ERR;
   }
   if( optInSlowPeriod < optInFastPeriod )
   {
      tempInteger = optInSlowPeriod;
      optInSlowPeriod = optInFastPeriod;
      optInFastPeriod = tempInteger;
   }
   retCode = TA_MA_Unguarded(startIdx,endIdx,inReal,optInFastPeriod,optInMAType,&outBegIdx2,&outNbElement2,tempBuffer);
   if( retCode == TA_SUCCESS )
   {
      retCode = TA_MA_Unguarded(startIdx,endIdx,inReal,optInSlowPeriod,optInMAType,&outBegIdx1,&outNbElement1,outReal);
      if( retCode == TA_SUCCESS )
      {
         tempInteger = outBegIdx1 - outBegIdx2;
         for( i = 0, j = tempInteger; i < outNbElement1; i += 1, j += 1 )
         {
            tempReal = outReal[i];
            if( !TA_IS_ZERO(tempReal) )
            {
               outReal[i] = (tempBuffer[j] - tempReal) / tempReal * 100.0;
            } else 
            {
               outReal[i] = 0.0;
            }
         }
         *outBegIdx= outBegIdx1;
         *outNBElement= outNbElement1;
      }
   }
   free(tempBuffer);
   return retCode;
}

TA_RetCode TA_S_PPO( int    startIdx,
                     int    endIdx,
                     const float inReal[],
                     int optInFastPeriod,
                     int optInSlowPeriod,
                     TA_MAType optInMAType,
                     int          *outBegIdx,
                     int          *outNBElement,
                     double        outReal[] )
{
   double *tempBuffer;
   TA_RetCode retCode;
   double tempReal;
   int tempInteger;
   int outBegIdx1;
   int outNbElement1;
   int outBegIdx2;
   int outNbElement2;
   int i;
   int j;

   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)optInFastPeriod == (int)0x80000000 )
      optInFastPeriod = 12;
   else if( (int)optInFastPeriod < 2 || (int)optInFastPeriod > 100000 )
      return TA_BAD_PARAM;
   if( (int)optInSlowPeriod == (int)0x80000000 )
      optInSlowPeriod = 26;
   else if( (int)optInSlowPeriod < 2 || (int)optInSlowPeriod > 100000 )
      return TA_BAD_PARAM;
   if( (int)optInMAType == (int)0x80000000 )
      optInMAType = 0;
   if( !outReal )
      return TA_BAD_PARAM;

   tempBuffer = malloc((endIdx - startIdx + 1) * sizeof(double));
   if( !tempBuffer )
   {
      return TA_ALLOC_ERR;
   }
   if( optInSlowPeriod < optInFastPeriod )
   {
      tempInteger = optInSlowPeriod;
      optInSlowPeriod = optInFastPeriod;
      optInFastPeriod = tempInteger;
   }
   retCode = TA_S_MA_Unguarded(startIdx,endIdx,inReal,optInFastPeriod,optInMAType,&outBegIdx2,&outNbElement2,tempBuffer);
   if( retCode == TA_SUCCESS )
   {
      retCode = TA_S_MA_Unguarded(startIdx,endIdx,inReal,optInSlowPeriod,optInMAType,&outBegIdx1,&outNbElement1,outReal);
      if( retCode == TA_SUCCESS )
      {
         tempInteger = outBegIdx1 - outBegIdx2;
         for( i = 0, j = tempInteger; i < outNbElement1; i += 1, j += 1 )
         {
            tempReal = outReal[i];
            if( !TA_IS_ZERO(tempReal) )
            {
               outReal[i] = (tempBuffer[j] - tempReal) / tempReal * 100.0;
            } else 
            {
               outReal[i] = 0.0;
            }
         }
         *outBegIdx= outBegIdx1;
         *outNBElement= outNbElement1;
      }
   }
   free(tempBuffer);
   return retCode;
}

TA_RetCode TA_S_PPO_Unguarded( int    startIdx,
                               int    endIdx,
                               const float inReal[],
                               int optInFastPeriod,
                               int optInSlowPeriod,
                               TA_MAType optInMAType,
                               int          *outBegIdx,
                               int          *outNBElement,
                               double        outReal[] )
{
   double *tempBuffer;
   TA_RetCode retCode;
   double tempReal;
   int tempInteger;
   int outBegIdx1;
   int outNbElement1;
   int outBegIdx2;
   int outNbElement2;
   int i;
   int j;

   tempBuffer = malloc((endIdx - startIdx + 1) * sizeof(double));
   if( !tempBuffer )
   {
      return TA_ALLOC_ERR;
   }
   if( optInSlowPeriod < optInFastPeriod )
   {
      tempInteger = optInSlowPeriod;
      optInSlowPeriod = optInFastPeriod;
      optInFastPeriod = tempInteger;
   }
   retCode = TA_S_MA_Unguarded(startIdx,endIdx,inReal,optInFastPeriod,optInMAType,&outBegIdx2,&outNbElement2,tempBuffer);
   if( retCode == TA_SUCCESS )
   {
      retCode = TA_S_MA_Unguarded(startIdx,endIdx,inReal,optInSlowPeriod,optInMAType,&outBegIdx1,&outNbElement1,outReal);
      if( retCode == TA_SUCCESS )
      {
         tempInteger = outBegIdx1 - outBegIdx2;
         for( i = 0, j = tempInteger; i < outNbElement1; i += 1, j += 1 )
         {
            tempReal = outReal[i];
            if( !TA_IS_ZERO(tempReal) )
            {
               outReal[i] = (tempBuffer[j] - tempReal) / tempReal * 100.0;
            } else 
            {
               outReal[i] = 0.0;
            }
         }
         *outBegIdx= outBegIdx1;
         *outNBElement= outNbElement1;
      }
   }
   free(tempBuffer);
   return retCode;
}

