/* 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.
 *  022203 MF   Add MAMA
 *  040503 MF   Add T3
 *  052603 MF   Adapt code to compile with .NET Managed C++
 *  111603 MF   Allow period of 1. Just copy input into output.
 *  060907 MF   Use TA_SMA/TA_EMA instead of internal implementation.
 */

TA_LIB_API int TA_MA_Lookback( int optInTimePeriod, TA_MAType optInMAType )
{
   int retValue;
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 30;
   else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 )
      return -1;
   if( (int)optInMAType == (int)0x80000000 )
      optInMAType = 0;
   if( optInTimePeriod <= 1 )
   {
      return 0;
   }
   switch( optInMAType )
   {
   case ENUM_CASE(MAType, TA_MAType_SMA, Sma):
      retValue = TA_SMA_Lookback(optInTimePeriod);
      break;
   case ENUM_CASE(MAType, TA_MAType_EMA, Ema):
      retValue = TA_EMA_Lookback(optInTimePeriod);
      break;
   case ENUM_CASE(MAType, TA_MAType_WMA, Wma):
      retValue = TA_WMA_Lookback(optInTimePeriod);
      break;
   case ENUM_CASE(MAType, TA_MAType_DEMA, Dema):
      retValue = TA_DEMA_Lookback(optInTimePeriod);
      break;
   case ENUM_CASE(MAType, TA_MAType_TEMA, Tema):
      retValue = TA_TEMA_Lookback(optInTimePeriod);
      break;
   case ENUM_CASE(MAType, TA_MAType_TRIMA, Trima):
      retValue = TA_TRIMA_Lookback(optInTimePeriod);
      break;
   case ENUM_CASE(MAType, TA_MAType_KAMA, Kama):
      retValue = TA_KAMA_Lookback(optInTimePeriod);
      break;
   case ENUM_CASE(MAType, TA_MAType_MAMA, Mama):
      retValue = TA_MAMA_Lookback(0.5,0.05);
      break;
   case ENUM_CASE(MAType, TA_MAType_T3, T3):
      retValue = TA_T3_Lookback(optInTimePeriod,0.7);
      break;
   default:
      retValue = 0;
      break;
   }
   return retValue;
}

TA_LIB_API TA_RetCode TA_MA( int    startIdx,
                             int    endIdx,
                             const double inReal[],
                             int optInTimePeriod,
                             TA_MAType optInMAType,
                             int          *outBegIdx,
                             int          *outNBElement,
                             double        outReal[] )
{
   double *dummyBuffer;
   TA_RetCode retCode;
   int nbElement;
   int outIdx;
   int todayIdx;

   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( (int)optInMAType == (int)0x80000000 )
      optInMAType = 0;
   if( !outReal )
      return TA_BAD_PARAM;

   if( optInTimePeriod == 1 )
   {
      nbElement = endIdx - startIdx + 1;
      *outNBElement= nbElement;
      for( todayIdx = startIdx, outIdx = 0; outIdx < nbElement; outIdx += 1, todayIdx += 1 )
      {
         outReal[outIdx] = inReal[todayIdx];
      }
      *outBegIdx= startIdx;
      return TA_SUCCESS;
   }
   /* Simply forward the job to the corresponding TA function. */
   switch( optInMAType )
   {
   case ENUM_CASE(MAType, TA_MAType_SMA, Sma):
      retCode = TA_SMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_EMA, Ema):
      retCode = TA_EMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_WMA, Wma):
      retCode = TA_WMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_DEMA, Dema):
      retCode = TA_DEMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_TEMA, Tema):
      retCode = TA_TEMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_TRIMA, Trima):
      retCode = TA_TRIMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_KAMA, Kama):
      retCode = TA_KAMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_MAMA, Mama):
      /* The optInTimePeriod is ignored and the FAMA output of the MAMA
       * is ignored.
       */
      dummyBuffer = malloc((endIdx - startIdx + 1) * sizeof(double));
      if( !dummyBuffer )
      {
         return TA_ALLOC_ERR;
      }
      retCode = TA_MAMA_Unguarded(startIdx,endIdx,inReal,0.5,0.05,outBegIdx,outNBElement,outReal,dummyBuffer);
      free(dummyBuffer);
      break;
   case ENUM_CASE(MAType, TA_MAType_T3, T3):
      retCode = TA_T3_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,0.7,outBegIdx,outNBElement,outReal);
      break;
   default:
      retCode = TA_BAD_PARAM;
      break;
   }
   return retCode;
}

