Class: RubyTechnicalAnalysis::StochasticOscillator

Inherits:
Indicator
  • Object
show all
Defined in:
lib/ruby_technical_analysis/indicators/stochastic_oscillator.rb

Overview

Stochastic Oscillator

Find more information at: www.investopedia.com/terms/s/stochasticoscillator.asp

Instance Attribute Summary collapse

Attributes inherited from Indicator

#series

Instance Method Summary collapse

Methods inherited from Indicator

call

Constructor Details

#initialize(series: [], k_periods: 14, k_slow_periods: 3, d_periods: 3) ⇒ StochasticOscillator

Returns a new instance of StochasticOscillator.

Parameters:

  • series (Array) (defaults to: [])

    An array of arrays containing high, low, close prices, e.g. [[high, low, close], [high, low, close]]

  • k_periods (Integer) (defaults to: 14)

    The number of periods to use in the calculation

  • k_slow_periods (Integer) (defaults to: 3)

    The number of periods to use in the calculation

  • d_periods (Integer) (defaults to: 3)

    The number of periods to use in the calculation



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/ruby_technical_analysis/indicators/stochastic_oscillator.rb', line 12

def initialize(series: [], k_periods: 14, k_slow_periods: 3, d_periods: 3)
  @k_periods = k_periods
  @k_slow_periods = k_slow_periods
  @d_periods = d_periods
  @lowest_lows = []
  @highest_highs = []
  @close_minus_lowest_lows = []
  @highest_highs_minus_lowest_lows = []
  @ks_sums_close_minus_lowest_lows = []
  @ks_sums_highest_highs_minus_lowest_lows = []
  @ks_sums_quotients_times_one_hundred = []
  @d_periods_sma = []

  super(series: series)
end

Instance Attribute Details

#d_periodsObject (readonly)

Returns the value of attribute d_periods.



6
7
8
# File 'lib/ruby_technical_analysis/indicators/stochastic_oscillator.rb', line 6

def d_periods
  @d_periods
end

#k_periodsObject (readonly)

Returns the value of attribute k_periods.



6
7
8
# File 'lib/ruby_technical_analysis/indicators/stochastic_oscillator.rb', line 6

def k_periods
  @k_periods
end

#k_slow_periodsObject (readonly)

Returns the value of attribute k_slow_periods.



6
7
8
# File 'lib/ruby_technical_analysis/indicators/stochastic_oscillator.rb', line 6

def k_slow_periods
  @k_slow_periods
end

Instance Method Details

#callFloat

Returns The current Stochastic Oscillator value.

Returns:

  • (Float)

    The current Stochastic Oscillator value



29
30
31
# File 'lib/ruby_technical_analysis/indicators/stochastic_oscillator.rb', line 29

def call
  calculate_stochastic_oscillator
end

#valid?Boolean

Returns Whether or not the object is valid.

Returns:

  • (Boolean)

    Whether or not the object is valid



34
35
36
# File 'lib/ruby_technical_analysis/indicators/stochastic_oscillator.rb', line 34

def valid?
  k_periods + d_periods <= series.length
end