Class: RTCBX

Inherits:
Object
  • Object
show all
Defined in:
lib/rtcbx/version.rb,
lib/rtcbx.rb,
lib/rtcbx/trader.rb,
lib/rtcbx/candles.rb,
lib/rtcbx/orderbook.rb,
lib/rtcbx/candles/candle.rb,
lib/rtcbx/orderbook/book_methods.rb,
lib/rtcbx/orderbook/book_analysis.rb

Overview

This class represents the current state of the CoinBase Exchange orderbook.

Direct Known Subclasses

Candles, Orderbook, Trader

Defined Under Namespace

Classes: Candles, Orderbook, Trader

Constant Summary collapse

PING_INTERVAL =

seconds in between pinging the connection.

2
VERSION =
'0.0.5'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}, &block) ⇒ RTCBX

Create a new RTCBX object with options and an optional block to be run when each message is called.

Generally you won’t call this directly. You’ll use RTCBX::Orderbook.new, RTCBX::Trader.new, or RTCBX::Candles.new.

You can also subclass RTCBX and call this method through super, as the classes mentioned above do.

RTCBX handles connecting to the Websocket, setting up the client, and managing the thread that consumes the Websocket feed.



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rtcbx.rb', line 57

def initialize(options = {}, &block)
  @product_id     = options.fetch(:product_id, 'BTC-USD')
  @start          = options.fetch(:start, true)
  @api_key        = options.fetch(:api_key, '')
  @api_secret     = options.fetch(:api_secret, '')
  @api_passphrase = options.fetch(:api_passphrase, '')
  @message_callbacks = []
  @message_callbacks << block if block_given?
  @client = Coinbase::Exchange::Client.new(
    api_key,
    api_secret,
    api_passphrase,
    product_id: product_id
  )
  @websocket = Coinbase::Exchange::Websocket.new(
    keepalive: true,
    product_id: product_id
  )
  @queue = Queue.new
  start! if start
end

Instance Attribute Details

#api_keyObject (readonly)

API key used to authenticate to the API Not required for Orderbook or Candles



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

def api_key
  @api_key
end

#clientObject (readonly)

The GDAX Client object You can use this if you need to make API calls



32
33
34
# File 'lib/rtcbx.rb', line 32

def client
  @client
end

#last_pongObject (readonly)

Epoch time indicating the last time we received a pong from GDAX in response to one of our pings



40
41
42
# File 'lib/rtcbx.rb', line 40

def last_pong
  @last_pong
end

#message_callbacksObject (readonly)

An array of blocks to be run each time a message comes in on the Websocket



25
26
27
# File 'lib/rtcbx.rb', line 25

def message_callbacks
  @message_callbacks
end

#product_idObject (readonly)

The GDAX product being tracked (eg. “BTC-USD”)



14
15
16
# File 'lib/rtcbx.rb', line 14

def product_id
  @product_id
end

#queueObject (readonly)

The message queue from the Websocket. The websocket_thread processes this queue



36
37
38
# File 'lib/rtcbx.rb', line 36

def queue
  @queue
end

#startObject (readonly)

Boolean, whether the orderbook goes live on creation or not If false, #start! must be called to initiate tracking.



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

def start
  @start
end

#websocketObject (readonly)

The Websocket object



28
29
30
# File 'lib/rtcbx.rb', line 28

def websocket
  @websocket
end

#websocket_threadObject (readonly)

The thread that consumes the websocket data



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

def websocket_thread
  @websocket_thread
end

Instance Method Details

#reset!Object

Stops, then starts the thread that consumes the Websocket feed



91
92
93
94
# File 'lib/rtcbx.rb', line 91

def reset!
  stop!
  start!
end

#start!Object

Starts the thread to consume the Websocket feed



80
81
82
# File 'lib/rtcbx.rb', line 80

def start!
  start_websocket_thread
end

#stop!Object

Stops the thread and disconnects from the Websocket



85
86
87
88
# File 'lib/rtcbx.rb', line 85

def stop!
  websocket_thread.kill
  websocket.stop!
end