Class: BudaApi::Models::OrderBook

Inherits:
BaseModel
  • Object
show all
Defined in:
lib/buda_api/models.rb

Overview

Order book model

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from BaseModel

#raw, #to_h, #to_json

Constructor Details

#initialize(data) ⇒ OrderBook

Returns a new instance of OrderBook.



121
122
123
124
125
# File 'lib/buda_api/models.rb', line 121

def initialize(data)
  super(data)
  @asks = (data["asks"] || []).map { |entry| OrderBookEntry.new(entry) }
  @bids = (data["bids"] || []).map { |entry| OrderBookEntry.new(entry) }
end

Instance Attribute Details

#asks ⇒ Object (readonly)

Returns the value of attribute asks.



127
128
129
# File 'lib/buda_api/models.rb', line 127

def asks
  @asks
end

#bids ⇒ Object (readonly)

Returns the value of attribute bids.



127
128
129
# File 'lib/buda_api/models.rb', line 127

def bids
  @bids
end

Instance Method Details

#best_ask ⇒ Object



129
130
131
# File 'lib/buda_api/models.rb', line 129

def best_ask
  @asks.first
end

#best_bid ⇒ Object



133
134
135
# File 'lib/buda_api/models.rb', line 133

def best_bid
  @bids.first
end

#spread ⇒ Object



137
138
139
140
141
# File 'lib/buda_api/models.rb', line 137

def spread
  return nil unless best_ask && best_bid
  
  best_ask.price - best_bid.price
end

#spread_percentage ⇒ Object



143
144
145
146
147
# File 'lib/buda_api/models.rb', line 143

def spread_percentage
  return nil unless best_ask && best_bid && best_bid.price > 0
  
  ((best_ask.price - best_bid.price) / best_bid.price * 100).round(4)
end