Module: Quant::Mixins::Stochastic

Included in:
Indicators::Indicator
Defined in:
lib/quant/mixins/stochastic.rb

Instance Method Summary collapse

Instance Method Details

#stochastic(source, period:) ⇒ Object

The main idea behind the Stochastic Oscillator is that closing prices should close near the same direction as the current trend. In a market trending up, prices will likely close near their high, and in a market trending down, prices close near their low.



16
17
18
19
20
21
22
23
# File 'lib/quant/mixins/stochastic.rb', line 16

def stochastic(source, period:)
  subset = values.last(period).map{ |p| p.send(source) }

  lowest, highest = subset.minimum.to_f, subset.maximum.to_f
  return 0.0 if (highest - lowest).zero?

  100.0 * (subset[-1] - lowest) / (highest - lowest)
end