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  *  AC       Angelo Ciceri
39  *  CSB      Christopher Barnhouse
40  *
41  * Change history:
42  *
43  *  MMDDYY BY      Description
44  *  -------------------------------------------------------------------
45  *  100204 AC      Creation
46  *  051005 CSB,AC  Fix #1199526 for out-of-bound write in output.
47  */
48 
49 /**** START GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
50 /* All code within this section is automatically
51  * generated by gen_code. Any modification will be lost
52  * next time gen_code is run.
53  */
54 /* Generated */
55 /* Generated */ #if defined( _MANAGED )
56 /* Generated */    #include "TA-Lib-Core.h"
57 /* Generated */    #define TA_INTERNAL_ERROR(Id) (RetCode::InternalError)
58 /* Generated */    namespace TicTacTec { namespace TA { namespace Library {
59 /* Generated */ #elif defined( _JAVA )
60 /* Generated */    #include "ta_defs.h"
61 /* Generated */    #include "ta_java_defs.h"
62 /* Generated */    #define TA_INTERNAL_ERROR(Id) (RetCode.InternalError)
63 /* Generated */ #else
64 /* Generated */    #include <string.h>
65 /* Generated */    #include <math.h>
66 /* Generated */    #include "ta_func.h"
67 /* Generated */ #endif
68 /* Generated */
69 /* Generated */ #ifndef TA_UTILITY_H
70 /* Generated */    #include "ta_utility.h"
71 /* Generated */ #endif
72 /* Generated */
73 /* Generated */ #ifndef TA_MEMORY_H
74 /* Generated */    #include "ta_memory.h"
75 /* Generated */ #endif
76 /* Generated */
77 /* Generated */ #define TA_PREFIX(x) TA_##x
78 /* Generated */ #define INPUT_TYPE   double
79 /* Generated */
80 /* Generated */ #if defined( _MANAGED )
CdlTristarLookback(void)81 /* Generated */ int Core::CdlTristarLookback( void )
82 /* Generated */
83 /* Generated */ #elif defined( _JAVA )
84 /* Generated */ public int cdlTristarLookback(  )
85 /* Generated */
86 /* Generated */ #else
87 /* Generated */ int TA_CDLTRISTAR_Lookback( void )
88 /* Generated */
89 /* Generated */ #endif
90 /**** END GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
91 {
92    /* insert local variable here */
93 
94 /**** START GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
95 /* Generated */ /* No parameters to validate. */
96 /**** END GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
97 
98    /* insert lookback code here. */
99     return TA_CANDLEAVGPERIOD(BodyDoji) + 2;
100 }
101 
102 /**** START GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
103 /*
104  * TA_CDLTRISTAR - Tristar Pattern
105  *
106  * Input  = Open, High, Low, Close
107  * Output = int
108  *
109  */
110 /* Generated */
111 /* Generated */ #if defined( _MANAGED ) && defined( USE_SUBARRAY )
112 /* Generated */ enum class Core::RetCode Core::CdlTristar( int    startIdx,
113 /* Generated */                                            int    endIdx,
114 /* Generated */                                            SubArray^    inOpen,
115 /* Generated */                                            SubArray^    inHigh,
116 /* Generated */                                            SubArray^    inLow,
117 /* Generated */                                            SubArray^    inClose,
118 /* Generated */                                            [Out]int%    outBegIdx,
119 /* Generated */                                            [Out]int%    outNBElement,
120 /* Generated */                                            cli::array<int>^  outInteger )
121 /* Generated */ #elif defined( _MANAGED )
122 /* Generated */ enum class Core::RetCode Core::CdlTristar( int    startIdx,
123 /* Generated */                                            int    endIdx,
124 /* Generated */                                            cli::array<double>^ inOpen,
125 /* Generated */                                            cli::array<double>^ inHigh,
126 /* Generated */                                            cli::array<double>^ inLow,
127 /* Generated */                                            cli::array<double>^ inClose,
128 /* Generated */                                            [Out]int%    outBegIdx,
129 /* Generated */                                            [Out]int%    outNBElement,
130 /* Generated */                                            cli::array<int>^  outInteger )
131 /* Generated */ #elif defined( _JAVA )
132 /* Generated */ public RetCode cdlTristar( int    startIdx,
133 /* Generated */                            int    endIdx,
134 /* Generated */                            double       inOpen[],
135 /* Generated */                            double       inHigh[],
136 /* Generated */                            double       inLow[],
137 /* Generated */                            double       inClose[],
138 /* Generated */                            MInteger     outBegIdx,
139 /* Generated */                            MInteger     outNBElement,
140 /* Generated */                            int           outInteger[] )
141 /* Generated */ #else
142 /* Generated */ TA_RetCode TA_CDLTRISTAR( int    startIdx,
143 /* Generated */                           int    endIdx,
144 /* Generated */                           const double inOpen[],
145 /* Generated */                           const double inHigh[],
146 /* Generated */                           const double inLow[],
147 /* Generated */                           const double inClose[],
148 /* Generated */                           int          *outBegIdx,
149 /* Generated */                           int          *outNBElement,
150 /* Generated */                           int           outInteger[] )
151 /* Generated */ #endif
152 /**** END GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
153 {
154    /* Insert local variables here. */
155     double BodyPeriodTotal;
156     int i, outIdx, BodyTrailingIdx, lookbackTotal;
157 
158 /**** START GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
159 /* Generated */
160 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
161 /* Generated */
162 /* Generated */    /* Validate the requested output range. */
163 /* Generated */    if( startIdx < 0 )
164 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
165 /* Generated */    if( (endIdx < 0) || (endIdx < startIdx))
166 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
167 /* Generated */
168 /* Generated */    #if !defined(_JAVA)
169 /* Generated */    /* Verify required price component. */
170 /* Generated */    if(!inOpen||!inHigh||!inLow||!inClose)
171 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
172 /* Generated */
173 /* Generated */    #endif /* !defined(_JAVA)*/
174 /* Generated */    #if !defined(_JAVA)
175 /* Generated */    if( !outInteger )
176 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
177 /* Generated */
178 /* Generated */    #endif /* !defined(_JAVA) */
179 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
180 /* Generated */
181 /**** END GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
182 
183    /* Identify the minimum number of price bar needed
184     * to calculate at least one output.
185     */
186 
187    lookbackTotal = LOOKBACK_CALL(CDLTRISTAR)();
188 
189    /* Move up the start index if there is not
190     * enough initial data.
191     */
192    if( startIdx < lookbackTotal )
193       startIdx = lookbackTotal;
194 
195    /* Make sure there is still something to evaluate. */
196    if( startIdx > endIdx )
197    {
198       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
199       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
200       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
201    }
202 
203    /* Do the calculation using tight loops. */
204    /* Add-up the initial period, except for the last value. */
205    BodyPeriodTotal = 0;
206    BodyTrailingIdx = startIdx -2 - TA_CANDLEAVGPERIOD(BodyDoji);
207 
208    i = BodyTrailingIdx;
209    while( i < startIdx-2 ) {
210         BodyPeriodTotal += TA_CANDLERANGE( BodyDoji, i );
211         i++;
212    }
213 
214    /* Proceed with the calculation for the requested range.
215     * Must have:
216     * - 3 consecutive doji days
217     * - the second doji is a star
218     * The meaning of "doji" is specified with TA_SetCandleSettings
219     * outInteger is positive (1 to 100) when bullish or negative (-1 to -100) when bearish
220     */
221    i = startIdx;
222    outIdx = 0;
223    do
224    {
225         if( TA_REALBODY(i-2) <= TA_CANDLEAVERAGE( BodyDoji, BodyPeriodTotal, i-2 ) &&    // 1st: doji
226             TA_REALBODY(i-1) <= TA_CANDLEAVERAGE( BodyDoji, BodyPeriodTotal, i-2 ) &&    // 2nd: doji
227             TA_REALBODY(i) <= TA_CANDLEAVERAGE( BodyDoji, BodyPeriodTotal, i-2 ) ) {     // 3rd: doji
228             outInteger[outIdx] = 0;
229             if ( TA_REALBODYGAPUP(i-1,i-2)                                                  // 2nd gaps up
230                  &&
231                  max(inOpen[i],inClose[i]) < max(inOpen[i-1],inClose[i-1])                  // 3rd is not higher than 2nd
232                )
233                 outInteger[outIdx] = -100;
234             if ( TA_REALBODYGAPDOWN(i-1,i-2)                                                // 2nd gaps down
235                  &&
236                  min(inOpen[i],inClose[i]) > min(inOpen[i-1],inClose[i-1])                  // 3rd is not lower than 2nd
237                )
238                 outInteger[outIdx] = +100;
239             outIdx++;
240         }
241         else
242             outInteger[outIdx++] = 0;
243         /* add the current range and subtract the first range: this is done after the pattern recognition
244          * when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
245          */
246         BodyPeriodTotal += TA_CANDLERANGE( BodyDoji, i-2 ) - TA_CANDLERANGE( BodyDoji, BodyTrailingIdx );
247         i++;
248         BodyTrailingIdx++;
249    } while( i <= endIdx );
250 
251    /* All done. Indicate the output limits and return. */
252    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
253    VALUE_HANDLE_DEREF(outBegIdx)    = startIdx;
254 
255    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
256 }
257 
258 /**** START GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
259 /* Generated */
260 /* Generated */ #define  USE_SINGLE_PRECISION_INPUT
261 /* Generated */ #if !defined( _MANAGED ) && !defined( _JAVA )
262 /* Generated */    #undef   TA_PREFIX
263 /* Generated */    #define  TA_PREFIX(x) TA_S_##x
264 /* Generated */ #endif
265 /* Generated */ #undef   INPUT_TYPE
266 /* Generated */ #define  INPUT_TYPE float
267 /* Generated */ #if defined( _MANAGED )
268 /* Generated */ enum class Core::RetCode Core::CdlTristar( int    startIdx,
269 /* Generated */                                            int    endIdx,
270 /* Generated */                                            cli::array<float>^ inOpen,
271 /* Generated */                                            cli::array<float>^ inHigh,
272 /* Generated */                                            cli::array<float>^ inLow,
273 /* Generated */                                            cli::array<float>^ inClose,
274 /* Generated */                                            [Out]int%    outBegIdx,
275 /* Generated */                                            [Out]int%    outNBElement,
276 /* Generated */                                            cli::array<int>^  outInteger )
277 /* Generated */ #elif defined( _JAVA )
278 /* Generated */ public RetCode cdlTristar( int    startIdx,
279 /* Generated */                            int    endIdx,
280 /* Generated */                            float        inOpen[],
281 /* Generated */                            float        inHigh[],
282 /* Generated */                            float        inLow[],
283 /* Generated */                            float        inClose[],
284 /* Generated */                            MInteger     outBegIdx,
285 /* Generated */                            MInteger     outNBElement,
286 /* Generated */                            int           outInteger[] )
287 /* Generated */ #else
288 /* Generated */ TA_RetCode TA_S_CDLTRISTAR( int    startIdx,
289 /* Generated */                             int    endIdx,
290 /* Generated */                             const float  inOpen[],
291 /* Generated */                             const float  inHigh[],
292 /* Generated */                             const float  inLow[],
293 /* Generated */                             const float  inClose[],
294 /* Generated */                             int          *outBegIdx,
295 /* Generated */                             int          *outNBElement,
296 /* Generated */                             int           outInteger[] )
297 /* Generated */ #endif
298 /* Generated */ {
299 /* Generated */     double BodyPeriodTotal;
300 /* Generated */     int i, outIdx, BodyTrailingIdx, lookbackTotal;
301 /* Generated */  #ifndef TA_FUNC_NO_RANGE_CHECK
302 /* Generated */     if( startIdx < 0 )
303 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
304 /* Generated */     if( (endIdx < 0) || (endIdx < startIdx))
305 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
306 /* Generated */     #if !defined(_JAVA)
307 /* Generated */     if(!inOpen||!inHigh||!inLow||!inClose)
308 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
309 /* Generated */     #endif
310 /* Generated */     #if !defined(_JAVA)
311 /* Generated */     if( !outInteger )
312 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
313 /* Generated */     #endif
314 /* Generated */  #endif
315 /* Generated */    lookbackTotal = LOOKBACK_CALL(CDLTRISTAR)();
316 /* Generated */    if( startIdx < lookbackTotal )
317 /* Generated */       startIdx = lookbackTotal;
318 /* Generated */    if( startIdx > endIdx )
319 /* Generated */    {
320 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
321 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
322 /* Generated */       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
323 /* Generated */    }
324 /* Generated */    BodyPeriodTotal = 0;
325 /* Generated */    BodyTrailingIdx = startIdx -2 - TA_CANDLEAVGPERIOD(BodyDoji);
326 /* Generated */    i = BodyTrailingIdx;
327 /* Generated */    while( i < startIdx-2 ) {
328 /* Generated */         BodyPeriodTotal += TA_CANDLERANGE( BodyDoji, i );
329 /* Generated */         i++;
330 /* Generated */    }
331 /* Generated */    i = startIdx;
332 /* Generated */    outIdx = 0;
333 /* Generated */    do
334 /* Generated */    {
335 /* Generated */         if( TA_REALBODY(i-2) <= TA_CANDLEAVERAGE( BodyDoji, BodyPeriodTotal, i-2 ) &&    // 1st: doji
336 /* Generated */             TA_REALBODY(i-1) <= TA_CANDLEAVERAGE( BodyDoji, BodyPeriodTotal, i-2 ) &&    // 2nd: doji
337 /* Generated */             TA_REALBODY(i) <= TA_CANDLEAVERAGE( BodyDoji, BodyPeriodTotal, i-2 ) ) {     // 3rd: doji
338 /* Generated */             outInteger[outIdx] = 0;
339 /* Generated */             if ( TA_REALBODYGAPUP(i-1,i-2)                                                  // 2nd gaps up
340 /* Generated */                  &&
341 /* Generated */                  max(inOpen[i],inClose[i]) < max(inOpen[i-1],inClose[i-1])                  // 3rd is not higher than 2nd
342 /* Generated */                )
343 /* Generated */                 outInteger[outIdx] = -100;
344 /* Generated */             if ( TA_REALBODYGAPDOWN(i-1,i-2)                                                // 2nd gaps down
345 /* Generated */                  &&
346 /* Generated */                  min(inOpen[i],inClose[i]) > min(inOpen[i-1],inClose[i-1])                  // 3rd is not lower than 2nd
347 /* Generated */                )
348 /* Generated */                 outInteger[outIdx] = +100;
349 /* Generated */             outIdx++;
350 /* Generated */         }
351 /* Generated */         else
352 /* Generated */             outInteger[outIdx++] = 0;
353 /* Generated */         BodyPeriodTotal += TA_CANDLERANGE( BodyDoji, i-2 ) - TA_CANDLERANGE( BodyDoji, BodyTrailingIdx );
354 /* Generated */         i++;
355 /* Generated */         BodyTrailingIdx++;
356 /* Generated */    } while( i <= endIdx );
357 /* Generated */    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
358 /* Generated */    VALUE_HANDLE_DEREF(outBegIdx)    = startIdx;
359 /* Generated */    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
360 /* Generated */ }
361 /* Generated */
362 /* Generated */ #if defined( _MANAGED )
363 /* Generated */ }}} // Close namespace TicTacTec.TA.Lib
364 /* Generated */ #endif
365 /**** END GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
366 
367