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  *  PP       Peter Pudaite
40  *  CF       Christo Fogelberg
41  *
42  * Change history:
43  *
44  *  MMDDYY BY    Description
45  *  -------------------------------------------------------------------
46  *  120802 MF    Template creation.
47  *  091503 PP    Reworked TA_SAR to allow customisation of more SAR params.
48  *  092103 MF    Some changes related on first round of tests
49  *  092303 PP    Minor bug fixes.
50  *  122104 MF,CF Fix#1089506 for out-of-bound access to ep_temp.
51  *
52  */
53 
54 /* SAR_ROUNDING is just for test purpose when cross-referencing that
55  * function with example from Wilder's book. Wilder is using two
56  * decimal rounding for simplification. TA-Lib does not round.
57  */
58 /* #define SAR_ROUNDING(x) x=round_pos_2(x) */
59 #define SAR_ROUNDING(x)
60 
61 
62 /**** START GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
63 /* All code within this section is automatically
64  * generated by gen_code. Any modification will be lost
65  * next time gen_code is run.
66  */
67 /* Generated */
68 /* Generated */ #if defined( _MANAGED )
69 /* Generated */    #include "TA-Lib-Core.h"
70 /* Generated */    #define TA_INTERNAL_ERROR(Id) (RetCode::InternalError)
71 /* Generated */    namespace TicTacTec { namespace TA { namespace Library {
72 /* Generated */ #elif defined( _JAVA )
73 /* Generated */    #include "ta_defs.h"
74 /* Generated */    #include "ta_java_defs.h"
75 /* Generated */    #define TA_INTERNAL_ERROR(Id) (RetCode.InternalError)
76 /* Generated */ #else
77 /* Generated */    #include <string.h>
78 /* Generated */    #include <math.h>
79 /* Generated */    #include "ta_func.h"
80 /* Generated */ #endif
81 /* Generated */
82 /* Generated */ #ifndef TA_UTILITY_H
83 /* Generated */    #include "ta_utility.h"
84 /* Generated */ #endif
85 /* Generated */
86 /* Generated */ #ifndef TA_MEMORY_H
87 /* Generated */    #include "ta_memory.h"
88 /* Generated */ #endif
89 /* Generated */
90 /* Generated */ #define TA_PREFIX(x) TA_##x
91 /* Generated */ #define INPUT_TYPE   double
92 /* Generated */
93 /* Generated */ #if defined( _MANAGED )
SarExtLookback(double optInStartValue,double optInOffsetOnReverse,double optInAccelerationInitLong,double optInAccelerationLong,double optInAccelerationMaxLong,double optInAccelerationInitShort,double optInAccelerationShort,double optInAccelerationMaxShort)94 /* Generated */ int Core::SarExtLookback( double        optInStartValue, /* From TA_REAL_MIN to TA_REAL_MAX */
95 /* Generated */                         double        optInOffsetOnReverse, /* From 0 to TA_REAL_MAX */
96 /* Generated */                         double        optInAccelerationInitLong, /* From 0 to TA_REAL_MAX */
97 /* Generated */                         double        optInAccelerationLong, /* From 0 to TA_REAL_MAX */
98 /* Generated */                         double        optInAccelerationMaxLong, /* From 0 to TA_REAL_MAX */
99 /* Generated */                         double        optInAccelerationInitShort, /* From 0 to TA_REAL_MAX */
100 /* Generated */                         double        optInAccelerationShort, /* From 0 to TA_REAL_MAX */
101 /* Generated */                         double        optInAccelerationMaxShort )  /* From 0 to TA_REAL_MAX */
102 /* Generated */
103 /* Generated */ #elif defined( _JAVA )
104 /* Generated */ public int sarExtLookback( double        optInStartValue, /* From TA_REAL_MIN to TA_REAL_MAX */
105 /* Generated */                          double        optInOffsetOnReverse, /* From 0 to TA_REAL_MAX */
106 /* Generated */                          double        optInAccelerationInitLong, /* From 0 to TA_REAL_MAX */
107 /* Generated */                          double        optInAccelerationLong, /* From 0 to TA_REAL_MAX */
108 /* Generated */                          double        optInAccelerationMaxLong, /* From 0 to TA_REAL_MAX */
109 /* Generated */                          double        optInAccelerationInitShort, /* From 0 to TA_REAL_MAX */
110 /* Generated */                          double        optInAccelerationShort, /* From 0 to TA_REAL_MAX */
111 /* Generated */                          double        optInAccelerationMaxShort )  /* From 0 to TA_REAL_MAX */
112 /* Generated */
113 /* Generated */ #else
114 /* Generated */ int TA_SAREXT_Lookback( double        optInStartValue, /* From TA_REAL_MIN to TA_REAL_MAX */
115 /* Generated */                       double        optInOffsetOnReverse, /* From 0 to TA_REAL_MAX */
116 /* Generated */                       double        optInAccelerationInitLong, /* From 0 to TA_REAL_MAX */
117 /* Generated */                       double        optInAccelerationLong, /* From 0 to TA_REAL_MAX */
118 /* Generated */                       double        optInAccelerationMaxLong, /* From 0 to TA_REAL_MAX */
119 /* Generated */                       double        optInAccelerationInitShort, /* From 0 to TA_REAL_MAX */
120 /* Generated */                       double        optInAccelerationShort, /* From 0 to TA_REAL_MAX */
121 /* Generated */                       double        optInAccelerationMaxShort )  /* From 0 to TA_REAL_MAX */
122 /* Generated */
123 /* Generated */ #endif
124 /**** END GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
125 {
126    /* insert local variable here */
127 
128 /**** START GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
129 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
130 /* Generated */    if( optInStartValue == TA_REAL_DEFAULT )
131 /* Generated */       optInStartValue = 0.000000e+0;
132 /* Generated */    else if( (optInStartValue < -3.000000e+37) ||/* Generated */  (optInStartValue > 3.000000e+37) )
133 /* Generated */       return -1;
134 /* Generated */
135 /* Generated */    if( optInOffsetOnReverse == TA_REAL_DEFAULT )
136 /* Generated */       optInOffsetOnReverse = 0.000000e+0;
137 /* Generated */    else if( (optInOffsetOnReverse < 0.000000e+0) ||/* Generated */  (optInOffsetOnReverse > 3.000000e+37) )
138 /* Generated */       return -1;
139 /* Generated */
140 /* Generated */    if( optInAccelerationInitLong == TA_REAL_DEFAULT )
141 /* Generated */       optInAccelerationInitLong = 2.000000e-2;
142 /* Generated */    else if( (optInAccelerationInitLong < 0.000000e+0) ||/* Generated */  (optInAccelerationInitLong > 3.000000e+37) )
143 /* Generated */       return -1;
144 /* Generated */
145 /* Generated */    if( optInAccelerationLong == TA_REAL_DEFAULT )
146 /* Generated */       optInAccelerationLong = 2.000000e-2;
147 /* Generated */    else if( (optInAccelerationLong < 0.000000e+0) ||/* Generated */  (optInAccelerationLong > 3.000000e+37) )
148 /* Generated */       return -1;
149 /* Generated */
150 /* Generated */    if( optInAccelerationMaxLong == TA_REAL_DEFAULT )
151 /* Generated */       optInAccelerationMaxLong = 2.000000e-1;
152 /* Generated */    else if( (optInAccelerationMaxLong < 0.000000e+0) ||/* Generated */  (optInAccelerationMaxLong > 3.000000e+37) )
153 /* Generated */       return -1;
154 /* Generated */
155 /* Generated */    if( optInAccelerationInitShort == TA_REAL_DEFAULT )
156 /* Generated */       optInAccelerationInitShort = 2.000000e-2;
157 /* Generated */    else if( (optInAccelerationInitShort < 0.000000e+0) ||/* Generated */  (optInAccelerationInitShort > 3.000000e+37) )
158 /* Generated */       return -1;
159 /* Generated */
160 /* Generated */    if( optInAccelerationShort == TA_REAL_DEFAULT )
161 /* Generated */       optInAccelerationShort = 2.000000e-2;
162 /* Generated */    else if( (optInAccelerationShort < 0.000000e+0) ||/* Generated */  (optInAccelerationShort > 3.000000e+37) )
163 /* Generated */       return -1;
164 /* Generated */
165 /* Generated */    if( optInAccelerationMaxShort == TA_REAL_DEFAULT )
166 /* Generated */       optInAccelerationMaxShort = 2.000000e-1;
167 /* Generated */    else if( (optInAccelerationMaxShort < 0.000000e+0) ||/* Generated */  (optInAccelerationMaxShort > 3.000000e+37) )
168 /* Generated */       return -1;
169 /* Generated */
170 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
171 /**** END GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
172 
173    /* insert lookback code here. */
174    UNUSED_VARIABLE(optInStartValue);
175    UNUSED_VARIABLE(optInOffsetOnReverse);
176    UNUSED_VARIABLE(optInAccelerationInitLong);
177    UNUSED_VARIABLE(optInAccelerationLong);
178    UNUSED_VARIABLE(optInAccelerationMaxLong);
179    UNUSED_VARIABLE(optInAccelerationInitShort);
180    UNUSED_VARIABLE(optInAccelerationShort);
181    UNUSED_VARIABLE(optInAccelerationMaxShort);
182 
183    /* SAR always sacrifices one price bar to establish the
184     * initial extreme price.
185     */
186 
187    return 1;
188 }
189 
190 /**** START GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
191 /*
192  * TA_SAREXT - Parabolic SAR - Extended
193  *
194  * Input  = High, Low
195  * Output = double
196  *
197  * Optional Parameters
198  * -------------------
199  * optInStartValue:(From TA_REAL_MIN to TA_REAL_MAX)
200  *    Start value and direction. 0 for Auto, >0 for Long, <0 for Short
201  *
202  * optInOffsetOnReverse:(From 0 to TA_REAL_MAX)
203  *    Percent offset added/removed to initial stop on short/long reversal
204  *
205  * optInAccelerationInitLong:(From 0 to TA_REAL_MAX)
206  *    Acceleration Factor initial value for the Long direction
207  *
208  * optInAccelerationLong:(From 0 to TA_REAL_MAX)
209  *    Acceleration Factor for the Long direction
210  *
211  * optInAccelerationMaxLong:(From 0 to TA_REAL_MAX)
212  *    Acceleration Factor maximum value for the Long direction
213  *
214  * optInAccelerationInitShort:(From 0 to TA_REAL_MAX)
215  *    Acceleration Factor initial value for the Short direction
216  *
217  * optInAccelerationShort:(From 0 to TA_REAL_MAX)
218  *    Acceleration Factor for the Short direction
219  *
220  * optInAccelerationMaxShort:(From 0 to TA_REAL_MAX)
221  *    Acceleration Factor maximum value for the Short direction
222  *
223  *
224  */
225 /* Generated */
226 /* Generated */ #if defined( _MANAGED ) && defined( USE_SUBARRAY )
227 /* Generated */ enum class Core::RetCode Core::SarExt( int    startIdx,
228 /* Generated */                                        int    endIdx,
229 /* Generated */                                        SubArray^    inHigh,
230 /* Generated */                                        SubArray^    inLow,
231 /* Generated */                                        double        optInStartValue, /* From TA_REAL_MIN to TA_REAL_MAX */
232 /* Generated */                                        double        optInOffsetOnReverse, /* From 0 to TA_REAL_MAX */
233 /* Generated */                                        double        optInAccelerationInitLong, /* From 0 to TA_REAL_MAX */
234 /* Generated */                                        double        optInAccelerationLong, /* From 0 to TA_REAL_MAX */
235 /* Generated */                                        double        optInAccelerationMaxLong, /* From 0 to TA_REAL_MAX */
236 /* Generated */                                        double        optInAccelerationInitShort, /* From 0 to TA_REAL_MAX */
237 /* Generated */                                        double        optInAccelerationShort, /* From 0 to TA_REAL_MAX */
238 /* Generated */                                        double        optInAccelerationMaxShort, /* From 0 to TA_REAL_MAX */
239 /* Generated */                                        [Out]int%    outBegIdx,
240 /* Generated */                                        [Out]int%    outNBElement,
241 /* Generated */                                        cli::array<double>^  outReal )
242 /* Generated */ #elif defined( _MANAGED )
243 /* Generated */ enum class Core::RetCode Core::SarExt( int    startIdx,
244 /* Generated */                                        int    endIdx,
245 /* Generated */                                        cli::array<double>^ inHigh,
246 /* Generated */                                        cli::array<double>^ inLow,
247 /* Generated */                                        double        optInStartValue, /* From TA_REAL_MIN to TA_REAL_MAX */
248 /* Generated */                                        double        optInOffsetOnReverse, /* From 0 to TA_REAL_MAX */
249 /* Generated */                                        double        optInAccelerationInitLong, /* From 0 to TA_REAL_MAX */
250 /* Generated */                                        double        optInAccelerationLong, /* From 0 to TA_REAL_MAX */
251 /* Generated */                                        double        optInAccelerationMaxLong, /* From 0 to TA_REAL_MAX */
252 /* Generated */                                        double        optInAccelerationInitShort, /* From 0 to TA_REAL_MAX */
253 /* Generated */                                        double        optInAccelerationShort, /* From 0 to TA_REAL_MAX */
254 /* Generated */                                        double        optInAccelerationMaxShort, /* From 0 to TA_REAL_MAX */
255 /* Generated */                                        [Out]int%    outBegIdx,
256 /* Generated */                                        [Out]int%    outNBElement,
257 /* Generated */                                        cli::array<double>^  outReal )
258 /* Generated */ #elif defined( _JAVA )
259 /* Generated */ public RetCode sarExt( int    startIdx,
260 /* Generated */                        int    endIdx,
261 /* Generated */                        double       inHigh[],
262 /* Generated */                        double       inLow[],
263 /* Generated */                        double        optInStartValue, /* From TA_REAL_MIN to TA_REAL_MAX */
264 /* Generated */                        double        optInOffsetOnReverse, /* From 0 to TA_REAL_MAX */
265 /* Generated */                        double        optInAccelerationInitLong, /* From 0 to TA_REAL_MAX */
266 /* Generated */                        double        optInAccelerationLong, /* From 0 to TA_REAL_MAX */
267 /* Generated */                        double        optInAccelerationMaxLong, /* From 0 to TA_REAL_MAX */
268 /* Generated */                        double        optInAccelerationInitShort, /* From 0 to TA_REAL_MAX */
269 /* Generated */                        double        optInAccelerationShort, /* From 0 to TA_REAL_MAX */
270 /* Generated */                        double        optInAccelerationMaxShort, /* From 0 to TA_REAL_MAX */
271 /* Generated */                        MInteger     outBegIdx,
272 /* Generated */                        MInteger     outNBElement,
273 /* Generated */                        double        outReal[] )
274 /* Generated */ #else
275 /* Generated */ TA_RetCode TA_SAREXT( int    startIdx,
276 /* Generated */                       int    endIdx,
277 /* Generated */                       const double inHigh[],
278 /* Generated */                       const double inLow[],
279 /* Generated */                       double        optInStartValue, /* From TA_REAL_MIN to TA_REAL_MAX */
280 /* Generated */                       double        optInOffsetOnReverse, /* From 0 to TA_REAL_MAX */
281 /* Generated */                       double        optInAccelerationInitLong, /* From 0 to TA_REAL_MAX */
282 /* Generated */                       double        optInAccelerationLong, /* From 0 to TA_REAL_MAX */
283 /* Generated */                       double        optInAccelerationMaxLong, /* From 0 to TA_REAL_MAX */
284 /* Generated */                       double        optInAccelerationInitShort, /* From 0 to TA_REAL_MAX */
285 /* Generated */                       double        optInAccelerationShort, /* From 0 to TA_REAL_MAX */
286 /* Generated */                       double        optInAccelerationMaxShort, /* From 0 to TA_REAL_MAX */
287 /* Generated */                       int          *outBegIdx,
288 /* Generated */                       int          *outNBElement,
289 /* Generated */                       double        outReal[] )
290 /* Generated */ #endif
291 /**** END GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
292 {
293 	/* insert local variable here */
294    ENUM_DECLARATION(RetCode) retCode;
295 
296    int isLong; /* > 0 indicates long. == 0 indicates short */
297    int todayIdx, outIdx;
298 
299    VALUE_HANDLE_INT(tempInt);
300 
301    double newHigh, newLow, prevHigh, prevLow;
302    double afLong, afShort, ep, sar;
303    ARRAY_LOCAL(ep_temp,1);
304 
305 /**** START GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
306 /* Generated */
307 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
308 /* Generated */
309 /* Generated */    /* Validate the requested output range. */
310 /* Generated */    if( startIdx < 0 )
311 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
312 /* Generated */    if( (endIdx < 0) || (endIdx < startIdx))
313 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
314 /* Generated */
315 /* Generated */    #if !defined(_JAVA)
316 /* Generated */    /* Verify required price component. */
317 /* Generated */    if(!inHigh||!inLow)
318 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
319 /* Generated */
320 /* Generated */    #endif /* !defined(_JAVA)*/
321 /* Generated */    if( optInStartValue == TA_REAL_DEFAULT )
322 /* Generated */       optInStartValue = 0.000000e+0;
323 /* Generated */    else if( (optInStartValue < -3.000000e+37) ||/* Generated */  (optInStartValue > 3.000000e+37) )
324 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
325 /* Generated */
326 /* Generated */    if( optInOffsetOnReverse == TA_REAL_DEFAULT )
327 /* Generated */       optInOffsetOnReverse = 0.000000e+0;
328 /* Generated */    else if( (optInOffsetOnReverse < 0.000000e+0) ||/* Generated */  (optInOffsetOnReverse > 3.000000e+37) )
329 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
330 /* Generated */
331 /* Generated */    if( optInAccelerationInitLong == TA_REAL_DEFAULT )
332 /* Generated */       optInAccelerationInitLong = 2.000000e-2;
333 /* Generated */    else if( (optInAccelerationInitLong < 0.000000e+0) ||/* Generated */  (optInAccelerationInitLong > 3.000000e+37) )
334 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
335 /* Generated */
336 /* Generated */    if( optInAccelerationLong == TA_REAL_DEFAULT )
337 /* Generated */       optInAccelerationLong = 2.000000e-2;
338 /* Generated */    else if( (optInAccelerationLong < 0.000000e+0) ||/* Generated */  (optInAccelerationLong > 3.000000e+37) )
339 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
340 /* Generated */
341 /* Generated */    if( optInAccelerationMaxLong == TA_REAL_DEFAULT )
342 /* Generated */       optInAccelerationMaxLong = 2.000000e-1;
343 /* Generated */    else if( (optInAccelerationMaxLong < 0.000000e+0) ||/* Generated */  (optInAccelerationMaxLong > 3.000000e+37) )
344 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
345 /* Generated */
346 /* Generated */    if( optInAccelerationInitShort == TA_REAL_DEFAULT )
347 /* Generated */       optInAccelerationInitShort = 2.000000e-2;
348 /* Generated */    else if( (optInAccelerationInitShort < 0.000000e+0) ||/* Generated */  (optInAccelerationInitShort > 3.000000e+37) )
349 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
350 /* Generated */
351 /* Generated */    if( optInAccelerationShort == TA_REAL_DEFAULT )
352 /* Generated */       optInAccelerationShort = 2.000000e-2;
353 /* Generated */    else if( (optInAccelerationShort < 0.000000e+0) ||/* Generated */  (optInAccelerationShort > 3.000000e+37) )
354 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
355 /* Generated */
356 /* Generated */    if( optInAccelerationMaxShort == TA_REAL_DEFAULT )
357 /* Generated */       optInAccelerationMaxShort = 2.000000e-1;
358 /* Generated */    else if( (optInAccelerationMaxShort < 0.000000e+0) ||/* Generated */  (optInAccelerationMaxShort > 3.000000e+37) )
359 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
360 /* Generated */
361 /* Generated */    #if !defined(_JAVA)
362 /* Generated */    if( !outReal )
363 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
364 /* Generated */
365 /* Generated */    #endif /* !defined(_JAVA) */
366 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
367 /* Generated */
368 /**** END GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
369 
370    /* Insert TA function code here. */
371 
372    /* This function is the same as TA_SAR, except that the caller has
373     * greater control on the SAR dynamic and initial state.
374     *
375     * In additon, the TA_SAREXT returns negative values when the position
376     * is short. This allow to distinguish when the SAR do actually reverse.
377     */
378 
379    /* Implementation of the SAR has been a little bit open to interpretation
380     * since Wilder (the original author) did not define a precise algorithm
381     * on how to bootstrap the algorithm. Take any existing software application
382     * and you will see slight variation on how the algorithm was adapted.
383     *
384     * What is the initial trade direction? Long or short?
385     * ===================================================
386     * The interpretation of what should be the initial SAR values is
387     * open to interpretation, particularly since the caller to the function
388     * does not specify the initial direction of the trade.
389     *
390     * In TA-Lib, the following default logic is used:
391     *  - Calculate +DM and -DM between the first and
392     *    second bar. The highest directional indication will
393     *    indicate the assumed direction of the trade for the second
394     *    price bar.
395     *  - In the case of a tie between +DM and -DM,
396     *    the direction is LONG by default.
397     *
398     * What is the initial "extreme point" and thus SAR?
399     * =================================================
400     * The following shows how different people took different approach:
401     *  - Metastock use the first price bar high/low depending of
402     *    the direction. No SAR is calculated for the first price
403     *    bar.
404     *  - Tradestation use the closing price of the second bar. No
405     *    SAR are calculated for the first price bar.
406     *  - Wilder (the original author) use the SIP from the
407     *    previous trade (cannot be implement here since the
408     *    direction and length of the previous trade is unknonw).
409     *  - The Magazine TASC seems to follow Wilder approach which
410     *    is not practical here.
411     *
412     * TA-Lib "consume" the first price bar and use its high/low as the
413     * initial SAR of the second price bar. I found that approach to be
414     * the closest to Wilders idea of having the first entry day use
415     * the previous extreme point, except that here the extreme point is
416     * derived solely from the first price bar. I found the same approach
417     * to be used by Metastock.
418     *
419     *
420     * Can I force the initial SAR?
421     * ============================
422     * Yes. Using the optInStartValue_0 parameter:
423     *  optInStartValue_0 >  0 : SAR is long at optInStartValue_0.
424     *  optInStartValue_0 <  0 : SAR is short at fabs(optInStartValue_0).
425     *
426     * And when optInStartValue_0 == 0, the logic is the same as for TA_SAR
427     * (See previous two sections).
428     */
429 
430    /* Identify the minimum number of price bar needed
431     * to calculate at least one output.
432     *
433     * Move up the start index if there is not
434     * enough initial data.
435     */
436    if( startIdx < 1 )
437       startIdx = 1;
438 
439    /* Make sure there is still something to evaluate. */
440    if( startIdx > endIdx )
441    {
442       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
443       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
444       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
445    }
446 
447 
448    /* Check if the acceleration factors are being defined by the user.
449     * Make sure the acceleration and maximum are coherent.
450     * If not, correct the acceleration.
451     * Default afLong = 0.02
452     * Default afShort = 0.02
453     */
454 
455    afLong = optInAccelerationInitLong;
456    afShort = optInAccelerationInitShort;
457 
458    if( afLong > optInAccelerationMaxLong )
459       afLong = optInAccelerationInitLong = optInAccelerationMaxLong;
460 
461    if( optInAccelerationLong > optInAccelerationMaxLong )
462       optInAccelerationLong = optInAccelerationMaxLong;
463 
464    if( afShort > optInAccelerationMaxShort)
465       afShort = optInAccelerationInitShort = optInAccelerationMaxShort;
466 
467    if( optInAccelerationShort > optInAccelerationMaxShort )
468       optInAccelerationShort = optInAccelerationMaxShort;
469 
470    /* Initialise SAR calculations */
471 
472    if(optInStartValue == 0) /* Default action */
473    {
474       /* Identify if the initial direction is long or short.
475        * (ep is just used as a temp buffer here, the name
476        *  of the parameter is not significant).
477        */
478       retCode = FUNCTION_CALL(MINUS_DM)( startIdx, startIdx, inHigh, inLow, 1,
479                                          VALUE_HANDLE_OUT(tempInt), VALUE_HANDLE_OUT(tempInt),
480 									     ep_temp );
481       if( ep_temp[0] > 0 )
482          isLong = 0;
483       else
484          isLong = 1;
485 
486       if( retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) )
487       {
488          VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
489          VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
490          return retCode;
491       }
492    }
493    else if( optInStartValue > 0 ) /* Start Long */
494    {
495       isLong = 1;
496    }
497    else /* optInStartValue_0 < 0 => Start Short */
498    {
499       isLong = 0;
500    }
501 
502    VALUE_HANDLE_DEREF(outBegIdx) = startIdx;
503    outIdx = 0;
504 
505 
506    /* Write the first SAR. */
507    todayIdx = startIdx;
508 
509    newHigh = inHigh[todayIdx-1];
510    newLow  = inLow[todayIdx-1];
511 
512    SAR_ROUNDING(newHigh);
513    SAR_ROUNDING(newLow);
514 
515    if(optInStartValue == 0) /* Default action */
516    {
517       if( isLong == 1 )
518       {
519          ep  = inHigh[todayIdx];
520          sar = newLow;
521       }
522       else
523       {
524          ep  = inLow[todayIdx];
525          sar = newHigh;
526       }
527    }
528    else if ( optInStartValue > 0 ) /* Start Long at specified value. */
529    {
530       ep  = inHigh[todayIdx];
531       sar = optInStartValue;
532    }
533    else /* if optInStartValue < 0 => Start Short at specified value. */
534    {
535       ep  = inLow[todayIdx];
536       sar = std_fabs(optInStartValue);
537    }
538 
539    SAR_ROUNDING(sar);
540 
541    /* Cheat on the newLow and newHigh for the
542     * first iteration.
543     */
544    newLow  = inLow[todayIdx];
545    newHigh = inHigh[todayIdx];
546 
547    while( todayIdx <= endIdx )
548    {
549       prevLow  = newLow;
550       prevHigh = newHigh;
551       newLow  = inLow[todayIdx];
552       newHigh = inHigh[todayIdx];
553       todayIdx++;
554 
555       SAR_ROUNDING(newLow);
556       SAR_ROUNDING(newHigh);
557 
558       if( isLong == 1 )
559       {
560          /* Switch to short if the low penetrates the SAR value. */
561          if( newLow <= sar )
562          {
563             /* Switch and Overide the SAR with the ep */
564             isLong = 0;
565             sar = ep;
566 
567             /* Make sure the overide SAR is within
568              * yesterday's and today's range.
569              */
570             if( sar < prevHigh )
571                sar = prevHigh;
572             if( sar < newHigh )
573                sar = newHigh;
574 
575             /* Output the overide SAR  */
576             if( optInOffsetOnReverse != 0.0 )
577                sar += sar * optInOffsetOnReverse;
578             outReal[outIdx++] = -sar;
579 
580             /* Adjust afShort and ep */
581             afShort = optInAccelerationInitShort;
582             ep = newLow;
583 
584             /* Calculate the new SAR */
585             sar = sar + afShort * (ep - sar);
586             SAR_ROUNDING( sar );
587 
588             /* Make sure the new SAR is within
589              * yesterday's and today's range.
590              */
591             if( sar < prevHigh )
592                sar = prevHigh;
593             if( sar < newHigh )
594                sar = newHigh;
595          }
596          else
597          {
598             /* No switch */
599 
600             /* Output the SAR (was calculated in the previous iteration) */
601             outReal[outIdx++] = sar;
602 
603             /* Adjust afLong and ep. */
604             if( newHigh > ep )
605             {
606                ep = newHigh;
607                afLong += optInAccelerationLong;
608                if( afLong > optInAccelerationMaxLong )
609                   afLong = optInAccelerationMaxLong;
610             }
611 
612             /* Calculate the new SAR */
613             sar = sar + afLong * (ep - sar);
614             SAR_ROUNDING( sar );
615 
616             /* Make sure the new SAR is within
617              * yesterday's and today's range.
618              */
619             if( sar > prevLow )
620                sar = prevLow;
621             if( sar > newLow )
622                sar = newLow;
623          }
624       }
625       else
626       {
627          /* Switch to long if the high penetrates the SAR value. */
628          if( newHigh >= sar )
629          {
630             /* Switch and Overide the SAR with the ep */
631             isLong = 1;
632             sar = ep;
633 
634             /* Make sure the overide SAR is within
635              * yesterday's and today's range.
636              */
637             if( sar > prevLow )
638                sar = prevLow;
639             if( sar > newLow )
640                sar = newLow;
641 
642             /* Output the overide SAR  */
643             if( optInOffsetOnReverse != 0.0 )
644                sar -= sar * optInOffsetOnReverse;
645             outReal[outIdx++] = sar;
646 
647             /* Adjust afLong and ep */
648             afLong = optInAccelerationInitLong;
649             ep = newHigh;
650 
651             /* Calculate the new SAR */
652             sar = sar + afLong * (ep - sar);
653             SAR_ROUNDING( sar );
654 
655             /* Make sure the new SAR is within
656              * yesterday's and today's range.
657              */
658             if( sar > prevLow )
659                sar = prevLow;
660             if( sar > newLow )
661                sar = newLow;
662          }
663          else
664          {
665             /* No switch */
666 
667             /* Output the SAR (was calculated in the previous iteration) */
668             outReal[outIdx++] = -sar;
669 
670             /* Adjust afShort and ep. */
671             if( newLow < ep )
672             {
673                ep = newLow;
674                afShort += optInAccelerationShort;
675                if( afShort > optInAccelerationMaxShort )
676                   afShort = optInAccelerationMaxShort;
677             }
678 
679             /* Calculate the new SAR */
680             sar = sar + afShort * (ep - sar);
681             SAR_ROUNDING( sar );
682 
683             /* Make sure the new SAR is within
684              * yesterday's and today's range.
685              */
686             if( sar < prevHigh )
687                sar = prevHigh;
688             if( sar < newHigh )
689                sar = newHigh;
690          }
691       }
692    }
693 
694    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
695 
696    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
697 }
698 
699 /**** START GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
700 /* Generated */
701 /* Generated */ #define  USE_SINGLE_PRECISION_INPUT
702 /* Generated */ #if !defined( _MANAGED ) && !defined( _JAVA )
703 /* Generated */    #undef   TA_PREFIX
704 /* Generated */    #define  TA_PREFIX(x) TA_S_##x
705 /* Generated */ #endif
706 /* Generated */ #undef   INPUT_TYPE
707 /* Generated */ #define  INPUT_TYPE float
708 /* Generated */ #if defined( _MANAGED )
709 /* Generated */ enum class Core::RetCode Core::SarExt( int    startIdx,
710 /* Generated */                                        int    endIdx,
711 /* Generated */                                        cli::array<float>^ inHigh,
712 /* Generated */                                        cli::array<float>^ inLow,
713 /* Generated */                                        double        optInStartValue, /* From TA_REAL_MIN to TA_REAL_MAX */
714 /* Generated */                                        double        optInOffsetOnReverse, /* From 0 to TA_REAL_MAX */
715 /* Generated */                                        double        optInAccelerationInitLong, /* From 0 to TA_REAL_MAX */
716 /* Generated */                                        double        optInAccelerationLong, /* From 0 to TA_REAL_MAX */
717 /* Generated */                                        double        optInAccelerationMaxLong, /* From 0 to TA_REAL_MAX */
718 /* Generated */                                        double        optInAccelerationInitShort, /* From 0 to TA_REAL_MAX */
719 /* Generated */                                        double        optInAccelerationShort, /* From 0 to TA_REAL_MAX */
720 /* Generated */                                        double        optInAccelerationMaxShort, /* From 0 to TA_REAL_MAX */
721 /* Generated */                                        [Out]int%    outBegIdx,
722 /* Generated */                                        [Out]int%    outNBElement,
723 /* Generated */                                        cli::array<double>^  outReal )
724 /* Generated */ #elif defined( _JAVA )
725 /* Generated */ public RetCode sarExt( int    startIdx,
726 /* Generated */                        int    endIdx,
727 /* Generated */                        float        inHigh[],
728 /* Generated */                        float        inLow[],
729 /* Generated */                        double        optInStartValue, /* From TA_REAL_MIN to TA_REAL_MAX */
730 /* Generated */                        double        optInOffsetOnReverse, /* From 0 to TA_REAL_MAX */
731 /* Generated */                        double        optInAccelerationInitLong, /* From 0 to TA_REAL_MAX */
732 /* Generated */                        double        optInAccelerationLong, /* From 0 to TA_REAL_MAX */
733 /* Generated */                        double        optInAccelerationMaxLong, /* From 0 to TA_REAL_MAX */
734 /* Generated */                        double        optInAccelerationInitShort, /* From 0 to TA_REAL_MAX */
735 /* Generated */                        double        optInAccelerationShort, /* From 0 to TA_REAL_MAX */
736 /* Generated */                        double        optInAccelerationMaxShort, /* From 0 to TA_REAL_MAX */
737 /* Generated */                        MInteger     outBegIdx,
738 /* Generated */                        MInteger     outNBElement,
739 /* Generated */                        double        outReal[] )
740 /* Generated */ #else
741 /* Generated */ TA_RetCode TA_S_SAREXT( int    startIdx,
742 /* Generated */                         int    endIdx,
743 /* Generated */                         const float  inHigh[],
744 /* Generated */                         const float  inLow[],
745 /* Generated */                         double        optInStartValue, /* From TA_REAL_MIN to TA_REAL_MAX */
746 /* Generated */                         double        optInOffsetOnReverse, /* From 0 to TA_REAL_MAX */
747 /* Generated */                         double        optInAccelerationInitLong, /* From 0 to TA_REAL_MAX */
748 /* Generated */                         double        optInAccelerationLong, /* From 0 to TA_REAL_MAX */
749 /* Generated */                         double        optInAccelerationMaxLong, /* From 0 to TA_REAL_MAX */
750 /* Generated */                         double        optInAccelerationInitShort, /* From 0 to TA_REAL_MAX */
751 /* Generated */                         double        optInAccelerationShort, /* From 0 to TA_REAL_MAX */
752 /* Generated */                         double        optInAccelerationMaxShort, /* From 0 to TA_REAL_MAX */
753 /* Generated */                         int          *outBegIdx,
754 /* Generated */                         int          *outNBElement,
755 /* Generated */                         double        outReal[] )
756 /* Generated */ #endif
757 /* Generated */ {
758 /* Generated */    ENUM_DECLARATION(RetCode) retCode;
759 /* Generated */    int isLong;
760 /* Generated */    int todayIdx, outIdx;
761 /* Generated */    VALUE_HANDLE_INT(tempInt);
762 /* Generated */    double newHigh, newLow, prevHigh, prevLow;
763 /* Generated */    double afLong, afShort, ep, sar;
764 /* Generated */    ARRAY_LOCAL(ep_temp,1);
765 /* Generated */  #ifndef TA_FUNC_NO_RANGE_CHECK
766 /* Generated */     if( startIdx < 0 )
767 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
768 /* Generated */     if( (endIdx < 0) || (endIdx < startIdx))
769 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
770 /* Generated */     #if !defined(_JAVA)
771 /* Generated */     if(!inHigh||!inLow)
772 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
773 /* Generated */     #endif
774 /* Generated */     if( optInStartValue == TA_REAL_DEFAULT )
775 /* Generated */        optInStartValue = 0.000000e+0;
776 /* Generated */     else if( (optInStartValue < -3.000000e+37) ||  (optInStartValue > 3.000000e+37) )
777 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
778 /* Generated */     if( optInOffsetOnReverse == TA_REAL_DEFAULT )
779 /* Generated */        optInOffsetOnReverse = 0.000000e+0;
780 /* Generated */     else if( (optInOffsetOnReverse < 0.000000e+0) ||  (optInOffsetOnReverse > 3.000000e+37) )
781 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
782 /* Generated */     if( optInAccelerationInitLong == TA_REAL_DEFAULT )
783 /* Generated */        optInAccelerationInitLong = 2.000000e-2;
784 /* Generated */     else if( (optInAccelerationInitLong < 0.000000e+0) ||  (optInAccelerationInitLong > 3.000000e+37) )
785 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
786 /* Generated */     if( optInAccelerationLong == TA_REAL_DEFAULT )
787 /* Generated */        optInAccelerationLong = 2.000000e-2;
788 /* Generated */     else if( (optInAccelerationLong < 0.000000e+0) ||  (optInAccelerationLong > 3.000000e+37) )
789 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
790 /* Generated */     if( optInAccelerationMaxLong == TA_REAL_DEFAULT )
791 /* Generated */        optInAccelerationMaxLong = 2.000000e-1;
792 /* Generated */     else if( (optInAccelerationMaxLong < 0.000000e+0) ||  (optInAccelerationMaxLong > 3.000000e+37) )
793 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
794 /* Generated */     if( optInAccelerationInitShort == TA_REAL_DEFAULT )
795 /* Generated */        optInAccelerationInitShort = 2.000000e-2;
796 /* Generated */     else if( (optInAccelerationInitShort < 0.000000e+0) ||  (optInAccelerationInitShort > 3.000000e+37) )
797 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
798 /* Generated */     if( optInAccelerationShort == TA_REAL_DEFAULT )
799 /* Generated */        optInAccelerationShort = 2.000000e-2;
800 /* Generated */     else if( (optInAccelerationShort < 0.000000e+0) ||  (optInAccelerationShort > 3.000000e+37) )
801 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
802 /* Generated */     if( optInAccelerationMaxShort == TA_REAL_DEFAULT )
803 /* Generated */        optInAccelerationMaxShort = 2.000000e-1;
804 /* Generated */     else if( (optInAccelerationMaxShort < 0.000000e+0) ||  (optInAccelerationMaxShort > 3.000000e+37) )
805 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
806 /* Generated */     #if !defined(_JAVA)
807 /* Generated */     if( !outReal )
808 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
809 /* Generated */     #endif
810 /* Generated */  #endif
811 /* Generated */    if( startIdx < 1 )
812 /* Generated */       startIdx = 1;
813 /* Generated */    if( startIdx > endIdx )
814 /* Generated */    {
815 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
816 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
817 /* Generated */       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
818 /* Generated */    }
819 /* Generated */    afLong = optInAccelerationInitLong;
820 /* Generated */    afShort = optInAccelerationInitShort;
821 /* Generated */    if( afLong > optInAccelerationMaxLong )
822 /* Generated */       afLong = optInAccelerationInitLong = optInAccelerationMaxLong;
823 /* Generated */    if( optInAccelerationLong > optInAccelerationMaxLong )
824 /* Generated */       optInAccelerationLong = optInAccelerationMaxLong;
825 /* Generated */    if( afShort > optInAccelerationMaxShort)
826 /* Generated */       afShort = optInAccelerationInitShort = optInAccelerationMaxShort;
827 /* Generated */    if( optInAccelerationShort > optInAccelerationMaxShort )
828 /* Generated */       optInAccelerationShort = optInAccelerationMaxShort;
829 /* Generated */    if(optInStartValue == 0)
830 /* Generated */    {
831 /* Generated */       retCode = FUNCTION_CALL(MINUS_DM)( startIdx, startIdx, inHigh, inLow, 1,
832 /* Generated */                                          VALUE_HANDLE_OUT(tempInt), VALUE_HANDLE_OUT(tempInt),
833 /* Generated */ 									     ep_temp );
834 /* Generated */       if( ep_temp[0] > 0 )
835 /* Generated */          isLong = 0;
836 /* Generated */       else
837 /* Generated */          isLong = 1;
838 /* Generated */       if( retCode != ENUM_VALUE(RetCode,TA_SUCCESS,Success) )
839 /* Generated */       {
840 /* Generated */          VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
841 /* Generated */          VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
842 /* Generated */          return retCode;
843 /* Generated */       }
844 /* Generated */    }
845 /* Generated */    else if( optInStartValue > 0 )
846 /* Generated */    {
847 /* Generated */       isLong = 1;
848 /* Generated */    }
849 /* Generated */    else
850 /* Generated */    {
851 /* Generated */       isLong = 0;
852 /* Generated */    }
853 /* Generated */    VALUE_HANDLE_DEREF(outBegIdx) = startIdx;
854 /* Generated */    outIdx = 0;
855 /* Generated */    todayIdx = startIdx;
856 /* Generated */    newHigh = inHigh[todayIdx-1];
857 /* Generated */    newLow  = inLow[todayIdx-1];
858 /* Generated */    SAR_ROUNDING(newHigh);
859 /* Generated */    SAR_ROUNDING(newLow);
860 /* Generated */    if(optInStartValue == 0)
861 /* Generated */    {
862 /* Generated */       if( isLong == 1 )
863 /* Generated */       {
864 /* Generated */          ep  = inHigh[todayIdx];
865 /* Generated */          sar = newLow;
866 /* Generated */       }
867 /* Generated */       else
868 /* Generated */       {
869 /* Generated */          ep  = inLow[todayIdx];
870 /* Generated */          sar = newHigh;
871 /* Generated */       }
872 /* Generated */    }
873 /* Generated */    else if ( optInStartValue > 0 )
874 /* Generated */    {
875 /* Generated */       ep  = inHigh[todayIdx];
876 /* Generated */       sar = optInStartValue;
877 /* Generated */    }
878 /* Generated */    else
879 /* Generated */    {
880 /* Generated */       ep  = inLow[todayIdx];
881 /* Generated */       sar = std_fabs(optInStartValue);
882 /* Generated */    }
883 /* Generated */    SAR_ROUNDING(sar);
884 /* Generated */    newLow  = inLow[todayIdx];
885 /* Generated */    newHigh = inHigh[todayIdx];
886 /* Generated */    while( todayIdx <= endIdx )
887 /* Generated */    {
888 /* Generated */       prevLow  = newLow;
889 /* Generated */       prevHigh = newHigh;
890 /* Generated */       newLow  = inLow[todayIdx];
891 /* Generated */       newHigh = inHigh[todayIdx];
892 /* Generated */       todayIdx++;
893 /* Generated */       SAR_ROUNDING(newLow);
894 /* Generated */       SAR_ROUNDING(newHigh);
895 /* Generated */       if( isLong == 1 )
896 /* Generated */       {
897 /* Generated */          if( newLow <= sar )
898 /* Generated */          {
899 /* Generated */             isLong = 0;
900 /* Generated */             sar = ep;
901 /* Generated */             if( sar < prevHigh )
902 /* Generated */                sar = prevHigh;
903 /* Generated */             if( sar < newHigh )
904 /* Generated */                sar = newHigh;
905 /* Generated */             if( optInOffsetOnReverse != 0.0 )
906 /* Generated */                sar += sar * optInOffsetOnReverse;
907 /* Generated */             outReal[outIdx++] = -sar;
908 /* Generated */             afShort = optInAccelerationInitShort;
909 /* Generated */             ep = newLow;
910 /* Generated */             sar = sar + afShort * (ep - sar);
911 /* Generated */             SAR_ROUNDING( sar );
912 /* Generated */             if( sar < prevHigh )
913 /* Generated */                sar = prevHigh;
914 /* Generated */             if( sar < newHigh )
915 /* Generated */                sar = newHigh;
916 /* Generated */          }
917 /* Generated */          else
918 /* Generated */          {
919 /* Generated */             outReal[outIdx++] = sar;
920 /* Generated */             if( newHigh > ep )
921 /* Generated */             {
922 /* Generated */                ep = newHigh;
923 /* Generated */                afLong += optInAccelerationLong;
924 /* Generated */                if( afLong > optInAccelerationMaxLong )
925 /* Generated */                   afLong = optInAccelerationMaxLong;
926 /* Generated */             }
927 /* Generated */             sar = sar + afLong * (ep - sar);
928 /* Generated */             SAR_ROUNDING( sar );
929 /* Generated */             if( sar > prevLow )
930 /* Generated */                sar = prevLow;
931 /* Generated */             if( sar > newLow )
932 /* Generated */                sar = newLow;
933 /* Generated */          }
934 /* Generated */       }
935 /* Generated */       else
936 /* Generated */       {
937 /* Generated */          if( newHigh >= sar )
938 /* Generated */          {
939 /* Generated */             isLong = 1;
940 /* Generated */             sar = ep;
941 /* Generated */             if( sar > prevLow )
942 /* Generated */                sar = prevLow;
943 /* Generated */             if( sar > newLow )
944 /* Generated */                sar = newLow;
945 /* Generated */             if( optInOffsetOnReverse != 0.0 )
946 /* Generated */                sar -= sar * optInOffsetOnReverse;
947 /* Generated */             outReal[outIdx++] = sar;
948 /* Generated */             afLong = optInAccelerationInitLong;
949 /* Generated */             ep = newHigh;
950 /* Generated */             sar = sar + afLong * (ep - sar);
951 /* Generated */             SAR_ROUNDING( sar );
952 /* Generated */             if( sar > prevLow )
953 /* Generated */                sar = prevLow;
954 /* Generated */             if( sar > newLow )
955 /* Generated */                sar = newLow;
956 /* Generated */          }
957 /* Generated */          else
958 /* Generated */          {
959 /* Generated */             outReal[outIdx++] = -sar;
960 /* Generated */             if( newLow < ep )
961 /* Generated */             {
962 /* Generated */                ep = newLow;
963 /* Generated */                afShort += optInAccelerationShort;
964 /* Generated */                if( afShort > optInAccelerationMaxShort )
965 /* Generated */                   afShort = optInAccelerationMaxShort;
966 /* Generated */             }
967 /* Generated */             sar = sar + afShort * (ep - sar);
968 /* Generated */             SAR_ROUNDING( sar );
969 /* Generated */             if( sar < prevHigh )
970 /* Generated */                sar = prevHigh;
971 /* Generated */             if( sar < newHigh )
972 /* Generated */                sar = newHigh;
973 /* Generated */          }
974 /* Generated */       }
975 /* Generated */    }
976 /* Generated */    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
977 /* Generated */    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
978 /* Generated */ }
979 /* Generated */
980 /* Generated */ #if defined( _MANAGED )
981 /* Generated */ }}} // Close namespace TicTacTec.TA.Lib
982 /* Generated */ #endif
983 /**** END GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
984 
985 
986