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  *  112400 MF   Template creation.
46  *  052603 MF   Adapt code to compile with .NET Managed C++
47  *
48  */
49 
50 /**** START GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
51 /* All code within this section is automatically
52  * generated by gen_code. Any modification will be lost
53  * next time gen_code is run.
54  */
55 /* Generated */
56 /* Generated */ #if defined( _MANAGED )
57 /* Generated */    #include "TA-Lib-Core.h"
58 /* Generated */    #define TA_INTERNAL_ERROR(Id) (RetCode::InternalError)
59 /* Generated */    namespace TicTacTec { namespace TA { namespace Library {
60 /* Generated */ #elif defined( _JAVA )
61 /* Generated */    #include "ta_defs.h"
62 /* Generated */    #include "ta_java_defs.h"
63 /* Generated */    #define TA_INTERNAL_ERROR(Id) (RetCode.InternalError)
64 /* Generated */ #else
65 /* Generated */    #include <string.h>
66 /* Generated */    #include <math.h>
67 /* Generated */    #include "ta_func.h"
68 /* Generated */ #endif
69 /* Generated */
70 /* Generated */ #ifndef TA_UTILITY_H
71 /* Generated */    #include "ta_utility.h"
72 /* Generated */ #endif
73 /* Generated */
74 /* Generated */ #ifndef TA_MEMORY_H
75 /* Generated */    #include "ta_memory.h"
76 /* Generated */ #endif
77 /* Generated */
78 /* Generated */ #define TA_PREFIX(x) TA_##x
79 /* Generated */ #define INPUT_TYPE   double
80 /* Generated */
81 /* Generated */ #if defined( _MANAGED )
SmaLookback(int optInTimePeriod)82 /* Generated */ int Core::SmaLookback( int           optInTimePeriod )  /* From 2 to 100000 */
83 /* Generated */
84 /* Generated */ #elif defined( _JAVA )
85 /* Generated */ public int smaLookback( int           optInTimePeriod )  /* From 2 to 100000 */
86 /* Generated */
87 /* Generated */ #else
88 /* Generated */ int TA_SMA_Lookback( int           optInTimePeriod )  /* From 2 to 100000 */
89 /* Generated */
90 /* Generated */ #endif
91 /**** END GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
92 {
93    /* insert local variable here */
94 
95 /**** START GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
96 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
97 /* Generated */    /* min/max are checked for optInTimePeriod. */
98 /* Generated */    if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )
99 /* Generated */       optInTimePeriod = 30;
100 /* Generated */    else if( ((int)optInTimePeriod < 2) || ((int)optInTimePeriod > 100000) )
101 /* Generated */       return -1;
102 /* Generated */
103 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
104 /**** END GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
105 
106    /* insert lookback code here. */
107 
108    return optInTimePeriod - 1;
109 }
110 
111 /**** START GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
112 /*
113  * TA_SMA - Simple Moving Average
114  *
115  * Input  = double
116  * Output = double
117  *
118  * Optional Parameters
119  * -------------------
120  * optInTimePeriod:(From 2 to 100000)
121  *    Number of period
122  *
123  *
124  */
125 /* Generated */
126 /* Generated */ #if defined( _MANAGED ) && defined( USE_SUBARRAY )
127 /* Generated */ enum class Core::RetCode Core::Sma( int    startIdx,
128 /* Generated */                                     int    endIdx,
129 /* Generated */                                     SubArray^    inReal,
130 /* Generated */                                     int           optInTimePeriod, /* From 2 to 100000 */
131 /* Generated */                                     [Out]int%    outBegIdx,
132 /* Generated */                                     [Out]int%    outNBElement,
133 /* Generated */                                     cli::array<double>^  outReal )
134 /* Generated */ #elif defined( _MANAGED )
135 /* Generated */ enum class Core::RetCode Core::Sma( int    startIdx,
136 /* Generated */                                     int    endIdx,
137 /* Generated */                                     cli::array<double>^ inReal,
138 /* Generated */                                     int           optInTimePeriod, /* From 2 to 100000 */
139 /* Generated */                                     [Out]int%    outBegIdx,
140 /* Generated */                                     [Out]int%    outNBElement,
141 /* Generated */                                     cli::array<double>^  outReal )
142 /* Generated */ #elif defined( _JAVA )
143 /* Generated */ public RetCode sma( int    startIdx,
144 /* Generated */                     int    endIdx,
145 /* Generated */                     double       inReal[],
146 /* Generated */                     int           optInTimePeriod, /* From 2 to 100000 */
147 /* Generated */                     MInteger     outBegIdx,
148 /* Generated */                     MInteger     outNBElement,
149 /* Generated */                     double        outReal[] )
150 /* Generated */ #else
151 /* Generated */ TA_RetCode TA_SMA( int    startIdx,
152 /* Generated */                    int    endIdx,
153 /* Generated */                    const double inReal[],
154 /* Generated */                    int           optInTimePeriod, /* From 2 to 100000 */
155 /* Generated */                    int          *outBegIdx,
156 /* Generated */                    int          *outNBElement,
157 /* Generated */                    double        outReal[] )
158 /* Generated */ #endif
159 /**** END GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
160 {
161    /* Insert local variables here. */
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   return FUNCTION_CALL(INT_SMA)( startIdx, endIdx,
192                                  inReal, optInTimePeriod,
193                                  outBegIdx, outNBElement, outReal );
194 }
195 
196 #if defined( _MANAGED ) && defined( USE_SUBARRAY ) && !defined(USE_SINGLE_PRECISION_INPUT)
197  enum class Core::RetCode Core::TA_INT_SMA( int     startIdx,
198                                                int     endIdx,
199 									           SubArray^ inReal,
200 								               int     optInTimePeriod,
201 								               [Out]int% outBegIdx,
202 								               [Out]int% outNBElement,
203 											   cli::array<double>^  outReal)
204 #elif defined( _MANAGED )
205  enum class Core::RetCode Core::TA_INT_SMA( int     startIdx,
206                                                int     endIdx,
207 									           cli::array<INPUT_TYPE>^ inReal,
208 								               int     optInTimePeriod,
209 								               [Out]int% outBegIdx,
210 								               [Out]int% outNBElement,
211 											   cli::array<double>^  outReal)
212 #elif defined( _JAVA )
213 RetCode TA_INT_SMA( int    startIdx,
214                     int    endIdx,
215                     INPUT_TYPE inReal[],
216                     int      optInTimePeriod, /* From 1 to TA_INTEGER_MAX */
217                     MInteger outBegIdx,
218                     MInteger outNBElement,
219                     double   outReal[] )
220 #else
221 TA_RetCode TA_PREFIX(INT_SMA)( int    startIdx,
222                                int    endIdx,
223                                const INPUT_TYPE *inReal,
224                                int      optInTimePeriod, /* From 1 to TA_INTEGER_MAX */
225                                int     *outBegIdx,
226                                int     *outNBElement,
227                                double  *outReal )
228 #endif
229 {
230    double periodTotal, tempReal;
231    int i, outIdx, trailingIdx, lookbackTotal;
232 
233    /* Identify the minimum number of price bar needed
234     * to calculate at least one output.
235     */
236    lookbackTotal = (optInTimePeriod-1);
237 
238    /* Move up the start index if there is not
239     * enough initial data.
240     */
241    if( startIdx < lookbackTotal )
242       startIdx = lookbackTotal;
243 
244    /* Make sure there is still something to evaluate. */
245    if( startIdx > endIdx )
246    {
247       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
248       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
249       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
250    }
251 
252    /* Do the MA calculation using tight loops. */
253    /* Add-up the initial period, except for the last value. */
254    periodTotal = 0;
255    trailingIdx = startIdx-lookbackTotal;
256 
257    i=trailingIdx;
258    if( optInTimePeriod > 1 )
259    {
260       while( i < startIdx )
261          periodTotal += inReal[i++];
262    }
263 
264    /* Proceed with the calculation for the requested range.
265     * Note that this algorithm allows the inReal and
266     * outReal to be the same buffer.
267     */
268    outIdx = 0;
269    do
270    {
271       periodTotal += inReal[i++];
272       tempReal = periodTotal;
273       periodTotal -= inReal[trailingIdx++];
274       outReal[outIdx++] = tempReal / optInTimePeriod;
275    } while( i <= endIdx );
276 
277    /* All done. Indicate the output limits and return. */
278    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
279    VALUE_HANDLE_DEREF(outBegIdx)    = startIdx;
280 
281    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
282 }
283 
284 
285 /**** START GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
286 /* Generated */
287 /* Generated */ #define  USE_SINGLE_PRECISION_INPUT
288 /* Generated */ #if !defined( _MANAGED ) && !defined( _JAVA )
289 /* Generated */    #undef   TA_PREFIX
290 /* Generated */    #define  TA_PREFIX(x) TA_S_##x
291 /* Generated */ #endif
292 /* Generated */ #undef   INPUT_TYPE
293 /* Generated */ #define  INPUT_TYPE float
294 /* Generated */ #if defined( _MANAGED )
295 /* Generated */ enum class Core::RetCode Core::Sma( int    startIdx,
296 /* Generated */                                     int    endIdx,
297 /* Generated */                                     cli::array<float>^ inReal,
298 /* Generated */                                     int           optInTimePeriod, /* From 2 to 100000 */
299 /* Generated */                                     [Out]int%    outBegIdx,
300 /* Generated */                                     [Out]int%    outNBElement,
301 /* Generated */                                     cli::array<double>^  outReal )
302 /* Generated */ #elif defined( _JAVA )
303 /* Generated */ public RetCode sma( int    startIdx,
304 /* Generated */                     int    endIdx,
305 /* Generated */                     float        inReal[],
306 /* Generated */                     int           optInTimePeriod, /* From 2 to 100000 */
307 /* Generated */                     MInteger     outBegIdx,
308 /* Generated */                     MInteger     outNBElement,
309 /* Generated */                     double        outReal[] )
310 /* Generated */ #else
311 /* Generated */ TA_RetCode TA_S_SMA( int    startIdx,
312 /* Generated */                      int    endIdx,
313 /* Generated */                      const float  inReal[],
314 /* Generated */                      int           optInTimePeriod, /* From 2 to 100000 */
315 /* Generated */                      int          *outBegIdx,
316 /* Generated */                      int          *outNBElement,
317 /* Generated */                      double        outReal[] )
318 /* Generated */ #endif
319 /* Generated */ {
320 /* Generated */  #ifndef TA_FUNC_NO_RANGE_CHECK
321 /* Generated */     if( startIdx < 0 )
322 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
323 /* Generated */     if( (endIdx < 0) || (endIdx < startIdx))
324 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
325 /* Generated */     #if !defined(_JAVA)
326 /* Generated */     if( !inReal ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
327 /* Generated */     #endif
328 /* Generated */     if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )
329 /* Generated */        optInTimePeriod = 30;
330 /* Generated */     else if( ((int)optInTimePeriod < 2) || ((int)optInTimePeriod > 100000) )
331 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
332 /* Generated */     #if !defined(_JAVA)
333 /* Generated */     if( !outReal )
334 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
335 /* Generated */     #endif
336 /* Generated */  #endif
337 /* Generated */   return FUNCTION_CALL(INT_SMA)( startIdx, endIdx,
338 /* Generated */                                  inReal, optInTimePeriod,
339 /* Generated */                                  outBegIdx, outNBElement, outReal );
340 /* Generated */ }
341 /* Generated */ #if defined( _MANAGED ) && defined( USE_SUBARRAY ) && !defined(USE_SINGLE_PRECISION_INPUT)
342 /* Generated */  enum class Core::RetCode Core::TA_INT_SMA( int     startIdx,
343 /* Generated */                                                int     endIdx,
344 /* Generated */ 									           SubArray^ inReal,
345 /* Generated */ 								               int     optInTimePeriod,
346 /* Generated */ 								               [Out]int% outBegIdx,
347 /* Generated */ 								               [Out]int% outNBElement,
348 /* Generated */ 											   cli::array<double>^  outReal)
349 /* Generated */ #elif defined( _MANAGED )
350 /* Generated */  enum class Core::RetCode Core::TA_INT_SMA( int     startIdx,
351 /* Generated */                                                int     endIdx,
352 /* Generated */ 									           cli::array<INPUT_TYPE>^ inReal,
353 /* Generated */ 								               int     optInTimePeriod,
354 /* Generated */ 								               [Out]int% outBegIdx,
355 /* Generated */ 								               [Out]int% outNBElement,
356 /* Generated */ 											   cli::array<double>^  outReal)
357 /* Generated */ #elif defined( _JAVA )
358 /* Generated */ RetCode TA_INT_SMA( int    startIdx,
359 /* Generated */                     int    endIdx,
360 /* Generated */                     INPUT_TYPE inReal[],
361 /* Generated */                     int      optInTimePeriod,
362 /* Generated */                     MInteger outBegIdx,
363 /* Generated */                     MInteger outNBElement,
364 /* Generated */                     double   outReal[] )
365 /* Generated */ #else
366 /* Generated */ TA_RetCode TA_PREFIX(INT_SMA)( int    startIdx,
367 /* Generated */                                int    endIdx,
368 /* Generated */                                const INPUT_TYPE *inReal,
369 /* Generated */                                int      optInTimePeriod,
370 /* Generated */                                int     *outBegIdx,
371 /* Generated */                                int     *outNBElement,
372 /* Generated */                                double  *outReal )
373 /* Generated */ #endif
374 /* Generated */ {
375 /* Generated */    double periodTotal, tempReal;
376 /* Generated */    int i, outIdx, trailingIdx, lookbackTotal;
377 /* Generated */    lookbackTotal = (optInTimePeriod-1);
378 /* Generated */    if( startIdx < lookbackTotal )
379 /* Generated */       startIdx = lookbackTotal;
380 /* Generated */    if( startIdx > endIdx )
381 /* Generated */    {
382 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
383 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
384 /* Generated */       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
385 /* Generated */    }
386 /* Generated */    periodTotal = 0;
387 /* Generated */    trailingIdx = startIdx-lookbackTotal;
388 /* Generated */    i=trailingIdx;
389 /* Generated */    if( optInTimePeriod > 1 )
390 /* Generated */    {
391 /* Generated */       while( i < startIdx )
392 /* Generated */          periodTotal += inReal[i++];
393 /* Generated */    }
394 /* Generated */    outIdx = 0;
395 /* Generated */    do
396 /* Generated */    {
397 /* Generated */       periodTotal += inReal[i++];
398 /* Generated */       tempReal = periodTotal;
399 /* Generated */       periodTotal -= inReal[trailingIdx++];
400 /* Generated */       outReal[outIdx++] = tempReal / optInTimePeriod;
401 /* Generated */    } while( i <= endIdx );
402 /* Generated */    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
403 /* Generated */    VALUE_HANDLE_DEREF(outBegIdx)    = startIdx;
404 /* Generated */    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
405 /* Generated */ }
406 /* Generated */
407 /* Generated */ #if defined( _MANAGED )
408 /* Generated */ }}} // Close namespace TicTacTec.TA.Lib
409 /* Generated */ #endif
410 /**** END GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
411 
412