Module: Coinbot::OrderBook::WrapUp

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

Class Method Summary collapse

Class Method Details

.candle(opts = {}) ⇒ Object

Supported Method Parameters

Coinbot::OrderBook::SMA.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
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
132
133
134
135
136
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
# File 'lib/coinbot/order_book/wrap_up.rb', line 12

public_class_method def self.candle(opts = {})
  option_choice = opts[:option_choice]
  env = opts[:env]
  terminal_win = opts[:terminal_win]
  event_history = opts[:event_history]
  indicator_history = opts[:indicator_history]
  bot_conf = opts[:bot_conf]

  last_ticker_price = event_history.order_book[:ticker_price].to_f
  this_product = event_history.order_book[:this_product]
  candles = event_history.order_book[:candles]
  # The Candle Period has Passed...
  # Set the close price to the last_order price
  # last_candle_open = candles.last[:candle_open].to_f
  # Override Candle Open, Low, High, Close Data
  # with Last Coinbase Record
  start_timestamp = Time.parse(
    candles.last[:begin_time]
  ).strftime('%Y-%m-%dT%H:%M%z')
  start_epoch = Time.parse(start_timestamp).strftime('%s').to_i

  # end_epoch = start_epoch + option_choice.candle_duration
  end_epoch = start_epoch + event_history.candle_duration
  end_timestamp = Time.at(end_epoch).strftime('%Y-%m-%dT%H:%M%z')

  rec_hist = Coinbot::API.get_candle_history(
    env: env,
    option_choice: option_choice,
    start_timestamp: start_timestamp,
    end_timestamp: end_timestamp
  )

  if rec_hist.any?
    coinbase_time = Time.at(
      rec_hist.last.first
    ).strftime('%Y-%m-%d %H:%M:%S.%N%z')
    candles.last[:begin_time] = coinbase_time
    candles.last[:candle_lowest] = rec_hist.last[1] 
    candles.last[:candle_highest] = rec_hist.last[2] 
    candles.last[:candle_open] = rec_hist.last[3] 
    last_ticker_price = candles.last[:candle_close] = rec_hist.last[4] 
    candles.last[:candle_volume] = rec_hist.last.last
  end

  last_rec_close = candles.last[:candle_close] if candles.length < 2
  last_rec_close = candles[-2][:candle_close] if candles.length > 1

  candles.last[:candle_open] = last_rec_close
  candles.last[:candle_lowest] = last_rec_close
  candles.last[:candle_highest] = last_rec_close
  candles.last[:candle_close] = last_rec_close

  if indicator_history.macd.any?
    uniq_macd_hist = indicator_history.macd.uniq { |h| h[:macd] unless h == '--' }
    candles.last[:macd_history] = uniq_macd_hist
    candles[..-9].each { |candle| candle[:macd_history] = [] }
  end
  indicator_history.macd = []

  if indicator_history.rsi.any?
    uniq_rsi_hist = indicator_history.rsi.uniq { |h| h[:rsi] unless h == '--' }
    candles.last[:rsi_history] = uniq_rsi_hist
    candles[..-9].each { |candle| candle[:rsi_history] = [] }
  end
  indicator_history.rsi = []

  # Only keep enough candles history in Order Book
  # to Calculate Custom EMA or Last 78 Candles
  # Whichever is greater.
  ema_high = bot_conf[:exponential_moving_avg][:high].to_i

  # Order Book Retention ---------------------------------------#
  # Defaults to 6 hours if candle_duration == 1 minute
  candle_retention = ema_high
  candle_retention = 180 if ema_high < 180
  candle_retention = ema_high if ema_high > 360
  if candles.length >= candle_retention
    # [retain_history..] << Not a typo - equivalent to
    # [retain_record..-1]
    retain_record = candle_retention * -1
    candle_history_arr = candles[retain_record..]
    candles = candle_history_arr
    event_history.order_book[:candles] = candles
  end

  order_just_retention = 900
  order_just_history = event_history.order_book[:order_justification_history]
  if order_just_history.length >= order_just_retention
    # [retain_history..] << Not a typo - equivalent to
    # [retain_record..-1]
    retain_record = order_just_retention * -1
    order_just_arr = order_just_history[retain_record..]
    order_just_history = order_just_arr
    event_history.order_book[:order_justification_history] = order_just_history
  end

  order_hist_retention = 900
  order_history = event_history.order_book[:order_history]
  if order_history.length >= order_hist_retention
    # [retain_history..] << Not a typo - equivalent to
    # [retain_record..-1]
    retain_record = order_hist_retention * -1
    order_hist_arr = order_history[retain_record..]
    order_history = order_hist_arr
    event_history.order_book[:order_history] = order_history
  end
  #-------------------------------------------------------------#

  # Recalculate Pie-in-the-Sky Recommendation
  tpm = bot_conf[:target_profit_margin_percent].to_f
  pie_buy_hash = candles.max_by do |candle|
    next if candle.nil?

    candle_lowest = candle[:candle_lowest].to_f
    candle_open = candle[:candle_open].to_f
    (1 - (candle_lowest / candle_open)) * 100 if candle_lowest.positive? &&
                                                 candle_open.positive?
  end
  pbh_low = pie_buy_hash[:candle_lowest].to_f
  pbh_open = pie_buy_hash[:candle_open].to_f
  pbh_percent = 0.00
  pbh_percent = ((1 - (pbh_low / pbh_open)) * 100) + tpm if pbh_low.positive? &&
                                                            pbh_open.positive?

  highest_pie_buy_p = format('%0.2f', pbh_percent)
  pie_buy_plus_tpm = format('%0.2f', highest_pie_buy_p.to_f + tpm)

  event_history.order_book[:highest_pie_in_sky_buy_percent] = highest_pie_buy_p

  pie_sell_hash = candles.max_by do |candle|
    next if candle.nil?

    candle_open = candle[:candle_open].to_f
    candle_highest = candle[:candle_highest].to_f
    (1 - (candle_open / candle_highest)) * 100 if candle_open.positive? &&
                                                  candle_highest.positive?
  end
  psh_open = pie_sell_hash[:candle_open].to_f
  psh_high = pie_sell_hash[:candle_highest].to_f
  psh_percent = 0.00
  psh_percent = ((1 - (psh_open / psh_high)) * 100) + tpm if psh_open.positive? &&
                                                             psh_high.positive?

  highest_pie_sell_p = format('%0.2f', psh_percent)
  pie_sell_plus_tpm = format('%0.2f', highest_pie_sell_p.to_f + tpm)
  event_history.order_book[:highest_pie_in_sky_sell_percent] = highest_pie_sell_p

  # Artificial Intelligence Feedback Loop
  # If the Bot Conf has artifical_intelligence: true
  # AND the last psh_percent > than the
  # target_profit_margin_percent value in the Bot Conf,
  # Update respective pie_in_sky values in bot conf,
  # Cancel any open Pie-in-the-Sky limit orders
  # AND reload the Bot Conf
  ai_enabled = bot_conf[:artifical_intelligence]
  current_pie_buy_p = bot_conf[:pie_in_sky_buy_percent].to_f
  current_pie_sell_p = bot_conf[:pie_in_sky_sell_percent].to_f
  if ai_enabled
    if pbh_percent > tpm
       # && pbh_percent > current_pie_buy_p

      Coinbot::BotConf.update(
        option_choice: option_choice,
        bot_conf: bot_conf,
        key: :pie_in_sky_buy_percent,
        value: pie_buy_plus_tpm
      )

      # Reload Bot Conf
      # terminal_win.key_press_event.key_r = true
    end

    if psh_percent > tpm
       # && psh_percent > current_pie_sell_p

      Coinbot::BotConf.update(
        option_choice: option_choice,
        bot_conf: bot_conf,
        key: :pie_in_sky_sell_percent,
        value: pie_sell_plus_tpm
      )

      # Reload Bot Conf
      # terminal_win.key_press_event.key_r = true
    end
  end

  # Refactor TPM to be 0.01 > than fee tier,
  # particularly as fee tier goes up or down
  fees = event_history.order_book[:fees]
  taker_rate = 0.5
  taker_rate = fees[:taker_fee_rate].to_f unless fees.empty?
  taker_fee = format('%0.2f', taker_rate * 100)
  min_tpm = (taker_fee.to_f * 2) + 0.01
  if ai_enabled && min_tpm != tpm
    bot_conf[:target_profit_margin_percent] = min_tpm.to_f
    Coinbot::BotConf.update(
      option_choice: option_choice,
      bot_conf: bot_conf,
      key: :target_profit_margin_percent,
      value: min_tpm.to_f
    )

    # Reload Bot Conf
    # terminal_win.key_press_event.key_r = true
  end

  # If fee tier amounts increase, be sure to refactor
  # Pie-in-Sky Buy/Sell Percent to be TPM + the Taker Fee
  # and cancel existing limit orders w/ previously
  # calculated Pie-in-Sky Sell Percent
  if ai_enabled && (
       min_tpm > current_pie_buy_p ||
       min_tpm > current_pie_sell_p
     )

    if min_tpm > current_pie_buy_p
      pie_in_sky_min = min_tpm.to_f + 1
      key = :pie_in_sky_sell_percent
      value = pie_in_sky_min
    end

    if min_tpm > current_pie_sell_p
      pie_in_sky_min = min_tpm.to_f + 1
      key = :pie_in_sky_sell_percent
      value = pie_in_sky_min
    end

    Coinbot::BotConf.update(
      option_choice: option_choice,
      bot_conf: bot_conf,
      key: key,
      value: value
    )

    # Reload Bot Conf
    # terminal_win.key_press_event.key_r = true
  end

  # If so inclined here is where GTT orders could be canceled
  # earlier than initially scheduled
  # after a certain duration (e.g. 5, 15, 30 mins)
  # last_order_update = Time.parse(event_history.order_book[:order_justification_history].last[:last_update])
  # diff_in_mins = ((Time.now - given_time) * 24 * 60).to_i

  # Reload Bot Conf
  terminal_win.key_press_event.key_r = true

  # Write Order Book to Disk
  terminal_win.key_press_event.key_w = true

  # TODO: If ! invested and current session is no
  # rated in the top N flash spikes / flash crashes
  # Create a new session w/ one of the Products
  # that's not running and terminate current session.

  # Create a new candle entry in order book via push
  candle_period = candles.last[:period]
  candle_period += 1
  candle_open = last_ticker_price
  candle_buy_tot = 0
  candle_sell_tot = 0

  new_candle = Coinbot::OrderBook::Generate.new_candle(
    candle_period: candle_period,
    begin_time: Time.now,
    candle_open: candle_open,
    candle_highest: 0.00,
    candle_lowest: 0.00,
    candle_close: 0.00,
    candle_volume: 0.00,
    candle_buy_tot: candle_buy_tot,
    candle_sell_tot: candle_sell_tot,
    macd_history: candles.last[:macd_history],
    rsi_history: candles.last[:rsi_history]
  )

  candles.push(new_candle)

  # Reset first candle back to default candle duration
  event_history.candle_duration = option_choice.candle_duration
rescue StandardError => e
  raise e
end

.helpObject

Display Usage for this Module



299
300
301
302
303
# File 'lib/coinbot/order_book/wrap_up.rb', line 299

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