Class: Trader::Book

Inherits:
Object
  • Object
show all
Defined in:
lib/trade-o-matic/structs/book.rb

Direct Known Subclasses

Market

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(_pair) ⇒ Book

Returns a new instance of Book.



5
6
7
8
9
# 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

Instance Attribute Details

#ask_slopeObject (readonly)

Returns the value of attribute ask_slope.



3
4
5
# File 'lib/trade-o-matic/structs/book.rb', line 3

def ask_slope
  @ask_slope
end

#bid_slopeObject (readonly)

Returns the value of attribute bid_slope.



3
4
5
# File 'lib/trade-o-matic/structs/book.rb', line 3

def bid_slope
  @bid_slope
end

#pairObject (readonly)

Returns the value of attribute pair.



3
4
5
# File 'lib/trade-o-matic/structs/book.rb', line 3

def pair
  @pair
end

Instance Method Details

#add_ask(_price, _volume) ⇒ Object



31
32
33
# 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) ⇒ Object



27
28
29
# 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) ⇒ Object



18
19
20
21
22
23
24
25
# 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) ⇒ Object



11
12
13
14
15
16
# 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) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# 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