Module: Orderbook::BookAnalysis

Included in:
Orderbook
Defined in:
lib/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



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

def ask_count
  @asks.count
end

#ask_volumeObject



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

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

#averageObject



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

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

#average_askObject



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

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

#average_bidObject



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

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

#bestObject



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

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

#best_askObject



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

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

#best_bidObject



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

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

#bid_countObject



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

def bid_count
  @bids.count
end

#bid_volumeObject



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

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

#countObject



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

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

#spreadObject



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

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

#summarizeObject



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

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



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

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