Sha256: 2304b38b421ecfebd745056ae30efcde83df660e722e3e222d8ba38587edd763

Contents?: true

Size: 1.26 KB

Versions: 7

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Cotcube
  module Indicators
    # returns the difference between the current value and the first available value in carrier.
    #    if block given, the block is evaluated as conditional, using current and carrier.get.last.
    #    if evaluation is false, carrier remains untouched and 0 is returned
    def change(key:, lookback: 1, debug: false)
      unless lookback.is_a?(Integer) && (lookback >= 1)
        raise ArgumentError, 'invalid lookback period, need integer >= 1'
      end

      carrier = Cotcube::Indicators::Carrier.new(length: lookback + 1)

      lambda do |x|
        current = x[key.to_sym]
        current = 0 unless current.is_a?(Numeric)
        carrier << 0 if carrier.empty?
        if debug
          puts "comparing #{current} from #{key.to_sym} ... "\
               "#{lookback} ... #{carrier.inspect} ... yield"\
               " #{block_given? ? yield(carrier.get.last, current) : ''}"
        end
        if (not block_given?) || yield(carrier.get.last, current)  # rubocop:disable Style/GuardClause
          carrier << current
          ret = carrier.size < lookback + 1 ? 0 : (carrier.get.last - carrier.get.first)
          return ret.to_f
        else
          return 0
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
cotcube-indicators-0.1.15 lib/cotcube-indicators/change.rb
cotcube-indicators-0.1.14 lib/cotcube-indicators/change.rb
cotcube-indicators-0.1.13 lib/cotcube-indicators/change.rb
cotcube-indicators-0.1.12 lib/cotcube-indicators/change.rb
cotcube-indicators-0.1.11 lib/cotcube-indicators/change.rb
cotcube-indicators-0.1.10 lib/cotcube-indicators/change.rb
cotcube-indicators-0.1.9 lib/cotcube-indicators/change.rb