/* 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
 *  JV       Jesus Viver <324122@cienz.unizar.es>
 *
 *
 * Change history:
 *
 *  MMDDYY BY   Description
 *  -------------------------------------------------------------------
 *  112400 MF   Template creation.
 *  010503 MF   Fix to always use SMA for the STDDEV (Thanks to JV).
 *  052603 MF   Adapt code to compile with .NET Managed C++
 */

TA_LIB_API int TA_BBANDS_Lookback( int optInTimePeriod, double optInNbDevUp, double optInNbDevDn, TA_MAType optInMAType )
{
   if( (int)optInTimePeriod == (int)0x80000000 )
      optInTimePeriod = 5;
   else if( (int)optInTimePeriod < 2 || (int)optInTimePeriod > 100000 )
      return -1;
   if( optInNbDevUp == -4e37 )
      optInNbDevUp = 2;
   if( optInNbDevDn == -4e37 )
      optInNbDevDn = 2;
   if( (int)optInMAType == (int)0x80000000 )
      optInMAType = 0;
   /* The lookback is driven by the middle band moving average. */
   return TA_MA_Lookback(optInTimePeriod,optInMAType);
}

TA_LIB_API TA_RetCode TA_BBANDS( int    startIdx,
                                 int    endIdx,
                                 const double inReal[],
                                 int optInTimePeriod,
                                 double optInNbDevUp,
                                 double optInNbDevDn,
                                 TA_MAType optInMAType,
                                 int          *outBegIdx,
                                 int          *outNBElement,
                                 double        outRealUpperBand[],
                                 double        outRealMiddleBand[],
                                 double        outRealLowerBand[] )
{
   TA_RetCode retCode;
   int i;
   double tempReal;
   double tempReal2;
   double *tempBuffer1;
   double *tempBuffer2;

   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 = 5;
   else if( (int)optInTimePeriod < 2 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( optInNbDevUp == -4e37 )
      optInNbDevUp = 2;
   if( optInNbDevDn == -4e37 )
      optInNbDevDn = 2;
   if( (int)optInMAType == (int)0x80000000 )
      optInMAType = 0;
   if( !outRealUpperBand )
      return TA_BAD_PARAM;
   if( !outRealMiddleBand )
      return TA_BAD_PARAM;
   if( !outRealLowerBand )
      return TA_BAD_PARAM;

   /* Identify TWO temporary buffer among the outputs.
    *
    * These temporary buffers allows to perform the
    * calculation without any memory allocation.
    *
    * Whenever possible, make the tempBuffer1 be the
    * middle band output. This will save one copy operation.
    */
   if( inReal == outRealUpperBand )
   {
      tempBuffer1 = outRealMiddleBand;
      tempBuffer2 = outRealLowerBand;
   } else if( inReal == outRealLowerBand )
   {
      tempBuffer1 = outRealMiddleBand;
      tempBuffer2 = outRealUpperBand;
   } else if( inReal == outRealMiddleBand )
   {
      tempBuffer1 = outRealLowerBand;
      tempBuffer2 = outRealUpperBand;
   } else 
   {
      tempBuffer1 = outRealMiddleBand;
      tempBuffer2 = outRealUpperBand;
   }
   /* Check that the caller is not doing tricky things.
    * (like using the input buffer in two output!)
    */
   if( tempBuffer1 == inReal || tempBuffer2 == inReal )
   {
      return TA_BAD_PARAM;
   }
   /* Calculate the middle band, which is a moving average.
    * The other two bands will simply add/substract the
    * standard deviation from this middle band.
    */
   retCode = TA_MA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,optInMAType,outBegIdx,outNBElement,tempBuffer1);
   if( retCode != TA_SUCCESS || (int)*outNBElement == 0 )
   {
      *outNBElement= 0;
      return retCode;
   }
   /* Calculate the standard deviation into tempBuffer2. */
   if( optInMAType == TA_MAType_SMA )
   {
      /* A small speed optimization by re-using the
       * already calculated SMA.
       */
      /* Inline stddev_using_precalc_ma */
      double _tempReal;
      double _periodTotal2;
      double _meanValue2;
      int _outIdx;
      int _startSum;
      int _endSum;
      _startSum = 1 + (int)*outBegIdx - optInTimePeriod;
      _endSum = (int)*outBegIdx;
      _periodTotal2 = 0;
      for( _outIdx = _startSum; _outIdx < _endSum; _outIdx += 1 )
      {
         _tempReal = inReal[_outIdx];
         _tempReal *= _tempReal;
         _periodTotal2 += _tempReal;
      }
      for( _outIdx = 0; _outIdx < (int)*outNBElement; _outIdx += 1, _startSum += 1, _endSum += 1 )
      {
         _tempReal = inReal[_endSum];
         _tempReal *= _tempReal;
         _periodTotal2 += _tempReal;
         _meanValue2 = _periodTotal2 / optInTimePeriod;
         _tempReal = inReal[_startSum];
         _tempReal *= _tempReal;
         _periodTotal2 -= _tempReal;
         _tempReal = tempBuffer1[_outIdx];
         _tempReal *= _tempReal;
         _meanValue2 -= _tempReal;
         if( !TA_IS_ZERO_OR_NEG(_meanValue2) )
         {
            tempBuffer2[_outIdx] = sqrt(_meanValue2);
         } else 
         {
            tempBuffer2[_outIdx] = 0.0;
         }
      }
   } else 
   {
      /* Calculate the Standard Deviation */
      retCode = TA_STDDEV_Unguarded((int)*outBegIdx,endIdx,inReal,optInTimePeriod,1.0,outBegIdx,outNBElement,tempBuffer2);
      if( retCode != TA_SUCCESS )
      {
         *outNBElement= 0;
         return retCode;
      }
   }
   /* Copy the MA calculation into the middle band ouput, unless
    * the calculation was done into it already!
    */
   if( tempBuffer1 != outRealMiddleBand )
   {
      memcpy(outRealMiddleBand,tempBuffer1,*outNBElement * sizeof(double));
   }
   /* Now do a tight loop to calculate the upper/lower band at
    * the same time.
    *
    * All the following 5 loops are doing the same, except there
    * is an attempt to speed optimize by eliminating uneeded
    * multiplication.
    */
   if( optInNbDevUp == optInNbDevDn )
   {
      if( optInNbDevUp == 1.0 )
      {
         /* No standard deviation multiplier needed. */
         for( i = 0; i < (int)*outNBElement; i += 1 )
         {
            tempReal = tempBuffer2[i];
            tempReal2 = outRealMiddleBand[i];
            outRealUpperBand[i] = tempReal2 + tempReal;
            outRealLowerBand[i] = tempReal2 - tempReal;
         }
      } else 
      {
         /* Upper/lower band use the same standard deviation multiplier. */
         for( i = 0; i < (int)*outNBElement; i += 1 )
         {
            tempReal = tempBuffer2[i] * optInNbDevUp;
            tempReal2 = outRealMiddleBand[i];
            outRealUpperBand[i] = tempReal2 + tempReal;
            outRealLowerBand[i] = tempReal2 - tempReal;
         }
      }
   } else if( optInNbDevUp == 1.0 )
   {
      /* Only lower band has a standard deviation multiplier. */
      for( i = 0; i < (int)*outNBElement; i += 1 )
      {
         tempReal = tempBuffer2[i];
         tempReal2 = outRealMiddleBand[i];
         outRealUpperBand[i] = tempReal2 + tempReal;
         outRealLowerBand[i] = tempReal2 - tempReal * optInNbDevDn;
      }
   } else if( optInNbDevDn == 1.0 )
   {
      /* Only upper band has a standard deviation multiplier. */
      for( i = 0; i < (int)*outNBElement; i += 1 )
      {
         tempReal = tempBuffer2[i];
         tempReal2 = outRealMiddleBand[i];
         outRealLowerBand[i] = tempReal2 - tempReal;
         outRealUpperBand[i] = tempReal2 + tempReal * optInNbDevUp;
      }
   } else 
   {
      /* Upper/lower band have distinctive standard deviation multiplier. */
      for( i = 0; i < (int)*outNBElement; i += 1 )
      {
         tempReal = tempBuffer2[i];
         tempReal2 = outRealMiddleBand[i];
         outRealUpperBand[i] = tempReal2 + tempReal * optInNbDevUp;
         outRealLowerBand[i] = tempReal2 - tempReal * optInNbDevDn;
      }
   }
   return TA_SUCCESS;
}

