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

#ask_countObject



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

def ask_count
  @asks.count
end

#ask_volumeObject



23
24
25
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 23

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

#averageObject



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

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

#average_askObject



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

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

#average_bidObject



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

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

#bestObject



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

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

#best_askObject



49
50
51
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 49

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

#best_bidObject



45
46
47
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 45

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

#bid_countObject



7
8
9
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 7

def bid_count
  @bids.count
end

#bid_volumeObject



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

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

#countObject



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

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

#spreadObject



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

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

#summarizeObject



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

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



27
28
29
# File 'lib/rtcbx/orderbook/book_analysis.rb', line 27

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