Class: Abot::Info::Table

Inherits:
Terminal::Table
  • Object
show all
Includes:
Helpers
Defined in:
lib/abot/info/table.rb

Constant Summary collapse

ALL_HEADINGS =
{
  name: 'Coin',
  name_mobile: 'Coin',
  sell_up: "sell\nup",
  sell_price: 'Ордер',
  max_price: 'Максимум 24h',
  min_price: 'Минимум 24h',
  current_price: 'Тек.цена',
  next_average_price: "Следующее\nусреднение",
  current_quote: "Тек.\nстоим.",
  current_profit: "Тек.\nпрофит",
  potential_profit: "Пот.\nпрофит",
  buy_date: "Дата покупки",
  timer: 'Время',
  current_price_perc_of_order: 'Тек.цена',
  max_price_perc_of_order: 'Максимум 24h',
}.freeze
HEADINGS_COINS_TABLE_DEFAULT =
%i[
  name
  sell_up
  sell_price
  max_price
  current_price
  min_price
  next_average_price
  current_quote
  buy_date
  timer
  current_profit
  potential_profit
].freeze
HEADINGS_COINS_TABLE_MOBILE =
%i[
  name_mobile
  sell_up
  sell_price
  current_price
  next_average_price
].freeze
COLUMN_WITH_TOTAL =
%i[
  current_profit
  potential_profit
].freeze

Constants included from Helpers

Helpers::AVERAGE_COLORS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Helpers

balance_str, check_abot, check_coins, potential_balance_str, symbol_price_str, today_info_str

Constructor Details

#initialize(options) ⇒ Table

Returns a new instance of Table.



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/abot/info/table.rb', line 54

def initialize(options)
  @account = options[:account]
  @columns = options[:columns] || HEADINGS_COINS_TABLE_DEFAULT
  @rate_coins = options.fetch(:rate_coins, [])
  @trade_params ||= DatabaseTable.data_settings
  @current_coins = Coin.current_coins(, trade_params)
  @telegram = Info::TelegramSender.new if options[:telegram_enable] == 'true'

  options = {
    headings: columns.map { |h| { value: ALL_HEADINGS[h], alignment: :center } },
    style: { border_x: '=' }
  }

  super options
end

Instance Attribute Details

#accountObject (readonly)

Returns the value of attribute account.



9
10
11
# File 'lib/abot/info/table.rb', line 9

def 
  @account
end

#columnsObject (readonly)

Returns the value of attribute columns.



9
10
11
# File 'lib/abot/info/table.rb', line 9

def columns
  @columns
end

#current_coinsObject (readonly)

Returns the value of attribute current_coins.



9
10
11
# File 'lib/abot/info/table.rb', line 9

def current_coins
  @current_coins
end

#rate_coinsObject (readonly)

Returns the value of attribute rate_coins.



9
10
11
# File 'lib/abot/info/table.rb', line 9

def rate_coins
  @rate_coins
end

#telegramObject (readonly)

Returns the value of attribute telegram.



9
10
11
# File 'lib/abot/info/table.rb', line 9

def telegram
  @telegram
end

#trade_paramsObject (readonly)

Returns the value of attribute trade_params.



9
10
11
# File 'lib/abot/info/table.rb', line 9

def trade_params
  @trade_params
end

Class Method Details

.last_columns_set(col) ⇒ Object



70
71
72
73
74
# File 'lib/abot/info/table.rb', line 70

def self.last_columns_set(col)
  col = col.map { |m| ALL_HEADINGS.keys.include?(m) ? m : nil }.compact
  COLUMN_WITH_TOTAL.each { |e| col << col.delete(e) }
  col.compact
end

Instance Method Details

#available_coins_numberObject



129
130
131
132
133
134
135
# File 'lib/abot/info/table.rb', line 129

def available_coins_number
  @available_coins_number ||= Coin.available_coins_number(
    current_coins,
    trade_params,
    .percent_free_balance(current_coins)
  )
end

#colspanObject



137
138
139
# File 'lib/abot/info/table.rb', line 137

def colspan
  (columns - COLUMN_WITH_TOTAL).count - 1
end

#generateObject



76
77
78
79
80
81
82
83
84
85
86
# File 'lib/abot/info/table.rb', line 76

def generate
  sorted_current_coins.each do |coin|
    row = columns.map do |m|
      coin.decorated_cell(m)
    end
    add_row row
  end
  add_separator
  add_row row_total
  self
end

#info_strObject



100
101
102
103
104
105
106
107
# File 'lib/abot/info/table.rb', line 100

def info_str
  "#{Helpers.check_coins(, trade_params["quote_asset"].split(' '), telegram)}" \
  "#{Helpers.check_abot(current_coins, telegram)}" \
  "#{Helpers.today_info_str}" \
  "#{Helpers.balance_str(, current_coins, trade_params)}\n" \
  "#{Helpers.potential_balance_str(, current_coins, trade_params)}\n" \
  "#{rate_coins_str}"
end

#rate_coins_strObject



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/abot/info/table.rb', line 109

def rate_coins_str
  result = ''
  rate_coins.each_with_index do |c, idx|
    number = colspan / 2
    number += 1 if number.zero?

    result += if idx != 0 && (idx % number).zero?
                "\n"
              elsif idx != 0
                ' '
              else
                ''
              end
    str = Helpers.symbol_price_str(, c.upcase)
    str.slice!('USDT')
    result += str
  end
  result
end

#row_totalObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/abot/info/table.rb', line 141

def row_total
  row_total = [
    total_averages,
    { value: info_str, colspan: colspan }
  ]
  quote_assets = trade_params['quote_asset'].split(' ')
  cell_current_profit = ''
  cell_potential_profit = ''
  quote_assets.each do |qa|
    if quote_assets.count > 1
      cell_current_profit += "#{Coin.sum_current_profit(current_coins, qa).round(2)} #{qa}\n"if columns.include?(:current_profit)
      cell_potential_profit += "#{Coin.sum_potential_profit(current_coins, qa).round(2)} #{qa}\n" if columns.include?(:potential_profit)
    else
      cell_current_profit += Coin.sum_current_profit(current_coins, qa).round(2).to_s if columns.include?(:current_profit)
      cell_potential_profit += Coin.sum_potential_profit(current_coins, qa).round(2).to_s if columns.include?(:potential_profit)
    end
  end
  row_total << cell_current_profit if columns.include?(:current_profit)
  row_total << cell_potential_profit if columns.include?(:potential_profit)
  row_total
end

#sorted_current_coinsObject



163
164
165
# File 'lib/abot/info/table.rb', line 163

def sorted_current_coins
  current_coins.sort_by(&:percent_to_order)
end

#total_averagesObject



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/abot/info/table.rb', line 88

def total_averages
  av_arr = []
  current_coins.each { |coin| av_arr[coin.num_averaging] = av_arr[coin.num_averaging].to_i + 1 }
  count = av_arr.compact.sum
  av_arr = av_arr.map.with_index do |av, idx|
    Paint["#{idx}: #{av.to_i}", (Helpers::AVERAGE_COLORS[idx] || :red)] if av.to_i != 0
  end.compact
  av_arr << "∑: #{count}"
  # av_arr << "M: #{available_coins_number}" if available_coins_number
  av_arr.join("\n")
end