Module: ChaikinMoneyFlow
- Included in:
- Array
- Defined in:
- lib/ruby-technical-analysis/indicators/chaikin_money_flow.rb
Overview
Chaikin Money Flow indicator Returns a current singular value
Instance Method Summary collapse
Instance Method Details
#chaikin_money_flow(period) ⇒ Object
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 |
# File 'lib/ruby-technical-analysis/indicators/chaikin_money_flow.rb', line 6 def chaikin_money_flow(period) highs = [] lows = [] closes = [] volumes = [] each do |h, l, c, v| highs << h lows << l closes << c volumes << v end if highs.size < period raise ArgumentError, "High array passed to Chaikin Money Flow cannot be less than the period argument." end if lows.size < period raise ArgumentError, "Low array passed to Chaikin Money Flow cannot be less than the period argument." end if closes.size < period raise ArgumentError, "Close array passed to Chaikin Money Flow cannot be less than the period argument." end if volumes.size < period raise ArgumentError, "Volume array passed to Chaikin Money Flow cannot be less than the period argument." end if size < period raise ArgumentError, "Array passed to Bollinger Bands cannot be less than the period argument." end highs = highs.last(period) lows = lows.last(period) closes = closes.last(period) volumes = volumes.last(period) num_sum = 0 vol_sum = 0 (0..(period - 1)).each do |i| vol_sum += volumes[i] cml = closes[i] - lows[i] hmc = highs[i] - closes[i] hml = highs[i] - lows[i] num_sum += ((cml - hmc).to_f / hml) * volumes[i] end cmf = num_sum.to_f / vol_sum cmf.round(5) end |