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  *  EKO      echo999@ifrance.com
40  *
41  * Change history:
42  *
43  *  MMDDYY BY   Description
44  *  -------------------------------------------------------------------
45  *  010802 MF   Template creation.
46  *  051103 EKO  Found bug and fix related to outFastD.
47  *  052603 MF   Adapt code to compile with .NET Managed C++
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 )
StochFLookback(int optInFastK_Period,int optInFastD_Period,MAType optInFastD_MAType)83 /* Generated */ int Core::StochFLookback( int           optInFastK_Period, /* From 1 to 100000 */
84 /* Generated */                         int           optInFastD_Period, /* From 1 to 100000 */
85 /* Generated */                         MAType        optInFastD_MAType ) /* Generated */
86 /* Generated */ #elif defined( _JAVA )
87 /* Generated */ public int stochFLookback( int           optInFastK_Period, /* From 1 to 100000 */
88 /* Generated */                          int           optInFastD_Period, /* From 1 to 100000 */
89 /* Generated */                          MAType        optInFastD_MAType ) /* Generated */
90 /* Generated */ #else
91 /* Generated */ int TA_STOCHF_Lookback( int           optInFastK_Period, /* From 1 to 100000 */
92 /* Generated */                       int           optInFastD_Period, /* From 1 to 100000 */
93 /* Generated */                       TA_MAType     optInFastD_MAType ) /* Generated */
94 /* Generated */ #endif
95 /**** END GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
96 {
97    /* insert local variable here */
98    int retValue;
99 
100 /**** START GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
101 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
102 /* Generated */    /* min/max are checked for optInFastK_Period. */
103 /* Generated */    if( (int)optInFastK_Period == TA_INTEGER_DEFAULT )
104 /* Generated */       optInFastK_Period = 5;
105 /* Generated */    else if( ((int)optInFastK_Period < 1) || ((int)optInFastK_Period > 100000) )
106 /* Generated */       return -1;
107 /* Generated */
108 /* Generated */    /* min/max are checked for optInFastD_Period. */
109 /* Generated */    if( (int)optInFastD_Period == TA_INTEGER_DEFAULT )
110 /* Generated */       optInFastD_Period = 3;
111 /* Generated */    else if( ((int)optInFastD_Period < 1) || ((int)optInFastD_Period > 100000) )
112 /* Generated */       return -1;
113 /* Generated */
114 /* Generated */    #if !defined(_MANAGED) && !defined(_JAVA)
115 /* Generated */    if( (int)optInFastD_MAType == TA_INTEGER_DEFAULT )
116 /* Generated */       optInFastD_MAType = (TA_MAType)0;
117 /* Generated */    else if( ((int)optInFastD_MAType < 0) || ((int)optInFastD_MAType > 8) )
118 /* Generated */       return -1;
119 /* Generated */
120 /* Generated */    #endif /* !defined(_MANAGED) && !defined(_JAVA)*/
121 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
122 /**** END GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
123 
124    /* insert lookback code here. */
125 
126    /* Account for the initial data needed for Fast-K. */
127    retValue = (optInFastK_Period - 1);
128 
129    /* Add the smoothing being done for Fast-D */
130    retValue += LOOKBACK_CALL(MA)( optInFastD_Period, optInFastD_MAType );
131 
132    return retValue;
133 }
134 
135 /**** START GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
136 /*
137  * TA_STOCHF - Stochastic Fast
138  *
139  * Input  = High, Low, Close
140  * Output = double, double
141  *
142  * Optional Parameters
143  * -------------------
144  * optInFastK_Period:(From 1 to 100000)
145  *    Time period for building the Fast-K line
146  *
147  * optInFastD_Period:(From 1 to 100000)
148  *    Smoothing for making the Fast-D line. Usually set to 3
149  *
150  * optInFastD_MAType:
151  *    Type of Moving Average for Fast-D
152  *
153  *
154  */
155 /* Generated */
156 /* Generated */ #if defined( _MANAGED ) && defined( USE_SUBARRAY )
157 /* Generated */ enum class Core::RetCode Core::StochF( int    startIdx,
158 /* Generated */                                        int    endIdx,
159 /* Generated */                                        SubArray^    inHigh,
160 /* Generated */                                        SubArray^    inLow,
161 /* Generated */                                        SubArray^    inClose,
162 /* Generated */                                        int           optInFastK_Period, /* From 1 to 100000 */
163 /* Generated */                                        int           optInFastD_Period, /* From 1 to 100000 */
164 /* Generated */                                        MAType        optInFastD_MAType,
165 /* Generated */                                        [Out]int%    outBegIdx,
166 /* Generated */                                        [Out]int%    outNBElement,
167 /* Generated */                                        cli::array<double>^  outFastK,
168 /* Generated */                                        cli::array<double>^  outFastD )
169 /* Generated */ #elif defined( _MANAGED )
170 /* Generated */ enum class Core::RetCode Core::StochF( int    startIdx,
171 /* Generated */                                        int    endIdx,
172 /* Generated */                                        cli::array<double>^ inHigh,
173 /* Generated */                                        cli::array<double>^ inLow,
174 /* Generated */                                        cli::array<double>^ inClose,
175 /* Generated */                                        int           optInFastK_Period, /* From 1 to 100000 */
176 /* Generated */                                        int           optInFastD_Period, /* From 1 to 100000 */
177 /* Generated */                                        MAType        optInFastD_MAType,
178 /* Generated */                                        [Out]int%    outBegIdx,
179 /* Generated */                                        [Out]int%    outNBElement,
180 /* Generated */                                        cli::array<double>^  outFastK,
181 /* Generated */                                        cli::array<double>^  outFastD )
182 /* Generated */ #elif defined( _JAVA )
183 /* Generated */ public RetCode stochF( int    startIdx,
184 /* Generated */                        int    endIdx,
185 /* Generated */                        double       inHigh[],
186 /* Generated */                        double       inLow[],
187 /* Generated */                        double       inClose[],
188 /* Generated */                        int           optInFastK_Period, /* From 1 to 100000 */
189 /* Generated */                        int           optInFastD_Period, /* From 1 to 100000 */
190 /* Generated */                        MAType        optInFastD_MAType,
191 /* Generated */                        MInteger     outBegIdx,
192 /* Generated */                        MInteger     outNBElement,
193 /* Generated */                        double        outFastK[],
194 /* Generated */                        double        outFastD[] )
195 /* Generated */ #else
196 /* Generated */ TA_RetCode TA_STOCHF( int    startIdx,
197 /* Generated */                       int    endIdx,
198 /* Generated */                       const double inHigh[],
199 /* Generated */                       const double inLow[],
200 /* Generated */                       const double inClose[],
201 /* Generated */                       int           optInFastK_Period, /* From 1 to 100000 */
202 /* Generated */                       int           optInFastD_Period, /* From 1 to 100000 */
203 /* Generated */                       TA_MAType     optInFastD_MAType,
204 /* Generated */                       int          *outBegIdx,
205 /* Generated */                       int          *outNBElement,
206 /* Generated */                       double        outFastK[],
207 /* Generated */                       double        outFastD[] )
208 /* Generated */ #endif
209 /**** END GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
210 {
211 	/* insert local variable here */
212    ENUM_DECLARATION(RetCode) retCode;
213    double lowest, highest, tmp, diff;
214    ARRAY_REF( tempBuffer );
215    int outIdx, lowestIdx, highestIdx;
216    int lookbackTotal, lookbackK, lookbackFastD;
217    int trailingIdx, today, i;
218 
219    #if !defined( _MANAGED ) && !defined(USE_SINGLE_PRECISION_INPUT) && !defined( _JAVA )
220    int bufferIsAllocated;
221    #endif
222 
223 /**** START GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
224 /* Generated */
225 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
226 /* Generated */
227 /* Generated */    /* Validate the requested output range. */
228 /* Generated */    if( startIdx < 0 )
229 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
230 /* Generated */    if( (endIdx < 0) || (endIdx < startIdx))
231 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
232 /* Generated */
233 /* Generated */    #if !defined(_JAVA)
234 /* Generated */    /* Verify required price component. */
235 /* Generated */    if(!inHigh||!inLow||!inClose)
236 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
237 /* Generated */
238 /* Generated */    #endif /* !defined(_JAVA)*/
239 /* Generated */    /* min/max are checked for optInFastK_Period. */
240 /* Generated */    if( (int)optInFastK_Period == TA_INTEGER_DEFAULT )
241 /* Generated */       optInFastK_Period = 5;
242 /* Generated */    else if( ((int)optInFastK_Period < 1) || ((int)optInFastK_Period > 100000) )
243 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
244 /* Generated */
245 /* Generated */    /* min/max are checked for optInFastD_Period. */
246 /* Generated */    if( (int)optInFastD_Period == TA_INTEGER_DEFAULT )
247 /* Generated */       optInFastD_Period = 3;
248 /* Generated */    else if( ((int)optInFastD_Period < 1) || ((int)optInFastD_Period > 100000) )
249 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
250 /* Generated */
251 /* Generated */    #if !defined(_MANAGED) && !defined(_JAVA)
252 /* Generated */    if( (int)optInFastD_MAType == TA_INTEGER_DEFAULT )
253 /* Generated */       optInFastD_MAType = (TA_MAType)0;
254 /* Generated */    else if( ((int)optInFastD_MAType < 0) || ((int)optInFastD_MAType > 8) )
255 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
256 /* Generated */
257 /* Generated */    #endif /* !defined(_MANAGED) && !defined(_JAVA)*/
258 /* Generated */    #if !defined(_JAVA)
259 /* Generated */    if( !outFastK )
260 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
261 /* Generated */
262 /* Generated */    if( !outFastD )
263 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
264 /* Generated */
265 /* Generated */    #endif /* !defined(_JAVA) */
266 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
267 /* Generated */
268 /**** END GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
269 
270    /* Insert TA function code here. */
271 
272    /* With stochastic, there is a total of 4 different lines that
273     * are defined: FASTK, FASTD, SLOWK and SLOWD.
274     *
275     * The D is the signal line usually drawn over its
276     * corresponding K function.
277     *
278     *                    (Today's Close - LowestLow)
279     *  FASTK(Kperiod) =  --------------------------- * 100
280     *                     (HighestHigh - LowestLow)
281     *
282     *  FASTD(FastDperiod, MA type) = MA Smoothed FASTK over FastDperiod
283     *
284     *  SLOWK(SlowKperiod, MA type) = MA Smoothed FASTK over SlowKperiod
285     *
286     *  SLOWD(SlowDperiod, MA Type) = MA Smoothed SLOWK over SlowDperiod
287     *
288     * The HighestHigh and LowestLow are the extreme values among the
289     * last 'Kperiod'.
290     *
291     * SLOWK and FASTD are equivalent when using the same period.
292     *
293     * The following shows how these four lines are made available in TA-LIB:
294     *
295     *  TA_STOCH  : Returns the SLOWK and SLOWD
296     *  TA_STOCHF : Returns the FASTK and FASTD
297     *
298     * The TA_STOCH function correspond to the more widely implemented version
299     * found in many software/charting package. The TA_STOCHF is more rarely
300     * used because its higher volatility cause often whipsaws.
301     */
302 
303    /* Identify the lookback needed. */
304    lookbackK      = optInFastK_Period-1;
305    lookbackFastD  = LOOKBACK_CALL(MA)( optInFastD_Period, optInFastD_MAType );
306    lookbackTotal  = lookbackK + lookbackFastD;
307 
308    /* Move up the start index if there is not
309     * enough initial data.
310     */
311    if( startIdx < lookbackTotal )
312       startIdx = lookbackTotal;
313 
314    /* Make sure there is still something to evaluate. */
315    if( startIdx > endIdx )
316    {
317       /* Succeed... but no data in the output. */
318       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
319       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
320       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
321    }
322 
323    /* Do the K calculation:
324     *
325     *    Kt = 100 x ((Ct-Lt)/(Ht-Lt))
326     *
327     * Kt is today stochastic
328     * Ct is today closing price.
329     * Lt is the lowest price of the last K Period (including today)
330     * Ht is the highest price of the last K Period (including today)
331     */
332 
333    /* Proceed with the calculation for the requested range.
334     * Note that this algorithm allows the input and
335     * output to be the same buffer.
336     */
337    outIdx = 0;
338 
339    /* Calculate just enough K for ending up with the caller
340     * requested range. (The range of k must consider all
341     * the lookback involve with the smoothing).
342     */
343    trailingIdx = startIdx-lookbackTotal;
344    today       = trailingIdx+lookbackK;
345    lowestIdx   = highestIdx = -1;
346    diff = highest = lowest  = 0.0;
347 
348    /* Allocate a temporary buffer large enough to
349     * store the K.
350     *
351     * If the output is the same as the input, great
352     * we just save ourself one memory allocation.
353     */
354    #if !defined( _MANAGED ) && !defined(USE_SINGLE_PRECISION_INPUT) && !defined( _JAVA )
355       bufferIsAllocated = 0;
356    #endif
357 
358    #if defined(USE_SINGLE_PRECISION_INPUT) || defined( USE_SUBARRAY )
359       /* Always alloc, since output is of different type and
360        * its allocated size is not guarantee to be as large as
361        * the input.
362        */
363       ARRAY_ALLOC( tempBuffer, endIdx-today+1 );
364    #else
365       if( (outFastK == inHigh) ||
366           (outFastK == inLow)  ||
367           (outFastK == inClose) )
368       {
369          tempBuffer = outFastK;
370       }
371       else if( (outFastD == inHigh) ||
372                (outFastD == inLow)  ||
373                (outFastD == inClose) )
374       {
375          tempBuffer = outFastD;
376       }
377       else
378       {
379          #if !defined( _MANAGED ) && !defined( _JAVA )
380             bufferIsAllocated = 1;
381          #endif
382          ARRAY_ALLOC(tempBuffer, endIdx-today+1 );
383       }
384    #endif
385 
386    /* Do the K calculation */
387    while( today <= endIdx )
388    {
389       /* Set the lowest low */
390       tmp = inLow[today];
391       if( lowestIdx < trailingIdx )
392       {
393          lowestIdx = trailingIdx;
394          lowest = inLow[lowestIdx];
395          i = lowestIdx;
396          while( ++i<=today )
397          {
398             tmp = inLow[i];
399             if( tmp < lowest )
400             {
401                lowestIdx = i;
402                lowest = tmp;
403             }
404          }
405          diff = (highest - lowest)/100.0;
406       }
407       else if( tmp <= lowest )
408       {
409          lowestIdx = today;
410          lowest = tmp;
411          diff = (highest - lowest)/100.0;
412       }
413 
414       /* Set the highest high */
415       tmp = inHigh[today];
416       if( highestIdx < trailingIdx )
417       {
418          highestIdx = trailingIdx;
419          highest = inHigh[highestIdx];
420          i = highestIdx;
421          while( ++i<=today )
422          {
423             tmp = inHigh[i];
424             if( tmp > highest )
425             {
426                highestIdx = i;
427                highest = tmp;
428             }
429          }
430          diff = (highest - lowest)/100.0;
431       }
432       else if( tmp >= highest )
433       {
434          highestIdx = today;
435          highest = tmp;
436          diff = (highest - lowest)/100.0;
437       }
438 
439       /* Calculate stochastic. */
440       if( diff != 0.0 )
441         tempBuffer[outIdx++] = (inClose[today]-lowest)/diff;
442       else
443         tempBuffer[outIdx++] = 0.0;
444 
445       trailingIdx++;
446       today++;
447    }
448 
449    /* Fast-K calculation completed. This K calculation is returned
450     * to the caller. It is smoothed to become Fast-D.
451     */
452    retCode = FUNCTION_CALL_DOUBLE(MA)( 0, outIdx-1,
453                                        tempBuffer, optInFastD_Period,
454                                        optInFastD_MAType,
455                                        outBegIdx, outNBElement, outFastD );
456 
457 
458    if( (retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) ) || ((int)VALUE_HANDLE_DEREF(outNBElement)) == 0 )
459    {
460       #if defined(USE_SINGLE_PRECISION_INPUT)
461          ARRAY_FREE( tempBuffer );
462       #else
463          ARRAY_FREE_COND( bufferIsAllocated, tempBuffer );
464       #endif
465       /* Something wrong happen? No further data? */
466       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
467       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
468       return retCode;
469    }
470 
471    /* Copy tempBuffer into the caller buffer.
472     * (Calculation could not be done directly in the
473     *  caller buffer because more input data then the
474     *  requested range was needed for doing %D).
475     */
476    ARRAY_MEMMOVE( outFastK, 0, tempBuffer, lookbackFastD, (int)VALUE_HANDLE_DEREF(outNBElement) );
477 
478    /* Don't need K anymore, free it if it was allocated here. */
479    #if defined(USE_SINGLE_PRECISION_INPUT)
480       ARRAY_FREE( tempBuffer );
481    #else
482       ARRAY_FREE_COND( bufferIsAllocated, tempBuffer );
483    #endif
484 
485    if( retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) )
486    {
487       /* Something wrong happen while processing %D? */
488       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
489       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
490       return retCode;
491    }
492 
493    /* Note: Keep the outBegIdx relative to the
494     *       caller input before returning.
495     */
496    VALUE_HANDLE_DEREF(outBegIdx) = startIdx;
497 
498    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
499 }
500 
501 /**** START GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
502 /* Generated */
503 /* Generated */ #define  USE_SINGLE_PRECISION_INPUT
504 /* Generated */ #if !defined( _MANAGED ) && !defined( _JAVA )
505 /* Generated */    #undef   TA_PREFIX
506 /* Generated */    #define  TA_PREFIX(x) TA_S_##x
507 /* Generated */ #endif
508 /* Generated */ #undef   INPUT_TYPE
509 /* Generated */ #define  INPUT_TYPE float
510 /* Generated */ #if defined( _MANAGED )
511 /* Generated */ enum class Core::RetCode Core::StochF( int    startIdx,
512 /* Generated */                                        int    endIdx,
513 /* Generated */                                        cli::array<float>^ inHigh,
514 /* Generated */                                        cli::array<float>^ inLow,
515 /* Generated */                                        cli::array<float>^ inClose,
516 /* Generated */                                        int           optInFastK_Period, /* From 1 to 100000 */
517 /* Generated */                                        int           optInFastD_Period, /* From 1 to 100000 */
518 /* Generated */                                        MAType        optInFastD_MAType,
519 /* Generated */                                        [Out]int%    outBegIdx,
520 /* Generated */                                        [Out]int%    outNBElement,
521 /* Generated */                                        cli::array<double>^  outFastK,
522 /* Generated */                                        cli::array<double>^  outFastD )
523 /* Generated */ #elif defined( _JAVA )
524 /* Generated */ public RetCode stochF( int    startIdx,
525 /* Generated */                        int    endIdx,
526 /* Generated */                        float        inHigh[],
527 /* Generated */                        float        inLow[],
528 /* Generated */                        float        inClose[],
529 /* Generated */                        int           optInFastK_Period, /* From 1 to 100000 */
530 /* Generated */                        int           optInFastD_Period, /* From 1 to 100000 */
531 /* Generated */                        MAType        optInFastD_MAType,
532 /* Generated */                        MInteger     outBegIdx,
533 /* Generated */                        MInteger     outNBElement,
534 /* Generated */                        double        outFastK[],
535 /* Generated */                        double        outFastD[] )
536 /* Generated */ #else
537 /* Generated */ TA_RetCode TA_S_STOCHF( int    startIdx,
538 /* Generated */                         int    endIdx,
539 /* Generated */                         const float  inHigh[],
540 /* Generated */                         const float  inLow[],
541 /* Generated */                         const float  inClose[],
542 /* Generated */                         int           optInFastK_Period, /* From 1 to 100000 */
543 /* Generated */                         int           optInFastD_Period, /* From 1 to 100000 */
544 /* Generated */                         TA_MAType     optInFastD_MAType,
545 /* Generated */                         int          *outBegIdx,
546 /* Generated */                         int          *outNBElement,
547 /* Generated */                         double        outFastK[],
548 /* Generated */                         double        outFastD[] )
549 /* Generated */ #endif
550 /* Generated */ {
551 /* Generated */    ENUM_DECLARATION(RetCode) retCode;
552 /* Generated */    double lowest, highest, tmp, diff;
553 /* Generated */    ARRAY_REF( tempBuffer );
554 /* Generated */    int outIdx, lowestIdx, highestIdx;
555 /* Generated */    int lookbackTotal, lookbackK, lookbackFastD;
556 /* Generated */    int trailingIdx, today, i;
557 /* Generated */    #if !defined( _MANAGED ) && !defined(USE_SINGLE_PRECISION_INPUT) && !defined( _JAVA )
558 /* Generated */    int bufferIsAllocated;
559 /* Generated */    #endif
560 /* Generated */  #ifndef TA_FUNC_NO_RANGE_CHECK
561 /* Generated */     if( startIdx < 0 )
562 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
563 /* Generated */     if( (endIdx < 0) || (endIdx < startIdx))
564 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
565 /* Generated */     #if !defined(_JAVA)
566 /* Generated */     if(!inHigh||!inLow||!inClose)
567 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
568 /* Generated */     #endif
569 /* Generated */     if( (int)optInFastK_Period == TA_INTEGER_DEFAULT )
570 /* Generated */        optInFastK_Period = 5;
571 /* Generated */     else if( ((int)optInFastK_Period < 1) || ((int)optInFastK_Period > 100000) )
572 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
573 /* Generated */     if( (int)optInFastD_Period == TA_INTEGER_DEFAULT )
574 /* Generated */        optInFastD_Period = 3;
575 /* Generated */     else if( ((int)optInFastD_Period < 1) || ((int)optInFastD_Period > 100000) )
576 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
577 /* Generated */     #if !defined(_MANAGED) && !defined(_JAVA)
578 /* Generated */     if( (int)optInFastD_MAType == TA_INTEGER_DEFAULT )
579 /* Generated */        optInFastD_MAType = (TA_MAType)0;
580 /* Generated */     else if( ((int)optInFastD_MAType < 0) || ((int)optInFastD_MAType > 8) )
581 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
582 /* Generated */     #endif
583 /* Generated */     #if !defined(_JAVA)
584 /* Generated */     if( !outFastK )
585 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
586 /* Generated */     if( !outFastD )
587 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
588 /* Generated */     #endif
589 /* Generated */  #endif
590 /* Generated */    lookbackK      = optInFastK_Period-1;
591 /* Generated */    lookbackFastD  = LOOKBACK_CALL(MA)( optInFastD_Period, optInFastD_MAType );
592 /* Generated */    lookbackTotal  = lookbackK + lookbackFastD;
593 /* Generated */    if( startIdx < lookbackTotal )
594 /* Generated */       startIdx = lookbackTotal;
595 /* Generated */    if( startIdx > endIdx )
596 /* Generated */    {
597 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
598 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
599 /* Generated */       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
600 /* Generated */    }
601 /* Generated */    outIdx = 0;
602 /* Generated */    trailingIdx = startIdx-lookbackTotal;
603 /* Generated */    today       = trailingIdx+lookbackK;
604 /* Generated */    lowestIdx   = highestIdx = -1;
605 /* Generated */    diff = highest = lowest  = 0.0;
606 /* Generated */    #if !defined( _MANAGED ) && !defined(USE_SINGLE_PRECISION_INPUT) && !defined( _JAVA )
607 /* Generated */       bufferIsAllocated = 0;
608 /* Generated */    #endif
609 /* Generated */    #if defined(USE_SINGLE_PRECISION_INPUT) || defined( USE_SUBARRAY )
610 /* Generated */       ARRAY_ALLOC( tempBuffer, endIdx-today+1 );
611 /* Generated */    #else
612 /* Generated */       if( (outFastK == inHigh) ||
613 /* Generated */           (outFastK == inLow)  ||
614 /* Generated */           (outFastK == inClose) )
615 /* Generated */       {
616 /* Generated */          tempBuffer = outFastK;
617 /* Generated */       }
618 /* Generated */       else if( (outFastD == inHigh) ||
619 /* Generated */                (outFastD == inLow)  ||
620 /* Generated */                (outFastD == inClose) )
621 /* Generated */       {
622 /* Generated */          tempBuffer = outFastD;
623 /* Generated */       }
624 /* Generated */       else
625 /* Generated */       {
626 /* Generated */          #if !defined( _MANAGED ) && !defined( _JAVA )
627 /* Generated */             bufferIsAllocated = 1;
628 /* Generated */          #endif
629 /* Generated */          ARRAY_ALLOC(tempBuffer, endIdx-today+1 );
630 /* Generated */       }
631 /* Generated */    #endif
632 /* Generated */    while( today <= endIdx )
633 /* Generated */    {
634 /* Generated */       tmp = inLow[today];
635 /* Generated */       if( lowestIdx < trailingIdx )
636 /* Generated */       {
637 /* Generated */          lowestIdx = trailingIdx;
638 /* Generated */          lowest = inLow[lowestIdx];
639 /* Generated */          i = lowestIdx;
640 /* Generated */          while( ++i<=today )
641 /* Generated */          {
642 /* Generated */             tmp = inLow[i];
643 /* Generated */             if( tmp < lowest )
644 /* Generated */             {
645 /* Generated */                lowestIdx = i;
646 /* Generated */                lowest = tmp;
647 /* Generated */             }
648 /* Generated */          }
649 /* Generated */          diff = (highest - lowest)/100.0;
650 /* Generated */       }
651 /* Generated */       else if( tmp <= lowest )
652 /* Generated */       {
653 /* Generated */          lowestIdx = today;
654 /* Generated */          lowest = tmp;
655 /* Generated */          diff = (highest - lowest)/100.0;
656 /* Generated */       }
657 /* Generated */       tmp = inHigh[today];
658 /* Generated */       if( highestIdx < trailingIdx )
659 /* Generated */       {
660 /* Generated */          highestIdx = trailingIdx;
661 /* Generated */          highest = inHigh[highestIdx];
662 /* Generated */          i = highestIdx;
663 /* Generated */          while( ++i<=today )
664 /* Generated */          {
665 /* Generated */             tmp = inHigh[i];
666 /* Generated */             if( tmp > highest )
667 /* Generated */             {
668 /* Generated */                highestIdx = i;
669 /* Generated */                highest = tmp;
670 /* Generated */             }
671 /* Generated */          }
672 /* Generated */          diff = (highest - lowest)/100.0;
673 /* Generated */       }
674 /* Generated */       else if( tmp >= highest )
675 /* Generated */       {
676 /* Generated */          highestIdx = today;
677 /* Generated */          highest = tmp;
678 /* Generated */          diff = (highest - lowest)/100.0;
679 /* Generated */       }
680 /* Generated */       if( diff != 0.0 )
681 /* Generated */         tempBuffer[outIdx++] = (inClose[today]-lowest)/diff;
682 /* Generated */       else
683 /* Generated */         tempBuffer[outIdx++] = 0.0;
684 /* Generated */       trailingIdx++;
685 /* Generated */       today++;
686 /* Generated */    }
687 /* Generated */    retCode = FUNCTION_CALL_DOUBLE(MA)( 0, outIdx-1,
688 /* Generated */                                        tempBuffer, optInFastD_Period,
689 /* Generated */                                        optInFastD_MAType,
690 /* Generated */                                        outBegIdx, outNBElement, outFastD );
691 /* Generated */    if( (retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) ) || ((int)VALUE_HANDLE_DEREF(outNBElement)) == 0 )
692 /* Generated */    {
693 /* Generated */       #if defined(USE_SINGLE_PRECISION_INPUT)
694 /* Generated */          ARRAY_FREE( tempBuffer );
695 /* Generated */       #else
696 /* Generated */          ARRAY_FREE_COND( bufferIsAllocated, tempBuffer );
697 /* Generated */       #endif
698 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
699 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
700 /* Generated */       return retCode;
701 /* Generated */    }
702 /* Generated */    ARRAY_MEMMOVE( outFastK, 0, tempBuffer, lookbackFastD, (int)VALUE_HANDLE_DEREF(outNBElement) );
703 /* Generated */    #if defined(USE_SINGLE_PRECISION_INPUT)
704 /* Generated */       ARRAY_FREE( tempBuffer );
705 /* Generated */    #else
706 /* Generated */       ARRAY_FREE_COND( bufferIsAllocated, tempBuffer );
707 /* Generated */    #endif
708 /* Generated */    if( retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) )
709 /* Generated */    {
710 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
711 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
712 /* Generated */       return retCode;
713 /* Generated */    }
714 /* Generated */    VALUE_HANDLE_DEREF(outBegIdx) = startIdx;
715 /* Generated */    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
716 /* Generated */ }
717 /* Generated */
718 /* Generated */ #if defined( _MANAGED )
719 /* Generated */ }}} // Close namespace TicTacTec.TA.Lib
720 /* Generated */ #endif
721 /**** END GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
722 
723