Class: Orderbook
- Inherits:
-
Object
- Object
- Orderbook
- 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.0'
Constants included from BookMethods
Instance Attribute Summary collapse
-
#asks ⇒ Object
readonly
Array of asks.
-
#bids ⇒ Object
readonly
Array of bids.
-
#client ⇒ Object
readonly
Coinbase::Exchange::Client object.
-
#em_thread ⇒ Object
readonly
Thread running the EM loop for the websocket.
-
#last_pong ⇒ Object
readonly
DateTime of last successful pong.
-
#last_sequence ⇒ Object
readonly
Sequence number of most recently received message.
-
#processing_thread ⇒ Object
readonly
Thread running the processing loop.
-
#product_id ⇒ Object
readonly
Product ID of the orderbook.
-
#queue ⇒ Object
readonly
Message queue for incoming messages.
-
#snapshot_sequence ⇒ Object
readonly
Sequence number from the initial level 3 snapshot.
-
#websocket ⇒ Object
readonly
Coinbase::Exchange::Websocket object.
Instance Method Summary collapse
-
#initialize(product_id: "BTC-USD", start: true, &block) ⇒ Orderbook
constructor
Creates a new live copy of the orderbook.
- #on_message(&block) ⇒ Object
-
#reset! ⇒ Object
Start and stop the Orderbook.
-
#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.
-
#stop! ⇒ Object
Stops the processing thread, EM thread, and the websocket.
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
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.
68 69 70 71 72 73 74 75 76 77 78 79 |
# File 'lib/orderbook.rb', line 68 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) = block if block_given? start && start! end |
Instance Attribute Details
#asks ⇒ Object (readonly)
Array of asks
24 25 26 |
# File 'lib/orderbook.rb', line 24 def asks @asks end |
#bids ⇒ Object (readonly)
Array of bids
20 21 22 |
# File 'lib/orderbook.rb', line 20 def bids @bids end |
#client ⇒ Object (readonly)
Coinbase::Exchange::Client object
44 45 46 |
# File 'lib/orderbook.rb', line 44 def client @client end |
#em_thread ⇒ Object (readonly)
Thread running the EM loop for the websocket
48 49 50 |
# File 'lib/orderbook.rb', line 48 def em_thread @em_thread end |
#last_pong ⇒ Object (readonly)
DateTime of last successful pong
56 57 58 |
# File 'lib/orderbook.rb', line 56 def last_pong @last_pong end |
#last_sequence ⇒ Object (readonly)
Sequence number of most recently received message
36 37 38 |
# File 'lib/orderbook.rb', line 36 def last_sequence @last_sequence end |
#processing_thread ⇒ Object (readonly)
Thread running the processing loop
52 53 54 |
# File 'lib/orderbook.rb', line 52 def processing_thread @processing_thread end |
#product_id ⇒ Object (readonly)
Product ID of the orderbook
28 29 30 |
# File 'lib/orderbook.rb', line 28 def product_id @product_id end |
#queue ⇒ Object (readonly)
Message queue for incoming messages.
60 61 62 |
# File 'lib/orderbook.rb', line 60 def queue @queue end |
#snapshot_sequence ⇒ Object (readonly)
Sequence number from the initial level 3 snapshot
32 33 34 |
# File 'lib/orderbook.rb', line 32 def snapshot_sequence @snapshot_sequence end |
#websocket ⇒ Object (readonly)
Coinbase::Exchange::Websocket object
40 41 42 |
# File 'lib/orderbook.rb', line 40 def websocket @websocket end |
Instance Method Details
#on_message(&block) ⇒ Object
111 112 113 |
# File 'lib/orderbook.rb', line 111 def (&block) = block end |
#reset! ⇒ Object
Start and stop the Orderbook. Used to reset Orderbook state with a fresh snapshot.
106 107 108 109 |
# File 'lib/orderbook.rb', line 106 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.
84 85 86 87 88 89 90 91 92 93 |
# File 'lib/orderbook.rb', line 84 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.
97 98 99 100 101 |
# File 'lib/orderbook.rb', line 97 def stop! @processing_thread.kill @em_thread.kill @websocket.stop! end |