TA_LIB_API TA_RetCode TA_BBANDS_Unguarded( int    startIdx,
                                           int    endIdx,
                                           const double inReal[],
                                           int optInTimePeriod,
                                           double optInNbDevUp,
                                           double optInNbDevDn,
                                           TA_MAType optInMAType,
                                           int          *outBegIdx,
                                           int          *outNBElement,
                                           double        outRealUpperBand[],
                                           double        outRealMiddleBand[],
                                           double        outRealLowerBand[] )
{
   TA_RetCode retCode;
   int i;
   double tempReal;
   double tempReal2;
   double *tempBuffer1;
   double *tempBuffer2;

   if( inReal == outRealUpperBand )
   {
      tempBuffer1 = outRealMiddleBand;
      tempBuffer2 = outRealLowerBand;
   } else if( inReal == outRealLowerBand )
   {
      tempBuffer1 = outRealMiddleBand;
      tempBuffer2 = outRealUpperBand;
   } else if( inReal == outRealMiddleBand )
   {
      tempBuffer1 = outRealLowerBand;
      tempBuffer2 = outRealUpperBand;
   } else 
   {
      tempBuffer1 = outRealMiddleBand;
      tempBuffer2 = outRealUpperBand;
   }
   if( tempBuffer1 == inReal || tempBuffer2 == inReal )
   {
      return TA_BAD_PARAM;
   }
   retCode = TA_MA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,optInMAType,outBegIdx,outNBElement,tempBuffer1);
   if( retCode != TA_SUCCESS || (int)*outNBElement == 0 )
   {
      *outNBElement= 0;
      return retCode;
   }
   if( optInMAType == TA_MAType_SMA )
   {
      double _tempReal;
      double _periodTotal2;
      double _meanValue2;
      int _outIdx;
      int _startSum;
      int _endSum;
      _startSum = 1 + (int)*outBegIdx - optInTimePeriod;
      _endSum = (int)*outBegIdx;
      _periodTotal2 = 0;
      for( _outIdx = _startSum; _outIdx < _endSum; _outIdx += 1 )
      {
         _tempReal = inReal[_outIdx];
         _tempReal *= _tempReal;
         _periodTotal2 += _tempReal;
      }
      for( _outIdx = 0; _outIdx < (int)*outNBElement; _outIdx += 1, _startSum += 1, _endSum += 1 )
      {
         _tempReal = inReal[_endSum];
         _tempReal *= _tempReal;
         _periodTotal2 += _tempReal;
         _meanValue2 = _periodTotal2 / optInTimePeriod;
         _tempReal = inReal[_startSum];
         _tempReal *= _tempReal;
         _periodTotal2 -= _tempReal;
         _tempReal = tempBuffer1[_outIdx];
         _tempReal *= _tempReal;
         _meanValue2 -= _tempReal;
         if( !TA_IS_ZERO_OR_NEG(_meanValue2) )
         {
            tempBuffer2[_outIdx] = sqrt(_meanValue2);
         } else 
         {
            tempBuffer2[_outIdx] = 0.0;
         }
      }
   } else 
   {
      retCode = TA_STDDEV_Unguarded((int)*outBegIdx,endIdx,inReal,optInTimePeriod,1.0,outBegIdx,outNBElement,tempBuffer2);
      if( retCode != TA_SUCCESS )
      {
         *outNBElement= 0;
         return retCode;
      }
   }
   if( tempBuffer1 != outRealMiddleBand )
   {
      memcpy(outRealMiddleBand,tempBuffer1,*outNBElement * sizeof(double));
   }
   if( optInNbDevUp == optInNbDevDn )
   {
      if( optInNbDevUp == 1.0 )
      {
         for( i = 0; i < (int)*outNBElement; i += 1 )
         {
            tempReal = tempBuffer2[i];
            tempReal2 = outRealMiddleBand[i];
            outRealUpperBand[i] = tempReal2 + tempReal;
            outRealLowerBand[i] = tempReal2 - tempReal;
         }
      } else 
      {
         for( i = 0; i < (int)*outNBElement; i += 1 )
         {
            tempReal = tempBuffer2[i] * optInNbDevUp;
            tempReal2 = outRealMiddleBand[i];
            outRealUpperBand[i] = tempReal2 + tempReal;
            outRealLowerBand[i] = tempReal2 - tempReal;
         }
      }
   } else if( optInNbDevUp == 1.0 )
   {
      for( i = 0; i < (int)*outNBElement; i += 1 )
      {
         tempReal = tempBuffer2[i];
         tempReal2 = outRealMiddleBand[i];
         outRealUpperBand[i] = tempReal2 + tempReal;
         outRealLowerBand[i] = tempReal2 - tempReal * optInNbDevDn;
      }
   } else if( optInNbDevDn == 1.0 )
   {
      for( i = 0; i < (int)*outNBElement; i += 1 )
      {
         tempReal = tempBuffer2[i];
         tempReal2 = outRealMiddleBand[i];
         outRealLowerBand[i] = tempReal2 - tempReal;
         outRealUpperBand[i] = tempReal2 + tempReal * optInNbDevUp;
      }
   } else 
   {
      for( i = 0; i < (int)*outNBElement; i += 1 )
      {
         tempReal = tempBuffer2[i];
         tempReal2 = outRealMiddleBand[i];
         outRealUpperBand[i] = tempReal2 + tempReal * optInNbDevUp;
         outRealLowerBand[i] = tempReal2 - tempReal * optInNbDevDn;
      }
   }
   return TA_SUCCESS;
}

