Class: DhanHQ::Indicators::SMA
- Inherits:
-
Object
- Object
- DhanHQ::Indicators::SMA
- Defined in:
- lib/DhanHQ/indicators.rb
Overview
Simple Moving Average (SMA)
Calculates the average of a specified number of data points.
Class Method Summary collapse
-
.calculate(data, period: 20) ⇒ Array<Float>
Calculate SMA for the given data.
Class Method Details
.calculate(data, period: 20) ⇒ Array<Float>
Calculate SMA for the given data.
21 22 23 24 25 26 27 28 29 30 |
# File 'lib/DhanHQ/indicators.rb', line 21 def self.calculate(data, period: 20) return [] if data.nil? || data.empty? || period < 1 data.each_index.map do |i| next nil if i < period - 1 window = data[(i - period + 1)..i] window.sum.to_f / period end end |