Class: Devilicious::OrderBook

Inherits:
Object
  • Object
show all
Defined in:
lib/devilicious/order_book.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(hash) ⇒ OrderBook

Returns a new instance of OrderBook.

Raises:

  • (ArgumentError)


6
7
8
9
10
11
# File 'lib/devilicious/order_book.rb', line 6

def initialize(hash)
  @asks = hash.delete(:asks).sort_by(&:price).freeze # buy
  @bids = hash.delete(:bids).sort_by(&:price).freeze # sell

  raise ArgumentError unless hash.empty?
end

Instance Attribute Details

#asksObject (readonly)

Returns the value of attribute asks.



3
4
5
# File 'lib/devilicious/order_book.rb', line 3

def asks
  @asks
end

#bidsObject (readonly)

Returns the value of attribute bids.



3
4
5
# File 'lib/devilicious/order_book.rb', line 3

def bids
  @bids
end

#marketObject

Returns the value of attribute market.



4
5
6
# File 'lib/devilicious/order_book.rb', line 4

def market
  @market
end

Instance Method Details

#highest_bidObject



13
14
15
# File 'lib/devilicious/order_book.rb', line 13

def highest_bid
  bids.last
end

#lowest_askObject



17
18
19
# File 'lib/devilicious/order_book.rb', line 17

def lowest_ask
  asks.first
end

#max_bid_price_for_volume(min_price, max_volume) ⇒ Object



34
35
36
37
# File 'lib/devilicious/order_book.rb', line 34

def max_bid_price_for_volume(min_price, max_volume)
  interesting_bids = interesting_offers(bids, ->(price) { price >= min_price }).reverse # reverse to start from most expensive
  best_offer_price_for_volume(interesting_bids, max_volume)
end

#min_ask_price_for_volume(max_price, max_volume) ⇒ Object



29
30
31
32
# File 'lib/devilicious/order_book.rb', line 29

def min_ask_price_for_volume(max_price, max_volume)
  interesting_asks = interesting_offers(asks, ->(price) { price <= max_price })
  best_offer_price_for_volume(interesting_asks, max_volume)
end

#weighted_asks_up_to(max_price) ⇒ Object



21
22
23
# File 'lib/devilicious/order_book.rb', line 21

def weighted_asks_up_to(max_price)
  weighted_offers(asks, ->(price) { price <= max_price })
end

#weighted_bids_down_to(min_price) ⇒ Object



25
26
27
# File 'lib/devilicious/order_book.rb', line 25

def weighted_bids_down_to(min_price)
  weighted_offers(bids, ->(price) { price >= min_price })
end