Module: Mexbt::Public

Includes:
Client, Common
Included in:
Mexbt
Defined in:
lib/mexbt/public.rb

Constant Summary

Constants included from Client

Client::SSL_VERSION

Instance Method Summary collapse

Methods included from Common

#trades

Methods included from Client

#auth_params, #call, #call_data, #private_key, #public_key, #url, #user_id

Instance Method Details

#currency_pairsObject Also known as: product_pairs



38
39
40
# File 'lib/mexbt/public.rb', line 38

def currency_pairs
  call("product-pairs")
end

#endpointObject



10
11
12
# File 'lib/mexbt/public.rb', line 10

def endpoint
  "https://public-api.mexbt.com"
end

#order_book(currency_pair: Mexbt.currency_pair) ⇒ Object Also known as: orders



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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

#simulate_market_order(side: :buy, first_currency: 0, second_currency: 0, currency_pair: Mexbt.currency_pair) ⇒ Object



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
# 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) ⇒ Object



14
15
16
# 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:) ⇒ Object



44
45
46
# 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