module Mexbt::Public

Public Instance Methods

currency_pairs() click to toggle source
# File lib/mexbt/public.rb, line 38
def currency_pairs
  call("product-pairs")
end
Also aliased as: product_pairs
endpoint() click to toggle source
# File lib/mexbt/public.rb, line 10
def endpoint
  "https://public-api.mexbt.com"
end
order_book(currency_pair: Mexbt.currency_pair) click to toggle source
# File lib/mexbt/public.rb, line 18
def order_book(currency_pair: Mexbt.currency_pair)
  begin
    call("order-book", { productPair: currency_pair })
  rescue RestClient::RequestFailed => e
    if currency_pair.to_s.downcase === "btcmxn"
      data_order_book = call_data("order-book/btcmxn")
      mapper = Proc.new do |o|
        {px: o.first, qty: o.last}
      end
      data_order_book[:asks] = data_order_book[:asks].map(&mapper)
      data_order_book[:bids] = data_order_book[:bids].map(&mapper)
      data_order_book
    else
      raise e
    end
  end
end
Also aliased as: orders
orders(currency_pair: Mexbt.currency_pair)
Alias for: order_book
product_pairs()
Alias for: currency_pairs
simulate_market_order(side: :buy, first_currency: 0, second_currency: 0, currency_pair: Mexbt.currency_pair) click to toggle source
# File lib/mexbt/public.rb, line 48
def simulate_market_order(side: :buy, first_currency: 0, second_currency: 0, currency_pair: Mexbt.currency_pair)
  if first_currency === 0 and second_currency === 0
    raise "You must specify either 'first_currency' or 'second_currency' (from the currency pair)"
  end
  order_book = order_book(currency_pair: currency_pair)
  orders =
    if side.to_s === 'buy'
      order_book["asks"].sort { |a, b| a["px"] <=> b["px"] }
    else
      order_book["bids"].sort { |a, b| b["px"] <=> a["px"] }
    end
  if second_currency > 0
    threshold_target = second_currency
    threshold_symbol = :cost
    other_symbol = :amount
  else
    threshold_target = first_currency
    threshold_symbol = :amount
    other_symbol = :cost
  end
  sums = {
    amount: BigDecimal.new(0, 15),
    cost: BigDecimal.new(0, 15)
  }
  orders.each do |order|
    next_order = {
      amount: BigDecimal.new(order["qty"], 15),
      cost: BigDecimal.new(order["px"], 15) * BigDecimal.new(order["qty"], 15)
    }
    threshold_check = sums[threshold_symbol] + next_order[threshold_symbol]
    if threshold_check > threshold_target
      over = threshold_check - threshold_target
      percent_needed = (next_order[threshold_symbol] - over) / next_order[threshold_symbol]
      sums[other_symbol] += next_order[other_symbol] * percent_needed
      sums[threshold_symbol] = threshold_target
      break
    else
      sums[:amount] += next_order[:amount]
      sums[:cost] += next_order[:cost]
      break if sums[threshold_symbol] == threshold_target
    end
  end
  if sums[threshold_symbol] < threshold_target
    raise "Order book does not contain enough orders to satify simulated order!"
  end
  res = {
    first_amount: round(sums[:amount], currency_pair, :first),
    second_amount: round(sums[:cost], currency_pair, :second)
  }
  ActiveSupport::HashWithIndifferentAccess.new(res)
end
ticker(currency_pair: Mexbt.currency_pair) click to toggle source
# File lib/mexbt/public.rb, line 14
def ticker(currency_pair: Mexbt.currency_pair)
  call("ticker", { productPair: currency_pair })
end
trades_by_date(currency_pair: Mexbt.currency_pair, from:, to:) click to toggle source
# File lib/mexbt/public.rb, line 44
def trades_by_date(currency_pair: Mexbt.currency_pair, from:, to:)
  call("trades-by-date", { ins: currency_pair, startDate: from, endDate: to })
end

Private Instance Methods

round(amount, pair, which) click to toggle source
# File lib/mexbt/public.rb, line 102
def round(amount, pair, which)
  currency = which === :first ? pair.to_s[0,3] : pair.to_s[-3..-1]
  decimal_places =
    if ['btc', 'ltc'].include?(currency.downcase)
      8
    else
      2
    end
  amount.round(decimal_places).to_f
end