module RTCBX::Orderbook::BookAnalysis

Simple collection of commands to get info about the orderbook. Add our own methods for calculating whatever it is you feel like calculating.

Public Instance Methods

aggregate(top_n = nil) click to toggle source

Aggregates the top_n current asks and bids. Pass `50` and you'll get the same thing tht Coinbase Pro calls a “Level 2 Orderbook”

# File lib/rtcbx/orderbook/book_analysis.rb, line 112
def aggregate(top_n = nil)
  { bids: aggregate_bids(top_n), asks: aggregate_asks(top_n) }
end
aggregate_asks(top_n = nil) click to toggle source

Aggregates the top_n current asks. Pass `50` and you'll get the same thing tht Coinbase Pro calls a “Level 2 Orderbook”

# File lib/rtcbx/orderbook/book_analysis.rb, line 95
def aggregate_asks(top_n = nil)
  aggregate = {}
  @asks.each do |ask|
    aggregate[ask[:price]] ||= aggregate_base
    aggregate[ask[:price]][:size] += ask[:size]
    aggregate[ask[:price]][:num_orders] += 1
  end
  top_n ||= aggregate.keys.count
  aggregate.keys.sort.first(top_n).map do |price|
    { price: price,
      size: aggregate[price][:size],
      num_orders: aggregate[price][:num_orders] }
  end
end
aggregate_bids(top_n = nil) click to toggle source

Aggregates the top_n current bids. Pass `50` and you'll get the same thing tht Coinbase Pro calls a “Level 2 Orderbook”

# File lib/rtcbx/orderbook/book_analysis.rb, line 78
def aggregate_bids(top_n = nil)
  aggregate = {}
  @bids.each do |bid|
    aggregate[bid[:price]] ||= aggregate_base
    aggregate[bid[:price]][:size] += bid[:size]
    aggregate[bid[:price]][:num_orders] += 1
  end
  top_n ||= aggregate.keys.count
  aggregate.keys.sort.reverse.first(top_n).map do |price|
    { price: price,
      size: aggregate[price][:size],
      num_orders: aggregate[price][:num_orders] }
  end
end
ask_count() click to toggle source

Number of all current asks

# File lib/rtcbx/orderbook/book_analysis.rb, line 15
def ask_count
  @asks.count
end
ask_volume() click to toggle source

The total volume of product across all current asks

# File lib/rtcbx/orderbook/book_analysis.rb, line 30
def ask_volume
  @asks.map { |x| x.fetch(:size) }.inject(:+)
end
average() click to toggle source

The average price across all orders

# File lib/rtcbx/orderbook/book_analysis.rb, line 52
def average
  { bid: average_bid, ask: average_ask }
end
average_ask() click to toggle source

The average ask price across all asks

# File lib/rtcbx/orderbook/book_analysis.rb, line 46
def average_ask
  asks = @asks.map { |x| x.fetch(:price) }
  asks.inject(:+) / asks.count
end
average_bid() click to toggle source

The average bid price across all bids

# File lib/rtcbx/orderbook/book_analysis.rb, line 40
def average_bid
  bids = @bids.map { |x| x.fetch(:price) }
  bids.inject(:+) / bids.count
end
best() click to toggle source

The prices of the best current bid and ask

# File lib/rtcbx/orderbook/book_analysis.rb, line 67
def best
  { bid: best_bid, ask: best_ask }
end
best_ask() click to toggle source

The price of the best current ask

# File lib/rtcbx/orderbook/book_analysis.rb, line 62
def best_ask
  @asks.min_by { |x| x.fetch(:price) }
end
best_bid() click to toggle source

The price of the best current bid

# File lib/rtcbx/orderbook/book_analysis.rb, line 57
def best_bid
  @bids.max_by { |x| x.fetch(:price) }
end
bid_count() click to toggle source

Number of all current bids

# File lib/rtcbx/orderbook/book_analysis.rb, line 10
def bid_count
  @bids.count
end
bid_volume() click to toggle source

The total volume of product across all current bids

# File lib/rtcbx/orderbook/book_analysis.rb, line 25
def bid_volume
  @bids.map { |x| x.fetch(:size) }.inject(:+)
end
count() click to toggle source

Number of all current orders

# File lib/rtcbx/orderbook/book_analysis.rb, line 20
def count
  { bid: bid_count, ask: ask_count }
end
spread() click to toggle source

The price difference between the best current bid and ask

# File lib/rtcbx/orderbook/book_analysis.rb, line 72
def spread
  best_ask.fetch(:price) - best_bid.fetch(:price)
end
summarize() click to toggle source

print a quick summary of the Orderbook

# File lib/rtcbx/orderbook/book_analysis.rb, line 117
def summarize
  print "# of asks: #{ask_count}\n# of bids: #{bid_count}\nAsk volume: #{ask_volume.to_s('F')}\nBid volume: #{bid_volume.to_s('F')}\n"
  $stdout.flush
end
volume() click to toggle source

The total volume of all product across current asks and bids

# File lib/rtcbx/orderbook/book_analysis.rb, line 35
def volume
  { bid: bid_volume, ask: ask_volume }
end

Private Instance Methods

aggregate_base() click to toggle source
# File lib/rtcbx/orderbook/book_analysis.rb, line 124
def aggregate_base
  { size: BigDecimal(0), num_orders: 0 }
end