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

Overview

Orderbook version number. I try to keep it semantic.

Defined Under Namespace

Modules: BookAnalysis, BookMethods

Constant Summary collapse

PING_INTERVAL =

seconds in between pinging the connection.

15
VERSION =
'4.0.1'

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(product_id: "BTC-USD", start: true, &block) ⇒ Orderbook

Creates a new live copy of the orderbook.

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

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



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/orderbook.rb', line 67

def initialize(product_id: "BTC-USD", start: true, &block)
  @product_id = product_id
  @bids = []
  @asks = []
  @snapshot_sequence = 0
  @last_sequence = 0
  @queue = Queue.new
  @websocket = Coinbase::Exchange::Websocket.new(keepalive: true, product_id: @product_id)
  @client = Coinbase::Exchange::Client.new('', '', '', product_id: @product_id)
  @on_message = block if block_given?
  start && start!
end

Instance Attribute Details

#asksObject (readonly)

Array of asks



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

def asks
  @asks
end

#bidsObject (readonly)

Array of bids



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

def bids
  @bids
end

#clientObject (readonly)

Coinbase::Exchange::Client object



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

def client
  @client
end

#em_threadObject (readonly)

Thread running the EM loop for the websocket



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

def em_thread
  @em_thread
end

#last_pongObject (readonly)

DateTime of last successful pong



55
56
57
# File 'lib/orderbook.rb', line 55

def last_pong
  @last_pong
end

#last_sequenceObject (readonly)

Sequence number of most recently received message



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

def last_sequence
  @last_sequence
end

#processing_threadObject (readonly)

Thread running the processing loop



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

def processing_thread
  @processing_thread
end

#product_idObject (readonly)

Product ID of the orderbook



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

def product_id
  @product_id
end

#queueObject (readonly)

Message queue for incoming messages.



59
60
61
# File 'lib/orderbook.rb', line 59

def queue
  @queue
end

#snapshot_sequenceObject (readonly)

Sequence number from the initial level 3 snapshot



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

def snapshot_sequence
  @snapshot_sequence
end

#websocketObject (readonly)

Coinbase::Exchange::Websocket object



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

def websocket
  @websocket
end

Instance Method Details

#on_message(&block) ⇒ Object



110
111
112
# File 'lib/orderbook.rb', line 110

def on_message(&block)
  @on_message = block
end

#reset!Object

Start and stop the Orderbook. Used to reset Orderbook state with a fresh snapshot.



105
106
107
108
# File 'lib/orderbook.rb', line 105

def reset!
  stop!
  start!
end

#start!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.



83
84
85
86
87
88
89
90
91
92
# File 'lib/orderbook.rb', line 83

def start!
  start_em_thread

  # Wait to make sure the snapshot sequence ID is higher than the sequence of
  # the first message in the queue.
  #
  sleep 0.3
  apply_orderbook_snapshot
  start_processing_thread
end

#stop!Object

Stops the processing thread, EM thread, and the websocket.



96
97
98
99
100
# File 'lib/orderbook.rb', line 96

def stop!
  @processing_thread.kill
  @em_thread.kill
  @websocket.stop!
end