lib/quant.rb in quant-0.0.3 vs lib/quant.rb in quant-0.0.4

- old
+ new

@@ -28,9 +28,32 @@ end end dc end + def self.tr(ohlc) + tr = [] + ohlc.each_with_index do |e, i| + tr << if i == 0 + not_a_number + else + high = e[1] + low = e[2] + last_close = ohlc[i - 1][3] + [ high - low, (high - last_close).abs, (last_close - low).abs ].max + end + end + tr + end + + def self.atr(ohlc, n) + tr = tr(ohlc) + tr.delete_if{ |e| e.nan? } + a = sma(tr, n) + 0.upto(ohlc.length - tr.length - 1) { a.unshift(not_a_number) } if ohlc.length > tr.length + a + end + private def self.not_a_number 0 / 0.0 end