class Bitshares::Market

Attributes

base[R]
quote[R]

Public Class Methods

new(quote, base) click to toggle source
# File lib/bitshares/market.rb, line 9
def initialize(quote, base)
  [quote, base].each &:upcase!
  @quote_hash, @base_hash = asset(quote), asset(base)
  @quote, @base = @quote_hash['symbol'], @base_hash['symbol']
  valid_market?(@quote_hash['id'], @base_hash['id'])
  @multiplier = multiplier
end

Public Instance Methods

get_asset_collateral() click to toggle source
# File lib/bitshares/market.rb, line 41
def get_asset_collateral # uses quote only, not base
  CLIENT.request('blockchain_market_get_asset_collateral', [quote])
end
highest_bid() click to toggle source
# File lib/bitshares/market.rb, line 27
def highest_bid
  return if bids.empty?
  price bids.first
end
last_fill() click to toggle source
# File lib/bitshares/market.rb, line 17
def last_fill
  return -1 if order_hist.empty?
  order_hist.first['bid_index']['order_price']['ratio'].to_f * multiplier
end
list_shorts(limit = nil) click to toggle source
# File lib/bitshares/market.rb, line 37
def list_shorts(limit = nil) # uses quote only, not base
  CLIENT.request('blockchain_market_list_shorts', [quote] + [limit])
end
lowest_ask() click to toggle source
# File lib/bitshares/market.rb, line 22
def lowest_ask
  return if asks.empty?
  price asks.first
end
method_missing(m, *args) click to toggle source
# File lib/bitshares/market.rb, line 45
def method_missing(m, *args)
  CLIENT.request('blockchain_market_' + m.to_s, [quote, base] + args)
end
mid_price() click to toggle source
# File lib/bitshares/market.rb, line 32
def mid_price
  return nil if highest_bid.nil? || lowest_ask.nil?
  (highest_bid + lowest_ask) / 2
end

Private Instance Methods

asks() click to toggle source
# File lib/bitshares/market.rb, line 71
def asks
  self.list_asks
end
asset(symbol) click to toggle source
# File lib/bitshares/market.rb, line 51
def asset(symbol) # returns hash
  CHAIN.get_asset(symbol) || (raise Error, "Invalid asset: #{symbol}")
end
bids() click to toggle source
# File lib/bitshares/market.rb, line 67
def bids
  self.list_bids
end
multiplier() click to toggle source
# File lib/bitshares/market.rb, line 63
def multiplier
  @base_hash['precision'].to_f / @quote_hash['precision']
end
order_hist() click to toggle source
# File lib/bitshares/market.rb, line 59
def order_hist
  self.order_history
end
price(order) click to toggle source
# File lib/bitshares/market.rb, line 75
def price(order) # CARE: preserve float precision with * NOT /
  order['market_index']['order_price']['ratio'].to_f * @multiplier
end
valid_market?(quote_id, base_id) click to toggle source
# File lib/bitshares/market.rb, line 55
def valid_market?(quote_id, base_id)
  raise Error, 'Invalid market; quote ID <= base ID' if quote_id <= base_id
end