Slide 14
Slide 14 text
def moving_average bars, lookback_period
if bars.length < lookback_period
raise "insufficient number of bars"
end
sum_of_prices = bars.last(lookback_period)
.inject(0.0) { |sum, bar| sum + bar[:adj_close].to_f }
sum_of_prices / lookback_period
end
fast_ma = moving_average bars, 15
slow_ma = moving_average bars, 50