class FinTools
Public Instance Methods
calcMACD(price_arr, ema_long, ema_short, ema_diff)
click to toggle source
# File lib/fintools.rb, line 43 def calcMACD(price_arr, ema_long, ema_short, ema_diff) closing_prices = price_arr.map { | r | r[:cls] } ema26 = calc_ema(closing_prices, ema_long) ema12 = calc_ema(closing_prices, ema_short) return calc_ema(closing_prices, ema_diff) end
calc_ema(arr, period)
click to toggle source
# File lib/fintools.rb, line 37 def calc_ema(arr, period) my_data = Indicators::Data.new(arr) return my_data.calc(:type => :ema, :params => period).output end
histvola(values)
click to toggle source
# File lib/fintools.rb, line 26 def histvola(values) stddev = standard_deviation(values) return stddev * Math.sqrt(254) end
mean(arr)
click to toggle source
# File lib/fintools.rb, line 10 def mean(arr) arr.sum / arr.length.to_f end
sample_variance(arr)
click to toggle source
# File lib/fintools.rb, line 14 def sample_variance(arr) m = mean(arr) sum = arr.inject(0) { | accum, i | accum + (i - m) ** 2 } sum / (arr.length - 1).to_f end
sharpe_ratio(rets)
click to toggle source
returns is an array of returns(percent)
# File lib/fintools.rb, line 32 def sharpe_ratio(rets) return mean(rets) / standard_deviation(rets) end
standard_deviation(arr)
click to toggle source
# File lib/fintools.rb, line 22 def standard_deviation(arr) return Math.sqrt(sample_variance(arr)) end
sum(arr)
click to toggle source
# File lib/fintools.rb, line 5 def sum(arr) arr.inject(0) { | accum, i | accum + i } end