Class: TaLib::Function

Inherits:
Object
  • Object
show all
Defined in:
ext/talib/talib.c

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Object

Create new instance of technical analysis function with given name.



184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# File 'ext/talib/talib.c', line 184

static VALUE ta_func_initialize(VALUE self, VALUE name) 
{
	TA_RetCode ret_code;
  const TA_FuncHandle *handle;
	TA_ParamHolder *ta_param_holder;
	ParamHolder *param_holder;

	Data_Get_Struct(self, ParamHolder, param_holder);		

  ret_code = TA_GetFuncHandle( RSTRING_PTR(name), &handle );
	if ( ret_code == TA_SUCCESS )
	{
		ret_code = TA_ParamHolderAlloc( handle, &ta_param_holder );
		if ( ret_code != TA_SUCCESS )
			rb_raise(rb_eRuntimeError, "unsuccess return code TA_ParamHolderAlloc");
		param_holder->p = ta_param_holder;
		rb_iv_set(self, "@name", name);
		rb_iv_set(self, "@result", rb_ary_new());
		return self;
	}
	rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetFuncHandle");
}

Instance Attribute Details

#resultObject

Class Method Details

.functionsObject

Return the hash of names for technical functions. The key is a name of funcion group, and value - is a list of function names.



243
244
245
246
# File 'ext/talib/talib.c', line 243

static VALUE ta_func_get_functions(VALUE self)
{
	return rb_cv_get(self, "@@functions");
}

.groupsObject

Return the list of names for technical function groups.



234
235
236
237
# File 'ext/talib/talib.c', line 234

static VALUE ta_func_get_groups(VALUE self)
{
	return rb_cv_get(self, "@@groups");
}

Instance Method Details

#call(in_start, in_end) ⇒ Object

Call technical function with input data from in_start..in_end.



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
# File 'ext/talib/talib.c', line 433

static VALUE ta_func_call(VALUE self, VALUE in_start, VALUE in_end)
{
	TA_RetCode ret_code;
	ParamHolder *param_holder;
	TA_Integer out_start, out_num;
	VALUE ary, sub_ary;
	int i,j;

	Data_Get_Struct(self, ParamHolder, param_holder);		
	ret_code = TA_CallFunc( param_holder->p, FIX2INT(in_start), FIX2INT(in_end), &out_start, &out_num);
	if ( ret_code != TA_SUCCESS )
		rb_raise(rb_eRuntimeError, "unsuccess return code TA_CallFunc");
	ary = rb_iv_get(self, "@result");
	for (i = 0; i<RARRAY_LEN(ary); i++)
		if (TYPE(rb_ary_entry(ary, i)) == T_ARRAY)
		{
			sub_ary = rb_ary_entry(ary, i);
			for (j=0; j<out_num; j++)
			{
				double el = ((double*)param_holder->out[i])[j];
				rb_ary_store(sub_ary, j, rb_float_new(el));
			}
		}
	return rb_ary_new3(2, INT2FIX(out_start), INT2FIX(out_num));
}

#in(index) ⇒ Object

Return input parameter info for the given input parameter index.



253
254
255
256
# File 'ext/talib/talib.c', line 253

static VALUE ta_func_input_param_info(VALUE self, VALUE index)
{
	return ta_func_param_info(TA_INPUT_PARAM, self, rb_iv_get(self, "@name"), index);
}

#in_int(index, array) ⇒ Object

Set input parameter (array of integer) for the given parameter index.



283
284
285
286
287
288
289
290
291
292
293
294
# File 'ext/talib/talib.c', line 283

static VALUE ta_func_setup_in_integer(VALUE self, VALUE param_index, VALUE in_array) 
{
	TA_RetCode ret_code;
	ParamHolder *param_holder;

	Data_Get_Struct(self, ParamHolder, param_holder);		
	ret_code = TA_SetInputParamIntegerPtr( param_holder->p, FIX2INT(param_index), (int*)(RARRAY_PTR(in_array)));
	if ( ret_code != TA_SUCCESS )
		rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetInputParamIntegerPtr");

	return T_TRUE;
}

