Class: Indicators::Sma

Inherits:
Object
  • Object
show all
Defined in:
lib/indicators/calculations/sma.rb

Overview

Simple Moving Average

Class Method Summary collapse

Class Method Details

.calculate(data, parameters) ⇒ Object

SMA: (sum of closing prices for x period)/x



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/indicators/calculations/sma.rb', line 7

def self.calculate data, parameters
  periods = parameters
  output = Array.new
  # Returns an array from the requested column and checks if there is enought data points.
  adj_closes = Indicators::Helper.validate_data(data, :adj_close, periods)

  adj_closes.each_with_index do |adj_close, index|
    start = index+1-periods
    if index+1 >= periods
      adj_closes_sum = adj_closes[start..index].sum
      output[index] = (adj_closes_sum/periods.to_f)
    else
      output[index] = nil
    end
  end
  return output
  
end