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

Returns a new instance of Main.



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
56
57
58
59
60
61
62
63
64
65
# File 'lib/indicators/main.rb', line 10

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

			if data.is_a?(Hash)
    @output = Hash.new
    data.each do |symbol, stock_data|
      # Check if this symbol was empty and don't go further with it.
      if stock_data.length == 0
        @output[symbol] = []
      else
        @output[symbol] = case type
                            when :sma then Indicators::Sma.calculate(Indicators::Parser.parse_data(stock_data), @params)
                            when :ema then Indicators::Ema.calculate(Indicators::Parser.parse_data(stock_data), @params)
                            when :bb then Indicators::Bb.calculate(Indicators::Parser.parse_data(stock_data), @params)
                            when :macd then Indicators::Macd.calculate(Indicators::Parser.parse_data(stock_data), @params)
                            when :rsi then Indicators::Rsi.calculate(Indicators::Parser.parse_data(stock_data), @params)
                            when :sto then Indicators::Sto.calculate(Indicators::Parser.parse_data(stock_data), @params)
                          end
        # Parser returns in {:date=>[2012.0, 2012.0, 2012.0], :open=>[409.4, 410.0, 414.95],} format
      end
    end
			else
@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 raise MainException, "You cannot calculate Stochastic Oscillator on array. Highs and lows are needed. Feel free Securities gem hash instead."
          end
			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