TA_RetCode TA_S_BBANDS( int    startIdx,
                        int    endIdx,
                        const float inReal[],
                        int optInTimePeriod,
                        double optInNbDevUp,
                        double optInNbDevDn,
                        TA_MAType optInMAType,
                        int          *outBegIdx,
                        int          *outNBElement,
                        double        outRealUpperBand[],
                        double        outRealMiddleBand[],
                        double        outRealLowerBand[] )
{
   TA_RetCode retCode;
   int i;
   double tempReal;
   double tempReal2;
   double *tempBuffer1;
   double *tempBuffer2;

   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 = 5;
   else if( (int)optInTimePeriod < 2 || (int)optInTimePeriod > 100000 )
      return TA_BAD_PARAM;
   if( optInNbDevUp == -4e37 )
      optInNbDevUp = 2;
   if( optInNbDevDn == -4e37 )
      optInNbDevDn = 2;
   if( (int)optInMAType == (int)0x80000000 )
      optInMAType = 0;
   if( !outRealUpperBand )
      return TA_BAD_PARAM;
   if( !outRealMiddleBand )
      return TA_BAD_PARAM;
   if( !outRealLowerBand )
      return TA_BAD_PARAM;

   if( (void *)inReal == (void *)outRealUpperBand )
   {
      tempBuffer1 = outRealMiddleBand;
      tempBuffer2 = outRealLowerBand;
   } else if( (void *)inReal == (void *)outRealLowerBand )
   {
      tempBuffer1 = outRealMiddleBand;
      tempBuffer2 = outRealUpperBand;
   } else if( (void *)inReal == (void *)outRealMiddleBand )
   {
      tempBuffer1 = outRealLowerBand;
      tempBuffer2 = outRealUpperBand;
   } else 
   {
      tempBuffer1 = outRealMiddleBand;
      tempBuffer2 = outRealUpperBand;
   }
   if( (void *)tempBuffer1 == (void *)inReal || (void *)tempBuffer2 == (void *)inReal )
   {
      return TA_BAD_PARAM;
   }
   retCode = TA_S_MA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,optInMAType,outBegIdx,outNBElement,tempBuffer1);
   if( retCode != TA_SUCCESS || (int)*outNBElement == 0 )
   {
      *outNBElement= 0;
      return retCode;
   }
   if( optInMAType == TA_MAType_SMA )
   {
      double _tempReal;
      double _periodTotal2;
      double _meanValue2;
      int _outIdx;
      int _startSum;
      int _endSum;
      _startSum = 1 + (int)*outBegIdx - optInTimePeriod;
      _endSum = (int)*outBegIdx;
      _periodTotal2 = 0;
      for( _outIdx = _startSum; _outIdx < _endSum; _outIdx += 1 )
      {
         _tempReal = inReal[_outIdx];
         _tempReal *= _tempReal;
         _periodTotal2 += _tempReal;
      }
      for( _outIdx = 0; _outIdx < (int)*outNBElement; _outIdx += 1, _startSum += 1, _endSum += 1 )
      {
         _tempReal = inReal[_endSum];
         _tempReal *= _tempReal;
         _periodTotal2 += _tempReal;
         _meanValue2 = _periodTotal2 / optInTimePeriod;
         _tempReal = inReal[_startSum];
         _tempReal *= _tempReal;
         _periodTotal2 -= _tempReal;
         _tempReal = tempBuffer1[_outIdx];
         _tempReal *= _tempReal;
         _meanValue2 -= _tempReal;
         if( !TA_IS_ZERO_OR_NEG(_meanValue2) )
         {
            tempBuffer2[_outIdx] = sqrt(_meanValue2);
         } else 
         {
            tempBuffer2[_outIdx] = 0.0;
         }
      }
   } else 
   {
      retCode = TA_S_STDDEV_Unguarded((int)*outBegIdx,endIdx,inReal,optInTimePeriod,1.0,outBegIdx,outNBElement,tempBuffer2);
      if( retCode != TA_SUCCESS )
      {
         *outNBElement= 0;
         return retCode;
      }
   }
   if( (void *)tempBuffer1 != (void *)outRealMiddleBand )
   {
      memcpy(outRealMiddleBand,tempBuffer1,*outNBElement * sizeof(double));
   }
   if( optInNbDevUp == optInNbDevDn )
   {
      if( optInNbDevUp == 1.0 )
      {
         for( i = 0; i < (int)*outNBElement; i += 1 )
         {
            tempReal = tempBuffer2[i];
            tempReal2 = outRealMiddleBand[i];
            outRealUpperBand[i] = tempReal2 + tempReal;
            outRealLowerBand[i] = tempReal2 - tempReal;
         }
      } else 
      {
         for( i = 0; i < (int)*outNBElement; i += 1 )
         {
            tempReal = tempBuffer2[i] * optInNbDevUp;
            tempReal2 = outRealMiddleBand[i];
            outRealUpperBand[i] = tempReal2 + tempReal;
            outRealLowerBand[i] = tempReal2 - tempReal;
         }
      }
   } else if( optInNbDevUp == 1.0 )
   {
      for( i = 0; i < (int)*outNBElement; i += 1 )
      {
         tempReal = tempBuffer2[i];
         tempReal2 = outRealMiddleBand[i];
         outRealUpperBand[i] = tempReal2 + tempReal;
         outRealLowerBand[i] = tempReal2 - tempReal * optInNbDevDn;
      }
   } else if( optInNbDevDn == 1.0 )
   {
      for( i = 0; i < (int)*outNBElement; i += 1 )
      {
         tempReal = tempBuffer2[i];
         tempReal2 = outRealMiddleBand[i];
         outRealLowerBand[i] = tempReal2 - tempReal;
         outRealUpperBand[i] = tempReal2 + tempReal * optInNbDevUp;
      }
   } else 
   {
      for( i = 0; i < (int)*outNBElement; i += 1 )
      {
         tempReal = tempBuffer2[i];
         tempReal2 = outRealMiddleBand[i];
         outRealUpperBand[i] = tempReal2 + tempReal * optInNbDevUp;
         outRealLowerBand[i] = tempReal2 - tempReal * optInNbDevDn;
      }
   }
   return TA_SUCCESS;
}

