Class: Orderbook

Inherits:
Object
  • Object
show all
Includes:
BookAnalysis, BookMethods
Defined in:
lib/orderbook.rb,
lib/orderbook/version.rb,
lib/orderbook/book_methods.rb,
lib/orderbook/book_analysis.rb,
lib/orderbook/real_time_book.rb

Overview

Orderbook version number. I try to keep it semantic.

Direct Known Subclasses

RealTimeBook

Defined Under Namespace

Modules: BookAnalysis, BookMethods Classes: RealTimeBook

Constant Summary collapse

PING_INTERVAL =

seconds in between pinging the connection.

15
VERSION =
'2.0.2'

Constants included from BookMethods

BookMethods::BIGDECIMAL_KEYS

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from BookAnalysis

#ask_count, #ask_volume, #average, #average_ask, #average_bid, #best, #best_ask, #best_bid, #bid_count, #bid_volume, #count, #spread, #summarize, #volume

Methods included from BookMethods

#apply

Constructor Details

#initialize(live = true, &block) ⇒ Orderbook

Creates a new live copy of the orderbook.

If live is set to false, the orderbook will not start automatically.

If a block is given it is passed each message as it is received.



58
59
60
61
62
63
64
65
66
67
# File 'lib/orderbook.rb', line 58

def initialize(live = true, &block)
  @bids = [{ price: nil, size: nil, order_id: nil }]
  @asks = [{ price: nil, size: nil, order_id: nil }]
  @first_sequence = 0
  @last_sequence = 0
  @websocket = Coinbase::Exchange::Websocket.new(keepalive: true)
  @client = Coinbase::Exchange::AsyncClient.new
  @callback = block if block_given?
  live && live!
end

Instance Attribute Details

#asksObject (readonly)

Array of asks



22
23
24
# File 'lib/orderbook.rb', line 22

def asks
  @asks
end

#bidsObject (readonly)

Array of bids



18
19
20
# File 'lib/orderbook.rb', line 18

def bids
  @bids
end

#callbackObject

Callback to pass each received message to



50
51
52
# File 'lib/orderbook.rb', line 50

def callback
  @callback
end

#clientObject (readonly)

Coinbase::Exchange::AsyncClient object



38
39
40
# File 'lib/orderbook.rb', line 38

def client
  @client
end

#first_sequenceObject (readonly)

Sequence number from the initial level 3 snapshot



26
27
28
# File 'lib/orderbook.rb', line 26

def first_sequence
  @first_sequence
end

#last_pongObject (readonly)

Last time a pong was received after a ping



46
47
48
# File 'lib/orderbook.rb', line 46

def last_pong
  @last_pong
end

#last_sequenceObject (readonly)

Sequence number of most recently received message



30
31
32
# File 'lib/orderbook.rb', line 30

def last_sequence
  @last_sequence
end

#threadObject (readonly)

Thread running the EM loop



42
43
44
# File 'lib/orderbook.rb', line 42

def thread
  @thread
end

#websocketObject (readonly)

Coinbase::Exchange::Websocket object



34
35
36
# File 'lib/orderbook.rb', line 34

def websocket
  @websocket
end

Instance Method Details

#live!Object

Used to start the thread that listens to updates on the websocket and applies them to the current orderbook to create a live book.



72
73
74
75
# File 'lib/orderbook.rb', line 72

def live!
  setup_websocket
  start_thread
end