Class: TaLib::TAFunc

Inherits:
Function show all
Defined in:
lib/tafunc.rb

Overview

more Util extensions for TaLib::Function.

Constant Summary collapse

PPREFIX =

:nodoc:

"param_"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Function

function_exists?, function_find, group_of_function

Constructor Details

#initialize(func, arr_in: [], arr_out: []) {|_self| ... } ⇒ TAFunc

Args

func

The function name.

Yields:

  • (_self)

Yield Parameters:

  • _self (TaLib::TAFunc)

    the object that the method was called on



450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
# File 'lib/tafunc.rb', line 450

def initialize( func, arr_in: [], arr_out: [] )
  func = func.to_s if func.class == Symbol
  func_renamed = self.class.function_find( func )
  case
    when func.class != String
      raise "Type error for the function name: #{func.class}(#{func})!"+
            " This should be in String."
    when TaLib::Function.function_exists?( func ) == false
      raise "No such function: #{func}!"+
            " Choose one of"+
            " #{TaLib::Function.functions.values.flatten.join(' ')}."
  end

  #
  super( func_renamed )

  # for recording parameter setting.
  # { idx => { :val => some_val,
  #            :type => type_name, }
  #
  @param_in  = {}
  @param_opt = {}
  @param_out = {}

  # define method for the function: func.
  # for example, param_in_real, param_opt_in_fast_period methods, etc.
  #
  __define_ifmethods

  # this must be after __define_ifmethods because we want to use
  # generated interface methods in yield block.
  #
  yield self if block_given?

end

Instance Attribute Details

#param_inObject (readonly)

current parameter values of each object of Function. for @param_in, @param_opt, @param_out.

See Also

  • TaLib::Function#ifs_all/ifs_ins/ifs_opts/ifs_outs.

  • TaLib::TAFunc.#hints



492
493
494
# File 'lib/tafunc.rb', line 492

def param_in
  @param_in
end

#param_optObject (readonly)

current parameter values of each object of Function. for @param_in, @param_opt, @param_out.

See Also

  • TaLib::Function#ifs_all/ifs_ins/ifs_opts/ifs_outs.

  • TaLib::TAFunc.#hints



492
493
494
# File 'lib/tafunc.rb', line 492

def param_opt
  @param_opt
end

#param_outObject (readonly)

current parameter values of each object of Function. for @param_in, @param_opt, @param_out.

See Also

  • TaLib::Function#ifs_all/ifs_ins/ifs_opts/ifs_outs.

  • TaLib::TAFunc.#hints



492
493
494
# File 'lib/tafunc.rb', line 492

def param_out
  @param_out
end

Class Method Details

.hints(verbose: false, group: "all", function: "all") ⇒ Object

self.hints provides the information about TA-Lib function. The information of them are extracted from talib library itself.

ATTENTION

library (talib_ruby) must support TaLib::Function.groups,functions.

Args

verbose:

parameter name only when false, or entire structs when true.

group:

group name of functions.

function:

name of function.

Description

  • one of group or function should be specified.

  • these args can be String, Array of String, or “all”

*

Return



684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
# File 'lib/tafunc.rb', line 684

def self.hints( verbose: false, group: "all", function: "all" )
  group_list = nil
  case
    when group == "all"
      group_list = self.groups
    when group.class == String
      group_list = [group]
    when group.class == Array
      group_list = group
    else
      raise "Type error #{group.class} for group!"+
        " Please specify group in String or Array of String."
  end

  func_list = nil
  tmp = self.functions
  case
    when function == "all"
      func_list = tmp.values.flatten
    when function.class == Array
      func_list = function
      group_list = []
      func_list.each{|f|
        group_list.push( tmp.keys.map{|e|
          (tmp[e].grep(f).size>0)? e : nil }.compact )
      }
      group_list.flatten!
    when function.class == String
      func_list  = [function]
      group_list = tmp.keys.map{|e|
                     (tmp[e].grep(function).size>0)? e : nil
                   }.compact
    else
  end

  #
  group_list = group_list.sort.uniq
  func_list  = func_list.sort.uniq
  #pp group_list
  #pp func_list

  #
  group_list.each{|grp|
    puts "==== #{grp} ===="
    self.functions[grp].each{|func|
      #puts func
      if func_list.grep(func).size > 0
        tmp = self.new(func)

        puts "<#{func}>"
        puts "inputs:"
        if verbose
          then pp tmp.ifs_ins
          else tmp.ifs_ins.each{|e|
            puts PPREFIX+e.param_name.underscore }
        end
        puts ""

        puts "options:"
        if verbose
          then pp tmp.ifs_opts
          else tmp.ifs_opts.each{|e|
            puts PPREFIX+e.param_name.underscore }
        end
        puts ""

        puts "outputs:"
        if verbose
          then pp tmp.ifs_outs
          else tmp.ifs_outs.each{|e|
            puts PPREFIX+e.param_name.underscore }
        end
        puts ""

        puts ""
      end
    }
  }

end

Instance Method Details

#call(*r) ⇒ Object

wrap Function#call to accept various kinds of args.

