Module: Cryptum::UI::Order::Timer

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

Overview

Update the Timer Section of the UI

Class Method Summary collapse

Class Method Details

.helpObject

Display Usage for this Module



124
125
126
127
128
129
# File 'lib/cryptum/ui/order/timer.rb', line 124

public_class_method def self.help
  puts "USAGE:
   #{self}.refresh(
   )
  "
end

.refresh(opts = {}) ⇒ Object

Supported Method Parameters

Cryptum::UI::Timer.refresh( )



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
# File 'lib/cryptum/ui/order/timer.rb', line 15

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

  last_trend_reset_time = event_history.order_book[:last_trend_reset]
  last_order_exec_time = event_history.order_book[:last_order_exec]

  # Market Trend Reset Timer
  time_between_trend_reset = option_choice.market_trend_reset
  trend_timer_begin = Time.parse(last_trend_reset_time)
  trend_timer_end = trend_timer_begin + time_between_trend_reset
  trend_time_remaining = trend_timer_end - Time.now

  if trend_time_remaining.zero? || trend_time_remaining.negative?
    event_history.order_book[:market_trend][:buy] = 0
    event_history.order_book[:market_trend][:sell] = 0
    event_history.order_book[:last_trend_reset] = Time.now.strftime(
      '%Y-%m-%d %H:%M:%S%z'
    )

    last_trend_reset_time = event_history.order_book[:last_trend_reset]
    time_between_trend_reset = option_choice.market_trend_reset
    trend_timer_begin = Time.parse(last_trend_reset_time)
    trend_timer_end = trend_timer_begin + time_between_trend_reset
    trend_time_remaining = trend_timer_end - Time.now
  end

  trend_countdown = Cryptum.beautify_large_number(
    value: format('%0.2f', trend_time_remaining)
  )

  # Market Trend Reset Timer
  time_between_order_exec = event_history.time_between_orders
  time_between_order_exec_out = Cryptum.beautify_large_number(
    value: format('%0.2f', time_between_order_exec)
  )
  order_begin_time = Time.parse(last_order_exec_time)
  order_end_time = order_begin_time + time_between_order_exec
  order_exec_time_remaining = order_end_time - Time.now
  order_countdown = Cryptum.beautify_large_number(
    value: format('%0.2f', order_exec_time_remaining)
  )

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

  color = :white
  color = indicator_status.market_trend[:color] if indicator_status.market_trend
  case color
  when :green
    intent = "- #{Cryptum.down_arrow} SEE ORDER HISTORY #{Cryptum.down_arrow} -"
  else
    speed = 'FAST'
    speed = 'SLOW' if buy_total >= sell_total

    intent = "- #{speed} BUY: #{order_countdown} of #{time_between_order_exec_out} -"
    intent = '- BUYING PAUSED -' if event_history.red_pill
  end

  # Have a Clock
  clock = Time.now.strftime('%Y-%m-%d %H:%M:%S%z')

  # UI
  col_just4 = (Curses.cols - Cryptum::UI.col_fourth) - 1

  # ROW 1
  # COLUMN 1
  out_line_no = 0
  order_timer_win.setpos(out_line_no, Cryptum::UI.col_first)
  order_timer_win.clrtoeol
  Cryptum::UI.colorize(
    ui_win: order_timer_win,
    color: :white,
    style: :bold,
    string: "#{option_choice.market_trend_reset_label} M. Trend Rst: #{trend_countdown}"
  )

  # COLUMN 2
  order_timer_win.setpos(
    out_line_no,
    Cryptum::UI.col_center(str: intent)
  )
  Cryptum::UI.colorize(
    ui_win: order_timer_win,
    color: :white,
    style: :bold,
    string: intent
  )

  # COLUMN 3
  order_timer_win.setpos(out_line_no, Cryptum::UI.col_fourth)
  Cryptum::UI.colorize(
    ui_win: order_timer_win,
    color: :white,
    style: :bold,
    string: clock.rjust(col_just4)
  )

  order_timer_win.refresh

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