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  *
40  *
41  * Change history:
42  *
43  *  MMDDYY BY   Description
44  *  -------------------------------------------------------------------
45  *  102304 AC   Creation
46  *
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 )
CdlHammerLookback(void)81 /* Generated */ int Core::CdlHammerLookback( void )
82 /* Generated */
83 /* Generated */ #elif defined( _JAVA )
84 /* Generated */ public int cdlHammerLookback(  )
85 /* Generated */
86 /* Generated */ #else
87 /* Generated */ int TA_CDLHAMMER_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 max( max( max( TA_CANDLEAVGPERIOD(BodyShort), TA_CANDLEAVGPERIOD(ShadowLong) ),
100                      TA_CANDLEAVGPERIOD(ShadowVeryShort) ),
101                 TA_CANDLEAVGPERIOD(Near)
102             ) + 1;
103 }
104 
105 /**** START GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
106 /*
107  * TA_CDLHAMMER - Hammer
108  *
109  * Input  = Open, High, Low, Close
110  * Output = int
111  *
112  */
113 /* Generated */
114 /* Generated */ #if defined( _MANAGED ) && defined( USE_SUBARRAY )
115 /* Generated */ enum class Core::RetCode Core::CdlHammer( int    startIdx,
116 /* Generated */                                           int    endIdx,
117 /* Generated */                                           SubArray^    inOpen,
118 /* Generated */                                           SubArray^    inHigh,
119 /* Generated */                                           SubArray^    inLow,
120 /* Generated */                                           SubArray^    inClose,
121 /* Generated */                                           [Out]int%    outBegIdx,
122 /* Generated */                                           [Out]int%    outNBElement,
123 /* Generated */                                           cli::array<int>^  outInteger )
124 /* Generated */ #elif defined( _MANAGED )
125 /* Generated */ enum class Core::RetCode Core::CdlHammer( int    startIdx,
126 /* Generated */                                           int    endIdx,
127 /* Generated */                                           cli::array<double>^ inOpen,
128 /* Generated */                                           cli::array<double>^ inHigh,
129 /* Generated */                                           cli::array<double>^ inLow,
130 /* Generated */                                           cli::array<double>^ inClose,
131 /* Generated */                                           [Out]int%    outBegIdx,
132 /* Generated */                                           [Out]int%    outNBElement,
133 /* Generated */                                           cli::array<int>^  outInteger )
134 /* Generated */ #elif defined( _JAVA )
135 /* Generated */ public RetCode cdlHammer( int    startIdx,
136 /* Generated */                           int    endIdx,
137 /* Generated */                           double       inOpen[],
138 /* Generated */                           double       inHigh[],
139 /* Generated */                           double       inLow[],
140 /* Generated */                           double       inClose[],
141 /* Generated */                           MInteger     outBegIdx,
142 /* Generated */                           MInteger     outNBElement,
143 /* Generated */                           int           outInteger[] )
144 /* Generated */ #else
145 /* Generated */ TA_RetCode TA_CDLHAMMER( int    startIdx,
146 /* Generated */                          int    endIdx,
147 /* Generated */                          const double inOpen[],
148 /* Generated */                          const double inHigh[],
149 /* Generated */                          const double inLow[],
150 /* Generated */                          const double inClose[],
151 /* Generated */                          int          *outBegIdx,
152 /* Generated */                          int          *outNBElement,
153 /* Generated */                          int           outInteger[] )
154 /* Generated */ #endif
155 /**** END GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
156 {
157    /* Insert local variables here. */
158     double BodyPeriodTotal, ShadowLongPeriodTotal, ShadowVeryShortPeriodTotal, NearPeriodTotal;
159     int i, outIdx, BodyTrailingIdx, ShadowLongTrailingIdx, ShadowVeryShortTrailingIdx, NearTrailingIdx, lookbackTotal;
160 
161 /**** START GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
162 /* Generated */
163 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
164 /* Generated */
165 /* Generated */    /* Validate the requested output range. */
166 /* Generated */    if( startIdx < 0 )
167 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
168 /* Generated */    if( (endIdx < 0) || (endIdx < startIdx))
169 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
170 /* Generated */
171 /* Generated */    #if !defined(_JAVA)
172 /* Generated */    /* Verify required price component. */
173 /* Generated */    if(!inOpen||!inHigh||!inLow||!inClose)
174 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
175 /* Generated */
176 /* Generated */    #endif /* !defined(_JAVA)*/
177 /* Generated */    #if !defined(_JAVA)
178 /* Generated */    if( !outInteger )
179 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
180 /* Generated */
181 /* Generated */    #endif /* !defined(_JAVA) */
182 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
183 /* Generated */
184 /**** END GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
185 
186    /* Identify the minimum number of price bar needed
187     * to calculate at least one output.
188     */
189 
190    lookbackTotal = LOOKBACK_CALL(CDLHAMMER)();
191 
192    /* Move up the start index if there is not
193     * enough initial data.
194     */
195    if( startIdx < lookbackTotal )
196       startIdx = lookbackTotal;
197 
198    /* Make sure there is still something to evaluate. */
199    if( startIdx > endIdx )
200    {
201       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
202       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
203       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
204    }
205 
206    /* Do the calculation using tight loops. */
207    /* Add-up the initial period, except for the last value. */
208    BodyPeriodTotal = 0;
209    BodyTrailingIdx = startIdx - TA_CANDLEAVGPERIOD(BodyShort);
210    ShadowLongPeriodTotal = 0;
211    ShadowLongTrailingIdx = startIdx - TA_CANDLEAVGPERIOD(ShadowLong);
212    ShadowVeryShortPeriodTotal = 0;
213    ShadowVeryShortTrailingIdx = startIdx - TA_CANDLEAVGPERIOD(ShadowVeryShort);
214    NearPeriodTotal = 0;
215    NearTrailingIdx = startIdx -1 - TA_CANDLEAVGPERIOD(Near);
216 
217    i = BodyTrailingIdx;
218    while( i < startIdx ) {
219         BodyPeriodTotal += TA_CANDLERANGE( BodyShort, i );
220         i++;
221    }
222    i = ShadowLongTrailingIdx;
223    while( i < startIdx ) {
224         ShadowLongPeriodTotal += TA_CANDLERANGE( ShadowLong, i );
225         i++;
226    }
227    i = ShadowVeryShortTrailingIdx;
228    while( i < startIdx ) {
229         ShadowVeryShortPeriodTotal += TA_CANDLERANGE( ShadowVeryShort, i );
230         i++;
231    }
232    i = NearTrailingIdx;
233    while( i < startIdx-1 ) {
234         NearPeriodTotal += TA_CANDLERANGE( Near, i );
235         i++;
236    }
237    i = startIdx;
238 
239    /* Proceed with the calculation for the requested range.
240     * Must have:
241     * - small real body
242     * - long lower shadow
243     * - no, or very short, upper shadow
244     * - body below or near the lows of the previous candle
245     * The meaning of "short", "long" and "near the lows" is specified with TA_SetCandleSettings;
246     * outInteger is positive (1 to 100): hammer is always bullish;
247     * the user should consider that a hammer must appear in a downtrend, while this function does not consider it
248     */
249    outIdx = 0;
250    do
251    {
252         if( TA_REALBODY(i) < TA_CANDLEAVERAGE( BodyShort, BodyPeriodTotal, i ) &&                        // small rb
253             TA_LOWERSHADOW(i) > TA_CANDLEAVERAGE( ShadowLong, ShadowLongPeriodTotal, i ) &&              // long lower shadow
254             TA_UPPERSHADOW(i) < TA_CANDLEAVERAGE( ShadowVeryShort, ShadowVeryShortPeriodTotal, i ) &&    // very short upper shadow
255             min( inClose[i], inOpen[i] ) <= inLow[i-1] + TA_CANDLEAVERAGE( Near, NearPeriodTotal, i-1 )  // rb near the prior candle's lows
256           )
257             outInteger[outIdx++] = 100;
258         else
259             outInteger[outIdx++] = 0;
260         /* add the current range and subtract the first range: this is done after the pattern recognition
261          * when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
262          */
263         BodyPeriodTotal += TA_CANDLERANGE( BodyShort, i )
264             - TA_CANDLERANGE( BodyShort, BodyTrailingIdx );
265         ShadowLongPeriodTotal += TA_CANDLERANGE( ShadowLong, i )
266             - TA_CANDLERANGE( ShadowLong, ShadowLongTrailingIdx );
267         ShadowVeryShortPeriodTotal += TA_CANDLERANGE( ShadowVeryShort, i )
268             - TA_CANDLERANGE( ShadowVeryShort, ShadowVeryShortTrailingIdx );
269         NearPeriodTotal += TA_CANDLERANGE( Near, i-1 )
270             - TA_CANDLERANGE( Near, NearTrailingIdx );
271         i++;
272         BodyTrailingIdx++;
273         ShadowLongTrailingIdx++;
274         ShadowVeryShortTrailingIdx++;
275         NearTrailingIdx++;
276    } while( i <= endIdx );
277 
278    /* All done. Indicate the output limits and return. */
279    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
280    VALUE_HANDLE_DEREF(outBegIdx)    = startIdx;
281 
282    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
283 }
284 
285 /**** START GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
286 /* Generated */
287 /* Generated */ #define  USE_SINGLE_PRECISION_INPUT
288 /* Generated */ #if !defined( _MANAGED ) && !defined( _JAVA )
289 /* Generated */    #undef   TA_PREFIX
290 /* Generated */    #define  TA_PREFIX(x) TA_S_##x
291 /* Generated */ #endif
292 /* Generated */ #undef   INPUT_TYPE
293 /* Generated */ #define  INPUT_TYPE float
294 /* Generated */ #if defined( _MANAGED )
295 /* Generated */ enum class Core::RetCode Core::CdlHammer( int    startIdx,
296 /* Generated */                                           int    endIdx,
297 /* Generated */                                           cli::array<float>^ inOpen,
298 /* Generated */                                           cli::array<float>^ inHigh,
299 /* Generated */                                           cli::array<float>^ inLow,
300 /* Generated */                                           cli::array<float>^ inClose,
301 /* Generated */                                           [Out]int%    outBegIdx,
302 /* Generated */                                           [Out]int%    outNBElement,
303 /* Generated */                                           cli::array<int>^  outInteger )
304 /* Generated */ #elif defined( _JAVA )
305 /* Generated */ public RetCode cdlHammer( int    startIdx,
306 /* Generated */                           int    endIdx,
307 /* Generated */                           float        inOpen[],
308 /* Generated */                           float        inHigh[],
309 /* Generated */                           float        inLow[],
310 /* Generated */                           float        inClose[],
311 /* Generated */                           MInteger     outBegIdx,
312 /* Generated */                           MInteger     outNBElement,
313 /* Generated */                           int           outInteger[] )
314 /* Generated */ #else
315 /* Generated */ TA_RetCode TA_S_CDLHAMMER( int    startIdx,
316 /* Generated */                            int    endIdx,
317 /* Generated */                            const float  inOpen[],
318 /* Generated */                            const float  inHigh[],
319 /* Generated */                            const float  inLow[],
320 /* Generated */                            const float  inClose[],
321 /* Generated */                            int          *outBegIdx,
322 /* Generated */                            int          *outNBElement,
323 /* Generated */                            int           outInteger[] )
324 /* Generated */ #endif
325 /* Generated */ {
326 /* Generated */     double BodyPeriodTotal, ShadowLongPeriodTotal, ShadowVeryShortPeriodTotal, NearPeriodTotal;
327 /* Generated */     int i, outIdx, BodyTrailingIdx, ShadowLongTrailingIdx, ShadowVeryShortTrailingIdx, NearTrailingIdx, lookbackTotal;
328 /* Generated */  #ifndef TA_FUNC_NO_RANGE_CHECK
329 /* Generated */     if( startIdx < 0 )
330 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
331 /* Generated */     if( (endIdx < 0) || (endIdx < startIdx))
332 /* Generated */        return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
333 /* Generated */     #if !defined(_JAVA)
334 /* Generated */     if(!inOpen||!inHigh||!inLow||!inClose)
335 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
336 /* Generated */     #endif
337 /* Generated */     #if !defined(_JAVA)
338 /* Generated */     if( !outInteger )
339 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
340 /* Generated */     #endif
341 /* Generated */  #endif
342 /* Generated */    lookbackTotal = LOOKBACK_CALL(CDLHAMMER)();
343 /* Generated */    if( startIdx < lookbackTotal )
344 /* Generated */       startIdx = lookbackTotal;
345 /* Generated */    if( startIdx > endIdx )
346 /* Generated */    {
347 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
348 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
349 /* Generated */       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
350 /* Generated */    }
351 /* Generated */    BodyPeriodTotal = 0;
352 /* Generated */    BodyTrailingIdx = startIdx - TA_CANDLEAVGPERIOD(BodyShort);
353 /* Generated */    ShadowLongPeriodTotal = 0;
354 /* Generated */    ShadowLongTrailingIdx = startIdx - TA_CANDLEAVGPERIOD(ShadowLong);
355 /* Generated */    ShadowVeryShortPeriodTotal = 0;
356 /* Generated */    ShadowVeryShortTrailingIdx = startIdx - TA_CANDLEAVGPERIOD(ShadowVeryShort);
357 /* Generated */    NearPeriodTotal = 0;
358 /* Generated */    NearTrailingIdx = startIdx -1 - TA_CANDLEAVGPERIOD(Near);
359 /* Generated */    i = BodyTrailingIdx;
360 /* Generated */    while( i < startIdx ) {
361 /* Generated */         BodyPeriodTotal += TA_CANDLERANGE( BodyShort, i );
362 /* Generated */         i++;
363 /* Generated */    }
364 /* Generated */    i = ShadowLongTrailingIdx;
365 /* Generated */    while( i < startIdx ) {
366 /* Generated */         ShadowLongPeriodTotal += TA_CANDLERANGE( ShadowLong, i );
367 /* Generated */         i++;
368 /* Generated */    }
369 /* Generated */    i = ShadowVeryShortTrailingIdx;
370 /* Generated */    while( i < startIdx ) {
371 /* Generated */         ShadowVeryShortPeriodTotal += TA_CANDLERANGE( ShadowVeryShort, i );
372 /* Generated */         i++;
373 /* Generated */    }
374 /* Generated */    i = NearTrailingIdx;
375 /* Generated */    while( i < startIdx-1 ) {
376 /* Generated */         NearPeriodTotal += TA_CANDLERANGE( Near, i );
377 /* Generated */         i++;
378 /* Generated */    }
379 /* Generated */    i = startIdx;
380 /* Generated */    outIdx = 0;
381 /* Generated */    do
382 /* Generated */    {
383 /* Generated */         if( TA_REALBODY(i) < TA_CANDLEAVERAGE( BodyShort, BodyPeriodTotal, i ) &&                        // small rb
384 /* Generated */             TA_LOWERSHADOW(i) > TA_CANDLEAVERAGE( ShadowLong, ShadowLongPeriodTotal, i ) &&              // long lower shadow
385 /* Generated */             TA_UPPERSHADOW(i) < TA_CANDLEAVERAGE( ShadowVeryShort, ShadowVeryShortPeriodTotal, i ) &&    // very short upper shadow
386 /* Generated */             min( inClose[i], inOpen[i] ) <= inLow[i-1] + TA_CANDLEAVERAGE( Near, NearPeriodTotal, i-1 )  // rb near the prior candle's lows
387 /* Generated */           )
388 /* Generated */             outInteger[outIdx++] = 100;
389 /* Generated */         else
390 /* Generated */             outInteger[outIdx++] = 0;
391 /* Generated */         BodyPeriodTotal += TA_CANDLERANGE( BodyShort, i )
392 /* Generated */             - TA_CANDLERANGE( BodyShort, BodyTrailingIdx );
393 /* Generated */         ShadowLongPeriodTotal += TA_CANDLERANGE( ShadowLong, i )
394 /* Generated */             - TA_CANDLERANGE( ShadowLong, ShadowLongTrailingIdx );
395 /* Generated */         ShadowVeryShortPeriodTotal += TA_CANDLERANGE( ShadowVeryShort, i )
396 /* Generated */             - TA_CANDLERANGE( ShadowVeryShort, ShadowVeryShortTrailingIdx );
397 /* Generated */         NearPeriodTotal += TA_CANDLERANGE( Near, i-1 )
398 /* Generated */             - TA_CANDLERANGE( Near, NearTrailingIdx );
399 /* Generated */         i++;
400 /* Generated */         BodyTrailingIdx++;
401 /* Generated */         ShadowLongTrailingIdx++;
402 /* Generated */         ShadowVeryShortTrailingIdx++;
403 /* Generated */         NearTrailingIdx++;
404 /* Generated */    } while( i <= endIdx );
405 /* Generated */    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
406 /* Generated */    VALUE_HANDLE_DEREF(outBegIdx)    = startIdx;
407 /* Generated */    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
408 /* Generated */ }
409 /* Generated */
410 /* Generated */ #if defined( _MANAGED )
411 /* Generated */ }}} // Close namespace TicTacTec.TA.Lib
412 /* Generated */ #endif
413 /**** END GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
414 
415