RTCBX
RTCBX uses the Coinbase (now GDAX) Exchange websocket feed to provide immediate access to the current state of the exchange without repeatedly polling parts of the RESTful API. It can:
- Keep a synchronized copy of the entire orderbook -
RTCBX::Orderbook - Calculate historic rates (candles) by the minute -
RTCBX::Candles - Place and track orders for an account -
RTCBX::Trader
Each type of RTCBX object will supports defining callbacks to run when:
- The
Orderbookchanges. - A new candle is generated.
- Your order(s) change status.
Installation
Add this line to your application's Gemfile:
gem 'rtcbx'
And then execute:
$ bundle
Or install it yourself as:
$ gem install rtcbx
Usage
require 'rtcbx'
RTCBX objects share a common interface:
#
# :product_id
# sets the currency (defaults to 'BTC-USD)
#
# :start
# run #start! at creation? (defaults to true)
#
rtcbx = RTCBX.new({product_id: 'BTC-GPB', start: false}) do |change|
# check some values, do some stuff
end
rtcbx.start! # Starts the websocket feed and tracking/update threads.
rtcbx.stop! # Stops the websocket feed and any tracking/update threads.
rtcbx.reset! # Calls #stop! then calls #start!.
Create a live updating Orderbook:
ob = RTCBX::Orderbook.newCreate an Orderbook object but don't fetch an orderbook or start live updating.
ob = RTCBX::Orderbook.new(start: false)
When you want it to go live:
ob.start!
When you want to stop it:
ob.stop!
Reset the orderbook by fetching a fresh orderbook snapshot. This just calls
stop! and then start! again:
ob.reset!
* Get the "BTC-GBP" orderbook instead of "BTC-USD":
```ruby
ob = RTCBX::Orderbook.new(product_id: "BTC-GBP")
Create a live Orderbook with a callback to fire on each message:
ob = RTCBX::Orderbook.new do || if .fetch 'type' == 'match' puts ob.spread.to_f('s') end endCreate or reset the message callback:
ob. do || puts ob.count endList current bids:
ob.bidsList current asks:
ob.asksShow sequence number for initial level 3 snapshot:
ob.snapshot_sequenceShow sequence number for the last message received
ob.last_sequenceShow the last Time a pong was received after a ping (ensures the connection is still alive):
ob.last_pongAggregates the top N bids in the Orderbook (optional parameter defaults to aggregate all available bids):
ob.aggregate_bids(10)Aggregates the first N asks in the Orderbook (optional parameter defaults to aggregate all available asks):
ob.aggregate_asks(10)Aggregates the top N bids and the first N asks in the Orderbook (optional parameter defaults to aggregate the entire Orderbook):
# Will perform the aggregation on each call. Avoid abusing this function since it may degrade performance. ob.aggregate(10)
Contributing
- Fork it ( https://github.com/mikerodrigues/orderbook/fork )
- Create your feature branch (
git checkout -b my-new-feature) - Commit your changes (
git commit -am 'Add some feature') - Push to the branch (
git push origin my-new-feature) - Create a new Pull Request