Class: TechnicalAnalysis::Uo

Inherits:
Indicator show all
Defined in:
lib/technical_analysis/indicators/uo.rb

Overview

Ultimate Oscillator

Class Method Summary collapse

Methods inherited from Indicator

find, roster

Class Method Details

.calculate(data, short_period: 7, medium_period: 14, long_period: 28, short_weight: 4, medium_weight: 2, long_weight: 1) ⇒ Array<UoValue>

Calculates the ultimate oscillator (UO) for the data over the given period en.wikipedia.org/wiki/Ultimate_oscillator

Parameters:

  • data (Array)

    Array of hashes with keys (:date_time, :high, :low, :close)

  • short_period (Integer) (defaults to: 7)

    The given short period

  • medium_period (Integer) (defaults to: 14)

    The given medium period

  • long_period (Integer) (defaults to: 28)

    The given long period

  • short_weight (Float) (defaults to: 4)

    Weight of short Buying Pressure average for UO

  • medium_weight (Float) (defaults to: 2)

    Weight of medium Buying Pressure average for UO

  • long_weight (Float) (defaults to: 1)

    Weight of long Buying Pressure average for UO

Returns:

  • (Array<UoValue>)

    An array of UoValue instances



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
99
# File 'lib/technical_analysis/indicators/uo.rb', line 57

def self.calculate(data, short_period: 7, medium_period: 14, long_period: 28, short_weight: 4, medium_weight: 2, long_weight: 1)
  short_period = short_period.to_i
  medium_period = medium_period.to_i
  long_period = long_period.to_i
  short_weight = short_weight.to_f
  medium_weight = medium_weight.to_f
  long_weight = long_weight.to_f
  Validation.validate_numeric_data(data, :high, :low, :close)
  Validation.validate_length(data, min_data_size(long_period: long_period))
  Validation.validate_date_time_key(data)

  data = data.sort_by { |row| row[:date_time] }

  output = []
  period_values = []
  prior_close = data.shift[:close]
  sum_of_weights = ArrayHelper.sum([short_weight, medium_weight, long_weight])

  data.each do |v|
    min_low_p_close = [v[:low], prior_close].min
    max_high_p_close = [v[:high], prior_close].max

    buying_pressure = v[:close] - min_low_p_close
    true_range = max_high_p_close - min_low_p_close

    period_values << { buying_pressure: buying_pressure, true_range: true_range }

    if period_values.size == long_period
      short_average = calculate_average(short_period, period_values)
      medium_average = calculate_average(medium_period, period_values)
      long_average = calculate_average(long_period, period_values)
      uo = 100 * (((short_weight * short_average) + (medium_weight * medium_average) + (long_weight * long_average)) / (sum_of_weights))

      output << UoValue.new(date_time: v[:date_time], uo: uo)

      period_values.shift
    end

    prior_close = v[:close]
  end

  output.sort_by(&:date_time).reverse
end

.indicator_nameString

Returns the name of the technical indicator

Returns:

  • (String)

    A string of the name of the technical indicator



15
16
17
# File 'lib/technical_analysis/indicators/uo.rb', line 15

def self.indicator_name
  "Ultimate Oscillator"
end

.indicator_symbolString

Returns the symbol of the technical indicator

Returns:

  • (String)

    A string of the symbol of the technical indicator



8
9
10
# File 'lib/technical_analysis/indicators/uo.rb', line 8

def self.indicator_symbol
  "uo"
end

.min_data_size(long_period: 28, **params) ⇒ 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



41
42
43
# File 'lib/technical_analysis/indicators/uo.rb', line 41

def self.min_data_size(long_period: 28, **params)
  long_period.to_i + 1
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



22
23
24
# File 'lib/technical_analysis/indicators/uo.rb', line 22

def self.valid_options
  %i(short_period medium_period long_period short_weight medium_weight long_weight)
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



31
32
33
# File 'lib/technical_analysis/indicators/uo.rb', line 31

def self.validate_options(options)
  Validation.validate_options(options, valid_options)
end