Sha256: 4d28dbb6d8a40b8681af990b1f3fc02c02d772f7b4be1411d78d29625c9383be

Contents?: true

Size: 1.03 KB

Versions: 1

Compression:

Stored size: 1.03 KB

Contents

require './lib/signal_tools/technicals/common'
require './lib/signal_tools/technicals/true_range'

module SignalTools::Technicals
  class AverageTrueRange
    include Common
    include TrueRange

    DEFAULT_PERIOD = 14

    attr_reader :period, :stock_data

    def initialize(stock_data, period)
      @period = period
      @stock_data = stock_data
    end

    def calculate
      average_true_ranges
    end

     # Takes a smoothing period and historical data and calculates the average
     # true ranges.
    def average_true_ranges
      trs = true_ranges(stock_data)
      atrs = [default_simple_average(trs.slice!(0...DEFAULT_PERIOD), DEFAULT_PERIOD)]
      trs.each { |tr| atrs << calculate_average_true_range(atrs.last, tr, period) }
      atrs
    end

    # Takes yesterday's average true range, today's true range, and the smoothing
    # period and calculates the day's average true range.
    def calculate_average_true_range(yesterday_atr, today_tr, period)
      (yesterday_atr * (period - 1) + today_tr) / period
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
signal_tools-0.3.1 lib/signal_tools/technicals/average_true_range.rb