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
- #ask_count ⇒ Object
- #ask_volume ⇒ Object
- #average ⇒ Object
- #average_ask ⇒ Object
- #average_bid ⇒ Object
- #best ⇒ Object
- #best_ask ⇒ Object
- #best_bid ⇒ Object
- #bid_count ⇒ Object
- #bid_volume ⇒ Object
- #count ⇒ Object
- #spread ⇒ Object
- #summarize ⇒ Object
- #volume ⇒ Object
Instance Method Details
#ask_count ⇒ Object
10 11 12 |
# File 'lib/orderbook/book_analysis.rb', line 10 def ask_count @asks.count end |
#ask_volume ⇒ Object
22 23 24 |
# File 'lib/orderbook/book_analysis.rb', line 22 def ask_volume @asks.map { |x| x.fetch(:size) }.inject(:+) end |
#average ⇒ Object
40 41 42 |
# File 'lib/orderbook/book_analysis.rb', line 40 def average { bid: average_bid, ask: average_ask } end |
#average_ask ⇒ Object
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_bid ⇒ Object
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 |
#best ⇒ Object
52 53 54 |
# File 'lib/orderbook/book_analysis.rb', line 52 def best { bid: best_bid, ask: best_ask } end |
#best_ask ⇒ Object
48 49 50 |
# File 'lib/orderbook/book_analysis.rb', line 48 def best_ask @asks.sort_by { |x| x.fetch(:price) }.first end |
#best_bid ⇒ Object
44 45 46 |
# File 'lib/orderbook/book_analysis.rb', line 44 def best_bid @bids.sort_by { |x| x.fetch(:price) }.last end |
#bid_count ⇒ Object
6 7 8 |
# File 'lib/orderbook/book_analysis.rb', line 6 def bid_count @bids.count end |
#bid_volume ⇒ Object
18 19 20 |
# File 'lib/orderbook/book_analysis.rb', line 18 def bid_volume @bids.map { |x| x.fetch(:size) }.inject(:+) end |
#count ⇒ Object
14 15 16 |
# File 'lib/orderbook/book_analysis.rb', line 14 def count { bid: bid_count, ask: ask_count } end |
#spread ⇒ Object
56 57 58 |
# File 'lib/orderbook/book_analysis.rb', line 56 def spread best_ask.fetch(:price) - best_bid.fetch(:price) end |
#summarize ⇒ Object
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 |
#volume ⇒ Object
26 27 28 |
# File 'lib/orderbook/book_analysis.rb', line 26 def volume { bid: bid_volume, ask: ask_volume } end |