Class: Devilicious::OrderBook
- Inherits:
-
Object
- Object
- Devilicious::OrderBook
- Defined in:
- lib/devilicious/order_book.rb
Instance Attribute Summary collapse
-
#asks ⇒ Object
readonly
Returns the value of attribute asks.
-
#bids ⇒ Object
readonly
Returns the value of attribute bids.
-
#market ⇒ Object
Returns the value of attribute market.
Instance Method Summary collapse
- #highest_bid ⇒ Object
-
#initialize(hash) ⇒ OrderBook
constructor
A new instance of OrderBook.
- #lowest_ask ⇒ Object
- #max_bid_price_for_volume(min_price, max_volume) ⇒ Object
- #min_ask_price_for_volume(max_price, max_volume) ⇒ Object
- #weighted_asks_up_to(max_price) ⇒ Object
- #weighted_bids_down_to(min_price) ⇒ Object
Constructor Details
#initialize(hash) ⇒ OrderBook
Returns a new instance of OrderBook.
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
#asks ⇒ Object (readonly)
Returns the value of attribute asks.
3 4 5 |
# File 'lib/devilicious/order_book.rb', line 3 def asks @asks end |
#bids ⇒ Object (readonly)
Returns the value of attribute bids.
3 4 5 |
# File 'lib/devilicious/order_book.rb', line 3 def bids @bids end |
#market ⇒ Object
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_bid ⇒ Object
13 14 15 |
# File 'lib/devilicious/order_book.rb', line 13 def highest_bid bids.last end |
#lowest_ask ⇒ Object
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 |