TA_LIB_API TA_RetCode TA_MA_Unguarded( int    startIdx,
                                       int    endIdx,
                                       const double inReal[],
                                       int optInTimePeriod,
                                       TA_MAType optInMAType,
                                       int          *outBegIdx,
                                       int          *outNBElement,
                                       double        outReal[] )
{
   double *dummyBuffer;
   TA_RetCode retCode;
   int nbElement;
   int outIdx;
   int todayIdx;

   if( optInTimePeriod == 1 )
   {
      nbElement = endIdx - startIdx + 1;
      *outNBElement= nbElement;
      for( todayIdx = startIdx, outIdx = 0; outIdx < nbElement; outIdx += 1, todayIdx += 1 )
      {
         outReal[outIdx] = inReal[todayIdx];
      }
      *outBegIdx= startIdx;
      return TA_SUCCESS;
   }
   switch( optInMAType )
   {
   case ENUM_CASE(MAType, TA_MAType_SMA, Sma):
      retCode = TA_SMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_EMA, Ema):
      retCode = TA_EMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_WMA, Wma):
      retCode = TA_WMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_DEMA, Dema):
      retCode = TA_DEMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_TEMA, Tema):
      retCode = TA_TEMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_TRIMA, Trima):
      retCode = TA_TRIMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_KAMA, Kama):
      retCode = TA_KAMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_MAMA, Mama):
      dummyBuffer = malloc((endIdx - startIdx + 1) * sizeof(double));
      if( !dummyBuffer )
      {
         return TA_ALLOC_ERR;
      }
      retCode = TA_MAMA_Unguarded(startIdx,endIdx,inReal,0.5,0.05,outBegIdx,outNBElement,outReal,dummyBuffer);
      free(dummyBuffer);
      break;
   case ENUM_CASE(MAType, TA_MAType_T3, T3):
      retCode = TA_T3_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,0.7,outBegIdx,outNBElement,outReal);
      break;
   default:
      retCode = TA_BAD_PARAM;
      break;
   }
   return retCode;
}

TA_RetCode TA_S_MA( int    startIdx,
                    int    endIdx,
                    const float inReal[],
                    int optInTimePeriod,
                    TA_MAType optInMAType,
                    int          *outBegIdx,
                    int          *outNBElement,
                    double        outReal[] )
{
   double *dummyBuffer;
   TA_RetCode retCode;
   int nbElement;
   int outIdx;
   int todayIdx;

   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( (int)optInMAType == (int)0x80000000 )
      optInMAType = 0;
   if( !outReal )
      return TA_BAD_PARAM;

   if( optInTimePeriod == 1 )
   {
      nbElement = endIdx - startIdx + 1;
      *outNBElement= nbElement;
      for( todayIdx = startIdx, outIdx = 0; outIdx < nbElement; outIdx += 1, todayIdx += 1 )
      {
         outReal[outIdx] = inReal[todayIdx];
      }
      *outBegIdx= startIdx;
      return TA_SUCCESS;
   }
   switch( optInMAType )
   {
   case ENUM_CASE(MAType, TA_MAType_SMA, Sma):
      retCode = TA_S_SMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_EMA, Ema):
      retCode = TA_S_EMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_WMA, Wma):
      retCode = TA_S_WMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_DEMA, Dema):
      retCode = TA_S_DEMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_TEMA, Tema):
      retCode = TA_S_TEMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_TRIMA, Trima):
      retCode = TA_S_TRIMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_KAMA, Kama):
      retCode = TA_S_KAMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_MAMA, Mama):
      dummyBuffer = malloc((endIdx - startIdx + 1) * sizeof(double));
      if( !dummyBuffer )
      {
         return TA_ALLOC_ERR;
      }
      retCode = TA_S_MAMA_Unguarded(startIdx,endIdx,inReal,0.5,0.05,outBegIdx,outNBElement,outReal,dummyBuffer);
      free(dummyBuffer);
      break;
   case ENUM_CASE(MAType, TA_MAType_T3, T3):
      retCode = TA_S_T3_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,0.7,outBegIdx,outNBElement,outReal);
      break;
   default:
      retCode = TA_BAD_PARAM;
      break;
   }
   return retCode;
}

TA_RetCode TA_S_MA_Unguarded( int    startIdx,
                              int    endIdx,
                              const float inReal[],
                              int optInTimePeriod,
                              TA_MAType optInMAType,
                              int          *outBegIdx,
                              int          *outNBElement,
                              double        outReal[] )
{
   double *dummyBuffer;
   TA_RetCode retCode;
   int nbElement;
   int outIdx;
   int todayIdx;

   if( optInTimePeriod == 1 )
   {
      nbElement = endIdx - startIdx + 1;
      *outNBElement= nbElement;
      for( todayIdx = startIdx, outIdx = 0; outIdx < nbElement; outIdx += 1, todayIdx += 1 )
      {
         outReal[outIdx] = inReal[todayIdx];
      }
      *outBegIdx= startIdx;
      return TA_SUCCESS;
   }
   switch( optInMAType )
   {
   case ENUM_CASE(MAType, TA_MAType_SMA, Sma):
      retCode = TA_S_SMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_EMA, Ema):
      retCode = TA_S_EMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_WMA, Wma):
      retCode = TA_S_WMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_DEMA, Dema):
      retCode = TA_S_DEMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_TEMA, Tema):
      retCode = TA_S_TEMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_TRIMA, Trima):
      retCode = TA_S_TRIMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_KAMA, Kama):
      retCode = TA_S_KAMA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,outBegIdx,outNBElement,outReal);
      break;
   case ENUM_CASE(MAType, TA_MAType_MAMA, Mama):
      dummyBuffer = malloc((endIdx - startIdx + 1) * sizeof(double));
      if( !dummyBuffer )
      {
         return TA_ALLOC_ERR;
      }
      retCode = TA_S_MAMA_Unguarded(startIdx,endIdx,inReal,0.5,0.05,outBegIdx,outNBElement,outReal,dummyBuffer);
      free(dummyBuffer);
      break;
   case ENUM_CASE(MAType, TA_MAType_T3, T3):
      retCode = TA_S_T3_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,0.7,outBegIdx,outNBElement,outReal);
      break;
   default:
      retCode = TA_BAD_PARAM;
      break;
   }
   return retCode;
}

