Module: Coinbot::OrderBook::MACD

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

Class Method Summary collapse

Class Method Details

.calculate(opts = {}) ⇒ Object

Supported Method Parameters

Coinbot::OrderBook.moving_average_convergence_divergence(

order_book: 'required - order book data structure'

)



137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/coinbot/order_book/macd.rb', line 137

public_class_method def self.calculate(opts = {})
  this_product = opts[:this_product]
  last_value = opts[:last_value]
  candles = opts[:candles]
  macd_history = opts[:macd_history]

  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

  fast_ema = Coinbot::OrderBook::EMA.calculate(
    candle_tot: 12,
    candle_key: :candle_close,
    candles: candles,
    last_value: last_value,
    this_product: this_product
  )

  slow_ema = Coinbot::OrderBook::EMA.calculate(
    candle_tot: 26,
    candle_key: :candle_close,
    candles: candles,
    last_value: last_value,
    this_product: this_product
  )

  if fast_ema != '--' && slow_ema != '--'
    macd_fast_avg = format(
      "%0.#{fiat_smallest_decimal}f",
      fast_ema
    )

    macd_slow_avg = format(
      "%0.#{fiat_smallest_decimal}f",
      slow_ema
    )

    bc_overrides = Coinbot::OrderBook.base_currency_overrides
    fiat_smallest_decimal = 8 if bc_overrides.include?(base_currency)

    macd = format(
      "%0.#{fiat_smallest_decimal}f",
      macd_fast_avg.to_f - macd_slow_avg.to_f
    )

    if candles.length >= 26
      # We've reached 26 candles - start returning
      # a macd value other than -- to UI

      signal = Coinbot::OrderBook::EMA.calculate(
        candle_tot: 9,
        candle_key: :macd_history,
        candles: candles,
        last_value: macd,
        this_product: this_product
      )

      if signal == '--'
        signal = '--'
        histogram = '--'
      else
        signal = format(
          "%0.#{fiat_smallest_decimal}f",
          signal
        )

        # Override base increment for macd to align
        # with tradingview.com
        histogram = format(
          "%0.#{fiat_smallest_decimal}f",
          macd.to_f - signal.to_f
        )
      end
    else
      # Return -- when candles < 35 BUT
      # macd begins to persist to order book
      # at candle 26 above.
      signal = '--'
      histogram = '--'
    end
  else
    # Return -- when candles < 26
    signal = '--'
    macd = '--'
    histogram = '--'
  end

  # last_histogram = macd_history.last[:histogram]
  # unless last_histogram == histogram
  macd_hash = {
    time: Time.now.strftime('%Y-%m-%d %H:%M:%S.%N%z'),
    histogram: histogram,
    macd_fast_avg: macd_fast_avg,
    macd_slow_avg: macd_slow_avg,
    macd: macd,
    signal: signal
  }
  macd_history.push(macd_hash)
  # end

  # Retain a sane amount of history
  macd_hist_retention = 900
  if macd_history.length >= macd_hist_retention
    # [retain_history..] << Not a typo - equivalent to
    # [retain_record..-1]
    retain_record = macd_hist_retention * -1
    macd_hist_arr = macd_history[retain_record..]
    macd_history = macd_hist_arr
  end

  macd_history
rescue StandardError => e
  raise e
end

.helpObject

Display Usage for this Module



254
255
256
257
258
# File 'lib/coinbot/order_book/macd.rb', line 254

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

.status(opts = {}) ⇒ Object

Supported Method Parameters

Coinbot::OrderBook::MACD.status( )



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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/coinbot/order_book/macd.rb', line 12

public_class_method def self.status(opts = {})
  indicator_status = opts[:indicator_status]
  macd_history = opts[:macd_history]
  candles = opts[:candles]
  invested = opts[:invested]

  indicator_hash = {}
  indicator_hash[:invested] = invested
  indicator_hash[:status] = 'MACD'

  return unless macd_history.length > 1

  umh = macd_history.uniq { |h| h[:histogram] }
  # Prepend macd_history from previous candle if
  # candle's macd history has only one record.
  if umh.length == 1 && candles.length > 1
    umh_two = candles[-2][:macd_history].uniq { |h| h[:histogram] }
    umh = umh_two + umh
  end

  if umh.length == 2 && candles.length > 1
    umh_three = candles[-2][:macd_history].uniq { |h| h[:histogram] }
    umh = umh_three + umh
  end

  macd_three = '--'
  macd_three = umh[-3][:histogram] if umh.length > 2 &&
                                      candles.length > 1

  macd_two = '--'
  macd_two = umh[-2][:histogram] if umh.length > 1 &&
                                    candles.length > 1

  macd_one = umh.last[:histogram]
  macd_one_m = umh.last[:macd]
  macd_one_s = umh.last[:signal]

  indicator_hash[:color] = :yellow

  if macd_one != '--' && macd_two != '--' && macd_three != '--'
    # indicator_hash[:color] = :green if (
    #                                    macd_one_m.to_f.negative? &&
    #                                    macd_one_s.to_f.negative?
    #                                   ) && (
    indicator_hash[:color] = :green if (
                                         macd_one_m.to_f.negative? &&
                                         macd_one_s.to_f.negative? &&
                                         (
                                           macd_one.to_f.zero? &&
                                           macd_two.to_f.negative? &&
                                           macd_three.to_f.negative?
                                         ) || (
                                           macd_one.to_f.positive? &&
                                           macd_two.to_f.negative? &&
                                           macd_three.to_f.negative?
                                         ) || (
                                           macd_one.to_f.zero? &&
                                           macd_two.to_f.zero? &&
                                           macd_three.to_f.negative?
                                         ) || (
                                           macd_one.to_f.positive? &&
                                           macd_two.to_f.positive? &&
                                           macd_three.to_f.negative?
                                         ) || (
                                           macd_one.to_f.positive? &&
                                           macd_two.to_f.zero? &&
                                           macd_three.to_f.negative?
                                         ) || (
                                           macd_one.to_f.positive? &&
                                           macd_two.to_f.zero? &&
                                           macd_three.to_f.zero?
                                         ) || (
                                           macd_one.to_f.positive? &&
                                           macd_two.to_f.positive? &&
                                           macd_three.to_f.zero?
                                         )
                                       )

    # indicator_hash[:color] = :red if (
    #                                    macd_one_m.to_f.positive? &&
    #                                    macd_one_s.to_f.positive?
    #                                  ) && (
    indicator_hash[:color] = :red if (
                                       (
                                         macd_one.to_f.zero? &&
                                         macd_two.to_f.positive? &&
                                         macd_three.to_f.positive?
                                       ) || (
                                         macd_one.to_f.negative? &&
                                         macd_two.to_f.positive? &&
                                         macd_three.to_f.positive?
                                       ) || (
                                         macd_one.to_f.zero? &&
                                         macd_two.to_f.zero? &&
                                         macd_three.to_f.positive?
                                       ) || (
                                         macd_one.to_f.negative? &&
                                         macd_two.to_f.negative? &&
                                         macd_three.to_f.positive?
                                       ) || (
                                         macd_one.to_f.negative? &&
                                         macd_two.to_f.zero? &&
                                         macd_three.to_f.positive?
                                       ) || (
                                         macd_one.to_f.negative? &&
                                         macd_two.to_f.zero? &&
                                         macd_three.to_f.zero?
                                       ) || (
                                         macd_one.to_f.negative? &&
                                         macd_two.to_f.negative? &&
                                         macd_three.to_f.zero?
                                       )
                                     )

  end

  indicator_status.macd = indicator_hash
rescue StandardError => e
  raise e
end