├── .gitignore ├── ext └── talib │ ├── extconf.rb │ └── talib.c ├── lib └── talib_ruby.rb ├── example ├── ma.rb └── adx.rb ├── Rakefile ├── talib_ruby.gemspec └── README.textile /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | *.zip 3 | *.xls 4 | pkg/ -------------------------------------------------------------------------------- /ext/talib/extconf.rb: -------------------------------------------------------------------------------- 1 | require 'mkmf' 2 | 3 | dir_config("talib") 4 | 5 | have_library("ta_lib", "TA_Initialize") 6 | create_makefile("talib") 7 | -------------------------------------------------------------------------------- /lib/talib_ruby.rb: -------------------------------------------------------------------------------- 1 | $:.unshift(File.dirname(__FILE__)) unless 2 | $:.include?(File.dirname(__FILE__)) || $:.include?(File.expand_path(File.dirname(__FILE__))) 3 | 4 | #require File.dirname(__FILE__) + '/../ext/talib/talib' 5 | require 'talib' 6 | 7 | module TalibRuby 8 | VERSION = '0.0.4' 9 | end -------------------------------------------------------------------------------- /example/ma.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'talib_ruby' 3 | 4 | # init input data 5 | a = Array.new 6 | 10.times { |i| a.push i.to_f } 7 | 8 | 10.times do |k| 9 | b = Array.new(10) 10 | l = TaLib::Function.new("MA") 11 | # setup input parameter 12 | l.in_real(0,a); 13 | # setup optional parameter 14 | l.opt_int(0,k+2); 15 | # setup output parameter 16 | l.out_real(0,b); 17 | l.call(0,9) 18 | p "k=#{k+2}" 19 | p b 20 | end 21 | -------------------------------------------------------------------------------- /example/adx.rb: -------------------------------------------------------------------------------- 1 | require 'rubygems' 2 | require 'talib_ruby' 3 | 4 | # init input data 5 | a = Array.new 6 | 10.times { |i| a.push i.to_f } 7 | 8 | h = (7..17).inject([]) { |list, i| list << i } 9 | l = (3..15).inject([]) { |list, i| list << i } 10 | c = (6..16).inject([]) { |list, i| list << i } 11 | 12 | 5.times do |k| 13 | 14 | b = Array.new(10) 15 | ta = TaLib::Function.new("ADX") 16 | # setup input price 17 | # open = nil, volume = nil, open_interest = nil 18 | ta.in_price(0, nil, h, l, c, nil, nil); 19 | # setup optional parameter 20 | ta.opt_int(0,k+2); 21 | # setup output parameter 22 | ta.out_real(0,b); 23 | ta.call(0,9) 24 | p "k=#{k+2}" 25 | p b 26 | end 27 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | begin 2 | require 'jeweler' 3 | Jeweler::Tasks.new do |gemspec| 4 | gemspec.name = "talib_ruby" 5 | gemspec.summary = "Ruby Wrapper for ta-lib" 6 | gemspec.description = "Ruby Wrapper for the Technical Analysis Library ta-lib" 7 | gemspec.email = "rivella50@gmail.com" 8 | gemspec.homepage = "http://github.com/rivella50/talib-ruby" 9 | gemspec.authors = ["Valentin Treu"] 10 | gemspec.extensions << 'ext/talib/extconf.rb' 11 | gemspec.version = '1.0.6' 12 | gemspec.files = FileList['example/ma.rb','ext/talib/talib.c', 'README.rdoc','lib/**/*.rb'] 13 | gemspec.test_files = [] 14 | end 15 | Jeweler::GemcutterTasks.new 16 | rescue LoadError 17 | puts "Jeweler not available. Install it with: sudo gem install jeweler" 18 | end 19 | -------------------------------------------------------------------------------- /talib_ruby.gemspec: -------------------------------------------------------------------------------- 1 | # Generated by jeweler 2 | # DO NOT EDIT THIS FILE DIRECTLY 3 | # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' 4 | # -*- encoding: utf-8 -*- 5 | 6 | Gem::Specification.new do |s| 7 | s.name = %q{talib_ruby} 8 | s.version = "1.0.5" 9 | 10 | s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= 11 | s.authors = ["Valentin Treu"] 12 | s.date = %q{2013-04-07} 13 | s.description = %q{Ruby Wrapper for the Technical Analysis Library ta-lib} 14 | s.email = %q{rivella50@gmail.com} 15 | s.extensions = ["ext/talib/extconf.rb", "ext/talib/extconf.rb"] 16 | s.extra_rdoc_files = [ 17 | "README.textile" 18 | ] 19 | s.files = [ 20 | "example/ma.rb", 21 | "ext/talib/talib.c", 22 | "lib/talib_ruby.rb" 23 | ] 24 | s.homepage = %q{http://github.com/rivella50/talib-ruby} 25 | s.require_paths = ["lib"] 26 | s.rubygems_version = %q{1.3.6} 27 | s.summary = %q{Ruby Wrapper for ta-lib} 28 | 29 | if s.respond_to? :specification_version then 30 | current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION 31 | s.specification_version = 3 32 | 33 | if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then 34 | else 35 | end 36 | else 37 | end 38 | end 39 | 40 | -------------------------------------------------------------------------------- /README.textile: -------------------------------------------------------------------------------- 1 | h2. talib-ruby 2 | 3 | Ruby Wrapper for "ta-lib":http://ta-lib.org/ 4 | 5 | This project has been started by Timur Adigamov on "Rubyforge":http://rubyforge.org/projects/talib-ruby/, 6 | but since it didn't build on my machine and wasn't complete i modified it slightly, et voila. 7 | 8 | h4. Important note for commit id "11277315208a8bfcfd2a3211e10b5732d6705fc7":https://github.com/rivella50/talib-ruby/commit/11277315208a8bfcfd2a3211e10b5732d6705fc7 and gem version 1.0.6 9 | 10 | From now on all returned lists from talib_ruby now correspond to what ta-lib returns, i.e. there will be no longer leading nil values in any outXYZ lists. 11 | The nil values will now be trailing nil values if your inserted outXYZ lists are too large (let's say you always insert out arrays with the same length as the inXYZ lists). 12 | Therefore the returned value outBegIdx is no more relevant for the outXYZ lists, since the first calculated value can be found right at index 0 in the outXYZ lists. 13 | All calculated values are still the same, it's only a shifting issue which has been corrected now. 14 | Thanks go to Masayuki Yamamoto who showed me this. 15 | 16 | h3. Install and build instructions 17 | 18 | h4. Install ta-lib: 19 | 20 | Install ta-lib from "MacPorts":http://www.macports.org/ports.php?by=name&substr=ta-lib: 21 | 22 | bc. sudo port install ta-lib 23 | 24 | Or install ta-lib from "Homebrew":https://github.com/mxcl/homebrew/blob/master/Library/Formula/ta-lib.rb: 25 | 26 | bc. sudo brew install ta-lib 27 | 28 | If you want to compile ta-lib from the sources ("ta-lib.org":http://www.ta-lib.org/hdr_dw.html) you have to use the following statements (thanks to rmldj): 29 | 30 | bc. ./configure LDFLAGS="-lm" 31 | make 32 | sudo make install 33 | 34 | h4. Install the ruby wrapper talib_ruby: 35 | 36 | bc. sudo env ARCHFLAGS="-arch PLATFORM" gem install talib_ruby -- --with-talib-include=ABSOLUTE_PATH_TO_TALIB_HEADERS --with-talib-lib=ABSOLUTE_PATH_TO_TALIB_LIBS 37 | 38 | * PLATFORM = [i386 | x86_64 | ...] 39 | * ABSOLUTE_PATH_TO_TALIB_HEADERS = The path to the ta-lib header files 40 | ** e.g. for Port: /opt/local/var/macports/software/ta-lib/0.4.0_0/opt/local/include 41 | ** e.g. for Homebrew: /usr/local/Cellar/ta-lib/0.4.0/include 42 | * ABSOLUTE_PATH_TO_TALIB_LIBS = The path to the ta-lib lib files 43 | ** e.g. fo Port: /opt/local/var/macports/software/ta-lib/0.4.0_0/opt/local/lib 44 | ** e.g. fo Homebrew: /usr/local/Cellar/ta-lib/0.4.0/lib 45 | 46 | Now ta-lib can be used by using _require 'talib_ruby'_ 47 | Needs at least Mac OSX 10.5. Has not been tested on Windows, but works there too what i've heard. 48 | Tested with Ruby 1.8.7, 1.9.3 and 2.0.0. 49 | 50 | h3. Example 51 | 52 | Calculation of Moving Average (MA): 53 | 54 | bc.. require 'rubygems' 55 | require 'talib_ruby' 56 | 57 | # init input data 58 | a = Array.new 59 | 10.times { |i| a.push i.to_f } 60 | 61 | 10.times do |k| 62 | b = Array.new(10) 63 | l = TaLib::Function.new("MA") 64 | # setup input parameter 65 | l.in_real(0,a) 66 | # setup optional parameter 67 | l.opt_int(0,k+2) 68 | # setup output parameter 69 | l.out_real(0,b) 70 | lookback = l.lookback 71 | l.call(0,9) 72 | p "k=#{k+2}" 73 | p b 74 | end 75 | 76 | h3. Useful links 77 | 78 | * "TA-Lib Forum":http://www.tadoc.org/forum/index.php?board=9.0 79 | * "XML explanation":http://ta-lib.svn.sourceforge.net/viewvc/ta-lib/trunk/ta-lib/ta_func_api.xml?view=markup of all supported Functions 80 | * "C/C++ API Documentation":http://ta-lib.org/d_api/d_api.html 81 | 82 | h3. Donate 83 | 84 | If you think this library is useful i would be grateful for a donation. 85 | !https://www.paypalobjects.com/en_US/i/btn/btn_donate_LG.gif!:https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=NCX9EKVWBTBBJ 86 | 87 | h3. License 88 | 89 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 90 | 91 | Copyright (c) 2013 by Valentin Treu, released under the MIT license. -------------------------------------------------------------------------------- /ext/talib/talib.c: -------------------------------------------------------------------------------- 1 | #include "ruby.h" 2 | #include 3 | 4 | static VALUE rb_mTaLib; 5 | static VALUE rb_cTAFunction; 6 | static VALUE rb_sInParamInfo; 7 | static VALUE rb_sOptInParamInfo; 8 | static VALUE rb_sOutParamInfo; 9 | 10 | #ifndef RUBY_19 11 | #ifndef RARRAY_LEN 12 | #define RARRAY_LEN(v) (RARRAY(v)->len) 13 | #endif 14 | #ifndef RARRAY_PTR 15 | #define RARRAY_PTR(v) (RARRAY(v)->ptr) 16 | #endif 17 | #ifndef RSTRING_PTR 18 | #define RSTRING_PTR(v) (RSTRING(v)->ptr) 19 | #endif 20 | #endif 21 | 22 | 23 | #define TA_INPUT_PARAM 1 24 | #define TA_OPTION_INPUT_PARAM 2 25 | #define TA_OUTPUT_PARAM 3 26 | #define IN_CNT 7 // allow up to 7 arrays of input 27 | #define OUT_CNT 3 // allow up to 3 arrays of outputs 28 | // combine all heap storage to this struct and free only this on ta_free 29 | typedef struct _ph { 30 | TA_ParamHolder *p; 31 | double* in[IN_CNT]; // johnribera@Hotmail: the usual case (double) 32 | double* out[OUT_CNT]; 33 | } ParamHolder; 34 | 35 | /* :nodoc: */ 36 | // get TA_FuncInfo by name 37 | static const TA_FuncInfo* abstract_layer_get_func_info(VALUE name) 38 | { 39 | TA_RetCode ret_code; 40 | const TA_FuncHandle *handle; 41 | const TA_FuncInfo *the_info; 42 | 43 | ret_code = TA_GetFuncHandle( RSTRING_PTR(name), &handle ); 44 | if ( ret_code == TA_SUCCESS ) 45 | { 46 | ret_code = TA_GetFuncInfo( handle, &the_info ); 47 | if ( ret_code == TA_SUCCESS ) 48 | return the_info; 49 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetFuncInfo"); 50 | } 51 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetFuncHandle"); 52 | } 53 | 54 | // get function parameter info 55 | static VALUE ta_func_param_info(int param_type, VALUE self, VALUE name, VALUE index) 56 | { 57 | TA_RetCode ret_code; 58 | const TA_FuncHandle *handle; 59 | 60 | ret_code = TA_GetFuncHandle( StringValuePtr(name), &handle ); 61 | if ( ret_code == TA_SUCCESS ) 62 | { 63 | switch (param_type) 64 | { 65 | case TA_INPUT_PARAM: 66 | { 67 | const TA_InputParameterInfo *param_info; 68 | 69 | ret_code = TA_GetInputParameterInfo( handle, FIX2INT(index), ¶m_info ); 70 | if (ret_code != TA_SUCCESS) 71 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetInputParameterInfo"); 72 | return rb_struct_new(rb_sInParamInfo, INT2FIX(param_info->type), rb_str_new2(param_info->paramName), INT2FIX(param_info->flags),NULL); 73 | } 74 | break; 75 | case TA_OPTION_INPUT_PARAM: 76 | { 77 | const TA_OptInputParameterInfo *param_info; 78 | 79 | ret_code = TA_GetOptInputParameterInfo( handle, FIX2INT(index), ¶m_info ); 80 | if (ret_code != TA_SUCCESS) 81 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetOptInputParameterInfo"); 82 | // FIXME: helpFile = Qnil 83 | // FIXME: dataSet = Qnil 84 | return rb_struct_new(rb_sOptInParamInfo, INT2FIX(param_info->type), rb_str_new2(param_info->paramName), INT2FIX(param_info->flags), rb_str_new2(param_info->displayName), Qnil, rb_float_new(param_info->defaultValue), rb_str_new2(param_info->hint), Qnil, NULL); 85 | } 86 | break; 87 | case TA_OUTPUT_PARAM: 88 | { 89 | const TA_OutputParameterInfo *param_info; 90 | 91 | ret_code = TA_GetOutputParameterInfo( handle, FIX2INT(index), ¶m_info ); 92 | if (ret_code != TA_SUCCESS) 93 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetOutputParameterInfo"); 94 | return rb_struct_new(rb_sOutParamInfo, INT2FIX(param_info->type), rb_str_new2(param_info->paramName), INT2FIX(param_info->flags),NULL); 95 | } 96 | break; 97 | } // switch 98 | } 99 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetFuncHandle"); 100 | } 101 | 102 | static double* FLT2DBL(double **dest, VALUE in_array) 103 | { 104 | VALUE *inp; 105 | int i; 106 | 107 | if(NIL_P(in_array)) return 0; 108 | 109 | inp = RARRAY_PTR(in_array); 110 | if (*dest) free(*dest); // only on 1st use do we skip this step 111 | *dest = calloc(1, sizeof(double) * RARRAY_LEN(in_array)); 112 | for (i = 0; i < RARRAY_LEN(in_array); i++) 113 | (*dest)[i] = NUM2DBL(inp[i]); 114 | return *dest; 115 | } 116 | 117 | void init_tables() 118 | { 119 | // temporary variable return code 120 | TA_RetCode ret_code; 121 | // functions group table 122 | TA_StringTable *group_table; 123 | // define class variable @@groups - array of function groups 124 | // define class variable @@functions - hash of array of functions 125 | ret_code = TA_GroupTableAlloc( &group_table ); 126 | if( ret_code == TA_SUCCESS ) 127 | { 128 | // functions table 129 | TA_StringTable *function_table; 130 | // ruby array for functions group table 131 | VALUE rb_group_table = rb_ary_new(); 132 | // ruby hash for functions 133 | VALUE rb_function_table = rb_hash_new(); 134 | unsigned i, j; 135 | 136 | for( i=0; i < group_table->size; i++ ) 137 | { 138 | // temporary ruby array for group's function table 139 | VALUE rb_temp_func_table = rb_ary_new(); 140 | 141 | rb_ary_push(rb_group_table, rb_str_new2(group_table->string[i])); 142 | ret_code = TA_FuncTableAlloc( group_table->string[i], &function_table ); 143 | if( ret_code == TA_SUCCESS ) 144 | { 145 | for ( j=0; j < function_table->size; j++) 146 | rb_ary_push(rb_temp_func_table, rb_str_new2(function_table->string[j])); 147 | rb_hash_aset(rb_function_table, rb_str_new2(group_table->string[i]), rb_temp_func_table); 148 | TA_FuncTableFree ( function_table ); 149 | } 150 | } 151 | rb_define_class_variable( rb_cTAFunction, "@@groups", rb_group_table ); 152 | rb_define_class_variable( rb_cTAFunction, "@@functions", rb_function_table ); 153 | TA_GroupTableFree( group_table ); 154 | } 155 | } 156 | 157 | // free function 158 | static void ta_func_free(void *phv) 159 | { 160 | int i = 0; // this now properly cleans up memory from very last call 161 | ParamHolder* ph = (ParamHolder*)phv; 162 | for (i = 0; i < IN_CNT; i++) if (ph->in[i]) free(ph->in[i]); 163 | for (i = 0; i < OUT_CNT; i++) if (ph->out[i]) free(ph->out[i]); 164 | TA_ParamHolderFree( ph->p ); 165 | free(ph); 166 | } 167 | 168 | // allocation function 169 | static VALUE ta_func_alloc(VALUE klass) 170 | { 171 | ParamHolder *param_holder; 172 | // call calloc() to ensure we set all values (ptrs to NULL/0) 173 | // so if (free(in/out)) returns false only the first time! 174 | param_holder = (ParamHolder*)calloc(1, sizeof(struct _ph)); 175 | return Data_Wrap_Struct(klass, 0, ta_func_free, param_holder); 176 | } 177 | 178 | /* 179 | * call-seq: initialize(name) 180 | * 181 | * Create new instance of technical analysis function with given _name_. 182 | * 183 | */ 184 | static VALUE ta_func_initialize(VALUE self, VALUE name) 185 | { 186 | TA_RetCode ret_code; 187 | const TA_FuncHandle *handle; 188 | TA_ParamHolder *ta_param_holder; 189 | ParamHolder *param_holder; 190 | 191 | Data_Get_Struct(self, ParamHolder, param_holder); 192 | 193 | ret_code = TA_GetFuncHandle( RSTRING_PTR(name), &handle ); 194 | if ( ret_code == TA_SUCCESS ) 195 | { 196 | ret_code = TA_ParamHolderAlloc( handle, &ta_param_holder ); 197 | if ( ret_code != TA_SUCCESS ) 198 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_ParamHolderAlloc"); 199 | param_holder->p = ta_param_holder; 200 | rb_iv_set(self, "@name", name); 201 | rb_iv_set(self, "@result", rb_ary_new()); 202 | return self; 203 | } 204 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetFuncHandle"); 205 | } 206 | 207 | /* 208 | * Return input parameters count. 209 | */ 210 | static VALUE ta_func_get_input_count(VALUE self) 211 | { 212 | return INT2NUM((abstract_layer_get_func_info(rb_iv_get(self, "@name")))->nbInput); 213 | } 214 | 215 | /* 216 | * Return option input parameters count. 217 | */ 218 | static VALUE ta_func_get_option_input_count(VALUE self) 219 | { 220 | return INT2NUM((abstract_layer_get_func_info(rb_iv_get(self, "@name")))->nbOptInput); 221 | } 222 | 223 | /* 224 | * Return output parameters count. 225 | */ 226 | static VALUE ta_func_get_output_count(VALUE self) 227 | { 228 | return INT2NUM((abstract_layer_get_func_info(rb_iv_get(self, "@name")))->nbOutput); 229 | } 230 | 231 | /* 232 | * Return the list of names for technical function groups. 233 | */ 234 | static VALUE ta_func_get_groups(VALUE self) 235 | { 236 | return rb_cv_get(self, "@@groups"); 237 | } 238 | 239 | /* 240 | * Return the hash of names for technical functions. 241 | * The key is a name of funcion group, and value - is a list of function names. 242 | */ 243 | static VALUE ta_func_get_functions(VALUE self) 244 | { 245 | return rb_cv_get(self, "@@functions"); 246 | } 247 | 248 | /* 249 | * call-seq: in(index) 250 | * 251 | * Return input parameter info for the given input parameter index. 252 | */ 253 | static VALUE ta_func_input_param_info(VALUE self, VALUE index) 254 | { 255 | return ta_func_param_info(TA_INPUT_PARAM, self, rb_iv_get(self, "@name"), index); 256 | } 257 | 258 | /* 259 | * call-seq: opt(index) 260 | * 261 | * Return option input parameter info for the given option parameter index. 262 | */ 263 | static VALUE ta_func_option_param_info(VALUE self, VALUE index) 264 | { 265 | return ta_func_param_info(TA_OPTION_INPUT_PARAM, self, rb_iv_get(self, "@name"), index); 266 | } 267 | 268 | /* 269 | * call-seq: out(index) 270 | * 271 | * Return output parameter info for the given output parameter index. 272 | */ 273 | static VALUE ta_func_output_param_info(VALUE self, VALUE index) 274 | { 275 | return ta_func_param_info(TA_OUTPUT_PARAM, self, rb_iv_get(self, "@name"), index); 276 | } 277 | 278 | /* 279 | * call-seq: in_int(index, array) 280 | * 281 | * Set input parameter (array of integer) for the given parameter index. 282 | */ 283 | static VALUE ta_func_setup_in_integer(VALUE self, VALUE param_index, VALUE in_array) 284 | { 285 | TA_RetCode ret_code; 286 | ParamHolder *param_holder; 287 | 288 | Data_Get_Struct(self, ParamHolder, param_holder); 289 | ret_code = TA_SetInputParamIntegerPtr( param_holder->p, FIX2INT(param_index), (int*)(RARRAY_PTR(in_array))); 290 | if ( ret_code != TA_SUCCESS ) 291 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetInputParamIntegerPtr"); 292 | 293 | return T_TRUE; 294 | } 295 | 296 | /* 297 | * call-seq: in_real(index, array) 298 | * 299 | * Set input parameter (array of real) for the given parameter index. 300 | */ 301 | static VALUE ta_func_setup_in_real(VALUE self, VALUE param_index, VALUE in_array) 302 | { 303 | double** dp; 304 | TA_RetCode ret_code; 305 | ParamHolder *param_holder; 306 | 307 | Data_Get_Struct(self, ParamHolder, param_holder); 308 | dp = param_holder->in; 309 | //FIXME: memory leak fixed: johnribera@hotmail.com (see: FL2DBL()) 310 | ret_code = TA_SetInputParamRealPtr( param_holder->p, FIX2INT(param_index), FLT2DBL(&dp[FIX2INT(param_index)], in_array)); 311 | if ( ret_code != TA_SUCCESS ) 312 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetInputParamRealPtr"); 313 | return T_TRUE; 314 | } 315 | 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) 317 | { 318 | double **dp; 319 | TA_RetCode ret_code; 320 | ParamHolder *param_holder; 321 | Data_Get_Struct(self, ParamHolder, param_holder); 322 | dp = param_holder->in; 323 | 324 | ret_code = TA_SetInputParamPricePtr( 325 | param_holder->p, 326 | FIX2INT(param_index), 327 | FLT2DBL(&dp[0], in_open), 328 | FLT2DBL(&dp[1], in_high), 329 | FLT2DBL(&dp[2], in_low), 330 | FLT2DBL(&dp[3], in_close), 331 | FLT2DBL(&dp[4], in_volume), 332 | FLT2DBL(&dp[5], in_oi) 333 | ); 334 | if ( ret_code != TA_SUCCESS ) 335 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetInputParamPricePtr"); 336 | 337 | return T_TRUE; 338 | } 339 | 340 | /* 341 | * call-seq: opt_int(index, value) 342 | * 343 | * Set option parameter (integer) for the given parameter index. 344 | */ 345 | static VALUE ta_func_setup_opt_in_integer(VALUE self, VALUE param_index, VALUE val) 346 | { 347 | TA_RetCode ret_code; 348 | ParamHolder *param_holder; 349 | Data_Get_Struct(self, ParamHolder, param_holder); 350 | ret_code = TA_SetOptInputParamInteger( param_holder->p, FIX2INT(param_index), FIX2INT(val)); 351 | if ( ret_code != TA_SUCCESS ) 352 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetOptInputParamIntegerPtr"); 353 | 354 | return val; 355 | } 356 | 357 | /* 358 | * call-seq: opt_real(index, value) 359 | * 360 | * Set option parameter (real) for the given parameter index. 361 | */ 362 | static VALUE ta_func_setup_opt_in_real(VALUE self, VALUE param_index, VALUE val) 363 | { 364 | TA_RetCode ret_code; 365 | ParamHolder *param_holder; 366 | 367 | Data_Get_Struct(self, ParamHolder, param_holder); 368 | ret_code = TA_SetOptInputParamReal( param_holder->p, FIX2INT(param_index), NUM2DBL(val)); 369 | if ( ret_code != TA_SUCCESS ) 370 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetOptInputParamRealPtr"); 371 | 372 | return val; 373 | } 374 | 375 | /* 376 | * call-seq: out_real(index, array) 377 | * 378 | * Set output parameter (array of real) for the given parameter index. 379 | */ 380 | static VALUE ta_func_setup_out_real(VALUE self, VALUE param_index, VALUE out_array) 381 | { 382 | TA_RetCode ret_code; 383 | ParamHolder *param_holder; 384 | long idx = FIX2INT(param_index); 385 | double **dp; 386 | if (idx > 2) 387 | rb_raise(rb_eRuntimeError, "param_index must be 0..2"); 388 | 389 | Data_Get_Struct(self, ParamHolder, param_holder); 390 | rb_ary_store(rb_iv_get(self, "@result"), idx, out_array); 391 | // FIXME: malloc w/o free: johnribera@hotmail.com fixed 392 | dp = &(param_holder->out[idx]); 393 | if (*dp) free(*dp); // not true only 1st time called (reusing same ptrs) 394 | 395 | *dp = (double*)malloc(RARRAY_LEN(out_array) * sizeof(double)); 396 | ret_code = TA_SetOutputParamRealPtr(param_holder->p, idx, *dp); 397 | 398 | if ( ret_code != TA_SUCCESS ) 399 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetOutputParamRealPtr"); 400 | 401 | return out_array; 402 | } 403 | 404 | static VALUE ta_func_setup_out_integer(VALUE self, VALUE param_index, VALUE out_array) 405 | { 406 | TA_RetCode ret_code; 407 | ParamHolder *param_holder; 408 | long idx = FIX2INT(param_index); 409 | int **ip; 410 | if (idx > 2) 411 | rb_raise(rb_eRuntimeError, "param_index must be 0..2"); 412 | 413 | Data_Get_Struct(self, ParamHolder, param_holder); 414 | rb_ary_store(rb_iv_get(self, "@result"), idx, out_array); 415 | 416 | // FIXME: malloc w/o free FIXED: johnribera@Hotmail.com 417 | ip = (int**)&(param_holder->out[idx]); 418 | if (*ip) free(*ip); // not true only very 1st time in 419 | *ip = (int*)malloc(RARRAY_LEN(out_array) * sizeof(int)); 420 | 421 | ret_code = TA_SetOutputParamIntegerPtr( param_holder->p, idx, *ip); 422 | if ( ret_code != TA_SUCCESS ) 423 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_SetOutputParamIntegerPtr"); 424 | 425 | return out_array; 426 | } 427 | 428 | /* 429 | * call-seq: call(in_start, in_end) 430 | * 431 | * Call technical function with input data from in_start..in_end. 432 | */ 433 | static VALUE ta_func_call(VALUE self, VALUE in_start, VALUE in_end) 434 | { 435 | TA_RetCode ret_code; 436 | ParamHolder *param_holder; 437 | TA_Integer out_start, out_num; 438 | VALUE ary, sub_ary; 439 | int i,j; 440 | 441 | Data_Get_Struct(self, ParamHolder, param_holder); 442 | ret_code = TA_CallFunc( param_holder->p, FIX2INT(in_start), FIX2INT(in_end), &out_start, &out_num); 443 | if ( ret_code != TA_SUCCESS ) 444 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_CallFunc"); 445 | ary = rb_iv_get(self, "@result"); 446 | for (i = 0; iout[i])[j]; 453 | rb_ary_store(sub_ary, j, rb_float_new(el)); 454 | } 455 | } 456 | return rb_ary_new3(2, INT2FIX(out_start), INT2FIX(out_num)); 457 | } 458 | 459 | static VALUE ta_func_lookback(VALUE self) 460 | { 461 | TA_RetCode ret_code; 462 | ParamHolder *param_holder; 463 | TA_Integer out_lookback; 464 | 465 | Data_Get_Struct(self, ParamHolder, param_holder); 466 | ret_code = TA_GetLookback(param_holder->p, &out_lookback); 467 | if ( ret_code != TA_SUCCESS ) 468 | rb_raise(rb_eRuntimeError, "unsuccess return code TA_GetLookback"); 469 | 470 | return INT2FIX(out_lookback); 471 | } 472 | 473 | void Init_talib() 474 | { 475 | 476 | /* 477 | * Ruby extension for technical functions library api. 478 | */ 479 | rb_mTaLib = rb_define_module("TaLib"); 480 | 481 | rb_define_const(rb_mTaLib, "TA_Input_Price", INT2FIX(TA_Input_Price)); 482 | rb_define_const(rb_mTaLib, "TA_Input_Real", INT2FIX(TA_Input_Real)); 483 | rb_define_const(rb_mTaLib, "TA_Input_Integer", INT2FIX(TA_Input_Integer)); 484 | rb_define_const(rb_mTaLib, "TA_IN_PRICE_OPEN", INT2FIX(TA_IN_PRICE_OPEN)); 485 | rb_define_const(rb_mTaLib, "TA_IN_PRICE_HIGH", INT2FIX(TA_IN_PRICE_HIGH)); 486 | rb_define_const(rb_mTaLib, "TA_IN_PRICE_LOW", INT2FIX(TA_IN_PRICE_LOW)); 487 | rb_define_const(rb_mTaLib, "TA_IN_PRICE_CLOSE", INT2FIX(TA_IN_PRICE_CLOSE)); 488 | rb_define_const(rb_mTaLib, "TA_IN_PRICE_VOLUME", INT2FIX(TA_IN_PRICE_VOLUME)); 489 | rb_define_const(rb_mTaLib, "TA_IN_PRICE_OPENINTEREST", INT2FIX(TA_IN_PRICE_OPENINTEREST)); 490 | rb_define_const(rb_mTaLib, "TA_IN_PRICE_TIMESTAMP", INT2FIX(TA_IN_PRICE_TIMESTAMP)); 491 | rb_define_const(rb_mTaLib, "TA_OptInput_RealRange", INT2FIX(TA_OptInput_RealRange)); 492 | rb_define_const(rb_mTaLib, "TA_OptInput_RealList", INT2FIX(TA_OptInput_RealList)); 493 | rb_define_const(rb_mTaLib, "TA_OptInput_IntegerRange", INT2FIX(TA_OptInput_IntegerRange)); 494 | rb_define_const(rb_mTaLib, "TA_OptInput_IntegerList", INT2FIX(TA_OptInput_IntegerList)); 495 | rb_define_const(rb_mTaLib, "TA_OPTIN_IS_PERCENT", INT2FIX(TA_OPTIN_IS_PERCENT)); 496 | rb_define_const(rb_mTaLib, "TA_OPTIN_IS_DEGREE", INT2FIX(TA_OPTIN_IS_DEGREE)); 497 | rb_define_const(rb_mTaLib, "TA_OPTIN_IS_CURRENCY", INT2FIX(TA_OPTIN_IS_CURRENCY)); 498 | rb_define_const(rb_mTaLib, "TA_OPTIN_ADVANCED", INT2FIX(TA_OPTIN_ADVANCED)); 499 | rb_define_const(rb_mTaLib, "TA_Output_Real", INT2FIX(TA_Output_Real)); 500 | rb_define_const(rb_mTaLib, "TA_Output_Integer", INT2FIX(TA_Output_Integer)); 501 | rb_define_const(rb_mTaLib, "TA_OUT_LINE", INT2FIX(TA_OUT_LINE)); 502 | rb_define_const(rb_mTaLib, "TA_OUT_DOT_LINE", INT2FIX(TA_OUT_DOT_LINE)); 503 | rb_define_const(rb_mTaLib, "TA_OUT_DASH_LINE", INT2FIX(TA_OUT_DASH_LINE)); 504 | rb_define_const(rb_mTaLib, "TA_OUT_DOT", INT2FIX(TA_OUT_DOT)); 505 | rb_define_const(rb_mTaLib, "TA_OUT_HISTO", INT2FIX(TA_OUT_HISTO)); 506 | rb_define_const(rb_mTaLib, "TA_OUT_PATTERN_BOOL", INT2FIX(TA_OUT_PATTERN_BOOL)); 507 | rb_define_const(rb_mTaLib, "TA_OUT_PATTERN_BULL_BEAR", INT2FIX(TA_OUT_PATTERN_BULL_BEAR)); 508 | rb_define_const(rb_mTaLib, "TA_OUT_PATTERN_STRENGTH", INT2FIX(TA_OUT_PATTERN_STRENGTH)); 509 | rb_define_const(rb_mTaLib, "TA_OUT_POSITIVE", INT2FIX(TA_OUT_POSITIVE)); 510 | rb_define_const(rb_mTaLib, "TA_OUT_NEGATIVE", INT2FIX(TA_OUT_NEGATIVE)); 511 | rb_define_const(rb_mTaLib, "TA_OUT_ZERO", INT2FIX(TA_OUT_ZERO)); 512 | rb_define_const(rb_mTaLib, "TA_OUT_UPPER_LIMIT", INT2FIX(TA_OUT_UPPER_LIMIT)); 513 | rb_define_const(rb_mTaLib, "TA_OUT_LOWER_LIMIT", INT2FIX(TA_OUT_LOWER_LIMIT)); 514 | rb_define_const(rb_mTaLib, "TA_MAType_SMA", INT2FIX((int)(TA_MAType_SMA))); 515 | rb_define_const(rb_mTaLib, "TA_MAType_EMA", INT2FIX((int)(TA_MAType_EMA))); 516 | rb_define_const(rb_mTaLib, "TA_MAType_WMA", INT2FIX((int)(TA_MAType_WMA))); 517 | rb_define_const(rb_mTaLib, "TA_MAType_DEMA", INT2FIX((int)(TA_MAType_DEMA))); 518 | rb_define_const(rb_mTaLib, "TA_MAType_TEMA", INT2FIX((int)(TA_MAType_TEMA))); 519 | rb_define_const(rb_mTaLib, "TA_MAType_TRIMA", INT2FIX((int)(TA_MAType_TRIMA))); 520 | rb_define_const(rb_mTaLib, "TA_MAType_KAMA", INT2FIX((int)(TA_MAType_KAMA))); 521 | rb_define_const(rb_mTaLib, "TA_MAType_MAMA", INT2FIX((int)(TA_MAType_MAMA))); 522 | rb_define_const(rb_mTaLib, "TA_MAType_T3", INT2FIX((int)(TA_MAType_T3))); 523 | 524 | rb_struct_define("TA_RealRange", "min", "max", "precision", NULL); 525 | rb_struct_define("TA_IntegerRange", "min", "max", NULL ); 526 | rb_struct_define("TA_RealDataPair", "value", "string", NULL ); 527 | rb_struct_define("TA_IntegerDataPair", "value", "string", NULL ); 528 | rb_struct_define("TA_RealList", "data", "nb_element", NULL ); 529 | rb_struct_define("TA_IntegerList", "data", "nb_element", NULL ); 530 | 531 | rb_sInParamInfo = rb_struct_define("TA_InputParameterInfo", "type", "param_name", "flags", NULL ); 532 | rb_sOptInParamInfo = rb_struct_define("TA_OptInputParameterInfo", "type", "param_name", "flags", "display_name", "data_set", "default_value", "hint", "help_file", NULL ); 533 | rb_sOutParamInfo = rb_struct_define("TA_OutputParameterInfo", "type", "param_name", "flags", NULL ); 534 | 535 | /* 536 | * Class for technical analysis function. 537 | */ 538 | rb_cTAFunction = rb_define_class_under(rb_mTaLib, "Function", rb_cObject); 539 | rb_define_alloc_func(rb_cTAFunction, ta_func_alloc); 540 | rb_define_method(rb_cTAFunction, "initialize", ta_func_initialize, 1); 541 | rb_define_attr(rb_cTAFunction, "result", 1, 1); 542 | 543 | rb_define_module_function( rb_cTAFunction, "groups", ta_func_get_groups, 0 ); 544 | rb_define_module_function( rb_cTAFunction, "functions", ta_func_get_functions, 0 ); 545 | 546 | rb_define_method( rb_cTAFunction, "ins", ta_func_get_input_count, 0 ); 547 | rb_define_method( rb_cTAFunction, "outs", ta_func_get_output_count, 0 ); 548 | rb_define_method( rb_cTAFunction, "opts", ta_func_get_option_input_count, 0 ); 549 | 550 | rb_define_method( rb_cTAFunction, "in", ta_func_input_param_info, 1); 551 | rb_define_method( rb_cTAFunction, "opt", ta_func_option_param_info, 1); 552 | rb_define_method( rb_cTAFunction, "out", ta_func_output_param_info, 1); 553 | 554 | rb_define_method( rb_cTAFunction, "in_int", ta_func_setup_in_integer, 2); 555 | rb_define_method( rb_cTAFunction, "in_real", ta_func_setup_in_real, 2); 556 | rb_define_method( rb_cTAFunction, "in_price", ta_func_setup_in_price, 7); 557 | rb_define_method( rb_cTAFunction, "opt_int", ta_func_setup_opt_in_integer, 2); 558 | rb_define_method( rb_cTAFunction, "opt_real", ta_func_setup_opt_in_real, 2); 559 | rb_define_method( rb_cTAFunction, "out_int", ta_func_setup_out_integer, 2); 560 | rb_define_method( rb_cTAFunction, "out_real", ta_func_setup_out_real, 2); 561 | 562 | rb_define_method( rb_cTAFunction, "lookback", ta_func_lookback, 0); 563 | rb_define_method( rb_cTAFunction, "call", ta_func_call, 2); 564 | } 565 | --------------------------------------------------------------------------------