Module: Cryptum::UI::Order::Execute

Defined in:
lib/cryptum/ui/order/execute.rb

Overview

Update the Execute Section of the UI (When Active)

Class Method Summary collapse

Class Method Details

.helpObject

Display Usage for this Module



683
684
685
686
687
688
689
690
# File 'lib/cryptum/ui/order/execute.rb', line 683

public_class_method def self.help
  puts "USAGE:
   #{self}.refresh(
     order_book: 'required - Order Book Data Structure',
     event: 'required - Event from Coinbase Web Socket'
   )
  "
end

.refresh(opts = {}) ⇒ Object

Supported Method Parameters

Cryptum::UI::Order::Execute.refresh(

order_book: 'required - Order Book Data Structure',
event: 'required - Event from Coinbase Web Socket'

)



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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
# File 'lib/cryptum/ui/order/execute.rb', line 17

public_class_method def self.refresh(opts = {})
  option_choice = opts[:option_choice]
  order_execute_win = opts[:order_execute_win]
  env = opts[:env]
  event_history = opts[:event_history]
  indicator_status = opts[:indicator_status]
  bot_conf = opts[:bot_conf]

  event_type = event_history.event_type
  event_side = event_history.event[:side].to_s.to_sym
  event_reason = event_history.event[:reason].to_s.to_sym

  ticker_price = event_history.order_book[:ticker_price].to_f
  open_24h = event_history.order_book[:open_24h].to_f
  this_product = event_history.order_book[:this_product]
  min_market_funds = this_product[:min_market_funds]
  base_increment = this_product[:base_increment]
  quote_increment = this_product[:quote_increment]
  crypto_smallest_decimal = base_increment.to_s.split('.')[-1].length
  fiat_smallest_decimal = quote_increment.to_s.split('.')[-1].length

  last_three_prices_arr = []
  last_ticker_price = event_history.order_book[:ticker_price].to_f
  second_to_last_ticker_price = event_history.order_book[:ticker_price_second_to_last].to_f
  third_to_last_ticker_price = event_history.order_book[:ticker_price_third_to_last].to_f
  last_three_prices_arr.push(last_ticker_price)
  last_three_prices_arr.push(second_to_last_ticker_price)
  last_three_prices_arr.push(third_to_last_ticker_price)
  limit_price = last_three_prices_arr.sort[1]
  return event_history unless limit_price.positive?

  # tpm = bot_conf[:target_profit_margin_percent].to_f
  # tpm_cast_as_decimal = tpm / 100

  order_history_meta = event_history.order_book[:order_history_meta]
  order_history = event_history.order_book[:order_history]

  margin_percent_open_24h = (1 - (open_24h / ticker_price)) * 100
  cast_margin_to_sec = margin_percent_open_24h * 0.1

  # Reset times to max or default depending on
  # BULL or BEAR market trend
  event_history.bullish_trend = true if cast_margin_to_sec.positive?
  event_history.bullish_trend = false if cast_margin_to_sec.negative?

  buy_total = event_history.order_book[:market_trend][:buy].to_i
  sell_total = event_history.order_book[:market_trend][:sell].to_i

  if event_history.order_book[:order_plan].length.positive?
    if event_history.order_ready
      event_history.order_book[:last_order_exec] = Time.now.strftime(
        '%Y-%m-%d %H:%M:%S%z'
      )
    end

    # BUY
    # Reset times to max or default depending on
    # BULL or BEAR market trend
    event_history.time_between_orders = event_history.time_between_orders_max if buy_total >= sell_total && !event_history.bullish_trend

    event_history.time_between_orders = event_history.time_between_orders_reset if sell_total > buy_total && !event_history.bullish_trend

    if event_history.order_ready &&
       indicator_status.action_signal == :buy &&
       !event_history.red_pill

      this_order = event_history.order_book[:order_plan].first

      # If price is greater than the lowest selling
      # limit order, force it to be slightly less
      last_order = order_history_meta.reverse.find { |o| o[:color].to_sym == :yellow }
      # lowest_selling_price = event_history.lowest_selling_price
      event_history.last_purchase_price = last_order[:price].to_f if last_order
      last_purchase_price = event_history.last_purchase_price

      limit_price = last_purchase_price - quote_increment.to_f if last_purchase_price.positive? && last_purchase_price < limit_price

      price = format(
        "%0.#{fiat_smallest_decimal}f",
        limit_price
      )

      # Debug last order -------------------------------#
      # TODO: if the order attempt is unsucessful at the adjusted price,
      # the order plan needs to be reset.
      debug_last_order = "Last Order: #{last_order}\n"
      debug_last_order += "Last Purchase Price: #{last_purchase_price}\n"
      debug_last_order += "Limit Price (Should be <= #{last_purchase_price}): #{price}"
      debug_last_order += "\n\n\n"

      Cryptum::Log.append(
        level: :debug,
        msg: debug_last_order,
        which_self: self,
        event_history: event_history
      )
      #-------------------------------------------------#

      size = this_order[:invest].to_f / limit_price

      loop do
        size = size.to_f + base_increment.to_f
        break if (size.to_f * price.to_f) > min_market_funds.to_f
      end

      size = format(
        "%0.#{crypto_smallest_decimal}f",
        size
      )

      fiat_invested_this_order = size.to_f * price.to_f

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

      event_history.red_pill = true if fiat_invested_this_order > fiat_avail_to_trade.to_f

      unless event_history.red_pill
        event_history = Cryptum::Event::Buy.crypto(
          option_choice: option_choice,
          env: env,
          price: price,
          size: size,
          event_history: event_history,
          bot_conf: bot_conf
        )
        order_history = event_history.order_book[:order_history]
        order_history_meta = event_history.order_book[:order_history_meta]
      end

      # SAUCE 4 (SAUCE 3 RELATES TO SAUCE 4)
      # Time between orders
      # Increment n Seconds between buys to
      # account for bearish and bullish trends
      dynamic_time_increment = cast_margin_to_sec * -1

      # Lets also take balance into play
      balance_as_arbitrary_float = fiat_avail_to_trade.to_f / 1_000_000
      tbo = dynamic_time_increment - balance_as_arbitrary_float

      event_history.time_between_orders += tbo

      # Time between orders should never
      # be less than event_history.time_between_orders_min
      event_history.time_between_orders = event_history.time_between_orders_min if event_history.time_between_orders < event_history.time_between_orders_min
      # Time between orders should never
      # be more than event_history.time_between_orders_max
      event_history.time_between_orders = event_history.time_between_orders_max if event_history.time_between_orders > event_history.time_between_orders_max
    end
  end

  # Update Orders w/ Colors
  if event_type == :open &&
     event_side == :buy

    buy_order_id = event_history.event[:order_id]
    order_history_meta.each do |meta|
      meta[:color] = :red if meta[:buy_order_id] == buy_order_id
    end
  end

  if event_type == :done &&
     event_side == :buy &&
     event_reason == :canceled

    # Remove all white hashes from order_history_meta
    order_history_meta.reject! do |ohm|
      ohm[:color].to_sym == :white
    end

    buy_order_id = event_history.event[:order_id]
    order_history_meta.each do |ohm|
      next unless ohm[:buy_order_id] == buy_order_id

      ohm[:done_at] = Time.now.strftime('%Y-%m-%d %H:%M:%S%z')
      ohm[:color] = :white
    end
  end

  if event_type == :done &&
     event_side == :buy &&
     event_reason != :canceled

    order_ready_to_sell_arr = order_history_meta.select do |meta|
      meta[:buy_order_id] == event_history.event[:order_id]
    end

    if order_ready_to_sell_arr.length.positive?
      order_ready_to_sell = order_ready_to_sell_arr.first
      buy_order_id = order_ready_to_sell[:buy_order_id]

      price = format(
        "%0.#{fiat_smallest_decimal}f",
        order_ready_to_sell[:target_price]
      )

      size = order_ready_to_sell[:size]

      event_history = Cryptum::Event::Sell.crypto(
        option_choice: option_choice,
        env: env,
        price: price,
        size: size,
        event_history: event_history,
        bot_conf: bot_conf,
        buy_order_id: buy_order_id
      )
      order_history = event_history.order_book[:order_history]
      order_history_meta = event_history.order_book[:order_history_meta]
    end
  end

  # Update Canceled Sell Orders w/ White &&
  # Include done_at Timestamp for 24h gain calc
  if event_type == :done &&
     event_side == :sell &&
     event_reason == :canceled

    sell_order_id = event_history.event[:order_id]
    order_history_meta.each do |meta|
      next unless meta[:sell_order_id] == sell_order_id

      meta[:done_at] = Time.now.strftime('%Y-%m-%d %H:%M:%S%z')

      # Reinitiate GTFO if the previous GTFO Order Expires.
      terminal_win.key_press_event.key_g = true if meta[:color] == :magenta
      meta[:color] = :white
    end
  end

  # Snag all orders bought YTD
  # oh_buy_ytd_arr = order_history.select do |oh|
  #   oh[:side] == 'buy' &&
  #     oh.key?(:done_at) &&
  #     Time.now.strftime('%Y') == Time.parse(oh[:done_at]).strftime('%Y')
  # end
  # order_hist_buy_usd_tot = oh_buy_ytd_arr.map do |oh|
  #   oh[:executed_value].to_f
  # end.sum

  # Snag all orders sold YTD
  oh_sold_ytd_arr = order_history.select do |oh|
    oh[:side] == 'sell' &&
      oh.key?(:done_at) &&
      Time.now.strftime('%Y') == Time.parse(oh[:done_at]).strftime('%Y')
  end
  # TODO: ensure oh_buy_ytd_arr && oh_sold_ytd_arr are same length
  # otherwise it throws off the profit calculations
  order_hist_sold_ytd = oh_sold_ytd_arr.length
  # order_hist_sold_usd_tot = oh_sold_ytd_arr.map do |oh|
  #   oh[:executed_value].to_f
  # end.sum
  # order_hist_profit_usd_ytd = order_hist_sold_usd_tot - order_hist_buy_usd_tot
  # order_hist_profit_usd_ytd_out = format('%0.2f', order_hist_profit_usd_ytd)

  # Snag all session orders sold YTD
  oh_meta_sold_arr = order_history_meta.select do |ohm|
    ohm[:color].to_sym == :green &&
      ohm.key?(:done_at) &&
      Time.now.strftime('%Y') == Time.parse(ohm[:done_at]).strftime('%Y')
  end
  order_hist_meta_sold = oh_meta_sold_arr.length

  # Update Completed Sell Orders w/ Green &&
  # Include done_at Timestamp for 24h gain calc
  if event_type == :done &&
     event_side == :sell &&
     event_reason != :canceled

    sell_order_id = event_history.event[:order_id]
    order_history_meta.each do |meta|
      next unless meta[:sell_order_id] == sell_order_id

      learning_arr = []
      oh_meta_sold_arr.each do |ohm|
        learning = ohm
        buy_order_id = ohm[:buy_order_id]
        sell_order_id = ohm[:sell_order_id]
        order_history_buy_details = order_history.select { |oh| oh[:id] == buy_order_id }
        order_history_sell_details = order_history.select { |oh| oh[:id] == sell_order_id }
        learning[:buy_details] = order_history_buy_details.first
        learning[:sell_details] = order_history_sell_details.first
        learning_arr.push(learning)
      end

      # oh_sold = order_history.find { |oh| oh[:id] == sell_order_id }
      # meta[:done_at] = Time.parse(oh_sold[:done_at]).strftime('%Y-%m-%d %H:%M:%S%z') unless oh_sold.nil?
      meta[:done_at] = meta[:sell_details][:done_at] unless meta[:sell_details].nil?
      meta[:done_at] ||= Time.now.strftime('%Y-%m-%d %H:%M:%S%z')
      meta[:color] = :green

      # Obtain buy && sell order details from order history
      # using buy && sell IDs from respective meta object
      # and append to JSON log to allow for machine learning.
      buy_order_id = meta[:buy_order_id]
      learning = meta
      order_history_buy_details = order_history.select { |oh| oh[:id] == buy_order_id }
      order_history_sell_details = order_history.select { |oh| oh[:id] == sell_order_id }
      learning[:buy_details] = order_history_buy_details.first
      learning[:sell_details] = order_history_sell_details.first
      learning_arr.push(learning)

      Cryptum::Log.append(
        level: :learning,
        msg: learning_arr,
        which_self: self,
        event_history: event_history
      )
    end
  end

  # Sync up sell order completion dates if possible
  out_of_sync_green_orders = oh_meta_sold_arr.reject do |ohm|
    ohm[:done_at] == ohm[:sell_details][:done_at]
  end
  out_of_sync_green_orders.each do |ohm|
    ohm[:done_at] = ohm[:sell_details][:done_at]
  end

  # OK, now let's tally up everything...
  twenty_four_hrs_ago = Time.now - 86_400

  # Snag all orders bought within past 24 hrs
  # oh_buy_twenty_four_arr = []
  # unless oh_buy_ytd_arr.empty?
  #   oh_buy_twenty_four_arr = oh_buy_ytd_arr.select do |o|
  #     Time.parse(o[:done_at]) >= twenty_four_hrs_ago
  #   end
  # end
  # order_hist_buy_twenty_four = oh_buy_twenty_four_arr.length

  # Snag all orders sold within past 24 hrs
  oh_sold_twenty_four_arr = []
  unless oh_sold_ytd_arr.empty?
    oh_sold_twenty_four_arr = oh_sold_ytd_arr.select do |o|
      Time.parse(o[:done_at]) >= twenty_four_hrs_ago
    end
  end
  order_hist_sold_twenty_four = oh_sold_twenty_four_arr.length

  # Calculate 24h gain
  # order_hist_buy_usd_24h = oh_buy_twenty_four_arr.map do |oh|
  #   oh[:excuted_value].to_f
  # end.sum

  # order_hist_sold_usd_24h = oh_sold_twenty_four_arr.map do |oh|
  #   oh[:executed_value].to_f
  # end.sum
  # order_hist_profit_usd_24h = order_hist_sold_usd_24h - order_hist_buy_usd_24h
  # order_hist_profit_usd_24h_out = format('%0.2f', order_hist_profit_usd_24h)

  # Snag all session orders sold within past 24 hrs
  ohm_sold_twenty_four_arr = []
  unless oh_meta_sold_arr.empty?
    ohm_sold_twenty_four_arr = oh_meta_sold_arr.select do |o|
      Time.parse(o[:sell_details][:done_at]) >= twenty_four_hrs_ago
    end
  end
  order_hist_meta_sold_twenty_four = ohm_sold_twenty_four_arr.length

  # Calculate Average Round Trip for Sold Orders
  # (i.e. Time between buy && sell dates)
  avg_round_trip_days = 0
  unless oh_meta_sold_arr.empty?
    round_trip_aggregate = 0
    oh_meta_sold_arr.each do |ohm|
      buy_date = Time.parse(ohm[:buy_details][:done_at])
      sell_date = Time.parse(ohm[:sell_details][:done_at])
      round_trip_aggregate += sell_date - buy_date
    end
    avg_round_trip_days = (round_trip_aggregate / oh_meta_sold_arr.length) / 86_400.0
  end
  avg_round_trip_days_out = format('%.2f', avg_round_trip_days)
  # Snag all expired orders
  # oh_meta_expired_arr = order_history_meta.select do |ohm|
  #   ohm[:color].to_sym == :white && ohm.key?(:done_at)
  # end
  # order_hist_meta_expired = oh_meta_expired_arr.length

  # Snag all expired orders within past 24 hrs
  # ohm_expire_twenty_four_arr = []
  # unless oh_meta_expired_arr.empty?
  #   ohm_expire_twenty_four_arr = oh_meta_expired_arr.select do |o|
  #     Time.parse(o[:done_at]) >= twenty_four_hrs_ago
  #   end
  # end
  # order_hist_meta_24h_expired = ohm_expire_twenty_four_arr.length

  # Calculate session gains YTD
  sess_gains_sum = oh_meta_sold_arr.map do |ohms|
    ohms[:profit].to_f
  end.sum

  sess_gains_out = Cryptum.beautify_large_number(
    value: format(
      '%0.2f',
      sess_gains_sum
    )
  )

  # Calculate session gains within past 24 hrs
  sess_gains_24h_sum = ohm_sold_twenty_four_arr.map do |ohms|
    ohms[:profit].to_f
  end.sum

  sess_gains_24h_out = Cryptum.beautify_large_number(
    value: format(
      '%0.2f',
      sess_gains_24h_sum
    )
  )

  total_to_sell = order_history.select do |oh|
    oh[:status].to_sym == :open && oh[:side].to_sym == :sell
  end.length

  event_history.open_sell_orders = total_to_sell
  oh_meta_total_selling_profit_arr = order_history_meta.select do |ohm|
    ohm[:color].to_sym == :yellow
  end
  total_selling_this_session = oh_meta_total_selling_profit_arr.length

  total_selling_profit = oh_meta_total_selling_profit_arr.map do |ohms|
    ohms[:profit].to_f
  end.sum

  total_selling_profit_out = Cryptum.beautify_large_number(
    value: format(
      '%0.2f',
      total_selling_profit
    )
  )

  # TODO: when event_history.open_sell_orders > event_history.open_sell_orders_max
  # Capture highest amount to sell, cancel all orders, and create _one_ limit sell
  # order set to a price of the previously captured highest amount to sell.
  event_history.open_sell_orders_merge = true if total_to_sell > event_history.open_sell_orders_max

  # TODO: Everything Above this Line Needs to be Indicators ^

  # UI
  col_just1 = Curses.cols - Cryptum::UI.col_first

  # ROW 1
  out_line_no = 0
  header_color = :white
  header_style = :bold
  style = :bold
  if event_history.order_execute_win_active
    header_color = :blue
    header_style = :reverse
  end

  Cryptum::UI.line(
    ui_win: order_execute_win,
    out_line_no: out_line_no
  )

  # ROW 2
  out_line_no += 1
  order_execute_win.setpos(out_line_no, Cryptum::UI.col_first)
  order_execute_win.clrtoeol
  Cryptum::UI.colorize(
    ui_win: order_execute_win,
    color: header_color,
    style: header_style,
    string: ''.ljust(col_just1, ' ')
  )

  header_str = '- ORDER HISTORY -'
  order_execute_win.setpos(
    out_line_no,
    Cryptum::UI.col_center(str: header_str)
  )

  Cryptum::UI.colorize(
    ui_win: order_execute_win,
    color: header_color,
    style: header_style,
    string: header_str
  )

  # ROWS 3-10
  remaining_blank_rows = 0
  max_rows_to_display = event_history.order_execute_max_rows_to_display
  remaining_blank_rows = max_rows_to_display if order_history_meta.empty?
  first_row = event_history.order_execute_index_offset
  last_row = first_row + max_rows_to_display
  if last_row >= order_history_meta.length
    last_row = order_history_meta.length - 1
    event_history.order_execute_max_records_available_to_display = last_row if last_row < max_rows_to_display
    first_row = last_row - event_history.order_execute_max_records_available_to_display
    event_history.order_execute_index_offset = first_row
    remaining_blank_rows = max_rows_to_display - last_row
  end

  if order_history_meta.any?
    selected_order = event_history.order_execute_selected_data
    order_history_meta.reverse[first_row..last_row].each do |meta|
      out_line_no += 1
      current_line = out_line_no - 2

      style = :normal
      if event_history.order_execute_row_to_select == current_line
        style = :highlight
        selected_order = meta
        selected_order[:color] = meta[:color]
      end

      invest_out = Cryptum.beautify_large_number(
        value: meta[:invest]
      )
      price_out = Cryptum.beautify_large_number(
        value: meta[:price]
      )
      size_out = Cryptum.beautify_large_number(
        value: meta[:size]
      )
      target_price_out = Cryptum.beautify_large_number(
        value: meta[:target_price]
      )
      plan_no = meta[:plan_no]

      order_exec_last_ln = '____-__-__ __:__:__-____'
      case meta[:color].to_sym
      when :cyan, :red
        buy_created_at_hash_arr = order_history.select do |oh|
          oh[:id] == meta[:buy_order_id]
        end

        unless buy_created_at_hash_arr.empty?
          order_exec_last_ln = Time.parse(
            buy_created_at_hash_arr.first[:created_at]
          ).strftime('%Y-%m-%d %H:%M:%S%z')
        end
      when :green, :white
        if meta[:done_at]
          order_exec_last_ln = Time.parse(
            meta[:done_at]
          ).strftime('%Y-%m-%d %H:%M:%S%z')
        end
      when :magenta, :yellow
        sell_created_at_hash_arr = order_history.select do |oh|
          oh[:id] == meta[:sell_order_id]
        end

        unless sell_created_at_hash_arr.empty?
          order_exec_last_ln = Time.parse(
            sell_created_at_hash_arr.first[:created_at]
          ).strftime('%Y-%m-%d %H:%M:%S%z')
        end
      end

      invest = "$#{invest_out} @ "
      tick = "$#{price_out} = "
      size = "*#{size_out} + "
      tpm_out = "#{meta[:tpm]}% = "
      targ_tick = "$#{target_price_out}"
      profit = "|Profit: $#{meta[:profit]}"

      order_exec_ln = "#{order_exec_last_ln}|#{invest}#{tick}#{size}#{tpm_out}#{targ_tick}#{profit}|#{plan_no}"

      order_execute_win.setpos(out_line_no, Cryptum::UI.col_first)
      order_execute_win.clrtoeol
      Cryptum::UI.colorize(
        ui_win: order_execute_win,
        color: meta[:color],
        style: style,
        string: order_exec_ln.ljust(col_just1, '.')
      )
    end
    event_history.order_execute_selected_data = selected_order
  end

  # Clear to SUMMARY
  # (Only Applicable if order_book[:order_history_meta] < max_rows_to_display)
  # out_line_no += 1
  if remaining_blank_rows.positive?
    rows_to_blank = out_line_no + remaining_blank_rows
    out_line_no += 1
    (out_line_no..rows_to_blank).each do |clr_line|
      out_line_no = clr_line
      order_execute_win.setpos(clr_line, Cryptum::UI.col_first)
      order_execute_win.clrtoeol
      Cryptum::UI.colorize(
        ui_win: order_execute_win,
        color: :white,
        style: :normal,
        string: ''.ljust(col_just1, ' ')
      )
    end
  end

  # ROW 10
  out_line_no += 1
  order_execute_win.setpos(out_line_no, Cryptum::UI.col_first)
  order_execute_win.clrtoeol
  Cryptum::UI.colorize(
    ui_win: order_execute_win,
    color: header_color,
    style: header_style,
    string: ''.ljust(col_just1, ' ')
  )

  sess_header_str = "SESS OPEN: #{total_selling_this_session} TPMD: $#{total_selling_profit_out} | "
  sess_header_str += "SOLD 24h: #{order_hist_meta_sold_twenty_four} YTD: #{order_hist_meta_sold} | "
  sess_header_str += "PROF 24h: $#{sess_gains_24h_out} YTD: $#{sess_gains_out} | "
  sess_header_str += "AvgRndT DAYS: #{avg_round_trip_days_out}"
  order_execute_win.setpos(
    out_line_no,
    Cryptum::UI.col_center(str: sess_header_str)
  )

  Cryptum::UI.colorize(
    ui_win: order_execute_win,
    color: header_color,
    style: header_style,
    string: sess_header_str
  )

  # ROW 11
  out_line_no += 1
  order_execute_win.setpos(out_line_no, Cryptum::UI.col_first)
  order_execute_win.clrtoeol
  Cryptum::UI.colorize(
    ui_win: order_execute_win,
    color: header_color,
    style: header_style,
    string: ''.ljust(col_just1, ' ')
  )

  tot_header_str = "TOTAL OPEN: #{total_to_sell} | "
  tot_header_str += "SOLD 24h: #{order_hist_sold_twenty_four} YTD: #{order_hist_sold_ytd}"
  # tot_header_str += "PROF 24h: $#{order_hist_profit_usd_24h_out} YTD: $#{order_hist_profit_usd_ytd_out}"
  order_execute_win.setpos(
    out_line_no,
    Cryptum::UI.col_center(str: tot_header_str)
  )

  Cryptum::UI.colorize(
    ui_win: order_execute_win,
    color: header_color,
    style: header_style,
    string: tot_header_str
  )

  # ROW 12
  out_line_no += 1
  Cryptum::UI.line(
    ui_win: order_execute_win,
    out_line_no: out_line_no
  )

  order_execute_win.refresh

  # Reset Order Ready && Open Sell Orders Merge (If Applicable) Booleans
  event_history.order_ready = false
  event_history.open_sell_orders_merge = false

  event_history
rescue Interrupt, StandardError => e
  Cryptum::Log.append(level: :error, msg: e, which_self: self, event_history: event_history)
end