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  *  010605 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 )
CdlCounterAttackLookback(void)81 /* Generated */ int Core::CdlCounterAttackLookback( void )
82 /* Generated */
83 /* Generated */ #elif defined( _JAVA )
84 /* Generated */ public int cdlCounterAttackLookback(  )
85 /* Generated */
86 /* Generated */ #else
87 /* Generated */ int TA_CDLCOUNTERATTACK_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( TA_CANDLEAVGPERIOD(Equal), TA_CANDLEAVGPERIOD(BodyLong)
100             ) + 1;
101 }
102 
103 /**** START GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
104 /*
105  * TA_CDLCOUNTERATTACK - Counterattack
106  *
107  * Input  = Open, High, Low, Close
108  * Output = int
109  *
110  */
111 /* Generated */
112 /* Generated */ #if defined( _MANAGED ) && defined( USE_SUBARRAY )
113 /* Generated */ enum class Core::RetCode Core::CdlCounterAttack( int    startIdx,
114 /* Generated */                                                  int    endIdx,
115 /* Generated */                                                  SubArray^    inOpen,
116 /* Generated */                                                  SubArray^    inHigh,
117 /* Generated */                                                  SubArray^    inLow,
118 /* Generated */                                                  SubArray^    inClose,
119 /* Generated */                                                  [Out]int%    outBegIdx,
120 /* Generated */                                                  [Out]int%    outNBElement,
121 /* Generated */                                                  cli::array<int>^  outInteger )
122 /* Generated */ #elif defined( _MANAGED )
123 /* Generated */ enum class Core::RetCode Core::CdlCounterAttack( int    startIdx,
124 /* Generated */                                                  int    endIdx,
125 /* Generated */                                                  cli::array<double>^ inOpen,
126 /* Generated */                                                  cli::array<double>^ inHigh,
127 /* Generated */                                                  cli::array<double>^ inLow,
128 /* Generated */                                                  cli::array<double>^ inClose,
129 /* Generated */                                                  [Out]int%    outBegIdx,
130 /* Generated */                                                  [Out]int%    outNBElement,
131 /* Generated */                                                  cli::array<int>^  outInteger )
132 /* Generated */ #elif defined( _JAVA )
133 /* Generated */ public RetCode cdlCounterAttack( int    startIdx,
134 /* Generated */                                  int    endIdx,
135 /* Generated */                                  double       inOpen[],
136 /* Generated */                                  double       inHigh[],
137 /* Generated */                                  double       inLow[],
138 /* Generated */                                  double       inClose[],
139 /* Generated */                                  MInteger     outBegIdx,
140 /* Generated */                                  MInteger     outNBElement,
141 /* Generated */                                  int           outInteger[] )
142 /* Generated */ #else
143 /* Generated */ TA_RetCode TA_CDLCOUNTERATTACK( int    startIdx,
144 /* Generated */                                 int    endIdx,
145 /* Generated */                                 const double inOpen[],
146 /* Generated */                                 const double inHigh[],
147 /* Generated */                                 const double inLow[],
148 /* Generated */                                 const double inClose[],
149 /* Generated */                                 int          *outBegIdx,
150 /* Generated */                                 int          *outNBElement,
151 /* Generated */                                 int           outInteger[] )
152 /* Generated */ #endif
153 /**** END GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
154 {
155    /* Insert local variables here. */
156     double EqualPeriodTotal;
157 	ARRAY_LOCAL(BodyLongPeriodTotal,2);
158     int i, outIdx, totIdx, EqualTrailingIdx, BodyLongTrailingIdx, lookbackTotal;
159 
160 /**** START GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
161 /* Generated */
162 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
163 /* Generated */
164 /* Generated */    /* Validate the requested output range. */
165 /* Generated */    if( startIdx < 0 )
166 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
167 /* Generated */    if( (endIdx < 0) || (endIdx < startIdx))
168 /* Generated */       return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
169 /* Generated */
170 /* Generated */    #if !defined(_JAVA)
171 /* Generated */    /* Verify required price component. */
172 /* Generated */    if(!inOpen||!inHigh||!inLow||!inClose)
173 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
174 /* Generated */
175 /* Generated */    #endif /* !defined(_JAVA)*/
176 /* Generated */    #if !defined(_JAVA)
177 /* Generated */    if( !outInteger )
178 /* Generated */       return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
179 /* Generated */
180 /* Generated */    #endif /* !defined(_JAVA) */
181 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
182 /* Generated */
183 /**** END GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
184 
185    /* Identify the minimum number of price bar needed
186     * to calculate at least one output.
187     */
188 
189    lookbackTotal = LOOKBACK_CALL(CDLCOUNTERATTACK)();
190 
191    /* Move up the start index if there is not
192     * enough initial data.
193     */
194    if( startIdx < lookbackTotal )
195       startIdx = lookbackTotal;
196 
197    /* Make sure there is still something to evaluate. */
198    if( startIdx > endIdx )
199    {
200       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
201       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
202       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
203    }
204 
205    /* Do the calculation using tight loops. */
206    /* Add-up the initial period, except for the last value. */
207    EqualPeriodTotal = 0;
208    EqualTrailingIdx = startIdx - TA_CANDLEAVGPERIOD(Equal);
209    BodyLongPeriodTotal[1] = 0;
210    BodyLongPeriodTotal[0] = 0;
211    BodyLongTrailingIdx = startIdx - TA_CANDLEAVGPERIOD(BodyLong);
212 
213    i = EqualTrailingIdx;
214    while( i < startIdx ) {
215         EqualPeriodTotal += TA_CANDLERANGE( Equal, i-1 );
216         i++;
217    }
218    i = BodyLongTrailingIdx;
219    while( i < startIdx ) {
220         BodyLongPeriodTotal[1] += TA_CANDLERANGE( BodyLong, i-1 );
221         BodyLongPeriodTotal[0] += TA_CANDLERANGE( BodyLong, i );
222         i++;
223    }
224    i = startIdx;
225 
226    /* Proceed with the calculation for the requested range.
227     * Must have:
228     * - first candle: long black (white)
229     * - second candle: long white (black) with close equal to the prior close
230     * The meaning of "equal" and "long" is specified with TA_SetCandleSettings
231     * outInteger is positive (1 to 100) when bullish or negative (-1 to -100) when bearish;
232     * the user should consider that counterattack is significant in a trend, while this function does not consider it
233     */
234    outIdx = 0;
235    do
236    {
237         if( TA_CANDLECOLOR(i-1) == -TA_CANDLECOLOR(i) &&                                        // opposite candles
238             TA_REALBODY(i-1) > TA_CANDLEAVERAGE( BodyLong, BodyLongPeriodTotal[1], i-1 ) &&     // 1st long
239             TA_REALBODY(i) > TA_CANDLEAVERAGE( BodyLong, BodyLongPeriodTotal[0], i ) &&         // 2nd long
240             inClose[i] <= inClose[i-1] + TA_CANDLEAVERAGE( Equal, EqualPeriodTotal, i-1 ) && // equal closes
241             inClose[i] >= inClose[i-1] - TA_CANDLEAVERAGE( Equal, EqualPeriodTotal, i-1 )
242           )
243             outInteger[outIdx++] = TA_CANDLECOLOR(i) * 100;
244         else
245             outInteger[outIdx++] = 0;
246         /* add the current range and subtract the first range: this is done after the pattern recognition
247          * when avgPeriod is not 0, that means "compare with the previous candles" (it excludes the current candle)
248          */
249         EqualPeriodTotal += TA_CANDLERANGE( Equal, i-1 ) - TA_CANDLERANGE( Equal, EqualTrailingIdx-1 );
250         for (totIdx = 1; totIdx >= 0; --totIdx)
251             BodyLongPeriodTotal[totIdx] += TA_CANDLERANGE( BodyLong, i-totIdx )
252                                          - TA_CANDLERANGE( BodyLong, BodyLongTrailingIdx-totIdx );
253         i++;
254         EqualTrailingIdx++;
255         BodyLongTrailingIdx++;
256    } while( i <= endIdx );
257 
258    /* All done. Indicate the output limits and return. */
259    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
260    VALUE_HANDLE_DEREF(outBegIdx)    = startIdx;
261 
262    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
263 }
264 
265 /**** START GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
266 /* Generated */
267 /* Generated */ #define  USE_SINGLE_PRECISION_INPUT
268 /* Generated */ #if !defined( _MANAGED ) && !defined( _JAVA )
269 /* Generated */    #undef   TA_PREFIX
270 /* Generated */    #define  TA_PREFIX(x) TA_S_##x
271 /* Generated */ #endif
272 /* Generated */ #undef   INPUT_TYPE
273 /* Generated */ #define  INPUT_TYPE float
274 /* Generated */ #if defined( _MANAGED )
275 /* Generated */ enum class Core::RetCode Core::CdlCounterAttack( int    startIdx,
276 /* Generated */                                                  int    endIdx,
277 /* Generated */                                                  cli::array<float>^ inOpen,
278 /* Generated */                                                  cli::array<float>^ inHigh,
279 /* Generated */                                                  cli::array<float>^ inLow,
280 /* Generated */                                                  cli::array<float>^ inClose,
281 /* Generated */                                                  [Out]int%    outBegIdx,
282 /* Generated */                                                  [Out]int%    outNBElement,
283 /* Generated */                                                  cli::array<int>^  outInteger )
284 /* Generated */ #elif defined( _JAVA )
285 /* Generated */ public RetCode cdlCounterAttack( int    startIdx,
286 /* Generated */                                  int    endIdx,
287 /* Generated */                                  float        inOpen[],
288 /* Generated */                                  float        inHigh[],
289 /* Generated */                                  float        inLow[],
290 /* Generated */                                  float        inClose[],
291 /* Generated */                                  MInteger     outBegIdx,
292 /* Generated */                                  MInteger     outNBElement,
293 /* Generated */                                  int           outInteger[] )
294 /* Generated */ #else
295 /* Generated */ TA_RetCode TA_S_CDLCOUNTERATTACK( int    startIdx,
296 /* Generated */                                   int    endIdx,
297 /* Generated */                                   const float  inOpen[],
298 /* Generated */                                   const float  inHigh[],
299 /* Generated */                                   const float  inLow[],
300 /* Generated */                                   const float  inClose[],
301 /* Generated */                                   int          *outBegIdx,
302 /* Generated */                                   int          *outNBElement,
303 /* Generated */                                   int           outInteger[] )
304 /* Generated */ #endif
305 /* Generated */ {
306 /* Generated */     double EqualPeriodTotal;
307 /* Generated */ 	ARRAY_LOCAL(BodyLongPeriodTotal,2);
308 /* Generated */     int i, outIdx, totIdx, EqualTrailingIdx, BodyLongTrailingIdx, lookbackTotal;
309 /* Generated */  #ifndef TA_FUNC_NO_RANGE_CHECK
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 */     #if !defined(_JAVA)
315 /* Generated */     if(!inOpen||!inHigh||!inLow||!inClose)
316 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
317 /* Generated */     #endif
318 /* Generated */     #if !defined(_JAVA)
319 /* Generated */     if( !outInteger )
320 /* Generated */        return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
321 /* Generated */     #endif
322 /* Generated */  #endif
323 /* Generated */    lookbackTotal = LOOKBACK_CALL(CDLCOUNTERATTACK)();
324 /* Generated */    if( startIdx < lookbackTotal )
325 /* Generated */       startIdx = lookbackTotal;
326 /* Generated */    if( startIdx > endIdx )
327 /* Generated */    {
328 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
329 /* Generated */       VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
330 /* Generated */       return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
331 /* Generated */    }
332 /* Generated */    EqualPeriodTotal = 0;
333 /* Generated */    EqualTrailingIdx = startIdx - TA_CANDLEAVGPERIOD(Equal);
334 /* Generated */    BodyLongPeriodTotal[1] = 0;
335 /* Generated */    BodyLongPeriodTotal[0] = 0;
336 /* Generated */    BodyLongTrailingIdx = startIdx - TA_CANDLEAVGPERIOD(BodyLong);
337 /* Generated */    i = EqualTrailingIdx;
338 /* Generated */    while( i < startIdx ) {
339 /* Generated */         EqualPeriodTotal += TA_CANDLERANGE( Equal, i-1 );
340 /* Generated */         i++;
341 /* Generated */    }
342 /* Generated */    i = BodyLongTrailingIdx;
343 /* Generated */    while( i < startIdx ) {
344 /* Generated */         BodyLongPeriodTotal[1] += TA_CANDLERANGE( BodyLong, i-1 );
345 /* Generated */         BodyLongPeriodTotal[0] += TA_CANDLERANGE( BodyLong, i );
346 /* Generated */         i++;
347 /* Generated */    }
348 /* Generated */    i = startIdx;
349 /* Generated */    outIdx = 0;
350 /* Generated */    do
351 /* Generated */    {
352 /* Generated */         if( TA_CANDLECOLOR(i-1) == -TA_CANDLECOLOR(i) &&                                        // opposite candles
353 /* Generated */             TA_REALBODY(i-1) > TA_CANDLEAVERAGE( BodyLong, BodyLongPeriodTotal[1], i-1 ) &&     // 1st long
354 /* Generated */             TA_REALBODY(i) > TA_CANDLEAVERAGE( BodyLong, BodyLongPeriodTotal[0], i ) &&         // 2nd long
355 /* Generated */             inClose[i] <= inClose[i-1] + TA_CANDLEAVERAGE( Equal, EqualPeriodTotal, i-1 ) && // equal closes
356 /* Generated */             inClose[i] >= inClose[i-1] - TA_CANDLEAVERAGE( Equal, EqualPeriodTotal, i-1 )
357 /* Generated */           )
358 /* Generated */             outInteger[outIdx++] = TA_CANDLECOLOR(i) * 100;
359 /* Generated */         else
360 /* Generated */             outInteger[outIdx++] = 0;
361 /* Generated */         EqualPeriodTotal += TA_CANDLERANGE( Equal, i-1 ) - TA_CANDLERANGE( Equal, EqualTrailingIdx-1 );
362 /* Generated */         for (totIdx = 1; totIdx >= 0; --totIdx)
363 /* Generated */             BodyLongPeriodTotal[totIdx] += TA_CANDLERANGE( BodyLong, i-totIdx )
364 /* Generated */                                          - TA_CANDLERANGE( BodyLong, BodyLongTrailingIdx-totIdx );
365 /* Generated */         i++;
366 /* Generated */         EqualTrailingIdx++;
367 /* Generated */         BodyLongTrailingIdx++;
368 /* Generated */    } while( i <= endIdx );
369 /* Generated */    VALUE_HANDLE_DEREF(outNBElement) = outIdx;
370 /* Generated */    VALUE_HANDLE_DEREF(outBegIdx)    = startIdx;
371 /* Generated */    return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
372 /* Generated */ }
373 /* Generated */
374 /* Generated */ #if defined( _MANAGED )
375 /* Generated */ }}} // Close namespace TicTacTec.TA.Lib
376 /* Generated */ #endif
377 /**** END GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
378 
379