Sha256: 5ee22d5d771f68ad2606bf019eb096e2be77cbaf8b6e2132a28f7822e8fc6396

Contents?: true

Size: 643 Bytes

Versions: 2

Compression:

Stored size: 643 Bytes

Contents

require "quant/version"

module Quant

  def self.sma(values, n)
    acc = 0
    values.inject_with_index([]) do |m, e, i|
      acc += e
      if i < (n - 1)
        m << not_a_number
      elsif i == (n - 1)
        m << acc / n
      else
        acc = acc - values[i - n]
        m << acc / n
      end
    end
  end

  def self.donchian_channel(ohlc, n)
    dc = []
    ohlc.each_with_index do |e, i|
      if i < (n - 1)
        dc << [not_a_number, not_a_number]
      else
        c = ohlc[(i - n + 1)..(i)].flatten
        dc << [c.max, c.min]
      end
    end
    dc
  end

  private

  def self.not_a_number
    0 / 0.0
  end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
quant-0.0.3 lib/quant.rb
quant-0.0.2 lib/quant.rb