class Trader::Book

Attributes

ask_slope[R]
bid_slope[R]
pair[R]
transactions[R]

Public Class Methods

new(_pair) click to toggle source
# File lib/trade-o-matic/structs/book.rb, line 5
def initialize(_pair)
  @pair = _pair
  @bid_slope = BidSlope.new _pair
  @ask_slope = AskSlope.new _pair
end

Public Instance Methods

add_ask(_price, _volume) click to toggle source
# File lib/trade-o-matic/structs/book.rb, line 31
def add_ask(_price, _volume)
  ask_slope.add_order _price, _volume
end
add_bid(_price, _volume) click to toggle source
# File lib/trade-o-matic/structs/book.rb, line 27
def add_bid(_price, _volume)
  bid_slope.add_order _price, _volume
end
add_transaction(_price, _volume, _time) click to toggle source
# File lib/trade-o-matic/structs/book.rb, line 18
def add_transaction(_price, _volume, _time)
  @transactions << Transaction.new(
    Price.new(pair.quote, _price),
    Price.new(pair.base, _volume),
    _time
  )
  nil
end
prepare(_ref_time=nil) click to toggle source
# File lib/trade-o-matic/structs/book.rb, line 11
def prepare(_ref_time=nil)
  @ref_time = _ref_time || Time.now
  @transactions = []
  @bid_slope.reset
  @ask_slope.reset
end
volume(_period_seconds) click to toggle source
# File lib/trade-o-matic/structs/book.rb, line 35
def volume(_period_seconds)
  ref_time = @ref_time - _period_seconds
  amount = @transactions.inject(0.0) do |r, trx|
    if trx.time >= ref_time
      r + trx.volume.amount
    else
      r
    end
  end

  Price.new pair.base, amount
end