Module: Coinbot::UI::Ticker

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

Class Method Summary collapse

Class Method Details

.helpObject

Display Usage for this Module



240
241
242
243
244
245
246
247
248
# File 'lib/coinbot/ui/ticker.rb', line 240

public_class_method def self.help
  puts "USAGE:
    #{self}.refresh(
      symbol: 'required - Symbol for this Session (e.g. btc-usd)',
      order_book: 'required - Order Book Data Structure',
      event: 'required - Event from Coinbase Web Socket'
    )
  "
end

.refresh(opts = {}) ⇒ Object



9
10
11
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
# File 'lib/coinbot/ui/ticker.rb', line 9

public_class_method def self.refresh(opts = {})
  option_choice = opts[:option_choice]
  start_time = opts[:start_time]
  ticker_win = opts[:ticker_win]
  key_press_event = opts[:key_press_event]
  order_book = opts[:order_book]
  event = opts[:event]

  this_product = order_book[:this_product]
  quote_increment = this_product[:quote_increment]
  fiat_smallest_decimal = quote_increment.to_s.split('.')[-1].length

  symbol_out = "#{this_product[:id]}.#{option_choice.trading_on}"
  fiat = this_product[:quote_currency].to_sym
  fiat_symbol = '?'
  fiat_symbol = '$' if fiat == :USD
  fiat_symbol = "\u20ac" if fiat == :EUR

  last_ticker_price = order_book[:ticker_price].to_f
  second_to_last_ticker_price = order_book[:ticker_price_second_to_last].to_f

  sequence = event[:sequence].to_i
  last_sequence = order_book[:sequence].to_i

  raise "ERROR: Sequence Order Mismatch - #{last_sequence} #{sequence}" unless sequence >= last_sequence

  order_book[:sequence] = sequence
  open_24h = event[:open_24h].to_f
  order_book[:open_24h] = open_24h

  open_24h_out = "#{Coinbot.open_symbol} #{fiat_symbol}#{format("%0.#{fiat_smallest_decimal}f", open_24h)}"

  ticker_fmt = "#{fiat_symbol}#{format("%0.#{fiat_smallest_decimal}f", event[:price].to_f)}"
  if last_ticker_price.zero?
    ticker_price_color = :white
    ticker_price_out = "#{Coinbot.flat_arrow} #{ticker_fmt}"
  elsif last_ticker_price < event[:price].to_f
    ticker_price_color = :green
    ticker_price_out = "#{Coinbot.up_arrow} #{ticker_fmt}"
  elsif last_ticker_price > event[:price].to_f
    ticker_price_color = :red
    ticker_price_out = "#{Coinbot.down_arrow} #{ticker_fmt}"
  else
    ticker_price_color = :yellow
    ticker_price_out = "#{Coinbot.flat_arrow} #{ticker_fmt}"
  end

  order_book[:ticker_price_third_to_last] = format("%0.#{fiat_smallest_decimal}f", second_to_last_ticker_price)
  order_book[:ticker_price_second_to_last] = format("%0.#{fiat_smallest_decimal}f", last_ticker_price)
  order_book[:ticker_price] = format("%0.#{fiat_smallest_decimal}f", event[:price].to_f)

  volume_24h = format('%0.7f', event[:volume_24h])
  order_book[:volume_24h] = volume_24h

  volume_30d = format('%0.7f', event[:volume_30d])

  # TODO: Potential for RACE CONDITIONS - MIGRATE TO
  # Coinbot::Event.parse
  # Indicate if 24 Hour High is Reached During Sesssion
  high_24h = format("%0.#{fiat_smallest_decimal}f ", event[:high_24h]).to_f
  order_book[:high_24h] = high_24h

  low_24h = format("%0.#{fiat_smallest_decimal}f ", event[:low_24h]).to_f
  order_book[:low_24h] = low_24h
  margin_percent_open_24h = (1 - (open_24h / order_book[:ticker_price].to_f)) * 100

  if margin_percent_open_24h.positive?
    margin_percent_open_24h_color = :green
    margin_percent_open_24h_out = "#{Coinbot.up_arrow} #{format('%0.4f', margin_percent_open_24h)}%"
  elsif margin_percent_open_24h.negative?
    # Space removed to account for negative number.
    margin_percent_open_24h_color = :red
    margin_percent_open_24h_out = "#{Coinbot.down_arrow}#{format('%0.4f', margin_percent_open_24h)}%"
  else
    margin_percent_open_24h_color = :yellow
    margin_percent_open_24h_out = "#{Coinbot.flat_arrow} #{format('%0.4f', margin_percent_open_24h)}%"
  end

  high_24h_out = format("%0.#{fiat_smallest_decimal}f", high_24h)
  low_24h_out = format("%0.#{fiat_smallest_decimal}f", low_24h)

  # best_bid = "#{fiat_symbol}#{event[:best_bid]}"
  # best_ask = "#{fiat_symbol}#{event[:best_ask]}"
  calc_market_spread = event[:best_ask].to_f - event[:best_bid].to_f
  market_spread = "#{fiat_symbol}#{format("%0.#{fiat_smallest_decimal}f", calc_market_spread)}"

  current_time_out = Time.now.strftime('%Y-%m-%d %H:%M:%S%z')
  # TODO: Everything Above this Line Needs to be Indicators ^

  # UI
  col_just1 = 15
  col_just2 = 14
  col_just3 = 21
  col_just3_alt = (Curses.cols - Coinbot::UI.col_third) - 1
  col_just4 = (Curses.cols - Coinbot::UI.col_fourth) - 1

  Coinbot::UI.detect_key_press_in_ui(
    key_press_event: key_press_event,
    ui_win: ticker_win
  )

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

  out_line_no += 1
  ticker_win.setpos(out_line_no, Coinbot::UI.col_first)
  ticker_win.clrtoeol
  if Curses.can_change_color?
    Coinbot::UI.colorize(
      ui_win: ticker_win,
      color: :rainbow,
      style: :bold,
      string: symbol_out.ljust(col_just1)
    )
  else
    Coinbot::UI.colorize(
      ui_win: ticker_win,
      color: :yellow,
      style: :bold,
      string: symbol_out.ljust(col_just1)
    )
  end

  ticker_win.setpos(out_line_no, Coinbot::UI.col_second)
  Coinbot::UI.colorize(
    ui_win: ticker_win,
    color: margin_percent_open_24h_color,
    style: :bold,
    string: margin_percent_open_24h_out.ljust(col_just2)
  )

  ticker_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: ticker_win,
    color: :blue,
    style: :bold,
    string: open_24h_out.ljust(col_just3)
  )

  ticker_win.setpos(out_line_no, Coinbot::UI.col_fourth)
  Coinbot::UI.colorize(
    ui_win: ticker_win,
    color: ticker_price_color,
    style: :bold,
    string: ticker_price_out.rjust(col_just4)
  )

  # ROW 2
  out_line_no += 1
  ticker_win.setpos(out_line_no, Coinbot::UI.col_first)
  ticker_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: ticker_win,
    color: :green,
    style: :bold,
    string: '24 Hr High:'
  )

  ticker_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: ticker_win,
    color: :green,
    string: "#{Coinbot.up_arrow} #{fiat_symbol}#{high_24h_out}".rjust(col_just3_alt, '.')
  )

  # ROW 3
  out_line_no += 1
  ticker_win.setpos(out_line_no, Coinbot::UI.col_first)
  ticker_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: ticker_win,
    color: :red,
    string: '24 Hr Low:'
  )

  ticker_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: ticker_win,
    color: :red,
    string: "#{Coinbot.down_arrow} #{fiat_symbol}#{low_24h_out}".rjust(col_just3_alt, '.')
  )

  # ROW 4
  out_line_no += 1
  ticker_win.setpos(out_line_no, Coinbot::UI.col_first)
  ticker_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: ticker_win,
    color: :blue,
    style: :bold,
    string: '24 Hr | 30 Day Market Volume:'
  )

  ticker_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: ticker_win,
    color: :blue,
    string: "#{volume_24h} | #{volume_30d}".rjust(col_just3_alt, '.')
  )

  # ROW 5
  out_line_no += 1
  ticker_win.setpos(out_line_no, Coinbot::UI.col_first)
  ticker_win.clrtoeol
  Coinbot::UI.colorize(
    ui_win: ticker_win,
    color: :cyan,
    style: :bold,
    string: 'Start Time:'
  )

  ticker_win.setpos(out_line_no, Coinbot::UI.col_third)
  Coinbot::UI.colorize(
    ui_win: ticker_win,
    color: :cyan,
    string: start_time.rjust(col_just3_alt, '.')
  )

  ticker_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