#in_price(param_index, in_open, in_high, in_low, in_close, in_volume, in_oi) ⇒ Object



316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# File 'ext/talib/talib.c', line 316

static VALUE ta_func_setup_in_price(VALUE self, VALUE param_index, VALUE in_open, VALUE in_high, VALUE in_low, VALUE in_close, VALUE in_volume, VALUE in_oi)
{
  double **dp;
	TA_RetCode ret_code;
	ParamHolder *param_holder;
  Data_Get_Struct(self, ParamHolder, param_holder);
  dp = param_holder->in;
  
  ret_code = TA_SetInputParamPricePtr( 
      param_holder->p, 
      FIX2INT(param_index), 
      FLT2DBL(&dp[0], in_open), 
      FLT2DBL(&dp[1], in_high), 
      FLT2DBL(&dp[2], in_low), 
      FLT2DBL(&dp[3], in_close), 
      FLT2DBL(&dp[4], in_volume), 
      FLT2DBL(&dp[5], in_oi)
  );
	if ( ret_code != TA_SUCCESS )
		rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetInputParamPricePtr");

	return T_TRUE;
}

#in_real(index, array) ⇒ Object

Set input parameter (array of real) for the given parameter index.



301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'ext/talib/talib.c', line 301

static VALUE ta_func_setup_in_real(VALUE self, VALUE param_index, VALUE in_array) 
{
	double** dp; 
	TA_RetCode ret_code;
	ParamHolder *param_holder;

	Data_Get_Struct(self, ParamHolder, param_holder);		
        dp = param_holder->in;
	//FIXME: memory leak fixed: [email protected] (see: FL2DBL())
	ret_code = TA_SetInputParamRealPtr( param_holder->p, FIX2INT(param_index), FLT2DBL(&dp[FIX2INT(param_index)], in_array));
	if ( ret_code != TA_SUCCESS )
		rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetInputParamRealPtr");
	return T_TRUE;
}

#insObject

Return input parameters count.



210
211
212
213
# File 'ext/talib/talib.c', line 210

static VALUE ta_func_get_input_count(VALUE self)
{
	return INT2NUM((abstract_layer_get_func_info(rb_iv_get(self, "@name")))->nbInput);
}

#lookbackObject



459
460
461
462
463
464
465
466
467
468
469
470
471
# File 'ext/talib/talib.c', line 459

static VALUE ta_func_lookback(VALUE self)
{
	TA_RetCode ret_code;
	ParamHolder *param_holder;
	TA_Integer out_lookback;

	Data_Get_Struct(self, ParamHolder, param_holder);
	ret_code = TA_GetLookback(param_holder->p, &out_lookback);
	if ( ret_code != TA_SUCCESS )
		rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetLookback");
	
  return INT2FIX(out_lookback);
}

#opt(index) ⇒ Object

Return option input parameter info for the given option parameter index.



263
264
265
266
# File 'ext/talib/talib.c', line 263

static VALUE ta_func_option_param_info(VALUE self, VALUE index)
{
	return ta_func_param_info(TA_OPTION_INPUT_PARAM, self, rb_iv_get(self, "@name"), index);
}

#opt_int(index, value) ⇒ Object

Set option parameter (integer) for the given parameter index.



345
346
347
348
349
350
351
352
353
354
355
# File 'ext/talib/talib.c', line 345

static VALUE ta_func_setup_opt_in_integer(VALUE self, VALUE param_index, VALUE val) 
{
	TA_RetCode ret_code;
	ParamHolder *param_holder;
	Data_Get_Struct(self, ParamHolder, param_holder);		
	ret_code = TA_SetOptInputParamInteger( param_holder->p, FIX2INT(param_index), FIX2INT(val));
	if ( ret_code != TA_SUCCESS )
		rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetOptInputParamIntegerPtr");

	return val;
}

