Class: Quant::Indicators::Rsi

Inherits:
Indicator show all
Defined in:
lib/quant/indicators/rsi.rb

Overview

The Relative Strength Index (RSI) is a momentum oscillator that measures the speed and change of price movements. This RSI indicator is adaptive and uses the half-period of the dominant cycle to calculate the RSI. It is further smoothed by an exponential moving average of the last three bars (or whatever the micro_period is set to).

The RSI oscillates between 0 and 1. Traditionally, and in this implementation, the RSI is considered overbought when above 0.7 and oversold when below 0.3.

Constant Summary

Constants inherited from Indicator

Indicator::PRIORITIES

Constants included from Mixins::UniversalFilters

Mixins::UniversalFilters::K

Instance Attribute Summary

Attributes inherited from Indicator

#p0, #p1, #p2, #p3, #series, #source, #t0, #t1, #t2, #t3

Instance Method Summary collapse

Methods inherited from Indicator

#<<, #[], #adaptive_half_period, #adaptive_period, dependent_indicator_classes, depends_on, #dominant_cycle, #dominant_cycle_indicator_class, #dominant_cycle_kind, #each, #indicator_name, #initialize, #input, #inspect, #max_period, #micro_period, #min_period, #p, #period_points, #pivot_kind, #points_class, #priority, register, #size, #t, #ticks, #values, #warmed_up?

Methods included from Mixins::FisherTransform

#fisher_transform, #inverse_fisher_transform, #relative_fisher_transform

Methods included from Mixins::Stochastic

#stochastic

Methods included from Mixins::SuperSmoother

#three_pole_super_smooth, #two_pole_super_smooth

Methods included from Mixins::HilbertTransform

#hilbert_transform

Methods included from Mixins::ExponentialMovingAverage

#exponential_moving_average

Methods included from Mixins::SimpleMovingAverage

#simple_moving_average

Methods included from Mixins::WeightedMovingAverage

#extended_weighted_moving_average, #weighted_moving_average

Methods included from Mixins::UniversalFilters

#universal_band_pass, #universal_ema, #universal_filter, #universal_one_pole_high_pass, #universal_one_pole_low_pass, #universal_two_pole_high_pass, #universal_two_pole_low_pass

Methods included from Mixins::ButterworthFilters

#three_pole_butterworth, #two_pole_butterworth

Methods included from Mixins::HighPassFilters

#high_pass_filter, #hpf2, #two_pole_high_pass_filter

Methods included from Mixins::Functions

#angle, #bars_to_alpha, #deg2rad, #period_to_alpha, #rad2deg

Constructor Details

This class inherits a constructor from Quant::Indicators::Indicator

Instance Method Details

#computeObject



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
# File 'lib/quant/indicators/rsi.rb', line 40

def compute
  # The High Pass filter is half the dominant cycle period while the
  # Low Pass Filter (super smoother) is the quarter dominant cycle period.
  p0.hp = high_pass_filter :input, period: half_period
  p0.filter = ema :hp, previous: :filter, period: quarter_period

  lp = p(half_period)
  p0.delta = p0.filter - lp.filter
  p0.delta > 0.0 ? p0.gain = p0.delta : p0.loss = p0.delta.abs

  period_points(half_period).tap do |period_points|
    p0.gains = period_points.map(&:gain).sum
    p0.losses = period_points.map(&:loss).sum
  end

  p0.denom = p0.gains + p0.losses

  if p0.denom > 0.0
    p0.inst_rsi = (p0.gains / p0.denom)
    p0.rsi = ema :inst_rsi, previous: :rsi, period: micro_period
  else
    p0.inst_rsi = 0.5
    p0.rsi = 0.5
  end
end

#half_periodObject



36
37
38
# File 'lib/quant/indicators/rsi.rb', line 36

def half_period
  (dc_period / 2) - 1
end

#quarter_periodObject



32
33
34
# File 'lib/quant/indicators/rsi.rb', line 32

def quarter_period
  half_period / 2
end