Module: Cryptum::UI::OrderPlan

Defined in:
lib/cryptum/ui/order_plan.rb

Class Method Summary collapse

Class Method Details

.helpObject

Display Usage for this Module



377
378
379
380
381
382
383
384
# File 'lib/cryptum/ui/order_plan.rb', line 377

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::Candle.refresh(

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

)



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

public_class_method def self.refresh(opts = {})
  option_choice = opts[:option_choice]
  order_plan_win = opts[:order_plan_win]
  event_history = opts[:event_history]
  key_press_event = opts[:key_press_event]
  indicator_status = opts[:indicator_status]
  bot_conf = opts[:bot_conf]
  fiat_portfolio_file = opts[:fiat_portfolio_file]

  ticker_price = event_history.order_book[:ticker_price].to_f
  return unless ticker_price.positive?

  this_product = event_history.order_book[:this_product]
  symbol_out = this_product[:id]
  quote_increment = this_product[:quote_increment]
  base_increment = this_product[:base_increment]
  # base_min_size = this_product[:base_min_size].to_f
  min_market_funds = this_product[:min_market_funds]

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

  autotrade_percent = bot_conf[:autotrade_portfolio_percent].to_f
  autotrade_cast_as_decimal = autotrade_percent / 100

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

  crypto_currency = option_choice.symbol.to_s.upcase.split('_').first.to_sym
  portfolio = event_history.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]
  total_holdings = format('%0.2f', fiat_portfolio.first[:total_holdings])
  fiat_balance = format('%0.2f', fiat_portfolio.first[:balance])
  fiat_avail_for_trade = format('%0.2f', fiat_portfolio.first[:available])

  fiat_budget = fiat_avail_for_trade.to_f
  current_fiat_invested_percent = (
    1 - (fiat_budget / total_holdings.to_f)
  ) * 100

  order_history = event_history.order_book[:order_history]
  open_orders = order_history.select do |order|
    order[:status] == 'open' 
  end
  total_open_orders = open_orders.length

  order_plan = event_history.order_book[:order_plan]

  current_crypto_fiat_value = format(
    '%0.2f',
    balance.to_f * ticker_price
  )

  crypto_invested_percent = format(
    '%0.2f',
    (current_crypto_fiat_value.to_f / total_holdings.to_f) * 100
  )

  event_history.red_pill = true if crypto_invested_percent.to_f > autotrade_percent

  if crypto_invested_percent.to_f <= autotrade_percent && (
       order_plan.empty? || (
         indicator_status.action_signal == :buy &&
         indicator_status.last_action_signal == :sell
       )
     )

    event_history.red_pill = false
    # Reset time between orders when generating new order plan
    event_history.time_between_orders = event_history.time_between_orders_reset if order_plan.empty?

    order_plan = []
    plan_no_slice = 1
    event_history.plan_no += 1
    plan_no = event_history.plan_no
    # Sum up currently invested amount so we don't
    # exceed autotrade % for next order plan generation
    allocation_decimal = 0.0
    risk_target = fiat_budget * autotrade_cast_as_decimal
    risk_alloc = risk_target

    loop do
      # Calculate min order size
      allocation_decimal += 0.0001
      # allocation_decimal += base_increment.to_f
      fiat_investing = risk_alloc * allocation_decimal

      next unless fiat_investing >= min_market_funds.to_f

      # fiat_investing = min_market_funds.to_f
      # allocation_decimal = fiat_investing / ticker_price

      risk_alloc = fiat_budget * autotrade_cast_as_decimal
      allocation_percent = allocation_decimal * 100
      fiat_returning = fiat_investing + (
        fiat_investing * tpm_cast_as_decimal
      )
      profit = fiat_returning - fiat_investing
  
      # Implemented a bugfix here to avoid a bogus single order when Tradeable
      # Balances are low.  Ensure everything still works as expected when
      # balances are higher.
      break if order_plan.map { |op| op[:invest].to_f }.sum > risk_target ||
               allocation_percent > 100

      order_slice = {}
      order_slice[:plan_no] = "#{plan_no}.#{plan_no_slice}"
      order_slice[:fiat_available] = format('%0.2f', fiat_budget)
      order_slice[:risk_alloc] = format('%0.2f', risk_alloc)
      order_slice[:allocation_decimal] = format(
        '%0.8f',
        allocation_decimal
      )
      order_slice[:allocation_percent] = format(
        '%0.2f',
        allocation_percent
      )
      order_slice[:invest] = format('%0.2f', fiat_investing)
      order_slice[:return] = format('%0.2f', fiat_returning)
      order_slice[:profit] = format('%0.2f', profit)

      order_plan.push(order_slice)
      fiat_budget -= fiat_investing
      plan_no_slice += 1
    end
    event_history.order_book[:order_plan] = order_plan
  end

  red_pill_alert = '| RED PILL ALERT |'
  red_pill_msg = "% #{symbol_out} > Autotrade %"
  event_history.red_pill = true if order_plan.empty?

  if event_history.red_pill
    order_plan_prefix = '--'
    max_order_plan_slices = '0'
    order_plan_volume_out = '0.00'
    order_plan_profit_sum_out = '0.00'
    order_plan_exec_percent = '0.00'
  else
    order_plan_len = order_plan.length
    order_plan_prefix = order_plan.last[:plan_no].split('.').first
    max_order_plan_slices = order_plan.last[:plan_no].split('.').last.to_i
    calc_order_plan_exec = (
      1 - (order_plan_len / max_order_plan_slices.to_f)
    ) * 100
    order_plan_volume = order_plan.map do |op|
      op[:invest].to_f
    end.sum
    order_plan_volume_out = Cryptum.beautify_large_number(
      value: format(
        '%0.2f',
        order_plan_volume
      )
    )
    order_plan_profit_sum = order_plan.map do |op|
      op[:profit].to_f
    end.sum
    order_plan_profit_sum_out = Cryptum.beautify_large_number(
      value: format(
        '%0.2f',
        order_plan_profit_sum
      )
    )
    order_plan_exec_percent = format('%0.2f', calc_order_plan_exec)
  end

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

  # UI
  col_just1 = (Curses.cols - Cryptum::UI.col_first) - 1
  col_just3 = (Curses.cols - Cryptum::UI.col_third) - 1
  col_just4 = Curses.cols - Cryptum::UI.col_fourth

  Cryptum::UI.detect_key_press_in_ui(
    key_press_event: key_press_event,
    ui_win: order_plan_win
  )

  # ROW 1
  out_line_no = 0
  Cryptum::UI.line(
    ui_win: order_plan_win,
    out_line_no: out_line_no
  )

  # ROW 2
  out_line_no += 1
  order_plan_win.setpos(out_line_no, Cryptum::UI.col_first)
  order_plan_win.clrtoeol
  Cryptum::UI.colorize(
    ui_win: order_plan_win,
    color: :white,
    style: :bold,
    string: "Order Plan ##{order_plan_prefix} Summary:"
  )

  order_plan_win.setpos(out_line_no, Cryptum::UI.col_third)
  Cryptum::UI.colorize(
    ui_win: order_plan_win,
    color: :white,
    string: "$#{order_plan_volume_out} w #{max_order_plan_slices} Slices | $#{order_plan_profit_sum_out} 2 Gain | #{order_plan_exec_percent}% Done".rjust(
      col_just3,
      '.'
    )
  )

  # ROW 3
  out_line_no += 1
  Cryptum::UI.line(
    ui_win: order_plan_win,
    out_line_no: out_line_no
  )

  # ROWS 4-10
  if event_history.red_pill
    out_line_no += 1
    order_plan_win.setpos(out_line_no, Cryptum::UI.col_first)
    order_plan_win.clrtoeol

    Cryptum::UI.colorize(
      ui_win: order_plan_win,
      color: :red,
      style: :highlight,
      string: ''.ljust(col_just1, ' ')
    )

    order_plan_win.setpos(
      out_line_no,
      Cryptum::UI.col_center(str: red_pill_alert)
    )
    Cryptum::UI.colorize(
      ui_win: order_plan_win,
      color: :red,
      style: :highlight,
      string: red_pill_alert
    )

    order_plan_win.setpos(out_line_no, Cryptum::UI.col_fourth)
    Cryptum::UI.colorize(
      ui_win: order_plan_win,
      color: :red,
      style: :highlight,
      string: ''.ljust(col_just4, ' ')
    )

    out_line_no += 1
    order_plan_win.setpos(out_line_no, Cryptum::UI.col_first)
    order_plan_win.clrtoeol

    Cryptum::UI.colorize(
      ui_win: order_plan_win,
      color: :yellow,
      string: ''.ljust(col_just1, ' ')
    )

    order_plan_win.setpos(
      out_line_no,
      Cryptum::UI.col_center(str: red_pill_msg)
    )
    Cryptum::UI.colorize(
      ui_win: order_plan_win,
      color: :yellow,
      style: :bold,
      string: red_pill_msg
    )

    order_plan_win.setpos(out_line_no, Cryptum::UI.col_fourth)
    Cryptum::UI.colorize(
      ui_win: order_plan_win,
      color: :yellow,
      string: ''.ljust(col_just4, ' ')
    )

    5.times.each do
      out_line_no += 1
      this_matrix_row = Cryptum::Matrix.generate(cols: Curses.cols)
      order_plan_win.setpos(out_line_no, Cryptum::UI.col_first)
      order_plan_win.clrtoeol
      Cryptum::UI.colorize(
        ui_win: order_plan_win,
        color: :red,
        style: :bold,
        string: this_matrix_row
      )
    end
  else
    color = plan_color = :white
    color = indicator_status.market_trend[:color] if indicator_status.market_trend
    plan_color = :cyan if color == :red
    plan_color = :green if color == :green

    order_plan[0..6].each do |order|
      out_line_no += 1
      style = :normal
      style = :highlight if out_line_no == 1
      fiat_avail_out = Cryptum.beautify_large_number(
        value: order[:fiat_available]
      )
      risk_alloc_out = Cryptum.beautify_large_number(
        value: order[:risk_alloc]
      )
      invest_out = Cryptum.beautify_large_number(
        value: order[:invest]
      )
      profit_out = Cryptum.beautify_large_number(
        value: order[:profit]
      )
      tpm_out = format('%0.2f', tpm)
      return_out = Cryptum.beautify_large_number(
        value: order[:return]
      )
      plan_no = "#{order[:plan_no]}|"
      fiat = "#{autotrade_percent}% of $#{fiat_avail_out} = "
      alloc = "$#{risk_alloc_out} @ #{order[:allocation_percent]}% = "
      invest = "$#{invest_out} + #{tpm_out}% = "

      returns  = "$#{return_out}|"
      profit = "Profit: $#{profit_out}"

      order_plan_invest = "#{plan_no}#{fiat}#{alloc}#{invest}"
      order_plan_return  = "#{returns}#{profit}"

      order_plan_win.setpos(out_line_no, Cryptum::UI.col_first)
      order_plan_win.clrtoeol
      Cryptum::UI.colorize(
        ui_win: order_plan_win,
        color: plan_color,
        style: style,
        string: "#{order_plan_invest}#{order_plan_return}".ljust(col_just1, '.')
      )
    end

    if order_plan.length < 9
      lines_to_clear = 9 - order_plan.length
      (1..lines_to_clear).each do |_line|
        out_line_no += 1
        order_plan_win.setpos(out_line_no, Cryptum::UI.col_first)
        order_plan_win.clrtoeol
      end
    end
  end

  order_plan_win.refresh

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