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  *  101003 MF   Initial Coding
47  *  062804 MF   Resolve div by zero bug on limit case.
48  *
49  */
50 
51 /**** START GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
52 /* All code within this section is automatically
53  * generated by gen_code. Any modification will be lost
54  * next time gen_code is run.
55  */
56 /* Generated */
57 /* Generated */ #if defined( _MANAGED )
58 /* Generated */    #include "TA-Lib-Core.h"
59 /* Generated */    #define TA_INTERNAL_ERROR(Id) (RetCode::InternalError)
60 /* Generated */    namespace TicTacTec { namespace TA { namespace Library {
61 /* Generated */ #elif defined( _JAVA )
62 /* Generated */    #include "ta_defs.h"
63 /* Generated */    #include "ta_java_defs.h"
64 /* Generated */    #define TA_INTERNAL_ERROR(Id) (RetCode.InternalError)
65 /* Generated */ #else
66 /* Generated */    #include <string.h>
67 /* Generated */    #include <math.h>
68 /* Generated */    #include "ta_func.h"
69 /* Generated */ #endif
70 /* Generated */
71 /* Generated */ #ifndef TA_UTILITY_H
72 /* Generated */    #include "ta_utility.h"
73 /* Generated */ #endif
74 /* Generated */
75 /* Generated */ #ifndef TA_MEMORY_H
76 /* Generated */    #include "ta_memory.h"
77 /* Generated */ #endif
78 /* Generated */
79 /* Generated */ #define TA_PREFIX(x) TA_##x
80 /* Generated */ #define INPUT_TYPE   double
81 /* Generated */
82 /* Generated */ #if defined( _MANAGED )
CorrelLookback(int optInTimePeriod)83 /* Generated */ int Core::CorrelLookback( int           optInTimePeriod )  /* From 1 to 100000 */
84 /* Generated */
85 /* Generated */ #elif defined( _JAVA )
86 /* Generated */ public int correlLookback( int           optInTimePeriod )  /* From 1 to 100000 */
87 /* Generated */
88 /* Generated */ #else
89 /* Generated */ int TA_CORREL_Lookback( int           optInTimePeriod )  /* From 1 to 100000 */
90 /* Generated */
91 /* Generated */ #endif
92 /**** END GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
93 {
94    /* insert local variable here */
95 
96 /**** START GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
97 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
98 /* Generated */    /* min/max are checked for optInTimePeriod. */
99 /* Generated */    if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )
100 /* Generated */       optInTimePeriod = 30;
101 /* Generated */    else if( ((int)optInTimePeriod < 1) || ((int)optInTimePeriod > 100000) )
102 /* Generated */       return -1;
103 /* Generated */
104 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
105 /**** END GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
106 
107    /* insert lookback code here. */
108    return optInTimePeriod-1;
109 }
110 
111 /**** START GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
112 /*
113  * TA_CORREL - Pearson's Correlation Coefficient (r)
114  *
115  * Input  = double, double
116  * Output = double
117  *
118  * Optional Parameters
119  * -------------------
120  * optInTimePeriod:(From 1 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::Correl( int    startIdx,
128 /* Generated */                                        int    endIdx,
129 /* Generated */                                        SubArray^    inReal0,
130 /* Generated */                                        SubArray^    inReal1,
131 /* Generated */                                        int           optInTimePeriod, /* From 1 to 100000 */
132 /* Generated */                                        [Out]int%    outBegIdx,
133 /* Generated */                                        [Out]int%    outNBElement,
134 /* Generated */                                        cli::array<double>^  outReal )
135 /* Generated */ #elif defined( _MANAGED )
136 /* Generated */ enum class Core::RetCode Core::Correl( int    startIdx,
137 /* Generated */                                        int    endIdx,
138 /* Generated */                                        cli::array<double>^ inReal0,
139 /* Generated */                                        cli::array<double>^ inReal1,
140 /* Generated */                                        int           optInTimePeriod, /* From 1 to 100000 */
141 /* Generated */                                        [Out]int%    outBegIdx,
142 /* Generated */                                        [Out]int%    outNBElement,
143 /* Generated */                                        cli::array<double>^  outReal )
144 /* Generated */ #elif defined( _JAVA )
145 /* Generated */ public RetCode correl( int    startIdx,
146 /* Generated */                        int    endIdx,
147 /* Generated */                        double       inReal0[],
148 /* Generated */                        double       inReal1[],
149 /* Generated */                        int           optInTimePeriod, /* From 1 to 100000 */
150 /* Generated */                        MInteger     outBegIdx,
151 /* Generated */                        MInteger     outNBElement,
152 /* Generated */                        double        outReal[] )
153 /* Generated */ #else
154 /* Generated */ TA_RetCode TA_CORREL( int    startIdx,
155 /* Generated */                       int    endIdx,
156 /* Generated */                       const double inReal0[],
157 /* Generated */                       const double inReal1[],
158 /* Generated */                       int           optInTimePeriod, /* From 1 to 100000 */
159 /* Generated */                       int          *outBegIdx,
160 /* Generated */                       int          *outNBElement,
161 /* Generated */                       double        outReal[] )
162 /* Generated */ #endif
163 /**** END GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
164 {
165 	/* insert local variable here */
166     double sumXY, sumX, sumY, sumX2, sumY2, x, y, trailingX, trailingY;
167     double tempReal;
168     int lookbackTotal, today, trailingIdx, outIdx;
169 
170 /**** START GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
171 /* Generated */
172 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
173 /* Generated */
174 /* Generated */    /* Validate the requested output range. */
175 /* Generated */    if( startIdx < 0 )
176 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
177 /* Generated */    if( (endIdx < 0) || (endIdx < startIdx))
178 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
179 /* Generated */
180 /* Generated */    #if !defined(_JAVA)
181 /* Generated */    if( !inReal0 ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
182 /* Generated */    if( !inReal1 ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
183 /* Generated */    #endif /* !defined(_JAVA)*/
184 /* Generated */    /* min/max are checked for optInTimePeriod. */
185 /* Generated */    if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )
186 /* Generated */       optInTimePeriod = 30;
187 /* Generated */    else if( ((int)optInTimePeriod < 1) || ((int)optInTimePeriod > 100000) )
188 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
189 /* Generated */
190 /* Generated */    #if !defined(_JAVA)
191 /* Generated */    if( !outReal )
192 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
193 /* Generated */
194 /* Generated */    #endif /* !defined(_JAVA) */
195 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
196 /* Generated */
197 /**** END GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
198 
199    /* Insert TA function code here. */
200 
201    /* Move up the start index if there is not
202     * enough initial data.
203     */
204    lookbackTotal = optInTimePeriod-1;
205    if( startIdx < lookbackTotal )
206       startIdx = lookbackTotal;
207 
208    /* Make sure there is still something to evaluate. */
209    if( startIdx > endIdx )
210    {
211       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
212       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
213       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
214    }
215 
216    VALUE_HANDLE_DEREF(outBegIdx)  = startIdx;
217    trailingIdx = startIdx - lookbackTotal;
218 
219    /* Calculate the initial values. */
220    sumXY = sumX = sumY = sumX2 = sumY2 = 0.0;
221    for( today=trailingIdx; today <= startIdx; today++ )
222    {
223       x = inReal0[today];
224       sumX  += x;
225       sumX2 += x*x;
226 
227       y = inReal1[today];
228       sumXY += x*y;
229       sumY  += y;
230       sumY2 += y*y;
231    }
232 
233    /* Write the first output.
234     * Save first the trailing values since the input
235     * and output might be the same array,
236     */
237    trailingX = inReal0[trailingIdx];
238    trailingY = inReal1[trailingIdx++];
239    tempReal = (sumX2-((sumX*sumX)/optInTimePeriod)) * (sumY2-((sumY*sumY)/optInTimePeriod));
240    if( !TA_IS_ZERO_OR_NEG(tempReal) )
241       outReal[0] = (sumXY-((sumX*sumY)/optInTimePeriod)) / std_sqrt(tempReal);
242    else
243       outReal[0] = 0.0;
244 
245    /* Tight loop to do subsequent values. */
246    outIdx = 1;
247    while( today <= endIdx )
248    {
249       /* Remove trailing values */
250       sumX  -= trailingX;
251       sumX2 -= trailingX*trailingX;
252 
253       sumXY -= trailingX*trailingY;
254       sumY  -= trailingY;
255       sumY2 -= trailingY*trailingY;
256 
257       /* Add new values */
258       x = inReal0[today];
259       sumX  += x;
260       sumX2 += x*x;
261 
262       y = inReal1[today++];
263       sumXY += x*y;
264       sumY  += y;
265       sumY2 += y*y;
266 
267       /* Output new coefficient.
268        * Save first the trailing values since the input
269        * and output might be the same array,
270        */
271       trailingX = inReal0[trailingIdx];
272       trailingY = inReal1[trailingIdx++];
273       tempReal = (sumX2-((sumX*sumX)/optInTimePeriod)) * (sumY2-((sumY*sumY)/optInTimePeriod));
274       if( !TA_IS_ZERO_OR_NEG(tempReal) )
275          outReal[outIdx++] = (sumXY-((sumX*sumY)/optInTimePeriod)) / std_sqrt(tempReal);
276       else
277          outReal[outIdx++] = 0.0;
278    }
279 
280    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
281 
282    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
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::Correl( int    startIdx,
296 /* Generated */                                        int    endIdx,
297 /* Generated */                                        cli::array<float>^ inReal0,
298 /* Generated */                                        cli::array<float>^ inReal1,
299 /* Generated */                                        int           optInTimePeriod, /* From 1 to 100000 */
300 /* Generated */                                        [Out]int%    outBegIdx,
301 /* Generated */                                        [Out]int%    outNBElement,
302 /* Generated */                                        cli::array<double>^  outReal )
303 /* Generated */ #elif defined( _JAVA )
304 /* Generated */ public RetCode correl( int    startIdx,
305 /* Generated */                        int    endIdx,
306 /* Generated */                        float        inReal0[],
307 /* Generated */                        float        inReal1[],
308 /* Generated */                        int           optInTimePeriod, /* From 1 to 100000 */
309 /* Generated */                        MInteger     outBegIdx,
310 /* Generated */                        MInteger     outNBElement,
311 /* Generated */                        double        outReal[] )
312 /* Generated */ #else
313 /* Generated */ TA_RetCode TA_S_CORREL( int    startIdx,
314 /* Generated */                         int    endIdx,
315 /* Generated */                         const float  inReal0[],
316 /* Generated */                         const float  inReal1[],
317 /* Generated */                         int           optInTimePeriod, /* From 1 to 100000 */
318 /* Generated */                         int          *outBegIdx,
319 /* Generated */                         int          *outNBElement,
320 /* Generated */                         double        outReal[] )
321 /* Generated */ #endif
322 /* Generated */ {
323 /* Generated */     double sumXY, sumX, sumY, sumX2, sumY2, x, y, trailingX, trailingY;
324 /* Generated */     double tempReal;
325 /* Generated */     int lookbackTotal, today, trailingIdx, outIdx;
326 /* Generated */  #ifndef TA_FUNC_NO_RANGE_CHECK
327 /* Generated */     if( startIdx < 0 )
328 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
329 /* Generated */     if( (endIdx < 0) || (endIdx < startIdx))
330 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
331 /* Generated */     #if !defined(_JAVA)
332 /* Generated */     if( !inReal0 ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
333 /* Generated */     if( !inReal1 ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
334 /* Generated */     #endif
335 /* Generated */     if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )
336 /* Generated */        optInTimePeriod = 30;
337 /* Generated */     else if( ((int)optInTimePeriod < 1) || ((int)optInTimePeriod > 100000) )
338 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
339 /* Generated */     #if !defined(_JAVA)
340 /* Generated */     if( !outReal )
341 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
342 /* Generated */     #endif
343 /* Generated */  #endif
344 /* Generated */    lookbackTotal = optInTimePeriod-1;
345 /* Generated */    if( startIdx < lookbackTotal )
346 /* Generated */       startIdx = lookbackTotal;
347 /* Generated */    if( startIdx > endIdx )
348 /* Generated */    {
349 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
350 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
351 /* Generated */       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
352 /* Generated */    }
353 /* Generated */    VALUE_HANDLE_DEREF(outBegIdx)  = startIdx;
354 /* Generated */    trailingIdx = startIdx - lookbackTotal;
355 /* Generated */    sumXY = sumX = sumY = sumX2 = sumY2 = 0.0;
356 /* Generated */    for( today=trailingIdx; today <= startIdx; today++ )
357 /* Generated */    {
358 /* Generated */       x = inReal0[today];
359 /* Generated */       sumX  += x;
360 /* Generated */       sumX2 += x*x;
361 /* Generated */       y = inReal1[today];
362 /* Generated */       sumXY += x*y;
363 /* Generated */       sumY  += y;
364 /* Generated */       sumY2 += y*y;
365 /* Generated */    }
366 /* Generated */    trailingX = inReal0[trailingIdx];
367 /* Generated */    trailingY = inReal1[trailingIdx++];
368 /* Generated */    tempReal = (sumX2-((sumX*sumX)/optInTimePeriod)) * (sumY2-((sumY*sumY)/optInTimePeriod));
369 /* Generated */    if( !TA_IS_ZERO_OR_NEG(tempReal) )
370 /* Generated */       outReal[0] = (sumXY-((sumX*sumY)/optInTimePeriod)) / std_sqrt(tempReal);
371 /* Generated */    else
372 /* Generated */       outReal[0] = 0.0;
373 /* Generated */    outIdx = 1;
374 /* Generated */    while( today <= endIdx )
375 /* Generated */    {
376 /* Generated */       sumX  -= trailingX;
377 /* Generated */       sumX2 -= trailingX*trailingX;
378 /* Generated */       sumXY -= trailingX*trailingY;
379 /* Generated */       sumY  -= trailingY;
380 /* Generated */       sumY2 -= trailingY*trailingY;
381 /* Generated */       x = inReal0[today];
382 /* Generated */       sumX  += x;
383 /* Generated */       sumX2 += x*x;
384 /* Generated */       y = inReal1[today++];
385 /* Generated */       sumXY += x*y;
386 /* Generated */       sumY  += y;
387 /* Generated */       sumY2 += y*y;
388 /* Generated */       trailingX = inReal0[trailingIdx];
389 /* Generated */       trailingY = inReal1[trailingIdx++];
390 /* Generated */       tempReal = (sumX2-((sumX*sumX)/optInTimePeriod)) * (sumY2-((sumY*sumY)/optInTimePeriod));
391 /* Generated */       if( !TA_IS_ZERO_OR_NEG(tempReal) )
392 /* Generated */          outReal[outIdx++] = (sumXY-((sumX*sumY)/optInTimePeriod)) / std_sqrt(tempReal);
393 /* Generated */       else
394 /* Generated */          outReal[outIdx++] = 0.0;
395 /* Generated */    }
396 /* Generated */    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
397 /* Generated */    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
398 /* Generated */ }
399 /* Generated */
400 /* Generated */ #if defined( _MANAGED )
401 /* Generated */ }}} // Close namespace TicTacTec.TA.Lib
402 /* Generated */ #endif
403 /**** END GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
404 
405