#opt_real(index, value) ⇒ Object

Set option parameter (real) for the given parameter index.



362
363
364
365
366
367
368
369
370
371
372
373
# File 'ext/talib/talib.c', line 362

static VALUE ta_func_setup_opt_in_real(VALUE self, VALUE param_index, VALUE val) 
{
	TA_RetCode ret_code;
	ParamHolder *param_holder;

	Data_Get_Struct(self, ParamHolder, param_holder);		
	ret_code = TA_SetOptInputParamReal( param_holder->p, FIX2INT(param_index), NUM2DBL(val));
	if ( ret_code != TA_SUCCESS )
		rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetOptInputParamRealPtr");

	return val;
}

#optsObject

Return option input parameters count.



218
219
220
221
# File 'ext/talib/talib.c', line 218

static VALUE ta_func_get_option_input_count(VALUE self)
{
	return INT2NUM((abstract_layer_get_func_info(rb_iv_get(self, "@name")))->nbOptInput);
}

#out(index) ⇒ Object

Return output parameter info for the given output parameter index.



273
274
275
276
# File 'ext/talib/talib.c', line 273

static VALUE ta_func_output_param_info(VALUE self, VALUE index)
{
	return ta_func_param_info(TA_OUTPUT_PARAM, self, rb_iv_get(self, "@name"), index);
}

#out_int(param_index, out_array) ⇒ Object



404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# File 'ext/talib/talib.c', line 404

static VALUE ta_func_setup_out_integer(VALUE self, VALUE param_index, VALUE out_array)
{
	TA_RetCode ret_code;
	ParamHolder *param_holder;
  long idx = FIX2INT(param_index); 
  int **ip;
	if (idx > 2)
		rb_raise(rb_eRuntimeError, "param_index must be 0..2");
	
	Data_Get_Struct(self, ParamHolder, param_holder);
	rb_ary_store(rb_iv_get(self, "@result"), idx, out_array);
	
	// FIXME: malloc w/o free FIXED: [email protected]
  ip = (int**)&(param_holder->out[idx]); 
  if (*ip) free(*ip); // not true only very 1st time in
	*ip = (int*)malloc(RARRAY_LEN(out_array) * sizeof(int));
	
	ret_code = TA_SetOutputParamIntegerPtr( param_holder->p, idx, *ip);
	if ( ret_code != TA_SUCCESS )
		rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetOutputParamIntegerPtr");

	return out_array;
}

#out_real(index, array) ⇒ Object

Set output parameter (array of real) for the given parameter index.



380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# File 'ext/talib/talib.c', line 380

static VALUE ta_func_setup_out_real(VALUE self, VALUE param_index, VALUE out_array) 
{
	TA_RetCode ret_code;
	ParamHolder *param_holder;
        long idx = FIX2INT(param_index);
	double **dp;
	if (idx > 2)
		rb_raise(rb_eRuntimeError, "param_index must be 0..2");
	
	Data_Get_Struct(self, ParamHolder, param_holder);		
	rb_ary_store(rb_iv_get(self, "@result"), idx, out_array);
	// FIXME: malloc w/o free: [email protected] fixed
	dp = &(param_holder->out[idx]); 
  if (*dp) free(*dp); // not true only 1st time called (reusing same ptrs)
	
	*dp = (double*)malloc(RARRAY_LEN(out_array) * sizeof(double));
	ret_code = TA_SetOutputParamRealPtr(param_holder->p, idx, *dp);
	
	if ( ret_code != TA_SUCCESS )
		rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetOutputParamRealPtr");

	return out_array; 
}

#outsObject

Return output parameters count.



226
227
228
229
# File 'ext/talib/talib.c', line 226

static VALUE ta_func_get_output_count(VALUE self)
{
	return INT2NUM((abstract_layer_get_func_info(rb_iv_get(self, "@name")))->nbOutput);
}