Class: Indicators::Main

Inherits:
Object
  • Object
show all
Defined in:
lib/indicators/main.rb

Defined Under Namespace

Classes: MainException

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, parameters) ⇒ Main



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/indicators/main.rb', line 9

def initialize data, parameters
  type = parameters[:type]
  all_params = parameters[:params]
  @abbr = type.to_s.upcase
  case type
  when :sma
    @params = Indicators::Helper.get_parameters(all_params, 0, 20)
  when :ema
    @params = Indicators::Helper.get_parameters(all_params, 0, 20)
  when :bb
    @params = Array.new
    @params << Indicators::Helper.get_parameters(all_params, 0, 20)
    @params << Indicators::Helper.get_parameters(all_params, 1, 2)
  when :macd
    @params = Array.new
    @params << Indicators::Helper.get_parameters(all_params, 0, 12)
    @params << Indicators::Helper.get_parameters(all_params, 1, 26)
    @params << Indicators::Helper.get_parameters(all_params, 2, 9)
  when :rsi
    @params = Indicators::Helper.get_parameters(all_params, 0, 14)
  when :sto
    @params = Array.new
    @params << Indicators::Helper.get_parameters(all_params, 0, 14)
    @params << Indicators::Helper.get_parameters(all_params, 1, 3)
    @params << Indicators::Helper.get_parameters(all_params, 2, 3)
  end

  # This is a Securities gem hash.
      if data[0].is_a?(Hash)
    data = Indicators::Parser.parse_data(data)
    # Parser returns in {:date=>[2012.0, 2012.0, 2012.0], :open=>[409.4, 410.0, 414.95],} format
      else
    # Don't let calculation start on a standard array for few instruments cause it needs more specific data.
    if type == :sto
      raise MainException, 'You cannot calculate Stochastic Oscillator on array. Highs and lows are needed. Feel free Securities gem hash instead.'
    end
  end
      @output = case type
           when :sma then Indicators::Sma.calculate(data, @params)
              when :ema then Indicators::Ema.calculate(data, @params)
              when :bb then Indicators::Bb.calculate(data, @params)
              when :macd then Indicators::Macd.calculate(data, @params)
              when :rsi then Indicators::Rsi.calculate(data, @params)
              when :sto then Indicators::Sto.calculate(data, @params)
         end
  return @output
end

Instance Attribute Details

#abbrObject (readonly)

Returns the value of attribute abbr.



5
6
7
# File 'lib/indicators/main.rb', line 5

def abbr
  @abbr
end

#outputObject (readonly)

Returns the value of attribute output.



5
6
7
# File 'lib/indicators/main.rb', line 5

def output
  @output
end

#paramsObject (readonly)

Returns the value of attribute params.



5
6
7
# File 'lib/indicators/main.rb', line 5

def params
  @params
end