# frozen_string_literal: true module Cryptum # Cryptum::UI Module used for Presenting the # Cryptum Curses Interface module UI # Update the Portfolio Section of the UI module Portfolio # Supported Method Parameters:: # Cryptum::UI::Candle.refresh( # order_book: 'required - Order Book Data Structure', # event: 'required - Event from Coinbase Web Socket' # ) public_class_method def self.refresh(opts = {}) option_choice = opts[:option_choice] portfolio_win = opts[:portfolio_win] event_history = opts[:event_history] bot_conf = opts[:bot_conf] ticker_price = event_history.order_book[:ticker_price].to_f this_product = event_history.order_book[:this_product] symbol_out = this_product[:id] base_increment = this_product[:base_increment] crypto_smallest_decimal = base_increment.to_s.split('.')[-1].length autotrade_percent = format( '%0.2f', bot_conf[:autotrade_portfolio_percent].to_f ) tpm = format( '%0.2f', bot_conf[:target_profit_margin_percent] ) crypto_currency = option_choice.symbol.to_s.upcase.split('_').first.to_sym portfolio = event_history.order_book[:portfolio] this_account = portfolio.select do |account| account[:currency] == crypto_currency.to_s end if this_account.empty? e = "ID for Crypto Currency, #{crypto_currency} Not Found" Cryptum::Log.append(level: :error, msg: e, which_self: self, event_history: event_history) end balance = format("%0.#{crypto_smallest_decimal}f", this_account.first[:balance]) balance_out = Cryptum.beautify_large_number( value: balance ) avail_for_trade = format("%0.#{crypto_smallest_decimal}f", this_account.first[:available]) avail_for_trade_out = Cryptum.beautify_large_number( value: avail_for_trade ) fiat_portfolio = event_history.order_book[:fiat_portfolio] fiat = option_choice.symbol.to_s.upcase.split('_').last.to_sym total_holdings = format('%0.2f', fiat_portfolio.first[:total_holdings]) total_holdings_out = Cryptum.beautify_large_number( value: total_holdings ) fiat_balance = format('%0.2f', fiat_portfolio.first[:balance]) fiat_balance_out = Cryptum.beautify_large_number( value: fiat_balance ) fiat_avail_for_trade = format('%0.2f', fiat_portfolio.first[:available]) fiat_avail_for_trade_out = Cryptum.beautify_large_number( value: fiat_avail_for_trade ) fees = event_history.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) volume_tier_out = Cryptum.beautify_large_number( value: volume_tier ) current_crypto_fiat_value = format( '%0.2f', balance.to_f * ticker_price ) current_crypto_fiat_value_out = Cryptum.beautify_large_number( value: current_crypto_fiat_value ) fiat_budget = fiat_avail_for_trade.to_f current_fiat_invested_percent = format( '%0.2f', (1 - (fiat_budget / total_holdings.to_f)) * 100 ) crypto_invested_percent = format( '%0.2f', current_crypto_fiat_value.to_f.fdiv(total_holdings.to_f) * 100 ) # TODO: Everything Above this Line Needs to be Indicators ^ # UI col_just3 = (Curses.cols - Cryptum::UI.col_third) - 1 # ROW 1 out_line_no = 0 Cryptum::UI.line( ui_win: portfolio_win, out_line_no: out_line_no ) # ROW 2 out_line_no += 1 portfolio_win.setpos(out_line_no, Cryptum::UI.col_first) portfolio_win.clrtoeol Cryptum::UI.colorize( ui_win: portfolio_win, color: :white, style: :bold, string: "Portfolio | % Tied Up | % #{symbol_out}:" ) portfolio_win.setpos(out_line_no, Cryptum::UI.col_third) Cryptum::UI.colorize( ui_win: portfolio_win, color: :white, string: "$#{total_holdings_out} | #{current_fiat_invested_percent}% | #{crypto_invested_percent}%".rjust(col_just3, '.') ) # ROW 3 out_line_no += 1 portfolio_win.setpos(out_line_no, Cryptum::UI.col_first) portfolio_win.clrtoeol Cryptum::UI.colorize( ui_win: portfolio_win, color: :cyan, string: 'TPM % | Autotrade %:' ) portfolio_win.setpos(out_line_no, Cryptum::UI.col_third) Cryptum::UI.colorize( ui_win: portfolio_win, color: :cyan, string: "#{tpm}% | #{autotrade_percent}%".rjust(col_just3, '.') ) # ROW 4 out_line_no += 1 portfolio_win.setpos(out_line_no, Cryptum::UI.col_first) portfolio_win.clrtoeol Cryptum::UI.colorize( ui_win: portfolio_win, color: :green, style: :bold, string: "#{fiat} Bal | Tradeable:" ) portfolio_win.setpos(out_line_no, Cryptum::UI.col_third) Cryptum::UI.colorize( ui_win: portfolio_win, color: :green, string: "$#{fiat_balance_out} | $#{fiat_avail_for_trade_out}".rjust( col_just3, '.' ) ) # ROW 5 out_line_no += 1 portfolio_win.setpos(out_line_no, Cryptum::UI.col_first) portfolio_win.clrtoeol Cryptum::UI.colorize( ui_win: portfolio_win, color: :yellow, style: :bold, string: 'Crypto Val | Bal | Tradeable:' ) portfolio_win.setpos(out_line_no, Cryptum::UI.col_third) Cryptum::UI.colorize( ui_win: portfolio_win, color: :yellow, string: "$#{current_crypto_fiat_value_out} | *#{balance_out} | *#{avail_for_trade_out}".rjust(col_just3, '.') ) # ROW 6 out_line_no += 1 portfolio_win.setpos(out_line_no, Cryptum::UI.col_first) portfolio_win.clrtoeol Cryptum::UI.colorize( ui_win: portfolio_win, color: :red, style: :bold, string: 'Maker & Taker Fees | My Volume:' ) portfolio_win.setpos(out_line_no, Cryptum::UI.col_third) Cryptum::UI.colorize( ui_win: portfolio_win, color: :red, string: "#{maker_fee}% & #{taker_fee}% | $#{volume_tier_out}".rjust(col_just3, '.') ) portfolio_win.refresh event_history rescue Interrupt, StandardError => e Cryptum::Log.append(level: :error, msg: e, which_self: self, event_history: event_history) end # Display Usage for this Module 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 end end end