Class: AdvancedMath::RelativeStrengthIndex

Inherits:
Object
  • Object
show all
Defined in:
lib/advanced_math/rsi.rb

Overview

Direct Known Subclasses

RSI

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(range) ⇒ RelativeStrengthIndex

Initialize the members

Raises:

  • (ArgumentError)


22
23
24
25
26
27
28
29
30
31
32
# File 'lib/advanced_math/rsi.rb', line 22

def initialize(range)
  raise ArgumentError, "Range is nil" unless (range)
  raise ArgumentError, "Range must be > 1" unless range.to_i > 1

  @verbose = false
  @values_up = []
  @values_down = []
  @range = range.to_i()
  @last_value = nil
  @count = 0
end

Instance Attribute Details

#countObject

Returns the value of attribute count.



17
18
19
# File 'lib/advanced_math/rsi.rb', line 17

def count
  @count
end

#last_valueObject

Returns the value of attribute last_value.



17
18
19
# File 'lib/advanced_math/rsi.rb', line 17

def last_value
  @last_value
end

#rangeObject

Returns the value of attribute range.



17
18
19
# File 'lib/advanced_math/rsi.rb', line 17

def range
  @range
end

#values_downObject

Returns the value of attribute values_down.



17
18
19
# File 'lib/advanced_math/rsi.rb', line 17

def values_down
  @values_down
end

#values_upObject

Returns the value of attribute values_up.



17
18
19
# File 'lib/advanced_math/rsi.rb', line 17

def values_up
  @values_up
end

#verboseObject

Returns the value of attribute verbose.



18
19
20
# File 'lib/advanced_math/rsi.rb', line 18

def verbose
  @verbose
end

Instance Method Details

#add(value) ⇒ Object

Add a value to the list. If the list is < @range, then return nil. Otherwise compute the RSI and return the value.

Raises:

  • (ArgumentError)


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/advanced_math/rsi.rb', line 39

def add(value)
  raise ArgumentError, "Value is nil" unless (value)
  puts "==================" if verbose()
  puts "value = #{value}" if verbose()

  if (nil == @last_value)
    @last_value = value.to_f()
    puts "Setting intial last_value = #{@last_value}" if verbose()
    return nil
  end

  @values_up.shift() if (range() == @values_up.length())
  @values_down.shift() if (range() == @values_down.length())

  diff = value.to_f - @last_value
  @last_value = value.to_f()
  if (0 > diff)
    @values_up << 0
    @values_down << diff.abs()
  elsif (0 < diff)
    @values_up << diff
    @values_down << 0
  else
    @values_up << 0
    @values_down << 0
  end
  puts "diff = #{diff}, values_up = #{@values_up.inspect()}, values_down = #{@values_down.inspect()}" if verbose()

  if (@values_up.length() < range())
    puts "#{@values_up.length()} < #{range()}" if verbose()
    return nil
  end
  if (@values_up.length() > range())
    puts "#{@values_up.length()} > #{range()}" if verbose()
  @values_up.shift()
  @values_down.shift()
  else
    puts "#{@values_up.length()} == #{range()}" if verbose()
  end

  sum = {:up => 0, :down => 0, :i_up => 0, :i_down => 0}
  
  @values_up.slice(0, range()-1).each()   { |value| sum[:up]   = sum[:up] + value }
  @values_down.slice(0, range()-1).each() { |value| sum[:down] = sum[:down] + value }
  if (count() == 0)
    sum[:up]   = sum[:up] + @values_up[range()-1]
    sum[:down] = sum[:down] + @values_down[range()-1]
  else
    sum[:up] = sum[:up]/(range()-1) + @values_up[range()-1]
    sum[:down] = sum[:down]/(range()-1) + @values_down[range()-1]
  end
  @count = count() + 1

  rs = (0 == sum[:down]) ? 100.0 : (sum[:up] / sum[:down])
  puts "rs = #{rs}" if verbose()

  rsi = 100.0 - (100.0 / (1.0 + rs))
  puts "rsi = #{rsi}" if verbose()
  return rsi
end

#add_array(values) ⇒ Object

Compute the RSI values for an array Return an array with the RSI values

Raises:

  • (ArgumentError)


104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/advanced_math/rsi.rb', line 104

def add_array(values)
  raise ArgumentError, "Value is nil" unless (values);
  raise ArgumentError, "Value is not an array" unless values.kind_of?(Array);

  output = []

  # Calculate the sum of the array
  values.each() { |value| output << add(value) }

  # Return the RSI array
  return output
end