Class: Indicators::Data

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

Defined Under Namespace

Classes: DataException

Constant Summary collapse

INDICATORS =
[:sma, :ema, :bb, :macd, :rsi, :sto]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(parameters) ⇒ Data

Returns a new instance of Data.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/indicators/data.rb', line 10

def initialize parameters
	@data = parameters

	# Check if data usable.
	if @data.nil? || @data.empty?
      raise DataException, "There is no data to work on."
    end
	unless @data.is_a?(Array) or @data.is_a?(Hash)
		raise DataException, "Alien data. Given data must be an array or a hash (got #{@data.class})."
	end

	if @data.is_a?(Hash)
		# Hacky, but a fast way to check if this is a dividends hash without the extra hassle.
		raise DataException, "Cannot use dividends values for technical analysis." if @data.to_s.include?(':dividends')
	end
end

Instance Attribute Details

#dataObject (readonly)

Returns the value of attribute data.



4
5
6
# File 'lib/indicators/data.rb', line 4

def data
  @data
end

#resultsObject (readonly)

Returns the value of attribute results.



4
5
6
# File 'lib/indicators/data.rb', line 4

def results
  @results
end

Instance Method Details

#calc(parameters) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/indicators/data.rb', line 27

def calc parameters
	# Check is parameters are usable.
	unless parameters.is_a?(Hash)
		raise DataException, 'Given parameters have to be a hash. FORMAT: .calc(:type => :ema, :params => 12)'
	end

	# If not specified, set default :type to :sma.
	parameters[:type] = :sma if parameters[:type].nil? or parameters[:type].empty?

	# Check if there is such indicator type supported.
	case
		when INDICATORS.include?(parameters[:type]) then @results = Indicators::Main.new(@data, parameters)
	else 
		raise DataException, "Invalid indicator type specified (#{parameters[:type]})."
	end
end