Class: Bitfinex::Models::OrderBook

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(snap = [], raw = false) ⇒ OrderBook

Returns a new instance of OrderBook.



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/models/order_book.rb', line 8

def initialize (snap = [], raw = false)
  @raw = raw

  if snap.instance_of?(OrderBook)
    @bids = snap.bids.dup
    @asks = snap.asks.dup
  elsif snap.kind_of?(Array)
    update_from_snapshot(snap)
  elsif snap.kind_of?(Hash)
    @bids = snap[:bids].dup
    @asks = snap[:asks].dup
  else
    @bids = []
    @asks = []
  end
end

Instance Attribute Details

#asksObject (readonly)

Returns the value of attribute asks.



6
7
8
# File 'lib/models/order_book.rb', line 6

def asks
  @asks
end

#bidsObject (readonly)

Returns the value of attribute bids.



6
7
8
# File 'lib/models/order_book.rb', line 6

def bids
  @bids
end

#rawObject (readonly)

Returns the value of attribute raw.



6
7
8
# File 'lib/models/order_book.rb', line 6

def raw
  @raw
end

Class Method Details

.unserialize(arr, raw = false) ⇒ Object



219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
# File 'lib/models/order_book.rb', line 219

def self.unserialize (arr, raw = false)
  if arr[0].kind_of?(Array)
    entries = arr.map { |e| OrderBook.unserialize(e, raw) }
    bids = entries.select { |e| (e[:rate] ? -e[:amount] : e[:amount]) > 0 }
    asks = entries.select { |e| (e[:rate] ? -e[:amount] : e[:amount]) < 0 }

    return {
      :bids => bids,
      :asks => asks
    }
  end

  if arr.size == 4
    if raw
      return {
        :order_id => arr[0],
        :period => arr[1],
        :rate => arr[2],
        :amount => arr[3]
      }
    else
      return {
        :rate => arr[0],
        :period => arr[1],
        :count => arr[2],
        :amount => arr[3]
      }
    end
  else
    if raw
      return {
        :order_id => arr[0],
        :price => arr[1],
        :amount => arr[2]
      }
    else
      return {
        :price => arr[0],
        :count => arr[1],
        :amount => arr[2]
      }
    end
  end
end

Instance Method Details

#ask_amountObject



114
115
116
117
118
119
120
121
122
# File 'lib/models/order_book.rb', line 114

def ask_amount
  amount = 0

  @asks.each do |entry|
    amount += entry.size == 4 ? entry[3] : entry[2]
  end

  amount.abs
end

#bid_amountObject



104
105
106
107
108
109
110
111
112
# File 'lib/models/order_book.rb', line 104

def bid_amount
  amount = 0

  @bids.each do |entry|
    amount += entry.size == 4 ? entry[3] : entry[2]
  end

  amount.abs
end

#checksumObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
# File 'lib/models/order_book.rb', line 128

def checksum
  data = []

  for i in 0...25
    bid = @bids[i]
    ask = @asks[i]

    if !bid.nil?
      price = bid[0]
      data.push(price)
      data.push(bid.size == 4 ? bid[3] : bid[2])
    end

    if !ask.nil?
      price = ask[0]
      data.push(price)
      data.push(ask.size == 4 ? ask[3] : ask[2])
    end
  end

  [Zlib::crc32(data.join(':'))].pack("I").unpack("i")[0]
end

#mid_priceObject



85
86
87
88
89
90
91
92
93
# File 'lib/models/order_book.rb', line 85

def mid_price
  ask = top_ask || 0
  bid = top_bid || 0

  return bid if ask == 0
  return ask if bid == 0

  return (bid + ask) / 2
end

#serializeObject



124
125
126
# File 'lib/models/order_book.rb', line 124

def serialize
  @asks + @bids
end

#spreadObject



95
96
97
98
99
100
101
102
# File 'lib/models/order_book.rb', line 95

def spread
  ask = top_ask || 0
  bid = top_bid || 0

  return 0 if ask == 0 || bid == 0

  return ask - bid
end

#top_askObject



75
76
77
78
79
80
81
82
83
# File 'lib/models/order_book.rb', line 75

def top_ask
  if self.raw
    priceI = (@bids[0].size == 4 || @asks[0].size == 4) ? 2 : 1
  else
    priceI = 0
  end
 
  (top_ask_level || [])[priceI] || nil
end

#top_ask_levelObject



71
72
73
# File 'lib/models/order_book.rb', line 71

def top_ask_level
  @asks[0] || nil
end

#top_bidObject



61
62
63
64
65
66
67
68
69
# File 'lib/models/order_book.rb', line 61

def top_bid
  if self.raw
    priceI = (@bids[0].size == 4 || @asks[0].size == 4) ? 2 : 1
  else
    priceI = 0
  end
  
  (top_bid_level || [])[priceI] || nil
end

#top_bid_levelObject



57
58
59
# File 'lib/models/order_book.rb', line 57

def top_bid_level
  @bids[0] || nil
end

#update_from_snapshot(snap = []) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/models/order_book.rb', line 25

def update_from_snapshot (snap = [])
  @bids = []
  @asks = []

  snap = [snap] if !snap[0].kind_of?(Array)

  snap.each do |entry|
    if entry.size == 4
      if entry[3] < 0
        @bids.push(entry)
      else
        @asks.push(entry)
      end
    else
      if entry[2] < 0
        @asks.push(entry)
      else
        @bids.push(entry)
      end
    end
  end

  if self.raw
    priceI = snap[0].size == 4 ? 2 : 1
  else
    priceI = 0
  end

  @bids.sort! { |a, b| b[priceI] <=> a[priceI]}
  @asks.sort! { |a, b| a[priceI] <=> b[priceI]}
end

#update_with(entry) ⇒ Object



151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
# File 'lib/models/order_book.rb', line 151

def update_with (entry)
  if @raw
    priceI = entry.size == 4 ? 2 : 1
    count = -1
  else
    priceI = 0
    count = entry.size == 4 ? entry[2] : entry[1]
  end

  price = entry[priceI]
  oID = entry[0] # only for raw books
  amount = entry.size == 4 ? entry[3] : entry[2]

  if entry.size == 4
    dir = amount < 0 ? 1 : -1
    side = amount < 0 ? @bids : @asks
  else
    dir = amount < 0 ? -1 : 1
    side = amount < 0 ? @asks : @bids
  end

  insertIndex = -1

  # apply insert directly if empty
  if (side.size == 0 && (@raw || count > 0))
    side.push(entry)
    return true
  end

  # match by price level, or order ID for raw books
  side.each_with_index do |pl, i|
    if (!@raw && pl[priceI] == price) || (@raw && pl[0] == oID)
      if (!@raw && count == 0) || (@raw && price == 0)
        side.slice!(i, 1)
        return true
      elsif !@raw || (@raw && price > 0)
        side.slice!(i, 1)
        break
      end
    end
  end

  if (@raw && price == 0) || (!@raw && count == 0)
    return false
  end

  side.each_with_index do |pl, i|
    if (insertIndex == -1 && (
      (dir == -1 && price < pl[priceI]) || # by price
      (dir == -1 && price == pl[priceI] && (raw && entry[0] < pl[0])) || # by order ID
      (dir == 1 && price > pl[priceI]) ||
      (dir == 1 && price == pl[priceI] && (raw && entry[0] < pl[0]))
    ))
      insertIndex = i
      break
    end
  end

  # add
  if (insertIndex == -1)
    side.push(entry)
  else
    side.insert(insertIndex, entry)
  end

  return true
end