Sha256: e230f3a4187f2c3f0c293515a3987a9c3517dc5142816b4eb3c9ffdea224cc44

Contents?: true

Size: 1.43 KB

Versions: 1

Compression:

Stored size: 1.43 KB

Contents

module SignalTools
  module Technicals
    class MACD
      attr_reader :fast, :slow, :signal, :data

      def initialize(data, fast, slow, signal)
        @data = data
        @fast = fast
        @slow = slow
        @signal = signal
      end

      def calculate
        # trim_data_to_range!(macd_points)
        macd_points
      end

      # Takes a period of days for fast, slow, signal, and time period (eg 8,17,9).
      def macd_points
        fast_ema_points = SignalTools::Technicals::EMA.new(data, fast).calculate
        slow_ema_points = SignalTools::Technicals::EMA.new(data, slow).calculate
        macd_and_divergence_points(fast_ema_points, slow_ema_points)
      end

      def macd_and_divergence_points(fast_ema_points, slow_ema_points)
        macds = differences_between_arrays(fast_ema_points, slow_ema_points)
        signal_points = SignalTools::Technicals::EMA.new(macds, signal).calculate
        divergences = differences_between_arrays(macds, signal_points)
        {:signal_points => signal_points, :divergences => divergences}
      end

      # Returns an array with the differences between the first_points and second_points
      def differences_between_arrays(first_points, second_points)
        SignalTools.truncate_to_shortest!(first_points, second_points)
        differences = []
        first_points.each_with_index { |fp, index| differences << fp - second_points[index] }
        differences
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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