class Abot::Info::BinanceAccount

Attributes

api_key[R]
secret_key[R]

Public Class Methods

new(api_key, secret_key) click to toggle source
# File lib/abot/info/binance_account.rb, line 8
def initialize(api_key, secret_key)
  @api_key = api_key
  @secret_key = secret_key
end

Public Instance Methods

account_info() click to toggle source
# File lib/abot/info/binance_account.rb, line 33
def account_info
  @account_info ||= Binance::Api::Account.info!(
    api_key: api_key,
    api_secret_key: secret_key,
    )
end
coins_without_order(quote_assets) click to toggle source
# File lib/abot/info/binance_account.rb, line 82
def coins_without_order(quote_assets)
  exception_coins = quote_assets + ['BNB'] + Coin::FIAT
  balances = account_info[:balances]
  result = balances.select do |s|
    s[:free].to_f != 0 &&
      !exception_coins.include?(s[:asset]) &&
      symbol_info(s[:asset] + 'USDT').try(:[], :askPrice).to_f * s[:free].to_f > 5 &&
      (
        begin
          btcusdt = get_symbols.find { |f| f[:symbol] == 'BTCUSDT' }[:askPrice].to_f
          if s[:asset] == 'BTC'
            symbol_info("#{s[:asset]}USDT").try(:[], :askPrice).to_f * s[:free].to_f > 5
          elsif get_symbols.find { |f| f[:symbol] == "#{s[:asset]}USDT" }.nil?
            coin = get_symbols.find { |f| f[:symbol] == "#{name}BTC" }
            coin[:askPrice].to_f * s[:free].to_f * btcusdt > 5
          else
            coin = get_symbols.find { |f| f[:symbol] == "#{s[:asset]}USDT" }
            coin[:askPrice].to_f * s[:free].to_f > 5
          end
        rescue StandardError
          false
        end
      )
  end
  result.pluck(:asset)
end
current_balance(balance_btc, quote) click to toggle source
# File lib/abot/info/binance_account.rb, line 21
def current_balance(balance_btc, quote)
  calculation_current_balance(balance_btc, quote)
end
current_balance_btc() click to toggle source
# File lib/abot/info/binance_account.rb, line 17
def current_balance_btc
  @current_balance_btc ||= calculation_current_balance_btc
end
current_balance_wl(current_coins, quote) click to toggle source
# File lib/abot/info/binance_account.rb, line 25
def current_balance_wl(current_coins, quote)
  calculation_current_balance_wl(current_coins, quote)
end
free_balance(quote) click to toggle source
# File lib/abot/info/binance_account.rb, line 13
def free_balance(quote)
  account_info[:balances].find { |f| f[:asset] == quote }.try(:[], :free).to_f
end
percent_free_balance(balance, quote) click to toggle source
# File lib/abot/info/binance_account.rb, line 29
def percent_free_balance(balance, quote)
  ((free_balance(quote) / balance) * 100).round(2)
end
potential_balance(current_coins, trade_params) click to toggle source
# File lib/abot/info/binance_account.rb, line 40
def potential_balance(current_coins, trade_params)
  return @potential_balance if @potential_balance

  balance = current_balance(current_balance_btc, 'USDT')
  quote_assets = trade_params['quote_asset'].split(' ')
  potential = 0.0
  btcusdt = get_symbols.find { |f| f[:symbol] == 'BTCUSDT' }

  quote_assets.each do |q|
    begin
      pp_q = Coin.sum_potential_profit(current_coins, q) - Coin.sum_current_profit(current_coins, q)
      if q == 'USDT'
        potential += pp_q
      elsif q == 'BTC'
        potential += pp_q * btcusdt[:askPrice].to_f
      elsif Coin::FIAT.include?(q)
        btc = pp_q / get_symbols.find { |f| f[:symbol] == "BTC#{quote_asset}" }[:askPrice].to_f
        potential += btc * btcusdt[:askPrice].to_f
      else
        c = pp_q / get_symbols.find { |f| f[:symbol] == "#{quote_asset}BTC" }[:askPrice].to_f
        potential += btcusdt[:askPrice].to_f / c
      end
    rescue StandardError
      puts "Ошибка подсчета потенц. баланса: #{q}"
    end
  end

  @potential_balance ||= potential + balance
end
symbol_info(symbol) click to toggle source
# File lib/abot/info/binance_account.rb, line 70
def symbol_info(symbol)
  get_symbols.find { |f| f[:symbol] == symbol }
end
symbol_max_price(symbol) click to toggle source
# File lib/abot/info/binance_account.rb, line 78
def symbol_max_price(symbol)
  symbol_info(symbol).try(:[], :highPrice).to_f
end
symbol_min_price(symbol) click to toggle source
# File lib/abot/info/binance_account.rb, line 74
def symbol_min_price(symbol)
  symbol_info(symbol).try(:[], :lowPrice).to_f
end

Private Instance Methods

calculation_current_balance(balance_btc, quote) click to toggle source
# File lib/abot/info/binance_account.rb, line 144
def calculation_current_balance(balance_btc, quote)
  symbols = get_symbols
  if Coin::FIAT.include?(quote)
    coin = symbols.find { |f| f[:symbol] == "BTC#{quote}" }
    balance_btc * coin.try(:[], :askPrice).to_f
  else
    coin = symbols.find { |f| f[:symbol] == "#{quote}BTC" }
    balance_btc / coin.try(:[], :askPrice).to_f
  end
end
calculation_current_balance_btc() click to toggle source
# File lib/abot/info/binance_account.rb, line 111
def calculation_current_balance_btc
  balance = 0.0
  account_coins = {}
  account_info[:balances].each do |coin|
    if coin[:free].to_f != 0 || coin[:locked].to_f != 0
      account_coins[coin[:asset].to_s] = coin[:free].to_f + coin[:locked].to_f
    end
  end
  if account_coins.present?
    symbols = get_symbols
    btcusdt = symbols.find { |f| f[:symbol] == 'BTCUSDT' }[:askPrice].to_f
    account_coins.each do |name, value|
      begin
        if name == 'BTC'
          balance += account_coins[name]
        elsif Coin::FIAT.include?(name)
          coin = symbols.find { |f| f[:symbol] == "BTC#{name}" }
          balance += value / coin[:askPrice].to_f
        elsif symbols.find { |f| f[:symbol] == "#{name}USDT" }.nil?
          coin = symbols.find { |f| f[:symbol] == "#{name}BTC" }
          balance += coin[:askPrice].to_f * value
        else
          coin = symbols.find { |f| f[:symbol] == "#{name}USDT" }
          balance += coin[:askPrice].to_f * value / btcusdt
        end
      rescue StandardError
        puts "Ошибка при подсчете: монета #{name}"
      end
    end
  end
  balance
end
calculation_current_balance_wl(current_coins, quote) click to toggle source
# File lib/abot/info/binance_account.rb, line 155
def calculation_current_balance_wl(current_coins, quote)
  balance = 0.0
  current_coins = current_coins.select { |s| s.quote_asset == quote }
  current_coins.each { |coin| balance += coin.current_quote }
  quote_info = account_info[:balances].find { |f| f[:asset] == quote }
  if quote_info.present?
    balance += (quote_info.try(:[], :free).to_f + quote_info.try(:[], :locked).to_f)
  end
  balance
end
get_symbols() click to toggle source
# File lib/abot/info/binance_account.rb, line 166
def get_symbols
  @get_symbols ||= Binance::Api.ticker!(
    type: 'daily',
    api_key: api_key,
    api_secret_key: secret_key
  )
end