Module: Coinbot::UI::Summary

Defined in:
lib/coinbot/ui/summary.rb

Overview

This plugin is used to Refresh the Coinbot Summary Section UI

Class Method Summary collapse

Class Method Details

.helpObject

Display Usage for this Module



465
466
467
468
469
470
471
# File 'lib/coinbot/ui/summary.rb', line 465

public_class_method def self.help
  puts "USAGE:
   #{self}.refresh(
     order_book: 'required - Order Book Data Structure'
   )
  "
end

.refresh(opts = {}) ⇒ Object

Supported Method Parameters

Coinbot::UI::Summary.refresh(

order_book: 'required - Order Book Data Structure'

)



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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
# File 'lib/coinbot/ui/summary.rb', line 14

public_class_method def self.refresh(opts = {})
  option_choice = opts[:option_choice]
  event_history = opts[:event_history]
  summary_win = opts[:summary_win]
  order_book = opts[:order_book]
  key_press_event = opts[:key_press_event]
  indicator_status = opts[:indicator_status]
  target_profit_margin_percent = opts[:target_profit_margin_percent]
  invested = opts[:invested]
  bot_conf = opts[:bot_conf]
  stuck_in_pos_status = opts[:stuck_in_pos_status]
  fiat_portfolio_file = opts[:fiat_portfolio_file]

  this_product = order_book[:this_product]
  quote_increment = this_product[:quote_increment]
  base_increment = this_product[:base_increment]

  fiat_smallest_decimal = quote_increment.to_s.split('.')[-1].length
  crypto_smallest_decimal = base_increment.to_s.split('.')[-1].length

  autotrade_portfolio_percent = bot_conf[:autotrade_portfolio_percent]

  fees = order_book[:fees]
  maker_fee = format('%0.2f', fees[:maker_fee_rate].to_f * 100)
  taker_fee = format('%0.2f', fees[:taker_fee_rate].to_f * 100)
  volume_tier = format('%0.2f', fees[:usd_volume].to_f)

  crypto_currency = option_choice.symbol.to_s.upcase.split('_').first.to_sym
  crypto_symbol = "\u2667"
  crypto_symbol = "\u0243" if crypto_currency == :BTC
  crypto_symbol = "\u2666" if crypto_currency == :ETH

  symbol_out = option_choice.symbol.to_s.gsub('_', '-').upcase.ljust(9, ' ')

  fiat = option_choice.symbol.to_s.upcase.split('_').last.to_sym
  fiat_symbol = '?'
  fiat_symbol = '$' if fiat == :USD
  fiat_symbol = "\u20ac" if fiat == :EUR

  order_history = order_book[:order_history]
  buy_orders_open = order_history.select do |orders|
    orders[:side] == 'buy' &&
      orders[:status] == 'open'
  end

  total_buy_orders_open = buy_orders_open.length
  projected_buy_total = 0.00
  if total_buy_orders_open.positive?
    # Calculate Projected Buy Total
    buy_orders_open.each do |this_open_buy_order|
      projected_buy_total += (this_open_buy_order[:price].to_f * this_open_buy_order[:size].to_f)
    end

    # Calculate Weighted Avg. of Open Buy Orders
    weighted_buy_prices_arr = []
    buy_orders_open.each do |order|
      weighted_hash = {}
      weighted_hash[:price] = order[:price].to_f
      weighted_hash[:size] = order[:size].to_f
      weighted_hash[:price_times_size] = order[:price].to_f * order[:size].to_f
      weighted_buy_prices_arr.push(weighted_hash)
    end

    avg_buy_scheduled = format("%0.#{fiat_smallest_decimal}f", (weighted_buy_prices_arr.sum { |orders| orders[:price_times_size].to_f } / weighted_buy_prices_arr.sum { |orders| orders[:size] })).to_f
  else
    avg_buy_scheduled = 0.00
  end

  buy_orders_done = order_history.select { |orders| orders[:side] == 'buy' && orders[:status] == 'done' && orders[:done_reason] == 'filled' }
  total_buy_orders_done = buy_orders_done.length
  if total_buy_orders_done.positive?
    # Calculate Weighted Avg. of Completed Buy Orders
    weighted_buy_prices_arr = []
    buy_orders_done.each do |order|
      weighted_hash = {}
      weighted_hash[:price] = order[:executed_value].to_f / order[:filled_size].to_f
      weighted_hash[:filled_size] = order[:filled_size].to_f
      weighted_hash[:price_times_filled_size] = (order[:executed_value].to_f / order[:filled_size].to_f) * order[:filled_size].to_f
      weighted_buy_prices_arr.push(weighted_hash)
    end
    avg_buy_executed = format("%0.#{fiat_smallest_decimal}f", (weighted_buy_prices_arr.sum { |orders| orders[:price_times_filled_size].to_f } / weighted_buy_prices_arr.sum { |orders| orders[:filled_size] })).to_f
    total_buy_executed = format('%0.2f', buy_orders_done.sum { |orders| orders[:executed_value].to_f }).to_f
    buy_fees = buy_orders_done.sum { |orders| orders[:fill_fees].to_f }
    total_invested = format('%0.2f', total_buy_executed.to_f - buy_fees.to_f).to_f
  else
    avg_buy_executed = format("%0.#{fiat_smallest_decimal}f", 0.00).to_f
    total_buy_executed = 0.00
    total_invested = 0.00
    buy_fees = 0.00
  end
  buy_fees_out = "#{fiat_symbol}#{format('%0.2f', buy_fees)}"
  order_book[:avg_buy_executed] = avg_buy_executed

  sell_orders_open = order_history.select do |orders|
    orders[:side] == 'sell' && orders[:status] == 'open'
  end

  total_sell_orders_open = sell_orders_open.length
  projected_sell_total = 0.00
  if total_sell_orders_open.positive?
    # Calculate Projected Sell Total
    sell_orders_open.each { |this_open_sell_order| projected_sell_total += (this_open_sell_order[:price].to_f * this_open_sell_order[:size].to_f) }

    # Calculate Weighted Avg. of Open Sell Orders
    weighted_sell_prices_arr = []
    sell_orders_open.each do |order|
      weighted_hash = {}
      weighted_hash[:price] = order[:price].to_f
      weighted_hash[:size] = order[:size].to_f
      weighted_hash[:price_times_size] = order[:price].to_f * order[:size].to_f
      weighted_sell_prices_arr.push(weighted_hash)
    end
    avg_sell_scheduled = format("%0.#{fiat_smallest_decimal}f", (weighted_sell_prices_arr.sum { |orders| orders[:price_times_size].to_f } / weighted_sell_prices_arr.sum { |orders| orders[:size] })).to_f
  else
    avg_sell_scheduled = 0.00
  end

  sell_orders_done = order_history.select { |orders| orders[:side] == 'sell' && orders[:status] == 'done' && orders[:done_reason] == 'filled' }
  total_sell_orders_done = sell_orders_done.length
  if total_sell_orders_done.positive?
    # Calculate Weighted Avg. of Completed Sell Orders
    weighted_sell_prices_arr = []
    sell_orders_done.each do |order|
      weighted_hash = {}
      weighted_hash[:price] = order[:executed_value].to_f / order[:filled_size].to_f
      weighted_hash[:filled_size] = order[:filled_size].to_f
      weighted_hash[:price_times_filled_size] = (order[:executed_value].to_f / order[:filled_size].to_f) * order[:filled_size].to_f
      weighted_sell_prices_arr.push(weighted_hash)
    end
    avg_sell_executed = format("%0.#{fiat_smallest_decimal}f", (weighted_sell_prices_arr.sum { |orders| orders[:price_times_filled_size].to_f } / weighted_sell_prices_arr.sum { |orders| orders[:filled_size] })).to_f
    total_sell_executed = format('%0.2f', sell_orders_done.sum { |orders| orders[:executed_value].to_f }).to_f
    sell_fees = sell_orders_done.sum { |orders| orders[:fill_fees].to_f }
    total_return = format('%0.2f', total_sell_executed.to_f - sell_fees.to_f).to_f
  else
    avg_sell_executed = format("%0.#{fiat_smallest_decimal}f", 0.00).to_f
    total_sell_executed = format('%0.2f', 0.00).to_f
    total_return = format('%0.2f', 0.00).to_f
    sell_fees = 0.00
  end
  sell_fees_out = "#{fiat_symbol}#{format('%0.2f', sell_fees)}"
  order_book[:avg_sell_executed] = avg_sell_executed

  total_profit = total_return.to_f
  total_profit = (total_return - total_invested).to_f if total_return.positive? && total_invested.positive?

  total_profit_color = :yellow
  total_profit_color = :green if total_profit.positive?
  total_profit_color = :red if total_profit.negative?

  total_invested_out = "#{fiat_symbol}#{format('%0.2f', total_invested)}"
  total_return_out = "#{fiat_symbol}#{format('%0.2f', total_return)}"
  total_profit_out = "#{fiat_symbol}#{format('%0.2f', total_profit)}"
  total_buy_executed_out = "#{fiat_symbol}#{format('%0.2f', total_buy_executed)}"
  total_sell_executed_out = "#{fiat_symbol}#{format('%0.2f', total_sell_executed)}"

  # TODO: Add an avg_investment_fee_percent_out variable
  # avg_investment_fee_percent = (buy_fees / total_buy_executed) * 100
  # avg_investment_fee_percent_out = "#{format('%0.4f', avg_investment_fee_percent)}%"

  # TODO: Add an avg_sell_fee_percent variable
  # avg_sell_fee_percent = (sell_fees / total_sell_executed) * 100
  # avg_sell_fee_percent_out = "#{format('%0.4f', avg_sell_fee_percent)}%"

  avg_buy_scheduled_out = "#{fiat_symbol}#{format("%0.#{fiat_smallest_decimal}f", avg_buy_scheduled)}"
  avg_buy_executed_out = "#{fiat_symbol}#{format("%0.#{fiat_smallest_decimal}f", avg_buy_executed)}"

  avg_sell_scheduled_out = "#{fiat_symbol}#{format("%0.#{fiat_smallest_decimal}f", avg_sell_scheduled)}"
  avg_sell_executed_out = "#{fiat_symbol}#{format("%0.#{fiat_smallest_decimal}f", avg_sell_executed)}"

  projected_sell_price = projected_sell_total.to_f
  projected_sell_price_out = "#{fiat_symbol}#{format('%0.2f', projected_sell_price)}"
  projected_sell_fee = (projected_sell_total * (taker_fee.to_f / 100)).to_f
  projected_sell_fee_out = "#{fiat_symbol}#{format('%0.2f', projected_sell_fee)}"
  projected_sell_difference = (projected_sell_price - projected_sell_fee).to_f
  projected_sell_difference_out = "#{fiat_symbol}#{format('%0.2f', projected_sell_difference)}"

  total_projected_return = (projected_sell_difference + total_return).to_f
  total_projected_return_out = "#{fiat_symbol}#{format('%0.2f', total_projected_return)}"
  total_profit_projected = (total_projected_return - total_invested).to_f
  total_profit_projected_out = "#{fiat_symbol}#{format('%0.2f', total_profit_projected)}"

  avg_profit_margin_percent = 100 - ((total_invested / total_projected_return) * 100)

  portfolio = order_book[:portfolio]
   = portfolio.select do ||
    [:currency] == crypto_currency.to_s
  end
  raise "ID for Crypto Currency, #{crypto_currency} Not Found" if .empty?

  balance = format("%0.#{crypto_smallest_decimal}f", .first[:balance])
  avail_for_trade = format("%0.#{crypto_smallest_decimal}f", .first[:available])

  fiat_portfolio = event_history.order_book[:fiat_portfolio]
  fiat_balance = format('%0.2f', fiat_portfolio.first[:balance])
  fiat_avail_for_trade = format('%0.2f', fiat_portfolio.first[:available])

  # TODO: Potential for RACE CONDITIONS - MIGRATE TO
  # Coinbot::Event.parse
  Coinbot::OrderBook::WeightedAvg.status(
    order_book: order_book,
    order_history: order_history,
    indicator_status: indicator_status,
    invested: invested
  )

  Coinbot::OrderBook::ProfitMargin.status(
    target_profit_margin_percent: target_profit_margin_percent,
    total_invested: total_invested,
    total_projected_return: total_projected_return,
    indicator_status: indicator_status,
    invested: invested
  )

  current_symbol_val = format("%0.#{fiat_smallest_decimal}f", stuck_in_pos_status[:current_symbol_value].to_f)
  total_invested_in_symbol_w_target_pm = format("%0.#{fiat_smallest_decimal}f", stuck_in_pos_status[:total_invested_in_symbol_w_target_pm].to_f)
  target_symbol_price = format("%0.#{fiat_smallest_decimal}f", stuck_in_pos_status[:target_symbol_price].to_f)

  autotrade_fiat_bal = "#{autotrade_portfolio_percent}% of #{fiat_symbol}#{fiat_balance}"
  fiat_avail_out = "#{fiat_symbol}#{fiat_avail_for_trade}"
  # TODO: Everything Above this Line Needs to be Indicators ^

  # UI
  col_just3 = (Curses.cols - Coinbot::UI.col_third) - 1

  key_press_event = Coinbot::UI.detect_key_press_in_ui(
    key_press_event: key_press_event,
    ui_win: summary_win
  )

  # ROW 1
  out_line_no = 0
  Coinbot::UI.line(
    ui_win: summary_win,
    out_line_no: out_line_no
  )

  # ROW 2
  out_line_no += 1
  summary_win.setpos(out_line_no, Coinbot::UI.col_first)
  summary_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :yellow,
    style: :bold,
    string: 'All-Time Invested - Fees:'
  )

  summary_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :yellow,
    string: "#{total_buy_executed_out} - #{buy_fees_out} = #{total_invested_out}".rjust(col_just3, '.')
  )

  # ROW 3
  out_line_no += 1
  summary_win.setpos(out_line_no, Coinbot::UI.col_first)
  summary_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :cyan,
    style: :bold,
    string: 'All-Time Returned - Fees:'
  )

  summary_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :cyan,
    string: "#{total_sell_executed_out} - #{sell_fees_out} = #{total_return_out}".rjust(col_just3, '.')
  )

  # ROW 4
  out_line_no += 1
  summary_win.setpos(out_line_no, Coinbot::UI.col_first)
  summary_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: total_profit_color,
    string: 'All-Time Profit:'
  )

  summary_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: total_profit_color,
    string: total_profit_out.to_s.rjust(col_just3, '.')
  )

  # ROW 5
  out_line_no += 1
  summary_win.setpos(out_line_no, Coinbot::UI.col_first)
  summary_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :red,
    style: :bold,
    string: 'Maker & Taker Fees | My Volume:'
  )

  summary_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :red,
    string: "#{maker_fee}% & #{taker_fee}% | $#{volume_tier}".rjust(col_just3, '.')
  )

  # ROW 6
  out_line_no += 1
  summary_win.setpos(out_line_no, Coinbot::UI.col_first)
  summary_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :yellow,
    style: :bold,
    string: 'Last Order Created At:'
  )

  summary_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :yellow,
    string: Time.parse(
      order_history.first[:created_at]
    ).localtime.strftime('%Y-%m-%d %H:%M:%S%z').rjust(col_just3, '.')
  )

  # ROW 7
  out_line_no += 1
  summary_win.setpos(out_line_no, Coinbot::UI.col_first)
  summary_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :yellow,
    style: :bold,
    string: 'Limit Buy Open | VWAP Exec:'
  )

  summary_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :yellow,
    string: "#{avg_buy_scheduled_out} | #{avg_buy_executed_out}".rjust(col_just3, '.')
  )

  # ROW 7
  out_line_no += 1
  summary_win.setpos(out_line_no, Coinbot::UI.col_first)
  summary_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :cyan,
    style: :bold,
    string: 'Limit Sell Open | VWAP Exec:'
  )

  summary_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :cyan,
    string: "#{avg_sell_scheduled_out} | #{avg_sell_executed_out}".rjust(col_just3, '.')
  )

  # ROW 8
  out_line_no += 1
  summary_win.setpos(out_line_no, Coinbot::UI.col_first)
  summary_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :magenta,
    string: 'Profit Margin of TPM:'
  )

  summary_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :magenta,
    string: "#{format('%0.2f', avg_profit_margin_percent)}% of #{format('%0.2f', target_profit_margin_percent)}%".rjust(col_just3, '.')
  )

  # ROW 9
  out_line_no += 1
  Coinbot::UI.line(
    ui_win: summary_win,
    out_line_no: out_line_no
  )

  # ROW 10
  out_line_no += 1
  summary_win.setpos(out_line_no, Coinbot::UI.col_first)
  summary_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :white,
    style: :bold,
    string: 'Crypto Bal | Tradeable:'
  )

  summary_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :white,
    string: "*#{balance} | *#{avail_for_trade}".rjust(col_just3, '.')
  )

  # ROW 11
  out_line_no += 1
  summary_win.setpos(out_line_no, Coinbot::UI.col_first)
  summary_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :yellow,
    style: :bold,
    string: 'Crypto Val | Target Bal @ Price:'
  )

  summary_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :yellow,
    string: "$#{current_symbol_val} | $#{total_invested_in_symbol_w_target_pm} @ $#{target_symbol_price}".rjust(col_just3, '.')
  )

  # ROW 12
  out_line_no += 1
  summary_win.setpos(out_line_no, Coinbot::UI.col_first)
  summary_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :white,
    style: :bold,
    string: "Autotrade % of #{fiat} Bal | Tradeable:"
  )

  summary_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: summary_win,
    color: :white,
    string: "#{autotrade_fiat_bal} | #{fiat_avail_out}".rjust(col_just3, '.')
  )

  summary_win.refresh
rescue Interrupt
  # Exit Gracefully if CTRL+C is Pressed During Session
  Coinbot.exit_gracefully(which_self: self)
rescue StandardError => e
  raise e
end