Module: Coinbot::OrderBook::EMA

Defined in:
lib/coinbot/order_book/ema.rb

Class Method Summary collapse

Class Method Details

.calculate(opts = {}) ⇒ Object

Supported Method Parameters

Coinbot::OrderBook::EMA.calculate( )



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
# File 'lib/coinbot/order_book/ema.rb', line 12

public_class_method def self.calculate(opts = {})
  candle_tot = opts[:candle_tot].to_i
  candle_key = opts[:candle_key]
  candles = opts[:candles]
  last_value = opts[:last_value].to_f
  this_product = opts[:this_product]

  quote_increment = this_product[:quote_increment]
  fiat_smallest_decimal = quote_increment.to_s.split('.')[-1].length
  base_currency = this_product[:base_currency].downcase.to_sym

  last_ema = Coinbot::OrderBook::SMA.calculate(
    candle_tot: candle_tot,
    candle_key: candle_key,
    last_value: last_value,
    candles: candles,
    this_product: this_product
  ).to_f

  if last_ema.zero?
    exponential_moving_average = '--'
  else
    bc_overrides = Coinbot::OrderBook.base_currency_overrides
    fiat_smallest_decimal = 8 if bc_overrides.include?(
      base_currency
    )

    alpha = 2.0 / (candle_tot + 1)
    # ema = (last_value - last_ema) * alpha + last_ema
    ema = (last_value * alpha) + (last_ema * (1 - alpha))

    exponential_moving_average = format(
      "%0.#{fiat_smallest_decimal}f",
      ema
    )
  end

  exponential_moving_average
rescue StandardError => e
  raise e
end

.helpObject

Display Usage for this Module



55
56
57
58
59
# File 'lib/coinbot/order_book/ema.rb', line 55

public_class_method def self.help
  puts "USAGE:
   golden_cross_indicator_hash = #{self}.status()
  "
end