/* TA-LIB Copyright (c) 1999-2026, 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// (regenerate: cd ta_codegen/generator && cargo run -- generate) */ #include #include #include "ta_func.h" #include "ta_utility.h" #include "ta_memory.h" #include "ta_func_stream_private.h" /* List of contributors: * * Initial Name/description * ------------------------------------------------------------------- * MF Mario Fortier * * * Change history: * * MMDDYY BY Description * ------------------------------------------------------------------- * 112400 MF Template creation. * 052603 MF Adapt code to compile with .NET Managed C++ */ TA_LIB_API int TA_SMA_Lookback( int optInTimePeriod ) { if( (int)optInTimePeriod == TA_INTEGER_DEFAULT ) optInTimePeriod = 30; else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 ) return -1; return optInTimePeriod - 1; } TA_LIB_API int TA_SMA_DisplayShift( int optInTimePeriod, int outputIdx ) { if( TA_SMA_Lookback( optInTimePeriod ) < 0 ) return INT_MIN; if( outputIdx < 0 || outputIdx >= 1 ) return INT_MIN; return 0; } TA_LIB_API TA_RetCode TA_SMA( int startIdx, int endIdx, const double inReal[], int optInTimePeriod, int *outBegIdx, int *outNBElement, double outReal[] ) { double periodTotal; double tempReal; int i; int outIdx; int trailingIdx; int lookbackTotal; if( (startIdx < 0) || (startIdx > TA_INDEX_MAX) ) return TA_OUT_OF_RANGE_START_INDEX; if( (endIdx < 0) || (endIdx > TA_INDEX_MAX) || (endIdx < startIdx) ) return TA_OUT_OF_RANGE_END_INDEX; if( (int)optInTimePeriod == TA_INTEGER_DEFAULT ) optInTimePeriod = 30; else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 ) return TA_BAD_PARAM; if( !inReal ) return TA_BAD_PARAM; if( !outBegIdx || !outNBElement ) return TA_BAD_PARAM; if( !outReal ) return TA_BAD_PARAM; /* Identify the minimum number of price bar needed * to calculate at least one output. */ lookbackTotal = (int)(optInTimePeriod - 1); /* Move up the start index if there is not * enough initial data. */ if( startIdx < lookbackTotal ) { startIdx = lookbackTotal; } /* Make sure there is still something to evaluate. */ if( startIdx > endIdx ) { *outBegIdx= 0; *outNBElement= 0; return TA_SUCCESS; } /* Do the MA calculation using tight loops. */ /* Add-up the initial period, except for the last value. */ periodTotal = 0.0; trailingIdx = startIdx - lookbackTotal; i = trailingIdx; if( optInTimePeriod > 1 ) { while( i < startIdx ) { periodTotal += (double)inReal[i]; i = i + 1; } } /* Proceed with the calculation for the requested range. * Note that this algorithm allows the inReal and * outReal to be the same buffer. */ outIdx = 0; while( i <= endIdx ) { periodTotal += (double)inReal[i]; i = i + 1; tempReal = periodTotal; periodTotal -= (double)inReal[trailingIdx]; trailingIdx = trailingIdx + 1; outReal[outIdx] = tempReal / (double)optInTimePeriod; outIdx = outIdx + 1; } /* All done. Indicate the output limits and return. */ *outNBElement= outIdx; *outBegIdx= startIdx; return TA_SUCCESS; } TA_RetCode TA_S_SMA( int startIdx, int endIdx, const float inReal[], int optInTimePeriod, int *outBegIdx, int *outNBElement, double outReal[] ) { double periodTotal; double tempReal; int i; int outIdx; int trailingIdx; int lookbackTotal; if( (startIdx < 0) || (startIdx > TA_INDEX_MAX) ) return TA_OUT_OF_RANGE_START_INDEX; if( (endIdx < 0) || (endIdx > TA_INDEX_MAX) || (endIdx < startIdx) ) return TA_OUT_OF_RANGE_END_INDEX; if( (int)optInTimePeriod == TA_INTEGER_DEFAULT ) optInTimePeriod = 30; else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 ) return TA_BAD_PARAM; if( !inReal ) return TA_BAD_PARAM; if( !outBegIdx || !outNBElement ) return TA_BAD_PARAM; if( !outReal ) return TA_BAD_PARAM; lookbackTotal = (int)(optInTimePeriod - 1); if( startIdx < lookbackTotal ) { startIdx = lookbackTotal; } if( startIdx > endIdx ) { *outBegIdx= 0; *outNBElement= 0; return TA_SUCCESS; } periodTotal = 0.0; trailingIdx = startIdx - lookbackTotal; i = trailingIdx; if( optInTimePeriod > 1 ) { while( i < startIdx ) { periodTotal += (double)inReal[i]; i = i + 1; } } outIdx = 0; while( i <= endIdx ) { periodTotal += (double)inReal[i]; i = i + 1; tempReal = periodTotal; periodTotal -= (double)inReal[trailingIdx]; trailingIdx = trailingIdx + 1; outReal[outIdx] = tempReal / (double)optInTimePeriod; outIdx = outIdx + 1; } *outNBElement= outIdx; *outBegIdx= startIdx; return TA_SUCCESS; } /**** Streaming API *****/ struct TA_SMA_Stream { /* The bars this handle has an output for (see TA_SMA_OutRange). */ int outRangeBegIdx; int outRangeCount; /* The value(s) at the last bar the stream counted (see TA_SMA_Value). */ double cur_outReal; int optInTimePeriod; double periodTotal; int ringPos_trailingIdx; int ringCap_trailingIdx; double *ring_trailingIdx_inReal; }; /* Private function, not in public API. */ static void TA_SMA_ReleaseImpl( struct TA_SMA_Stream *sp ) { if( !sp ) return; if( sp->ring_trailingIdx_inReal ) TA_Free( sp->ring_trailingIdx_inReal ); TA_Free( sp ); } /* Private function, not in public API. */ static void TA_SMA_StepImpl( struct TA_SMA_Stream *sp, double inReal, double *outReal ) { double tempReal; if( sp->ringCap_trailingIdx == 0 ) { sp->ring_trailingIdx_inReal[0] = inReal; } sp->periodTotal += (double)inReal; tempReal = sp->periodTotal; sp->periodTotal -= (double)sp->ring_trailingIdx_inReal[sp->ringPos_trailingIdx]; *outReal= tempReal / (double)sp->optInTimePeriod; sp->cur_outReal = *outReal; sp->ring_trailingIdx_inReal[sp->ringPos_trailingIdx] = inReal; sp->ringPos_trailingIdx = sp->ringPos_trailingIdx + 1; if( sp->ringPos_trailingIdx >= sp->ringCap_trailingIdx ) { sp->ringPos_trailingIdx = 0; } } static TA_RetCode TA_SMA_OpenImpl( struct TA_SMA_Stream **stream, const double inReal[], int startIdx, int historyLen, int optInTimePeriod, int *outBegIdx, int *outNBElement, double outReal[], int outStride ) { struct TA_SMA_Stream *sp; int endIdx; if( !stream ) return TA_BAD_PARAM; *stream = NULL; if( historyLen < 1 ) return TA_OUT_OF_RANGE_START_INDEX; if( historyLen > TA_INDEX_MAX + 1 ) return TA_OUT_OF_RANGE_END_INDEX; if( !inReal || !outReal ) return TA_BAD_PARAM; if( (int)optInTimePeriod == TA_INTEGER_DEFAULT ) optInTimePeriod = 30; else if( (int)optInTimePeriod < 1 || (int)optInTimePeriod > 100000 ) return TA_BAD_PARAM; if( startIdx > historyLen - 1 ) { *outBegIdx = 0; *outNBElement = 0; return TA_INSUFFICIENT_HISTORY; } endIdx = historyLen - 1; { double periodTotal = 0.0; double tempReal; int i; int outIdx; int trailingIdx; int lookbackTotal; /* Identify the minimum number of price bar needed * to calculate at least one output. */ lookbackTotal = (int)(optInTimePeriod - 1); /* Move up the start index if there is not * enough initial data. */ if( startIdx < lookbackTotal ) { startIdx = lookbackTotal; } /* Make sure there is still something to evaluate. */ if( startIdx > endIdx ) { *outBegIdx= 0; *outNBElement= 0; return TA_INSUFFICIENT_HISTORY; } /* Do the MA calculation using tight loops. */ /* Add-up the initial period, except for the last value. */ periodTotal = 0.0; trailingIdx = startIdx - lookbackTotal; i = trailingIdx; if( optInTimePeriod > 1 ) { while( i < startIdx ) { periodTotal += (double)inReal[i]; i = i + 1; } } /* Proceed with the calculation for the requested range. * Note that this algorithm allows the inReal and * outReal to be the same buffer. */ outIdx = 0; while( i <= endIdx ) { periodTotal += (double)inReal[i]; i = i + 1; tempReal = periodTotal; periodTotal -= (double)inReal[trailingIdx]; trailingIdx = trailingIdx + 1; outReal[outIdx * outStride] = tempReal / (double)optInTimePeriod; outIdx = outIdx + 1; } /* All done. Indicate the output limits and return. */ *outNBElement= outIdx; *outBegIdx= startIdx; /* Capture the live batch state into the handle. */ sp = (struct TA_SMA_Stream *)TA_Malloc( sizeof(*sp) ); if( !sp ) { return TA_ALLOC_ERR; } memset( sp, 0, sizeof(*sp) ); sp->optInTimePeriod = optInTimePeriod; sp->periodTotal = periodTotal; sp->ringCap_trailingIdx = (int)(i - trailingIdx); if( sp->ringCap_trailingIdx < 0 || sp->ringCap_trailingIdx > historyLen ) { TA_SMA_ReleaseImpl( sp ); return TA_INTERNAL_ERROR(383); } { size_t allocN = (size_t)(sp->ringCap_trailingIdx > 0 ? sp->ringCap_trailingIdx : 1); sp->ring_trailingIdx_inReal = (double *)TA_Malloc( sizeof(double) * allocN ); if( !sp->ring_trailingIdx_inReal ) { TA_SMA_ReleaseImpl( sp ); return TA_ALLOC_ERR; } memcpy( sp->ring_trailingIdx_inReal, inReal + (historyLen - sp->ringCap_trailingIdx), sizeof(double) * (size_t)sp->ringCap_trailingIdx ); } sp->ringPos_trailingIdx = 0; sp->outRangeBegIdx = *outBegIdx; sp->outRangeCount = *outNBElement; sp->cur_outReal = outReal[(*outNBElement - 1) * outStride]; *stream = sp; return TA_SUCCESS; } } /* Private function, not in public API. */ TA_RetCode TA_SMA_OpenInternal( struct TA_SMA_Stream **stream, const double inReal[], int startIdx, int historyLen, int optInTimePeriod, double *outReal ) { TA_RetCode retCode; int dummyBegIdx = 0; int dummyNBElement = 0; double sink_outReal = 0.0; retCode = TA_SMA_OpenImpl( stream, inReal, startIdx, historyLen, optInTimePeriod, &dummyBegIdx, &dummyNBElement, &sink_outReal, 0 ); if( retCode == TA_SUCCESS ) { *outReal = sink_outReal; } return retCode; } TA_LIB_API TA_RetCode TA_SMA_Open( TA_SMA_Stream **stream, const double inReal[], int historyLen, int optInTimePeriod, double *outReal ) { if( !stream ) return TA_BAD_PARAM; *stream = NULL; if( historyLen < 1 ) return TA_OUT_OF_RANGE_START_INDEX; if( historyLen > TA_INDEX_MAX + 1 ) return TA_OUT_OF_RANGE_END_INDEX; if( !inReal || !outReal ) return TA_BAD_PARAM; return TA_SMA_OpenInternal( stream, inReal, 0, historyLen, optInTimePeriod, outReal ); } TA_LIB_API TA_RetCode TA_SMA_OpenAndFill( TA_SMA_Stream **stream, const double inReal[], int historyLen, int optInTimePeriod, int *outBegIdx, int *outNBElement, double outReal[] ) { if( !stream ) return TA_BAD_PARAM; *stream = NULL; if( historyLen < 1 ) return TA_OUT_OF_RANGE_START_INDEX; if( historyLen > TA_INDEX_MAX + 1 ) return TA_OUT_OF_RANGE_END_INDEX; if( !inReal || !outBegIdx || !outNBElement || !outReal ) return TA_BAD_PARAM; if( (const void *)outReal == (const void *)inReal ) return TA_BAD_PARAM; return TA_SMA_OpenAndFillInternal( stream, inReal, 0, historyLen, optInTimePeriod, outBegIdx, outNBElement, outReal ); } /* Private function, not in public API. */ TA_RetCode TA_SMA_OpenAndFillInternal( struct TA_SMA_Stream **stream, const double inReal[], int startIdx, int historyLen, int optInTimePeriod, int *outBegIdx, int *outNBElement, double outReal[] ) { return TA_SMA_OpenImpl( stream, inReal, startIdx, historyLen, optInTimePeriod, outBegIdx, outNBElement, outReal, 1 ); } TA_LIB_API TA_RetCode TA_SMA_Update( TA_SMA_Stream *stream, double inReal, double *outReal ) { if( !stream ) return TA_BAD_PARAM; if( stream->outRangeBegIdx + stream->outRangeCount > TA_INDEX_MAX ) return TA_OUT_OF_RANGE_END_INDEX; if( !outReal ) return TA_BAD_PARAM; if( !TA_IS_FINITE( inReal ) ) return TA_BAD_PARAM; TA_SMA_StepImpl( stream, inReal, outReal ); stream->outRangeCount++; return TA_SUCCESS; } TA_LIB_API TA_RetCode TA_SMA_Peek( const TA_SMA_Stream *stream, double inReal, double *outReal ) { const struct TA_SMA_Stream *sp = stream; double tempReal; double periodTotal; double *ring_trailingIdx_inReal; int pkSlot0 = -1; double pkVal0 = 0.0; if( !stream || !outReal ) return TA_BAD_PARAM; if( !TA_IS_FINITE( inReal ) ) return TA_BAD_PARAM; periodTotal = sp->periodTotal; ring_trailingIdx_inReal = sp->ring_trailingIdx_inReal; if( sp->ringCap_trailingIdx == 0 ) { pkSlot0 = 0; pkVal0 = inReal; } periodTotal += (double)inReal; tempReal = periodTotal; periodTotal -= (double)((sp->ringPos_trailingIdx != pkSlot0) ? ring_trailingIdx_inReal[sp->ringPos_trailingIdx] : pkVal0); *outReal= tempReal / (double)sp->optInTimePeriod; return TA_SUCCESS; } TA_LIB_API TA_RetCode TA_SMA_Close( TA_SMA_Stream *stream ) { TA_SMA_ReleaseImpl( stream ); return TA_SUCCESS; } /* Private function, not in public API. */ void TA_SMA_StepTape( struct TA_SMA_Stream *sp, const double tape[], int tapeBase, int tapeMask, double inReal, double *outReal ) { double tempReal; sp->periodTotal += (double)inReal; tempReal = sp->periodTotal; sp->periodTotal -= (double)tape[(tapeBase - sp->ringCap_trailingIdx) & tapeMask]; *outReal= tempReal / (double)sp->optInTimePeriod; sp->cur_outReal = *outReal; sp->outRangeCount++; } /* Private function, not in public API. */ void TA_SMA_PeekTape( const struct TA_SMA_Stream *sp, const double tape[], int tapeBase, int tapeMask, double inReal, double *outReal ) { double tempReal; double periodTotal; int pkSlot0 = -1; double pkVal0 = 0.0; periodTotal = sp->periodTotal; pkSlot0 = tapeBase & tapeMask; pkVal0 = inReal; periodTotal += (double)inReal; tempReal = periodTotal; periodTotal -= (double)((((tapeBase - sp->ringCap_trailingIdx) & tapeMask) != pkSlot0) ? tape[(tapeBase - sp->ringCap_trailingIdx) & tapeMask] : pkVal0); *outReal= tempReal / (double)sp->optInTimePeriod; } /* Private function, not in public API. */ int TA_SMA_TapeDetach( struct TA_SMA_Stream *sp ) { int reach = 0; if( sp->ring_trailingIdx_inReal ) { TA_Free( sp->ring_trailingIdx_inReal ); sp->ring_trailingIdx_inReal = NULL; } if( sp->ringCap_trailingIdx > reach ) reach = sp->ringCap_trailingIdx; return reach; } TA_LIB_API TA_RetCode TA_SMA_Value( const TA_SMA_Stream *stream, double *outReal ) { if( !stream || !outReal ) return TA_BAD_PARAM; *outReal = stream->cur_outReal; return TA_SUCCESS; } TA_LIB_API TA_RetCode TA_SMA_OutRange( const TA_SMA_Stream *stream, int *outBegIdx, int *outNBElement ) { if( !stream || !outBegIdx || !outNBElement ) return TA_BAD_PARAM; *outBegIdx = stream->outRangeBegIdx; *outNBElement = stream->outRangeCount; return TA_SUCCESS; } TA_LIB_API TA_RetCode TA_SMA_Advance( TA_SMA_Stream *stream ) { if( !stream ) return TA_BAD_PARAM; if( stream->outRangeBegIdx + stream->outRangeCount > TA_INDEX_MAX ) return TA_OUT_OF_RANGE_END_INDEX; stream->outRangeCount++; return TA_SUCCESS; } TA_LIB_API TA_RetCode TA_SMA_Clone( const TA_SMA_Stream *stream, TA_SMA_Stream **clone ) { struct TA_SMA_Stream *sp; if( !clone ) return TA_BAD_PARAM; *clone = NULL; if( !stream ) return TA_BAD_PARAM; sp = (struct TA_SMA_Stream *)TA_Malloc( sizeof(*sp) ); if( !sp ) return TA_ALLOC_ERR; *sp = *stream; sp->ring_trailingIdx_inReal = NULL; if( stream->ring_trailingIdx_inReal ) { size_t copyN = (size_t)(sp->ringCap_trailingIdx > 0 ? sp->ringCap_trailingIdx : 1); sp->ring_trailingIdx_inReal = (double *)TA_Malloc( sizeof(double) * copyN ); if( !sp->ring_trailingIdx_inReal ) { TA_SMA_Close( sp ); return TA_ALLOC_ERR; } memcpy( sp->ring_trailingIdx_inReal, stream->ring_trailingIdx_inReal, sizeof(double) * copyN ); } *clone = sp; return TA_SUCCESS; }