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.

Raises:



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

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)
		raise DataException, "Alien data. Given data must be an array (got #{@data.class})."
	end

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

Instance Attribute Details

#data ⇒ Object (readonly)

Returns the value of attribute data.



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

def data
  @data
end

#results ⇒ Object (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



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

def calc parameters
	# Check is parameters are usable.
	unless parameters.is_a?(Hash)
		raise DataException, 'Given parameters have to be a hash.'
	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