Module: RTCBX::Orderbook::BookAnalysis

Included in:
RTCBX::Orderbook
Defined in:
lib/rtcbx/orderbook/book_analysis.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#aggregate(top_n = nil) ⇒ Object

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”



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

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



95
96
97
98
99
100
101
102
103
104
105
106
107
108
# 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) ⇒ Object

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



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# 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_countObject

Number of all current asks



15
16
17
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 15

def ask_count
  @asks.count
end

#ask_volumeObject

The total volume of product across all current asks



30
31
32
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 30

def ask_volume
  @asks.map { |x| x.fetch(:size) }.inject(:+)
end

#averageObject

The average price across all orders



52
53
54
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 52

def average
  { bid: average_bid, ask: average_ask }
end

#average_askObject

The average ask price across all asks



46
47
48
49
# 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_bidObject

The average bid price across all bids



40
41
42
43
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 40

def average_bid
  bids = @bids.map { |x| x.fetch(:price) }
  bids.inject(:+) / bids.count
end

#bestObject

The prices of the best current bid and ask



67
68
69
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 67

def best
  { bid: best_bid, ask: best_ask }
end

#best_askObject

The price of the best current ask



62
63
64
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 62

def best_ask
  @asks.min_by { |x| x.fetch(:price) }
end

#best_bidObject

The price of the best current bid



57
58
59
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 57

def best_bid
  @bids.max_by { |x| x.fetch(:price) }
end

#bid_countObject

Number of all current bids



10
11
12
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 10

def bid_count
  @bids.count
end

#bid_volumeObject

The total volume of product across all current bids



25
26
27
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 25

def bid_volume
  @bids.map { |x| x.fetch(:size) }.inject(:+)
end

#countObject

Number of all current orders



20
21
22
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 20

def count
  { bid: bid_count, ask: ask_count }
end

#spreadObject

The price difference between the best current bid and ask



72
73
74
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 72

def spread
  best_ask.fetch(:price) - best_bid.fetch(:price)
end

#summarizeObject

print a quick summary of the Orderbook



117
118
119
120
# 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

#volumeObject

The total volume of all product across current asks and bids



35
36
37
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 35

def volume
  { bid: bid_volume, ask: ask_volume }
end