TA_RetCode TA_S_BBANDS_Unguarded( int    startIdx,
                                  int    endIdx,
                                  const float inReal[],
                                  int optInTimePeriod,
                                  double optInNbDevUp,
                                  double optInNbDevDn,
                                  TA_MAType optInMAType,
                                  int          *outBegIdx,
                                  int          *outNBElement,
                                  double        outRealUpperBand[],
                                  double        outRealMiddleBand[],
                                  double        outRealLowerBand[] )
{
   TA_RetCode retCode;
   int i;
   double tempReal;
   double tempReal2;
   double *tempBuffer1;
   double *tempBuffer2;

   if( (void *)inReal == (void *)outRealUpperBand )
   {
      tempBuffer1 = outRealMiddleBand;
      tempBuffer2 = outRealLowerBand;
   } else if( (void *)inReal == (void *)outRealLowerBand )
   {
      tempBuffer1 = outRealMiddleBand;
      tempBuffer2 = outRealUpperBand;
   } else if( (void *)inReal == (void *)outRealMiddleBand )
   {
      tempBuffer1 = outRealLowerBand;
      tempBuffer2 = outRealUpperBand;
   } else 
   {
      tempBuffer1 = outRealMiddleBand;
      tempBuffer2 = outRealUpperBand;
   }
   if( (void *)tempBuffer1 == (void *)inReal || (void *)tempBuffer2 == (void *)inReal )
   {
      return TA_BAD_PARAM;
   }
   retCode = TA_S_MA_Unguarded(startIdx,endIdx,inReal,optInTimePeriod,optInMAType,outBegIdx,outNBElement,tempBuffer1);
   if( retCode != TA_SUCCESS || (int)*outNBElement == 0 )
   {
      *outNBElement= 0;
      return retCode;
   }
   if( optInMAType == TA_MAType_SMA )
   {
      double _tempReal;
      double _periodTotal2;
      double _meanValue2;
      int _outIdx;
      int _startSum;
      int _endSum;
      _startSum = 1 + (int)*outBegIdx - optInTimePeriod;
      _endSum = (int)*outBegIdx;
      _periodTotal2 = 0;
      for( _outIdx = _startSum; _outIdx < _endSum; _outIdx += 1 )
      {
         _tempReal = inReal[_outIdx];
         _tempReal *= _tempReal;
         _periodTotal2 += _tempReal;
      }
      for( _outIdx = 0; _outIdx < (int)*outNBElement; _outIdx += 1, _startSum += 1, _endSum += 1 )
      {
         _tempReal = inReal[_endSum];
         _tempReal *= _tempReal;
         _periodTotal2 += _tempReal;
         _meanValue2 = _periodTotal2 / optInTimePeriod;
         _tempReal = inReal[_startSum];
         _tempReal *= _tempReal;
         _periodTotal2 -= _tempReal;
         _tempReal = tempBuffer1[_outIdx];
         _tempReal *= _tempReal;
         _meanValue2 -= _tempReal;
         if( !TA_IS_ZERO_OR_NEG(_meanValue2) )
         {
            tempBuffer2[_outIdx] = sqrt(_meanValue2);
         } else 
         {
            tempBuffer2[_outIdx] = 0.0;
         }
      }
   } else 
   {
      retCode = TA_S_STDDEV_Unguarded((int)*outBegIdx,endIdx,inReal,optInTimePeriod,1.0,outBegIdx,outNBElement,tempBuffer2);
      if( retCode != TA_SUCCESS )
      {
         *outNBElement= 0;
         return retCode;
      }
   }
   if( (void *)tempBuffer1 != (void *)outRealMiddleBand )
   {
      memcpy(outRealMiddleBand,tempBuffer1,*outNBElement * sizeof(double));
   }
   if( optInNbDevUp == optInNbDevDn )
   {
      if( optInNbDevUp == 1.0 )
      {
         for( i = 0; i < (int)*outNBElement; i += 1 )
         {
            tempReal = tempBuffer2[i];
            tempReal2 = outRealMiddleBand[i];
            outRealUpperBand[i] = tempReal2 + tempReal;
            outRealLowerBand[i] = tempReal2 - tempReal;
         }
      } else 
      {
         for( i = 0; i < (int)*outNBElement; i += 1 )
         {
            tempReal = tempBuffer2[i] * optInNbDevUp;
            tempReal2 = outRealMiddleBand[i];
            outRealUpperBand[i] = tempReal2 + tempReal;
            outRealLowerBand[i] = tempReal2 - tempReal;
         }
      }
   } else if( optInNbDevUp == 1.0 )
   {
      for( i = 0; i < (int)*outNBElement; i += 1 )
      {
         tempReal = tempBuffer2[i];
         tempReal2 = outRealMiddleBand[i];
         outRealUpperBand[i] = tempReal2 + tempReal;
         outRealLowerBand[i] = tempReal2 - tempReal * optInNbDevDn;
      }
   } else if( optInNbDevDn == 1.0 )
   {
      for( i = 0; i < (int)*outNBElement; i += 1 )
      {
         tempReal = tempBuffer2[i];
         tempReal2 = outRealMiddleBand[i];
         outRealLowerBand[i] = tempReal2 - tempReal;
         outRealUpperBand[i] = tempReal2 + tempReal * optInNbDevUp;
      }
   } else 
   {
      for( i = 0; i < (int)*outNBElement; i += 1 )
      {
         tempReal = tempBuffer2[i];
         tempReal2 = outRealMiddleBand[i];
         outRealUpperBand[i] = tempReal2 + tempReal * optInNbDevUp;
         outRealLowerBand[i] = tempReal2 - tempReal * optInNbDevDn;
      }
   }
   return TA_SUCCESS;
}

