1 /* TA-LIB Copyright (c) 1999-2007, Mario Fortier
2  * All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or
5  * without modification, are permitted provided that the following
6  * conditions are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  *   notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  *   notice, this list of conditions and the following disclaimer in
13  *   the documentation and/or other materials provided with the
14  *   distribution.
15  *
16  * - Neither name of author nor the names of its contributors
17  *   may be used to endorse or promote products derived from this
18  *   software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24  * REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
26  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
31  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 /* List of contributors:
35  *
36  *  Initial  Name/description
37  *  -------------------------------------------------------------------
38  *  MF       Mario Fortier
39  *
40  *
41  * Change history:
42  *
43  *  MMDDYY BY   Description
44  *  -------------------------------------------------------------------
45  *  120802 MF   Template creation.
46  *
47  */
48 
49 /**** START GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
50 /* All code within this section is automatically
51  * generated by gen_code. Any modification will be lost
52  * next time gen_code is run.
53  */
54 /* Generated */
55 /* Generated */ #if defined( _MANAGED )
56 /* Generated */    #include "TA-Lib-Core.h"
57 /* Generated */    #define TA_INTERNAL_ERROR(Id) (RetCode::InternalError)
58 /* Generated */    namespace TicTacTec { namespace TA { namespace Library {
59 /* Generated */ #elif defined( _JAVA )
60 /* Generated */    #include "ta_defs.h"
61 /* Generated */    #include "ta_java_defs.h"
62 /* Generated */    #define TA_INTERNAL_ERROR(Id) (RetCode.InternalError)
63 /* Generated */ #else
64 /* Generated */    #include <string.h>
65 /* Generated */    #include <math.h>
66 /* Generated */    #include "ta_func.h"
67 /* Generated */ #endif
68 /* Generated */
69 /* Generated */ #ifndef TA_UTILITY_H
70 /* Generated */    #include "ta_utility.h"
71 /* Generated */ #endif
72 /* Generated */
73 /* Generated */ #ifndef TA_MEMORY_H
74 /* Generated */    #include "ta_memory.h"
75 /* Generated */ #endif
76 /* Generated */
77 /* Generated */ #define TA_PREFIX(x) TA_##x
78 /* Generated */ #define INPUT_TYPE   double
79 /* Generated */
80 /* Generated */ #if defined( _MANAGED )
SumLookback(int optInTimePeriod)81 /* Generated */ int Core::SumLookback( int           optInTimePeriod )  /* From 2 to 100000 */
82 /* Generated */
83 /* Generated */ #elif defined( _JAVA )
84 /* Generated */ public int sumLookback( int           optInTimePeriod )  /* From 2 to 100000 */
85 /* Generated */
86 /* Generated */ #else
87 /* Generated */ int TA_SUM_Lookback( int           optInTimePeriod )  /* From 2 to 100000 */
88 /* Generated */
89 /* Generated */ #endif
90 /**** END GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
91 {
92    /* insert local variable here */
93 
94 /**** START GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
95 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
96 /* Generated */    /* min/max are checked for optInTimePeriod. */
97 /* Generated */    if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )
98 /* Generated */       optInTimePeriod = 30;
99 /* Generated */    else if( ((int)optInTimePeriod < 2) || ((int)optInTimePeriod > 100000) )
100 /* Generated */       return -1;
101 /* Generated */
102 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
103 /**** END GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
104 
105    /* insert lookback code here. */
106    return optInTimePeriod-1;
107 }
108 
109 /**** START GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
110 /*
111  * TA_SUM - Summation
112  *
113  * Input  = double
114  * Output = double
115  *
116  * Optional Parameters
117  * -------------------
118  * optInTimePeriod:(From 2 to 100000)
119  *    Number of period
120  *
121  *
122  */
123 /* Generated */
124 /* Generated */ #if defined( _MANAGED ) && defined( USE_SUBARRAY )
125 /* Generated */ enum class Core::RetCode Core::Sum( int    startIdx,
126 /* Generated */                                     int    endIdx,
127 /* Generated */                                     SubArray^    inReal,
128 /* Generated */                                     int           optInTimePeriod, /* From 2 to 100000 */
129 /* Generated */                                     [Out]int%    outBegIdx,
130 /* Generated */                                     [Out]int%    outNBElement,
131 /* Generated */                                     cli::array<double>^  outReal )
132 /* Generated */ #elif defined( _MANAGED )
133 /* Generated */ enum class Core::RetCode Core::Sum( int    startIdx,
134 /* Generated */                                     int    endIdx,
135 /* Generated */                                     cli::array<double>^ inReal,
136 /* Generated */                                     int           optInTimePeriod, /* From 2 to 100000 */
137 /* Generated */                                     [Out]int%    outBegIdx,
138 /* Generated */                                     [Out]int%    outNBElement,
139 /* Generated */                                     cli::array<double>^  outReal )
140 /* Generated */ #elif defined( _JAVA )
141 /* Generated */ public RetCode sum( int    startIdx,
142 /* Generated */                     int    endIdx,
143 /* Generated */                     double       inReal[],
144 /* Generated */                     int           optInTimePeriod, /* From 2 to 100000 */
145 /* Generated */                     MInteger     outBegIdx,
146 /* Generated */                     MInteger     outNBElement,
147 /* Generated */                     double        outReal[] )
148 /* Generated */ #else
149 /* Generated */ TA_RetCode TA_SUM( int    startIdx,
150 /* Generated */                    int    endIdx,
151 /* Generated */                    const double inReal[],
152 /* Generated */                    int           optInTimePeriod, /* From 2 to 100000 */
153 /* Generated */                    int          *outBegIdx,
154 /* Generated */                    int          *outNBElement,
155 /* Generated */                    double        outReal[] )
156 /* Generated */ #endif
157 /**** END GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
158 {
159 	/* insert local variable here */
160    double periodTotal, tempReal;
161    int i, outIdx, trailingIdx, lookbackTotal;
162 
163 /**** START GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
164 /* Generated */
165 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
166 /* Generated */
167 /* Generated */    /* Validate the requested output range. */
168 /* Generated */    if( startIdx < 0 )
169 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
170 /* Generated */    if( (endIdx < 0) || (endIdx < startIdx))
171 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
172 /* Generated */
173 /* Generated */    #if !defined(_JAVA)
174 /* Generated */    if( !inReal ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
175 /* Generated */    #endif /* !defined(_JAVA)*/
176 /* Generated */    /* min/max are checked for optInTimePeriod. */
177 /* Generated */    if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )
178 /* Generated */       optInTimePeriod = 30;
179 /* Generated */    else if( ((int)optInTimePeriod < 2) || ((int)optInTimePeriod > 100000) )
180 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
181 /* Generated */
182 /* Generated */    #if !defined(_JAVA)
183 /* Generated */    if( !outReal )
184 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
185 /* Generated */
186 /* Generated */    #endif /* !defined(_JAVA) */
187 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
188 /* Generated */
189 /**** END GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
190 
191    /* Insert TA function code here. */
192 
193    /* Identify the minimum number of price bar needed
194     * to calculate at least one output.
195     */
196    lookbackTotal = (optInTimePeriod-1);
197 
198    /* Move up the start index if there is not
199     * enough initial data.
200     */
201    if( startIdx < lookbackTotal )
202       startIdx = lookbackTotal;
203 
204    /* Make sure there is still something to evaluate. */
205    if( startIdx > endIdx )
206    {
207       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
208       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
209       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
210    }
211 
212    /* Do the MA calculation using tight loops. */
213 
214    /* Add-up the initial period, except for the last value. */
215    periodTotal = 0;
216    trailingIdx = startIdx-lookbackTotal;
217 
218    i=trailingIdx;
219    if( optInTimePeriod > 1 )
220    {
221       while( i < startIdx )
222          periodTotal += inReal[i++];
223    }
224 
225    /* Proceed with the calculation for the requested range.
226     * Note that this algorithm allows the inReal and
227     * outReal to be the same buffer.
228     */
229    outIdx = 0;
230    do
231    {
232       periodTotal += inReal[i++];
233       tempReal = periodTotal;
234       periodTotal -= inReal[trailingIdx++];
235       outReal[outIdx++] = tempReal;
236    } while( i <= endIdx );
237 
238    /* All done. Indicate the output limits and return. */
239    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
240    VALUE_HANDLE_DEREF(outBegIdx)    = startIdx;
241 
242    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
243 }
244 
245 /**** START GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
246 /* Generated */
247 /* Generated */ #define  USE_SINGLE_PRECISION_INPUT
248 /* Generated */ #if !defined( _MANAGED ) && !defined( _JAVA )
249 /* Generated */    #undef   TA_PREFIX
250 /* Generated */    #define  TA_PREFIX(x) TA_S_##x
251 /* Generated */ #endif
252 /* Generated */ #undef   INPUT_TYPE
253 /* Generated */ #define  INPUT_TYPE float
254 /* Generated */ #if defined( _MANAGED )
255 /* Generated */ enum class Core::RetCode Core::Sum( int    startIdx,
256 /* Generated */                                     int    endIdx,
257 /* Generated */                                     cli::array<float>^ inReal,
258 /* Generated */                                     int           optInTimePeriod, /* From 2 to 100000 */
259 /* Generated */                                     [Out]int%    outBegIdx,
260 /* Generated */                                     [Out]int%    outNBElement,
261 /* Generated */                                     cli::array<double>^  outReal )
262 /* Generated */ #elif defined( _JAVA )
263 /* Generated */ public RetCode sum( int    startIdx,
264 /* Generated */                     int    endIdx,
265 /* Generated */                     float        inReal[],
266 /* Generated */                     int           optInTimePeriod, /* From 2 to 100000 */
267 /* Generated */                     MInteger     outBegIdx,
268 /* Generated */                     MInteger     outNBElement,
269 /* Generated */                     double        outReal[] )
270 /* Generated */ #else
271 /* Generated */ TA_RetCode TA_S_SUM( int    startIdx,
272 /* Generated */                      int    endIdx,
273 /* Generated */                      const float  inReal[],
274 /* Generated */                      int           optInTimePeriod, /* From 2 to 100000 */
275 /* Generated */                      int          *outBegIdx,
276 /* Generated */                      int          *outNBElement,
277 /* Generated */                      double        outReal[] )
278 /* Generated */ #endif
279 /* Generated */ {
280 /* Generated */    double periodTotal, tempReal;
281 /* Generated */    int i, outIdx, trailingIdx, lookbackTotal;
282 /* Generated */  #ifndef TA_FUNC_NO_RANGE_CHECK
283 /* Generated */     if( startIdx < 0 )
284 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
285 /* Generated */     if( (endIdx < 0) || (endIdx < startIdx))
286 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
287 /* Generated */     #if !defined(_JAVA)
288 /* Generated */     if( !inReal ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
289 /* Generated */     #endif
290 /* Generated */     if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )
291 /* Generated */        optInTimePeriod = 30;
292 /* Generated */     else if( ((int)optInTimePeriod < 2) || ((int)optInTimePeriod > 100000) )
293 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
294 /* Generated */     #if !defined(_JAVA)
295 /* Generated */     if( !outReal )
296 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
297 /* Generated */     #endif
298 /* Generated */  #endif
299 /* Generated */    lookbackTotal = (optInTimePeriod-1);
300 /* Generated */    if( startIdx < lookbackTotal )
301 /* Generated */       startIdx = lookbackTotal;
302 /* Generated */    if( startIdx > endIdx )
303 /* Generated */    {
304 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
305 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
306 /* Generated */       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
307 /* Generated */    }
308 /* Generated */    periodTotal = 0;
309 /* Generated */    trailingIdx = startIdx-lookbackTotal;
310 /* Generated */    i=trailingIdx;
311 /* Generated */    if( optInTimePeriod > 1 )
312 /* Generated */    {
313 /* Generated */       while( i < startIdx )
314 /* Generated */          periodTotal += inReal[i++];
315 /* Generated */    }
316 /* Generated */    outIdx = 0;
317 /* Generated */    do
318 /* Generated */    {
319 /* Generated */       periodTotal += inReal[i++];
320 /* Generated */       tempReal = periodTotal;
321 /* Generated */       periodTotal -= inReal[trailingIdx++];
322 /* Generated */       outReal[outIdx++] = tempReal;
323 /* Generated */    } while( i <= endIdx );
324 /* Generated */    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
325 /* Generated */    VALUE_HANDLE_DEREF(outBegIdx)    = startIdx;
326 /* Generated */    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
327 /* Generated */ }
328 /* Generated */
329 /* Generated */ #if defined( _MANAGED )
330 /* Generated */ }}} // Close namespace TicTacTec.TA.Lib
331 /* Generated */ #endif
332 /**** END GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
333 
334