Class: Indicators::Main

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

Overview

Moving averages

Defined Under Namespace

Classes: MainException

Class Method Summary collapse

Class Method Details

.calculate(data, parameters) ⇒ Object



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
# File 'lib/indicators/main.rb', line 10

def self.calculate data, parameters
			if data.is_a?(Hash)
    results = 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
        results[symbol] = []
      else
        results[symbol] = case parameters[:type]
                            when :sma then sma(Indicators::Parser.parse_data(stock_data), parameters[:variables])
                            when :ema then ema(Indicators::Parser.parse_data(stock_data), parameters[:variables])
                            when :bb then bb(Indicators::Parser.parse_data(stock_data), parameters[:variables])
                            when :macd then macd(Indicators::Parser.parse_data(stock_data), parameters[:variables])
                            when :rsi then rsi(Indicators::Parser.parse_data(stock_data), parameters[:variables])
                            when :sto then sto(Indicators::Parser.parse_data(stock_data), parameters[:variables])
                          end
        # Parser returns in {:date=>[2012.0, 2012.0, 2012.0], :open=>[409.4, 410.0, 414.95],} format
      end
    end
			else
results = case parameters[:type]
            when :sma then sma(data, parameters[:variables])
                when :ema then ema(data, parameters[:variables])
                when :bb then bb(data, parameters[:variables])
                when :macd then macd(data, parameters[:variables])
                when :rsi then rsi(data, parameters[:variables])
                when :sto then raise MainException, "You cannot calculate Stochastic Oscillator on closing prices. Use Securities hash instead."
          end
			end
			return results
end