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 *
40 *
41 * Change history:
42 *
43 * MMDDYY BY Description
44 * -------------------------------------------------------------------
45 * 112400 MF Template creation.
46 * 052603 MF Adapt code to compile with .NET Managed C++
47 *
48 */
49
50 /**** START GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
51 /* All code within this section is automatically
52 * generated by gen_code. Any modification will be lost
53 * next time gen_code is run.
54 */
55 /* Generated */
56 /* Generated */ #if defined( _MANAGED )
57 /* Generated */ #include "TA-Lib-Core.h"
58 /* Generated */ #define TA_INTERNAL_ERROR(Id) (RetCode::InternalError)
59 /* Generated */ namespace TicTacTec { namespace TA { namespace Library {
60 /* Generated */ #elif defined( _JAVA )
61 /* Generated */ #include "ta_defs.h"
62 /* Generated */ #include "ta_java_defs.h"
63 /* Generated */ #define TA_INTERNAL_ERROR(Id) (RetCode.InternalError)
64 /* Generated */ #else
65 /* Generated */ #include <string.h>
66 /* Generated */ #include <math.h>
67 /* Generated */ #include "ta_func.h"
68 /* Generated */ #endif
69 /* Generated */
70 /* Generated */ #ifndef TA_UTILITY_H
71 /* Generated */ #include "ta_utility.h"
72 /* Generated */ #endif
73 /* Generated */
74 /* Generated */ #ifndef TA_MEMORY_H
75 /* Generated */ #include "ta_memory.h"
76 /* Generated */ #endif
77 /* Generated */
78 /* Generated */ #define TA_PREFIX(x) TA_##x
79 /* Generated */ #define INPUT_TYPE double
80 /* Generated */
81 /* Generated */ #if defined( _MANAGED )
MomLookback(int optInTimePeriod)82 /* Generated */ int Core::MomLookback( int optInTimePeriod ) /* From 1 to 100000 */
83 /* Generated */
84 /* Generated */ #elif defined( _JAVA )
85 /* Generated */ public int momLookback( int optInTimePeriod ) /* From 1 to 100000 */
86 /* Generated */
87 /* Generated */ #else
88 /* Generated */ int TA_MOM_Lookback( int optInTimePeriod ) /* From 1 to 100000 */
89 /* Generated */
90 /* Generated */ #endif
91 /**** END GENCODE SECTION 1 - DO NOT DELETE THIS LINE ****/
92 {
93 /* insert local variable here */
94
95 /**** START GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
96 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
97 /* Generated */ /* min/max are checked for optInTimePeriod. */
98 /* Generated */ if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )
99 /* Generated */ optInTimePeriod = 10;
100 /* Generated */ else if( ((int)optInTimePeriod < 1) || ((int)optInTimePeriod > 100000) )
101 /* Generated */ return -1;
102 /* Generated */
103 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
104 /**** END GENCODE SECTION 2 - DO NOT DELETE THIS LINE ****/
105
106 /* insert lookback code here. */
107
108 return optInTimePeriod;
109
110 }
111
112 /**** START GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
113 /*
114 * TA_MOM - Momentum
115 *
116 * Input = double
117 * Output = double
118 *
119 * Optional Parameters
120 * -------------------
121 * optInTimePeriod:(From 1 to 100000)
122 * Number of period
123 *
124 *
125 */
126 /* Generated */
127 /* Generated */ #if defined( _MANAGED ) && defined( USE_SUBARRAY )
128 /* Generated */ enum class Core::RetCode Core::Mom( int startIdx,
129 /* Generated */ int endIdx,
130 /* Generated */ SubArray^ inReal,
131 /* Generated */ int optInTimePeriod, /* From 1 to 100000 */
132 /* Generated */ [Out]int% outBegIdx,
133 /* Generated */ [Out]int% outNBElement,
134 /* Generated */ cli::array<double>^ outReal )
135 /* Generated */ #elif defined( _MANAGED )
136 /* Generated */ enum class Core::RetCode Core::Mom( int startIdx,
137 /* Generated */ int endIdx,
138 /* Generated */ cli::array<double>^ inReal,
139 /* Generated */ int optInTimePeriod, /* From 1 to 100000 */
140 /* Generated */ [Out]int% outBegIdx,
141 /* Generated */ [Out]int% outNBElement,
142 /* Generated */ cli::array<double>^ outReal )
143 /* Generated */ #elif defined( _JAVA )
144 /* Generated */ public RetCode mom( int startIdx,
145 /* Generated */ int endIdx,
146 /* Generated */ double inReal[],
147 /* Generated */ int optInTimePeriod, /* From 1 to 100000 */
148 /* Generated */ MInteger outBegIdx,
149 /* Generated */ MInteger outNBElement,
150 /* Generated */ double outReal[] )
151 /* Generated */ #else
152 /* Generated */ TA_RetCode TA_MOM( int startIdx,
153 /* Generated */ int endIdx,
154 /* Generated */ const double inReal[],
155 /* Generated */ int optInTimePeriod, /* From 1 to 100000 */
156 /* Generated */ int *outBegIdx,
157 /* Generated */ int *outNBElement,
158 /* Generated */ double outReal[] )
159 /* Generated */ #endif
160 /**** END GENCODE SECTION 3 - DO NOT DELETE THIS LINE ****/
161 {
162 /* Insert local variables here. */
163
164 int inIdx, outIdx, trailingIdx;
165
166 /**** START GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
167 /* Generated */
168 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
169 /* Generated */
170 /* Generated */ /* Validate the requested output range. */
171 /* Generated */ if( startIdx < 0 )
172 /* Generated */ return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
173 /* Generated */ if( (endIdx < 0) || (endIdx < startIdx))
174 /* Generated */ return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
175 /* Generated */
176 /* Generated */ #if !defined(_JAVA)
177 /* Generated */ if( !inReal ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
178 /* Generated */ #endif /* !defined(_JAVA)*/
179 /* Generated */ /* min/max are checked for optInTimePeriod. */
180 /* Generated */ if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )
181 /* Generated */ optInTimePeriod = 10;
182 /* Generated */ else if( ((int)optInTimePeriod < 1) || ((int)optInTimePeriod > 100000) )
183 /* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
184 /* Generated */
185 /* Generated */ #if !defined(_JAVA)
186 /* Generated */ if( !outReal )
187 /* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
188 /* Generated */
189 /* Generated */ #endif /* !defined(_JAVA) */
190 /* Generated */ #endif /* TA_FUNC_NO_RANGE_CHECK */
191 /* Generated */
192 /**** END GENCODE SECTION 4 - DO NOT DELETE THIS LINE ****/
193
194 /* Insert TA function code here. */
195
196 /* The interpretation of the rate of change varies widely depending
197 * which software and/or books you are refering to.
198 *
199 * The following is the table of Rate-Of-Change implemented in TA-LIB:
200 * MOM = (price - prevPrice) [Momentum]
201 * ROC = ((price/prevPrice)-1)*100 [Rate of change]
202 * ROCP = (price-prevPrice)/prevPrice [Rate of change Percentage]
203 * ROCR = (price/prevPrice) [Rate of change ratio]
204 * ROCR100 = (price/prevPrice)*100 [Rate of change ratio 100 Scale]
205 *
206 * Here are the equivalent function in other software:
207 * TA-Lib | Tradestation | Metastock
208 * =================================================
209 * MOM | Momentum | ROC (Point)
210 * ROC | ROC | ROC (Percent)
211 * ROCP | PercentChange | -
212 * ROCR | - | -
213 * ROCR100 | - | MO
214 *
215 * The MOM function is the only one who is not normalized, and thus
216 * should be avoided for comparing different time serie of prices.
217 *
218 * ROC and ROCP are centered at zero and can have positive and negative
219 * value. Here are some equivalence:
220 * ROC = ROCP/100
221 * = ((price-prevPrice)/prevPrice)/100
222 * = ((price/prevPrice)-1)*100
223 *
224 * ROCR and ROCR100 are ratio respectively centered at 1 and 100 and are
225 * always positive values.
226 */
227
228 /* Move up the start index if there is not
229 * enough initial data.
230 */
231 if( startIdx < optInTimePeriod )
232 startIdx = optInTimePeriod;
233
234 /* Make sure there is still something to evaluate. */
235 if( startIdx > endIdx )
236 {
237 VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
238 VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
239 return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
240 }
241
242 /* Calculate Momentum:
243 * Just substract the value from 'period' ago from
244 * current value.
245 */
246 outIdx = 0;
247 inIdx = startIdx;
248 trailingIdx = startIdx - optInTimePeriod;
249
250 while( inIdx <= endIdx )
251 outReal[outIdx++] = inReal[inIdx++] - inReal[trailingIdx++];
252
253 /* Set output limits. */
254 VALUE_HANDLE_DEREF(outNBElement) = outIdx;
255 VALUE_HANDLE_DEREF(outBegIdx) = startIdx;
256
257 return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
258 }
259
260 /**** START GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
261 /* Generated */
262 /* Generated */ #define USE_SINGLE_PRECISION_INPUT
263 /* Generated */ #if !defined( _MANAGED ) && !defined( _JAVA )
264 /* Generated */ #undef TA_PREFIX
265 /* Generated */ #define TA_PREFIX(x) TA_S_##x
266 /* Generated */ #endif
267 /* Generated */ #undef INPUT_TYPE
268 /* Generated */ #define INPUT_TYPE float
269 /* Generated */ #if defined( _MANAGED )
270 /* Generated */ enum class Core::RetCode Core::Mom( int startIdx,
271 /* Generated */ int endIdx,
272 /* Generated */ cli::array<float>^ inReal,
273 /* Generated */ int optInTimePeriod, /* From 1 to 100000 */
274 /* Generated */ [Out]int% outBegIdx,
275 /* Generated */ [Out]int% outNBElement,
276 /* Generated */ cli::array<double>^ outReal )
277 /* Generated */ #elif defined( _JAVA )
278 /* Generated */ public RetCode mom( int startIdx,
279 /* Generated */ int endIdx,
280 /* Generated */ float inReal[],
281 /* Generated */ int optInTimePeriod, /* From 1 to 100000 */
282 /* Generated */ MInteger outBegIdx,
283 /* Generated */ MInteger outNBElement,
284 /* Generated */ double outReal[] )
285 /* Generated */ #else
286 /* Generated */ TA_RetCode TA_S_MOM( int startIdx,
287 /* Generated */ int endIdx,
288 /* Generated */ const float inReal[],
289 /* Generated */ int optInTimePeriod, /* From 1 to 100000 */
290 /* Generated */ int *outBegIdx,
291 /* Generated */ int *outNBElement,
292 /* Generated */ double outReal[] )
293 /* Generated */ #endif
294 /* Generated */ {
295 /* Generated */ int inIdx, outIdx, trailingIdx;
296 /* Generated */ #ifndef TA_FUNC_NO_RANGE_CHECK
297 /* Generated */ if( startIdx < 0 )
298 /* Generated */ return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_START_INDEX,OutOfRangeStartIndex);
299 /* Generated */ if( (endIdx < 0) || (endIdx < startIdx))
300 /* Generated */ return ENUM_VALUE(RetCode,TA_OUT_OF_RANGE_END_INDEX,OutOfRangeEndIndex);
301 /* Generated */ #if !defined(_JAVA)
302 /* Generated */ if( !inReal ) return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
303 /* Generated */ #endif
304 /* Generated */ if( (int)optInTimePeriod == TA_INTEGER_DEFAULT )
305 /* Generated */ optInTimePeriod = 10;
306 /* Generated */ else if( ((int)optInTimePeriod < 1) || ((int)optInTimePeriod > 100000) )
307 /* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
308 /* Generated */ #if !defined(_JAVA)
309 /* Generated */ if( !outReal )
310 /* Generated */ return ENUM_VALUE(RetCode,TA_BAD_PARAM,BadParam);
311 /* Generated */ #endif
312 /* Generated */ #endif
313 /* Generated */ if( startIdx < optInTimePeriod )
314 /* Generated */ startIdx = optInTimePeriod;
315 /* Generated */ if( startIdx > endIdx )
316 /* Generated */ {
317 /* Generated */ VALUE_HANDLE_DEREF_TO_ZERO(outBegIdx);
318 /* Generated */ VALUE_HANDLE_DEREF_TO_ZERO(outNBElement);
319 /* Generated */ return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
320 /* Generated */ }
321 /* Generated */ outIdx = 0;
322 /* Generated */ inIdx = startIdx;
323 /* Generated */ trailingIdx = startIdx - optInTimePeriod;
324 /* Generated */ while( inIdx <= endIdx )
325 /* Generated */ outReal[outIdx++] = inReal[inIdx++] - inReal[trailingIdx++];
326 /* Generated */ VALUE_HANDLE_DEREF(outNBElement) = outIdx;
327 /* Generated */ VALUE_HANDLE_DEREF(outBegIdx) = startIdx;
328 /* Generated */ return ENUM_VALUE(RetCode,TA_SUCCESS,Success);
329 /* Generated */ }
330 /* Generated */
331 /* Generated */ #if defined( _MANAGED )
332 /* Generated */ }}} // Close namespace TicTacTec.TA.Lib
333 /* Generated */ #endif
334 /**** END GENCODE SECTION 5 - DO NOT DELETE THIS LINE ****/
335
336