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 * 062804 MF Resolve div by zero bug on limit case.
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 )
RsiLookback(int optInTimePeriod)82 /* Generated */ int Core::RsiLookback( int optInTimePeriod ) /* From 2 to 100000 */
83 /* Generated */
84 /* Generated */ #elif defined( _JAVA )
85 /* Generated */ public int rsiLookback( int optInTimePeriod ) /* From 2 to 100000 */
86 /* Generated */
87 /* Generated */ #else
88 /* Generated */ int TA_RSI_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 int retValue;
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 = 14;
101 /* Generated */ else if( ((int)optInTimePeriod < 2) || ((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
109 retValue = optInTimePeriod + TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_RSI,Rsi);
110 if( TA_GLOBALS_COMPATIBILITY == ENUM_VALUE(Compatibility,TA_COMPATIBILITY_METASTOCK,Metastock) )
111 retValue--;
112
113 return retValue;
114 }
115
116 /**** START GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
117 /*
118 * TA_RSI - Relative Strength Index
119 *
120 * Input = double
121 * Output = double
122 *
123 * Optional Parameters
124 * -------------------
125 * optInTimePeriod:(From 2 to 100000)
126 * Number of period
127 *
128 *
129 */
130 /* Generated */
131 /* Generated */ #if defined( _MANAGED ) && defined( USE_SUBARRAY )
132 /* Generated */ enum class Core::RetCode Core::Rsi( int startIdx,
133 /* Generated */ int endIdx,
134 /* Generated */ SubArray^ inReal,
135 /* Generated */ int optInTimePeriod, /* From 2 to 100000 */
136 /* Generated */ [Out]int% outBegIdx,
137 /* Generated */ [Out]int% outNBElement,
138 /* Generated */ cli::array<double>^ outReal )
139 /* Generated */ #elif defined( _MANAGED )
140 /* Generated */ enum class Core::RetCode Core::Rsi( int startIdx,
141 /* Generated */ int endIdx,
142 /* Generated */ cli::array<double>^ inReal,
143 /* Generated */ int optInTimePeriod, /* From 2 to 100000 */
144 /* Generated */ [Out]int% outBegIdx,
145 /* Generated */ [Out]int% outNBElement,
146 /* Generated */ cli::array<double>^ outReal )
147 /* Generated */ #elif defined( _JAVA )
148 /* Generated */ public RetCode rsi( int startIdx,
149 /* Generated */ int endIdx,
150 /* Generated */ double inReal[],
151 /* Generated */ int optInTimePeriod, /* From 2 to 100000 */
152 /* Generated */ MInteger outBegIdx,
153 /* Generated */ MInteger outNBElement,
154 /* Generated */ double outReal[] )
155 /* Generated */ #else
156 /* Generated */ TA_RetCode TA_RSI( int startIdx,
157 /* Generated */ int endIdx,
158 /* Generated */ const double inReal[],
159 /* Generated */ int optInTimePeriod, /* From 2 to 100000 */
160 /* Generated */ int *outBegIdx,
161 /* Generated */ int *outNBElement,
162 /* Generated */ double outReal[] )
163 /* Generated */ #endif
164 /**** END GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
165 {
166 /* Insert local variables here. */
167 int outIdx;
168
169 int today, lookbackTotal, unstablePeriod, i;
170 double prevGain, prevLoss, prevValue, savePrevValue;
171 double tempValue1, tempValue2;
172
173 /**** START GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
174 /* Generated */
175 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
176 /* Generated */
177 /* Generated */ /* Validate the requested output range. */
178 /* Generated */ if( startIdx < 0 )
179 /* Generated */ return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
180 /* Generated */ if( (endIdx < 0) || (endIdx < startIdx))
181 /* Generated */ return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
182 /* Generated */
183 /* Generated */ #if !defined(_JAVA)
184 /* Generated */ if( !inReal ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
185 /* Generated */ #endif /* !defined(_JAVA)*/
186 /* Generated */ /* min/max are checked for optInTimePeriod. */
187 /* Generated */ if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )
188 /* Generated */ optInTimePeriod = 14;
189 /* Generated */ else if( ((int)optInTimePeriod < 2) || ((int)optInTimePeriod > 100000) )
190 /* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
191 /* Generated */
192 /* Generated */ #if !defined(_JAVA)
193 /* Generated */ if( !outReal )
194 /* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
195 /* Generated */
196 /* Generated */ #endif /* !defined(_JAVA) */
197 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
198 /* Generated */
199 /**** END GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
200
201 /* Insert TA function code here. */
202
203 /* The following algorithm is base on the original
204 * work from Wilder's and shall represent the
205 * original idea behind the classic RSI.
206 *
207 * Metastock is starting the calculation one price
208 * bar earlier. To make this possible, they assume
209 * that the very first bar will be identical to the
210 * previous one (no gain or loss).
211 */
212
213 /* If changing this function, please check also CMO
214 * which is mostly identical (just different in one step
215 * of calculation).
216 */
217
218 VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
219 VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
220
221 /* Adjust startIdx to account for the lookback period. */
222 lookbackTotal = LOOKBACK_CALL(RSI)( optInTimePeriod );
223
224 if( startIdx < lookbackTotal )
225 startIdx = lookbackTotal;
226
227 /* Make sure there is still something to evaluate. */
228 if( startIdx > endIdx )
229 return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
230
231 outIdx = 0; /* Index into the output. */
232
233 /* Trap special case where the period is '1'.
234 * In that case, just copy the input into the
235 * output for the requested range (as-is !)
236 */
237 if( optInTimePeriod == 1 )
238 {
239 VALUE_HANDLE_DEREF(outBegIdx) = startIdx;
240 i = (endIdx-startIdx)+1;
241 VALUE_HANDLE_DEREF(outNBElement) = i;
242 #if defined( USE_SUBARRAY ) && !defined( USE_SINGLE_PRECISION_INPUT )
243 ARRAY_MEMMOVE( outReal, 0, (inReal->mDataArray), (inReal->mOffset)+startIdx, i );
244 #else
245 ARRAY_MEMMOVE( outReal, 0, inReal, startIdx, i );
246 #endif
247 return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
248 }
249
250 /* Accumulate Wilder's "Average Gain" and "Average Loss"
251 * among the initial period.
252 */
253 today = startIdx-lookbackTotal;
254 prevValue = inReal[today];
255
256 unstablePeriod = TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_RSI,Rsi);
257
258 /* If there is no unstable period,
259 * calculate the 'additional' initial
260 * price bar who is particuliar to
261 * metastock.
262 * If there is an unstable period,
263 * no need to calculate since this
264 * first value will be surely skip.
265 */
266 if( (unstablePeriod == 0) &&
267 (TA_GLOBALS_COMPATIBILITY == ENUM_VALUE(Compatibility,TA_COMPATIBILITY_METASTOCK,Metastock)))
268 {
269 /* Preserve prevValue because it may get
270 * overwritten by the output.
271 *(because output ptr could be the same as input ptr).
272 */
273 savePrevValue = prevValue;
274
275 /* No unstable period, so must calculate first output
276 * particular to Metastock.
277 * (Metastock re-use the first price bar, so there
278 * is no loss/gain at first. Beats me why they
279 * are doing all this).
280 */
281 prevGain = 0.0;
282 prevLoss = 0.0;
283 for( i=optInTimePeriod; i > 0; i-- )
284 {
285 tempValue1 = inReal[today++];
286 tempValue2 = tempValue1 - prevValue;
287 prevValue = tempValue1;
288 if( tempValue2 < 0 )
289 prevLoss -= tempValue2;
290 else
291 prevGain += tempValue2;
292 }
293
294
295 tempValue1 = prevLoss/optInTimePeriod;
296 tempValue2 = prevGain/optInTimePeriod;
297
298 /* Write the output. */
299 tempValue1 = tempValue2+tempValue1;
300 if( !TA_IS_ZERO(tempValue1) )
301 outReal[outIdx++] = 100*(tempValue2/tempValue1);
302 else
303 outReal[outIdx++] = 0.0;
304
305 /* Are we done? */
306 if( today > endIdx )
307 {
308 VALUE_HANDLE_DEREF(outBegIdx) = startIdx;
309 VALUE_HANDLE_DEREF(outNBElement) = outIdx;
310 return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
311 }
312
313 /* Start over for the next price bar. */
314 today -= optInTimePeriod;
315 prevValue = savePrevValue;
316 }
317
318
319 /* Remaining of the processing is identical
320 * for both Classic calculation and Metastock.
321 */
322 prevGain = 0.0;
323 prevLoss = 0.0;
324 today++;
325 for( i=optInTimePeriod; i > 0; i-- )
326 {
327 tempValue1 = inReal[today++];
328 tempValue2 = tempValue1 - prevValue;
329 prevValue = tempValue1;
330 if( tempValue2 < 0 )
331 prevLoss -= tempValue2;
332 else
333 prevGain += tempValue2;
334 }
335
336
337 /* Subsequent prevLoss and prevGain are smoothed
338 * using the previous values (Wilder's approach).
339 * 1) Multiply the previous by 'period-1'.
340 * 2) Add today value.
341 * 3) Divide by 'period'.
342 */
343 prevLoss /= optInTimePeriod;
344 prevGain /= optInTimePeriod;
345
346 /* Often documentation present the RSI calculation as follow:
347 * RSI = 100 - (100 / 1 + (prevGain/prevLoss))
348 *
349 * The following is equivalent:
350 * RSI = 100 * (prevGain/(prevGain+prevLoss))
351 *
352 * The second equation is used here for speed optimization.
353 */
354 if( today > startIdx )
355 {
356 tempValue1 = prevGain+prevLoss;
357 if( !TA_IS_ZERO(tempValue1) )
358 outReal[outIdx++] = 100.0*(prevGain/tempValue1);
359 else
360 outReal[outIdx++] = 0.0;
361 }
362 else
363 {
364 /* Skip the unstable period. Do the processing
365 * but do not write it in the output.
366 */
367 while( today < startIdx )
368 {
369 tempValue1 = inReal[today];
370 tempValue2 = tempValue1 - prevValue;
371 prevValue = tempValue1;
372
373 prevLoss *= (optInTimePeriod-1);
374 prevGain *= (optInTimePeriod-1);
375 if( tempValue2 < 0 )
376 prevLoss -= tempValue2;
377 else
378 prevGain += tempValue2;
379
380 prevLoss /= optInTimePeriod;
381 prevGain /= optInTimePeriod;
382
383 today++;
384 }
385 }
386
387 /* Unstable period skipped... now continue
388 * processing if needed.
389 */
390 while( today <= endIdx )
391 {
392 tempValue1 = inReal[today++];
393 tempValue2 = tempValue1 - prevValue;
394 prevValue = tempValue1;
395
396 prevLoss *= (optInTimePeriod-1);
397 prevGain *= (optInTimePeriod-1);
398 if( tempValue2 < 0 )
399 prevLoss -= tempValue2;
400 else
401 prevGain += tempValue2;
402
403 prevLoss /= optInTimePeriod;
404 prevGain /= optInTimePeriod;
405 tempValue1 = prevGain+prevLoss;
406 if( !TA_IS_ZERO(tempValue1) )
407 outReal[outIdx++] = 100.0*(prevGain/tempValue1);
408 else
409 outReal[outIdx++] = 0.0;
410 }
411
412 VALUE_HANDLE_DEREF(outBegIdx) = startIdx;
413 VALUE_HANDLE_DEREF(outNBElement) = outIdx;
414
415 return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
416 }
417
418
419 /**** START GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
420 /* Generated */
421 /* Generated */ #define USE_SINGLE_PRECISION_INPUT
422 /* Generated */ #if !defined( _MANAGED ) && !defined( _JAVA )
423 /* Generated */ #undef TA_PREFIX
424 /* Generated */ #define TA_PREFIX(x) TA_S_##x
425 /* Generated */ #endif
426 /* Generated */ #undef INPUT_TYPE
427 /* Generated */ #define INPUT_TYPE float
428 /* Generated */ #if defined( _MANAGED )
429 /* Generated */ enum class Core::RetCode Core::Rsi( int startIdx,
430 /* Generated */ int endIdx,
431 /* Generated */ cli::array<float>^ inReal,
432 /* Generated */ int optInTimePeriod, /* From 2 to 100000 */
433 /* Generated */ [Out]int% outBegIdx,
434 /* Generated */ [Out]int% outNBElement,
435 /* Generated */ cli::array<double>^ outReal )
436 /* Generated */ #elif defined( _JAVA )
437 /* Generated */ public RetCode rsi( int startIdx,
438 /* Generated */ int endIdx,
439 /* Generated */ float inReal[],
440 /* Generated */ int optInTimePeriod, /* From 2 to 100000 */
441 /* Generated */ MInteger outBegIdx,
442 /* Generated */ MInteger outNBElement,
443 /* Generated */ double outReal[] )
444 /* Generated */ #else
445 /* Generated */ TA_RetCode TA_S_RSI( int startIdx,
446 /* Generated */ int endIdx,
447 /* Generated */ const float inReal[],
448 /* Generated */ int optInTimePeriod, /* From 2 to 100000 */
449 /* Generated */ int *outBegIdx,
450 /* Generated */ int *outNBElement,
451 /* Generated */ double outReal[] )
452 /* Generated */ #endif
453 /* Generated */ {
454 /* Generated */ int outIdx;
455 /* Generated */ int today, lookbackTotal, unstablePeriod, i;
456 /* Generated */ double prevGain, prevLoss, prevValue, savePrevValue;
457 /* Generated */ double tempValue1, tempValue2;
458 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
459 /* Generated */ if( startIdx < 0 )
460 /* Generated */ return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
461 /* Generated */ if( (endIdx < 0) || (endIdx < startIdx))
462 /* Generated */ return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
463 /* Generated */ #if !defined(_JAVA)
464 /* Generated */ if( !inReal ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
465 /* Generated */ #endif
466 /* Generated */ if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )
467 /* Generated */ optInTimePeriod = 14;
468 /* Generated */ else if( ((int)optInTimePeriod < 2) || ((int)optInTimePeriod > 100000) )
469 /* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
470 /* Generated */ #if !defined(_JAVA)
471 /* Generated */ if( !outReal )
472 /* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
473 /* Generated */ #endif
474 /* Generated */ #endif
475 /* Generated */ VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
476 /* Generated */ VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
477 /* Generated */ lookbackTotal = LOOKBACK_CALL(RSI)( optInTimePeriod );
478 /* Generated */ if( startIdx < lookbackTotal )
479 /* Generated */ startIdx = lookbackTotal;
480 /* Generated */ if( startIdx > endIdx )
481 /* Generated */ return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
482 /* Generated */ outIdx = 0;
483 /* Generated */ if( optInTimePeriod == 1 )
484 /* Generated */ {
485 /* Generated */ VALUE_HANDLE_DEREF(outBegIdx) = startIdx;
486 /* Generated */ i = (endIdx-startIdx)+1;
487 /* Generated */ VALUE_HANDLE_DEREF(outNBElement) = i;
488 /* Generated */ #if defined( USE_SUBARRAY ) && !defined( USE_SINGLE_PRECISION_INPUT )
489 /* Generated */ ARRAY_MEMMOVE( outReal, 0, (inReal->mDataArray), (inReal->mOffset)+startIdx, i );
490 /* Generated */ #else
491 /* Generated */ ARRAY_MEMMOVE( outReal, 0, inReal, startIdx, i );
492 /* Generated */ #endif
493 /* Generated */ return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
494 /* Generated */ }
495 /* Generated */ today = startIdx-lookbackTotal;
496 /* Generated */ prevValue = inReal[today];
497 /* Generated */ unstablePeriod = TA_GLOBALS_UNSTABLE_PERIOD(TA_FUNC_UNST_RSI,Rsi);
498 /* Generated */ if( (unstablePeriod == 0) &&
499 /* Generated */ (TA_GLOBALS_COMPATIBILITY == ENUM_VALUE(Compatibility,TA_COMPATIBILITY_METASTOCK,Metastock)))
500 /* Generated */ {
501 /* Generated */ savePrevValue = prevValue;
502 /* Generated */ prevGain = 0.0;
503 /* Generated */ prevLoss = 0.0;
504 /* Generated */ for( i=optInTimePeriod; i > 0; i-- )
505 /* Generated */ {
506 /* Generated */ tempValue1 = inReal[today++];
507 /* Generated */ tempValue2 = tempValue1 - prevValue;
508 /* Generated */ prevValue = tempValue1;
509 /* Generated */ if( tempValue2 < 0 )
510 /* Generated */ prevLoss -= tempValue2;
511 /* Generated */ else
512 /* Generated */ prevGain += tempValue2;
513 /* Generated */ }
514 /* Generated */ tempValue1 = prevLoss/optInTimePeriod;
515 /* Generated */ tempValue2 = prevGain/optInTimePeriod;
516 /* Generated */ tempValue1 = tempValue2+tempValue1;
517 /* Generated */ if( !TA_IS_ZERO(tempValue1) )
518 /* Generated */ outReal[outIdx++] = 100*(tempValue2/tempValue1);
519 /* Generated */ else
520 /* Generated */ outReal[outIdx++] = 0.0;
521 /* Generated */ if( today > endIdx )
522 /* Generated */ {
523 /* Generated */ VALUE_HANDLE_DEREF(outBegIdx) = startIdx;
524 /* Generated */ VALUE_HANDLE_DEREF(outNBElement) = outIdx;
525 /* Generated */ return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
526 /* Generated */ }
527 /* Generated */ today -= optInTimePeriod;
528 /* Generated */ prevValue = savePrevValue;
529 /* Generated */ }
530 /* Generated */ prevGain = 0.0;
531 /* Generated */ prevLoss = 0.0;
532 /* Generated */ today++;
533 /* Generated */ for( i=optInTimePeriod; i > 0; i-- )
534 /* Generated */ {
535 /* Generated */ tempValue1 = inReal[today++];
536 /* Generated */ tempValue2 = tempValue1 - prevValue;
537 /* Generated */ prevValue = tempValue1;
538 /* Generated */ if( tempValue2 < 0 )
539 /* Generated */ prevLoss -= tempValue2;
540 /* Generated */ else
541 /* Generated */ prevGain += tempValue2;
542 /* Generated */ }
543 /* Generated */ prevLoss /= optInTimePeriod;
544 /* Generated */ prevGain /= optInTimePeriod;
545 /* Generated */ if( today > startIdx )
546 /* Generated */ {
547 /* Generated */ tempValue1 = prevGain+prevLoss;
548 /* Generated */ if( !TA_IS_ZERO(tempValue1) )
549 /* Generated */ outReal[outIdx++] = 100.0*(prevGain/tempValue1);
550 /* Generated */ else
551 /* Generated */ outReal[outIdx++] = 0.0;
552 /* Generated */ }
553 /* Generated */ else
554 /* Generated */ {
555 /* Generated */ while( today < startIdx )
556 /* Generated */ {
557 /* Generated */ tempValue1 = inReal[today];
558 /* Generated */ tempValue2 = tempValue1 - prevValue;
559 /* Generated */ prevValue = tempValue1;
560 /* Generated */ prevLoss *= (optInTimePeriod-1);
561 /* Generated */ prevGain *= (optInTimePeriod-1);
562 /* Generated */ if( tempValue2 < 0 )
563 /* Generated */ prevLoss -= tempValue2;
564 /* Generated */ else
565 /* Generated */ prevGain += tempValue2;
566 /* Generated */ prevLoss /= optInTimePeriod;
567 /* Generated */ prevGain /= optInTimePeriod;
568 /* Generated */ today++;
569 /* Generated */ }
570 /* Generated */ }
571 /* Generated */ while( today <= endIdx )
572 /* Generated */ {
573 /* Generated */ tempValue1 = inReal[today++];
574 /* Generated */ tempValue2 = tempValue1 - prevValue;
575 /* Generated */ prevValue = tempValue1;
576 /* Generated */ prevLoss *= (optInTimePeriod-1);
577 /* Generated */ prevGain *= (optInTimePeriod-1);
578 /* Generated */ if( tempValue2 < 0 )
579 /* Generated */ prevLoss -= tempValue2;
580 /* Generated */ else
581 /* Generated */ prevGain += tempValue2;
582 /* Generated */ prevLoss /= optInTimePeriod;
583 /* Generated */ prevGain /= optInTimePeriod;
584 /* Generated */ tempValue1 = prevGain+prevLoss;
585 /* Generated */ if( !TA_IS_ZERO(tempValue1) )
586 /* Generated */ outReal[outIdx++] = 100.0*(prevGain/tempValue1);
587 /* Generated */ else
588 /* Generated */ outReal[outIdx++] = 0.0;
589 /* Generated */ }
590 /* Generated */ VALUE_HANDLE_DEREF(outBegIdx) = startIdx;
591 /* Generated */ VALUE_HANDLE_DEREF(outNBElement) = outIdx;
592 /* Generated */ return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
593 /* Generated */ }
594 /* Generated */
595 /* Generated */ #if defined( _MANAGED )
596 /* Generated */ }}} // Close namespace TicTacTec.TA.Lib
597 /* Generated */ #endif
598 /**** END GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
599
600