Method: TechnicalAnalysis::Uo.calculate
- Defined in:
- lib/technical_analysis/indicators/uo.rb
.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
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 = v[:close] - min_low_p_close true_range = max_high_p_close - min_low_p_close period_values << { 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 |