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

TA_LIB_API int TA_DEMA_Lookback( int optInTimePeriod )
{
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 30;
   else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 )
      return -1;
   /* Get lookback for one EMA.
    * Multiply by two (because double smoothing).
    */
   return TA_EMA_Lookback(optInTimePeriod) * 2;
}

TA_LIB_API TA_RetCode TA_DEMA( int    startIdx,
                               int    endIdx,
                               const double inReal[],
                               int optInTimePeriod,
                               int          *outBegIdx,
                               int          *outNBElement,
                               double        outReal[] )
{
   double *firstEMA;
   double *secondEMA;
   int firstEMABegIdx;
   int firstEMANbElement;
   int secondEMABegIdx;
   int secondEMANbElement;
   int tempInt;
   int outIdx;
   int firstEMAIdx;
   int lookbackTotal;
   int lookbackEMA;
   TA_RetCode retCode;

   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;

   /* For an explanation of this function, please read
    *
    * Stocks & Commodities V. 12:1 (11-19):
    *   Smoothing Data With Faster Moving Averages
    * Stocks & Commodities V. 12:2 (72-80):
    *   Smoothing Data With Less Lag
    *
    * Both magazine articles written by Patrick G. Mulloy
    *
    * Essentially, a DEMA of time serie 't' is:
    *   EMA2 = EMA(EMA(t,period),period)
    *   DEMA = 2*EMA(t,period)- EMA2
    *
    * DEMA offers a moving average with less lags then the
    * traditional EMA.
    *
    * Do not confuse a DEMA with the EMA2. Both are called
    * "Double EMA" in the litterature, but EMA2 is a simple
    * EMA of an EMA, while DEMA is a compostie of a single
    * EMA with EMA2.
    *
    * TEMA is very similar (and from the same author).
    */
   /* Will change only on success. */
   *outNBElement= 0;
   *outBegIdx= 0;
   /* Adjust startIdx to account for the lookback period. */
   lookbackEMA = TA_EMA_Lookback(optInTimePeriod);
   lookbackTotal = lookbackEMA * 2;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   /* Make sure there is still something to evaluate. */
   if( startIdx > endIdx )
   {
      return TA_SUCCESS;
   }
   /* Allocate a temporary buffer for the firstEMA.
    *
    * When possible, re-use the outputBuffer for temp
    * calculation.
    */
   if( inReal == outReal )
   {
      firstEMA = outReal;
   } else 
   {
      tempInt = lookbackTotal + (endIdx - startIdx) + 1;
      firstEMA = malloc(tempInt * sizeof(double));
      if( !firstEMA )
      {
         return TA_ALLOC_ERR;
      }
   }
   /* Calculate the first EMA */
   retCode = TA_EMA_Unguarded(startIdx - lookbackEMA,endIdx,inReal,optInTimePeriod,&firstEMABegIdx,&firstEMANbElement,firstEMA);
   /* Verify for failure or if not enough data after
    * calculating the first EMA.
    */
   if( retCode != TA_SUCCESS || firstEMANbElement == 0 )
   {
      if( firstEMA != outReal )
      {
         free(firstEMA);
      }
      return retCode;
   }
   /* Allocate a temporary buffer for storing the EMA of the EMA. */
   secondEMA = malloc(firstEMANbElement * sizeof(double));
   if( !secondEMA )
   {
      if( firstEMA != outReal )
      {
         free(firstEMA);
      }
      return TA_ALLOC_ERR;
   }
   retCode = TA_EMA_Unguarded(0,firstEMANbElement - 1,firstEMA,optInTimePeriod,&secondEMABegIdx,&secondEMANbElement,secondEMA);
   /* Return empty output on failure or if not enough data after
    * calculating the second EMA.
    */
   if( retCode != TA_SUCCESS || secondEMANbElement == 0 )
   {
      if( firstEMA != outReal )
      {
         free(firstEMA);
      }
      free(secondEMA);
      return retCode;
   }
   /* Iterate through the second EMA and write the DEMA into
    * the output.
    */
   firstEMAIdx = secondEMABegIdx;
   outIdx = 0;
   while( outIdx < secondEMANbElement )
   {
      outReal[outIdx] = 2.0 * firstEMA[firstEMAIdx++] - secondEMA[outIdx];
      outIdx += 1;
   }
   if( firstEMA != outReal )
   {
      free(firstEMA);
   }
   free(secondEMA);
   /* Succeed. Indicate where the output starts relative to
    * the caller input.
    */
   *outBegIdx= firstEMABegIdx + secondEMABegIdx;
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

TA_LIB_API TA_RetCode TA_DEMA_Unguarded( int    startIdx,
                                         int    endIdx,
                                         const double inReal[],
                                         int optInTimePeriod,
                                         int          *outBegIdx,
                                         int          *outNBElement,
                                         double        outReal[] )
{
   double *firstEMA;
   double *secondEMA;
   int firstEMABegIdx;
   int firstEMANbElement;
   int secondEMABegIdx;
   int secondEMANbElement;
   int tempInt;
   int outIdx;
   int firstEMAIdx;
   int lookbackTotal;
   int lookbackEMA;
   TA_RetCode retCode;

   *outNBElement= 0;
   *outBegIdx= 0;
   lookbackEMA = TA_EMA_Lookback(optInTimePeriod);
   lookbackTotal = lookbackEMA * 2;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      return TA_SUCCESS;
   }
   if( inReal == outReal )
   {
      firstEMA = outReal;
   } else 
   {
      tempInt = lookbackTotal + (endIdx - startIdx) + 1;
      firstEMA = malloc(tempInt * sizeof(double));
      if( !firstEMA )
      {
         return TA_ALLOC_ERR;
      }
   }
   retCode = TA_EMA_Unguarded(startIdx - lookbackEMA,endIdx,inReal,optInTimePeriod,&firstEMABegIdx,&firstEMANbElement,firstEMA);
   if( retCode != TA_SUCCESS || firstEMANbElement == 0 )
   {
      if( firstEMA != outReal )
      {
         free(firstEMA);
      }
      return retCode;
   }
   secondEMA = malloc(firstEMANbElement * sizeof(double));
   if( !secondEMA )
   {
      if( firstEMA != outReal )
      {
         free(firstEMA);
      }
      return TA_ALLOC_ERR;
   }
   retCode = TA_EMA_Unguarded(0,firstEMANbElement - 1,firstEMA,optInTimePeriod,&secondEMABegIdx,&secondEMANbElement,secondEMA);
   if( retCode != TA_SUCCESS || secondEMANbElement == 0 )
   {
      if( firstEMA != outReal )
      {
         free(firstEMA);
      }
      free(secondEMA);
      return retCode;
   }
   firstEMAIdx = secondEMABegIdx;
   outIdx = 0;
   while( outIdx < secondEMANbElement )
   {
      outReal[outIdx] = 2.0 * firstEMA[firstEMAIdx++] - secondEMA[outIdx];
      outIdx += 1;
   }
   if( firstEMA != outReal )
   {
      free(firstEMA);
   }
   free(secondEMA);
   *outBegIdx= firstEMABegIdx + secondEMABegIdx;
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

TA_RetCode TA_S_DEMA( int    startIdx,
                      int    endIdx,
                      const float inReal[],
                      int optInTimePeriod,
                      int          *outBegIdx,
                      int          *outNBElement,
                      double        outReal[] )
{
   double *firstEMA;
   double *secondEMA;
   int firstEMABegIdx;
   int firstEMANbElement;
   int secondEMABegIdx;
   int secondEMANbElement;
   int tempInt;
   int outIdx;
   int firstEMAIdx;
   int lookbackTotal;
   int lookbackEMA;
   TA_RetCode retCode;

   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;

   *outNBElement= 0;
   *outBegIdx= 0;
   lookbackEMA = TA_EMA_Lookback(optInTimePeriod);
   lookbackTotal = lookbackEMA * 2;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      return TA_SUCCESS;
   }
   if( (void *)inReal == (void *)outReal )
   {
      firstEMA = outReal;
   } else 
   {
      tempInt = lookbackTotal + (endIdx - startIdx) + 1;
      firstEMA = malloc(tempInt * sizeof(double));
      if( !firstEMA )
      {
         return TA_ALLOC_ERR;
      }
   }
   retCode = TA_S_EMA_Unguarded(startIdx - lookbackEMA,endIdx,inReal,optInTimePeriod,&firstEMABegIdx,&firstEMANbElement,firstEMA);
   if( retCode != TA_SUCCESS || firstEMANbElement == 0 )
   {
      if( (void *)firstEMA != (void *)outReal )
      {
         free(firstEMA);
      }
      return retCode;
   }
   secondEMA = malloc(firstEMANbElement * sizeof(double));
   if( !secondEMA )
   {
      if( (void *)firstEMA != (void *)outReal )
      {
         free(firstEMA);
      }
      return TA_ALLOC_ERR;
   }
   retCode = TA_EMA_Unguarded(0,firstEMANbElement - 1,firstEMA,optInTimePeriod,&secondEMABegIdx,&secondEMANbElement,secondEMA);
   if( retCode != TA_SUCCESS || secondEMANbElement == 0 )
   {
      if( (void *)firstEMA != (void *)outReal )
      {
         free(firstEMA);
      }
      free(secondEMA);
      return retCode;
   }
   firstEMAIdx = secondEMABegIdx;
   outIdx = 0;
   while( outIdx < secondEMANbElement )
   {
      outReal[outIdx] = 2.0 * firstEMA[firstEMAIdx++] - secondEMA[outIdx];
      outIdx += 1;
   }
   if( (void *)firstEMA != (void *)outReal )
   {
      free(firstEMA);
   }
   free(secondEMA);
   *outBegIdx= firstEMABegIdx + secondEMABegIdx;
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

TA_RetCode TA_S_DEMA_Unguarded( int    startIdx,
                                int    endIdx,
                                const float inReal[],
                                int optInTimePeriod,
                                int          *outBegIdx,
                                int          *outNBElement,
                                double        outReal[] )
{
   double *firstEMA;
   double *secondEMA;
   int firstEMABegIdx;
   int firstEMANbElement;
   int secondEMABegIdx;
   int secondEMANbElement;
   int tempInt;
   int outIdx;
   int firstEMAIdx;
   int lookbackTotal;
   int lookbackEMA;
   TA_RetCode retCode;

   *outNBElement= 0;
   *outBegIdx= 0;
   lookbackEMA = TA_EMA_Lookback(optInTimePeriod);
   lookbackTotal = lookbackEMA * 2;
   if( startIdx < lookbackTotal )
   {
      startIdx = lookbackTotal;
   }
   if( startIdx > endIdx )
   {
      return TA_SUCCESS;
   }
   if( (void *)inReal == (void *)outReal )
   {
      firstEMA = outReal;
   } else 
   {
      tempInt = lookbackTotal + (endIdx - startIdx) + 1;
      firstEMA = malloc(tempInt * sizeof(double));
      if( !firstEMA )
      {
         return TA_ALLOC_ERR;
      }
   }
   retCode = TA_S_EMA_Unguarded(startIdx - lookbackEMA,endIdx,inReal,optInTimePeriod,&firstEMABegIdx,&firstEMANbElement,firstEMA);
   if( retCode != TA_SUCCESS || firstEMANbElement == 0 )
   {
      if( (void *)firstEMA != (void *)outReal )
      {
         free(firstEMA);
      }
      return retCode;
   }
   secondEMA = malloc(firstEMANbElement * sizeof(double));
   if( !secondEMA )
   {
      if( (void *)firstEMA != (void *)outReal )
      {
         free(firstEMA);
      }
      return TA_ALLOC_ERR;
   }
   retCode = TA_EMA_Unguarded(0,firstEMANbElement - 1,firstEMA,optInTimePeriod,&secondEMABegIdx,&secondEMANbElement,secondEMA);
   if( retCode != TA_SUCCESS || secondEMANbElement == 0 )
   {
      if( (void *)firstEMA != (void *)outReal )
      {
         free(firstEMA);
      }
      free(secondEMA);
      return retCode;
   }
   firstEMAIdx = secondEMABegIdx;
   outIdx = 0;
   while( outIdx < secondEMANbElement )
   {
      outReal[outIdx] = 2.0 * firstEMA[firstEMAIdx++] - secondEMA[outIdx];
      outIdx += 1;
   }
   if( (void *)firstEMA != (void *)outReal )
   {
      free(firstEMA);
   }
   free(secondEMA);
   *outBegIdx= firstEMABegIdx + secondEMABegIdx;
   *outNBElement= outIdx;
   return TA_SUCCESS;
}

