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
-
#aggregate(top_n = nil) ⇒ Object
Aggregates the
top_ncurrent asks and bids. -
#aggregate_asks(top_n = nil) ⇒ Object
Aggregates the
top_ncurrent asks. -
#aggregate_bids(top_n = nil) ⇒ Object
Aggregates the
top_ncurrent bids. -
#ask_count ⇒ Object
Number of all current asks.
-
#ask_volume ⇒ Object
The total volume of product across all current asks.
-
#average ⇒ Object
The average price across all orders.
-
#average_ask ⇒ Object
The average ask price across all asks.
-
#average_bid ⇒ Object
The average bid price across all bids.
-
#best ⇒ Object
The prices of the best current bid and ask.
-
#best_ask ⇒ Object
The price of the best current ask.
-
#best_bid ⇒ Object
The price of the best current bid.
-
#bid_count ⇒ Object
Number of all current bids.
-
#bid_volume ⇒ Object
The total volume of product across all current bids.
-
#count ⇒ Object
Number of all current orders.
-
#spread ⇒ Object
The price difference between the best current bid and ask.
-
#summarize ⇒ Object
print a quick summary of the
Orderbook. -
#volume ⇒ Object
The total volume of all product across current asks and bids.
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 GDAX 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 GDAX calls a “Level 2 Orderbook”
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 94 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 GDAX calls a “Level 2 Orderbook”
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 76 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 ⇒ Object
Number of all current asks
13 14 15 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 13 def ask_count @asks.count end |
#ask_volume ⇒ Object
The total volume of product across all current asks
28 29 30 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 28 def ask_volume @asks.map { |x| x.fetch(:size) }.inject(:+) end |
#average ⇒ Object
The average price across all orders
50 51 52 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 50 def average { bid: average_bid, ask: average_ask } end |
#average_ask ⇒ Object
The average ask price across all asks
44 45 46 47 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 44 def average_ask asks = @asks.map { |x| x.fetch(:price) } asks.inject(:+) / asks.count end |
#average_bid ⇒ Object
The average bid price across all bids
38 39 40 41 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 38 def average_bid bids = @bids.map { |x| x.fetch(:price) } bids.inject(:+) / bids.count end |
#best ⇒ Object
The prices of the best current bid and ask
65 66 67 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 65 def best { bid: best_bid, ask: best_ask } end |
#best_ask ⇒ Object
The price of the best current ask
60 61 62 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 60 def best_ask @asks.sort_by { |x| x.fetch(:price) }.first end |
#best_bid ⇒ Object
The price of the best current bid
55 56 57 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 55 def best_bid @bids.sort_by { |x| x.fetch(:price) }.last end |
#bid_count ⇒ Object
Number of all current bids
8 9 10 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 8 def bid_count @bids.count end |
#bid_volume ⇒ Object
The total volume of product across all current bids
23 24 25 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 23 def bid_volume @bids.map { |x| x.fetch(:size) }.inject(:+) end |
#count ⇒ Object
Number of all current orders
18 19 20 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 18 def count { bid: bid_count, ask: ask_count } end |
#spread ⇒ Object
The price difference between the best current bid and ask
70 71 72 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 70 def spread best_ask.fetch(:price) - best_bid.fetch(:price) end |
#summarize ⇒ Object
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 |
#volume ⇒ Object
The total volume of all product across current asks and bids
33 34 35 |
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 33 def volume { bid: bid_volume, ask: ask_volume } end |