module 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
ask_count()
click to toggle source
# File lib/orderbook/book_analysis.rb, line 10 def ask_count @asks.count end
ask_volume()
click to toggle source
# File lib/orderbook/book_analysis.rb, line 22 def ask_volume @asks.map { |x| x.fetch(:size) }.inject(:+) end
average()
click to toggle source
# File lib/orderbook/book_analysis.rb, line 40 def average { bid: average_bid, ask: average_ask } end
average_ask()
click to toggle source
# File lib/orderbook/book_analysis.rb, line 35 def average_ask asks = @asks.map { |x| x.fetch(:price) } asks.inject(:+) / asks.count end
average_bid()
click to toggle source
# File lib/orderbook/book_analysis.rb, line 30 def average_bid bids = @bids.map { |x| x.fetch(:price) } bids.inject(:+) / bids.count end
best()
click to toggle source
# File lib/orderbook/book_analysis.rb, line 52 def best { bid: best_bid, ask: best_ask } end
best_ask()
click to toggle source
# File lib/orderbook/book_analysis.rb, line 48 def best_ask @asks.sort_by { |x| x.fetch(:price) }.first end
best_bid()
click to toggle source
# File lib/orderbook/book_analysis.rb, line 44 def best_bid @bids.sort_by { |x| x.fetch(:price) }.last end
bid_count()
click to toggle source
# File lib/orderbook/book_analysis.rb, line 6 def bid_count @bids.count end
bid_volume()
click to toggle source
# File lib/orderbook/book_analysis.rb, line 18 def bid_volume @bids.map { |x| x.fetch(:size) }.inject(:+) end
count()
click to toggle source
# File lib/orderbook/book_analysis.rb, line 14 def count { bid: bid_count, ask: ask_count } end
spread()
click to toggle source
# File lib/orderbook/book_analysis.rb, line 56 def spread best_ask.fetch(:price) - best_bid.fetch(:price) end
summarize()
click to toggle source
# 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
volume()
click to toggle source
# File lib/orderbook/book_analysis.rb, line 26 def volume { bid: bid_volume, ask: ask_volume } end