Class: FinTools

Inherits:
Object
  • Object
show all
Defined in:
lib/fintools.rb

Instance Method Summary collapse

Instance Method Details

#calc_ema(arr, period) ⇒ Object



37
38
39
40
# 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

#calcMACD(price_arr, ema_long, ema_short, ema_diff) ⇒ Object



43
44
45
46
47
48
49
50
51
# 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

#histvola(values) ⇒ Object



26
27
28
29
# File 'lib/fintools.rb', line 26

def histvola(values)
  stddev = standard_deviation(values)
  return stddev * Math.sqrt(254)
end

#mean(arr) ⇒ Object



10
11
12
# File 'lib/fintools.rb', line 10

def mean(arr)
  arr.sum / arr.length.to_f
end

#sample_variance(arr) ⇒ Object



14
15
16
17
18
19
20
# 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) ⇒ Object

returns is an array of returns(percent)



32
33
34
# File 'lib/fintools.rb', line 32

def sharpe_ratio(rets)
  return mean(rets) / standard_deviation(rets)
end

#standard_deviation(arr) ⇒ Object



22
23
24
# File 'lib/fintools.rb', line 22

def standard_deviation(arr)
  return Math.sqrt(sample_variance(arr))
end

#sum(arr) ⇒ Object



5
6
7
8
# File 'lib/fintools.rb', line 5

def sum(arr)
  arr.inject(0) { | accum, i | accum + i
}
end