Class: TechnicalAnalysis::Indicator

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

Direct Known Subclasses

Adi, Adtv, Adx, Ao, Atr, Bb, Cci, Cmf, Cr, Dc, Dlr, Dpo, Dr, Ema, Eom, Fi, Ichimoku, Kc, Kst, Macd, Mfi, Mi, Nvi, Obv, ObvMean, Rsi, Sma, Sr, Trix, Tsi, Uo, Vi, Vpt, Vwap, Wma, Wr

Class Method Summary collapse

Class Method Details

.calculate(indicator_symbol, data, calculation, options = {}) ⇒ Object

Find the applicable indicator and looks up the value

Parameters:

  • indicator_symbol (String)

    Downcased string of the indicator symbol

  • data (Array)

    Array of hashes of price data to perform calcualtion on

  • calculation (Symbol)

    The calculation to be performed on the requested indicator and params

  • options (Hash) (defaults to: {})

    A hash containing options for the requested calculation

Returns:

  • Returns the requested calculation



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/technical_analysis/indicators/indicator.rb', line 78

def self.calculate(indicator_symbol, data, calculation, options={})
  return nil unless CALCULATIONS.include? calculation

  indicator = find(indicator_symbol)
  raise "Indicator not found!" if indicator.nil?

  case calculation
  when :indicator_name; indicator.indicator_name
  when :indicator_symbol; indicator.indicator_symbol
  when :technicals; indicator.calculate(data, options)
  when :min_data_size; indicator.min_data_size(options)
  when :valid_options; indicator.valid_options
  when :validate_options; indicator.validate_options(options)
  else nil
  end
end

.find(indicator_symbol) ⇒ Object

Finds the applicable indicator and returns an instance

Parameters:

  • indicator_symbol (String)

    Downcased string of the indicator symbol

Returns:

  • TechnicalAnalysis::ClassName



62
63
64
65
66
67
68
# File 'lib/technical_analysis/indicators/indicator.rb', line 62

def self.find(indicator_symbol)
  roster.each do |indicator|
    return indicator if indicator.indicator_symbol == indicator_symbol
  end

  nil
end

.indicator_nameString

Returns the name of the technical indicator

Returns:

  • (String)

    A string of the name of the technical indicator



134
135
136
# File 'lib/technical_analysis/indicators/indicator.rb', line 134

def self.indicator_name
  raise "#{self.name} did not implement indicator_name"
end

.indicator_symbolString

Returns the symbol of the technical indicator

Returns:

  • (String)

    A string of the symbol of the technical indicator



127
128
129
# File 'lib/technical_analysis/indicators/indicator.rb', line 127

def self.indicator_symbol
  raise "#{self.name} did not implement indicator_symbol"
end

.min_data_size(indicator_symbol, options) ⇒ Integer

Calculates the minimum number of observations needed to calculate the technical indicator

Parameters:

  • options (Hash)

    The options for the technical indicator

Returns:

  • (Integer)

    Returns the minimum number of observations needed to calculate the technical indicator based on the options provided



101
102
103
104
# File 'lib/technical_analysis/indicators/indicator.rb', line 101

def self.min_data_size(indicator_symbol, options)
  raise "#{self.name} did not implement min_data_size"
  nil
end

.rosterArray

Returns an array of TechnicalAnalysis modules

Returns:

  • (Array)

    A list of TechnicalAnalysis::Class



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
# File 'lib/technical_analysis/indicators/indicator.rb', line 18

def self.roster
  [
    TechnicalAnalysis::Adi,
    TechnicalAnalysis::Adtv,
    TechnicalAnalysis::Adx,
    TechnicalAnalysis::Ao,
    TechnicalAnalysis::Atr,
    TechnicalAnalysis::Bb,
    TechnicalAnalysis::Cci,
    TechnicalAnalysis::Cmf,
    TechnicalAnalysis::Cr,
    TechnicalAnalysis::Dc,
    TechnicalAnalysis::Dlr,
    TechnicalAnalysis::Dpo,
    TechnicalAnalysis::Dr,
    TechnicalAnalysis::Eom,
    TechnicalAnalysis::Fi,
    TechnicalAnalysis::Ichimoku,
    TechnicalAnalysis::Kc,
    TechnicalAnalysis::Kst,
    TechnicalAnalysis::Macd,
    TechnicalAnalysis::Mfi,
    TechnicalAnalysis::Mi,
    TechnicalAnalysis::Nvi,
    TechnicalAnalysis::Obv,
    TechnicalAnalysis::ObvMean,
    TechnicalAnalysis::Rsi,
    TechnicalAnalysis::Sma,
    TechnicalAnalysis::Sr,
    TechnicalAnalysis::Trix,
    TechnicalAnalysis::Tsi,
    TechnicalAnalysis::Uo,
    TechnicalAnalysis::Vi,
    TechnicalAnalysis::Vpt,
    TechnicalAnalysis::Vwap,
    TechnicalAnalysis::Wr,
  ]
end

.valid_optionsArray

Returns an array of valid keys for options for this technical indicator

Returns:

  • (Array)

    An array of keys as symbols for valid options for this technical indicator



119
120
121
122
# File 'lib/technical_analysis/indicators/indicator.rb', line 119

def self.valid_options
  raise "#{self.name} did not implement valid_options"
  []
end

.validate_options(options) ⇒ Boolean

Validates the provided options for this technical indicator

Parameters:

  • options (Hash)

    The options for the technical indicator to be validated

Returns:

  • (Boolean)

    Returns true if options are valid or raises a ValidationError if they’re not



111
112
113
114
# File 'lib/technical_analysis/indicators/indicator.rb', line 111

def self.validate_options(options)
  raise "#{self.name} did not implement validate_options"
  false
end