Args

*r

range of input array in several ways: no args: from pram_in_* m, n: direct indexes m..n: range object array: array

Return

due to the function.



573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
# File 'lib/tafunc.rb', line 573

def call( *r )
  m, n = nil, nil

  # specifies m and n, simulation range in input data.
  case
    when r.size == 0  # no args.
      raise "No setting of param_in_* for #{name}!" if @param_in[0].nil?
      m, n = 0, @param_in[0][:val].size-1
    when r.first.class == Range   # Range is given.
      m, n = r.first.first, r.first.last
    when r.size == 2              # 2 indexes are given.
      #puts "couple of index."
      m, n = r.first, r.last
    when r.first.class == Array   # Array is given.
      #puts "array."
      self.param_in_real = r.first if @param_in[0].nil?
      m, n = 0, r.first.size-1
    else
      raise "Strange args: #{r}! Should be in one of Nothing,"+
            " two indexes, Array or Range."
  end

  #puts "idx: #{m}, #{n}"
  param_size = case @param_in[0][:type]
                 when :TA_Input_Price
                   [ @param_in[0][:val][:open],
                     @param_in[0][:val][:high],
                     @param_in[0][:val][:low],
                     @param_in[0][:val][:close],
                   ].map{|e| (e.nil?)? 0 : e.size }.max
                  when :TA_Input_Real
                    self.param_in_real.size
                  when :TA_Input_Integer
                    raise "Not yet implemented."
                  else
                    raise "Strange type for input parameter:"+
                      " #{@param_in[0][:type]}."
                  end

  case
    when m > n
      raise "calculation range(#{m},#{n}) is currently not supported!"
    when n >= param_size
      raise "#{n} is too big!"+
        " less than or equal to #{self.param_in_real.size-1}"
  end

  #
  tmp = super( m, n )
  ret = param_out_setting
  ret.merge!( { :start_idx => tmp[0], :num_elements => tmp[1], } )

  #
  return ret

end

#etype_attrObject



221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'lib/tafunc.rb', line 221

def etype_attr
  {
  "in"  => { 0 => "price",
             1 => "real",
             2 => "int", },
  "opt" => { 0 => "real",
             1 => "real",
             2 => "int",
             3 => "int", },
  "out" => { 0 => "real",
             1 => "int",  },
  }
end

#hints(verbose: false, group: "all", function: name) ⇒ Object

Wrapper of the class method: hints.

See Also

  • self.hints



666
667
668
# File 'lib/tafunc.rb', line 666

def hints( verbose: false, group: "all", function: name )
  self.class.hints( verbose: verbose, group: group, function: function )
end

#kObject

wrap the original in,opt,out_int,real,price to record values. For example,

def in_real( idx, val )
  __in_param_record( idx, val )
  in_real_orig(idx, val)
end


541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
# File 'lib/tafunc.rb', line 541

table_for_param.keys.each{|k|
  table_for_param[k].each{|v|
    if v == 'price'
      then define_method( k+"_"+v ) {|idx,val|
             # attention: val must be lvalue when out_*.
             eval("__param_#{k}_record( idx, val )")
             eval("#{k}_#{v}_orig(idx,"+
                  " val[:open], val[:high], val[:low], val[:close],"+
                  " val[:volume], val[:oi] )")
           }

      # real or int
      else define_method( k+"_"+v ) {|idx,val|
             # attention: val must be lvalue when out_*.
             eval("__param_#{k}_record( idx, val )")
             eval("#{k}_#{v}_orig(idx, val)")
           }
    end
  }
}

#param_attr(kind = "(in|opt|out)") ⇒ Object



440
441
442
# File 'lib/tafunc.rb', line 440

def param_attr( kind = "(in|opt|out)" )
  self.param_methods( kind ).grep(/[^=]$/)
end

#param_methods(kind = "(in|opt|out)") ⇒ Object

get defined singleton methods of param_in,opt,out_*.



436
437
438
439
# File 'lib/tafunc.rb', line 436

def param_methods( kind = "(in|opt|out)" )
  kind = kind.to_s if kind.class == Symbol
  self.singleton_methods.grep(/^param_#{kind}_.+$/)
end

#param_out_setting(h = {}, force_mode: false) ⇒ Object

auto prepare output arrays.

Requirements

all in-parameters have already been set.

Args

h

output hash to be set.

force_mode:

force to create new array for output (default: false)

Return

h

{ :output_parameter1 => [ nil, nil, … ],

:output_parameter2 => [ nil, nil, ... ], }

TODO

  • currently tested only MA, MACDEXT.



642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
# File 'lib/tafunc.rb', line 642

def param_out_setting( h = {}, force_mode: false )


  # get output attributes (arrays to prepare).
  tmp = self.param_attr( :out )

  # prepare arrays and set them.
  tmp.each{|a|
    #
    if force_mode or self.send( a ).nil?
      then h[a] = Array.new( self.param_in_real.size )
           self.send( (a.to_s+'=').to_sym, h[a] )
      else h[a] = self.send( a )  # call getter.
    end
  }

  return h
end