Deribit
API Client for v1 Deribit API
Installation
Add this line to your application's Gemfile:
gem 'deribit'And then execute:
$ bundle
Or install it yourself as:
$ gem install deribit
Usage
Example
require 'deribit'
# main server
api = Deribit::API.new("KEY", "SECRET")
# test server
api = Deribit::API.new("KEY", "SECRET", test_server: true)
api.index
api.account_info
API
Deribit::API.new(key, secret)
Constructor creates new API client.
Parameters
| Name | Type | Decription |
|---|---|---|
key |
string |
Optional, Access Key needed to access Private functions |
secret |
string |
Optional, Access Secret needed to access Private functions |
Methods
-
instruments(expired: false, only_active: true)- Doc, publicReturns active instruments Parameters
Name Type Decription expiredbooleanOptional, for returning all expired instruments. only_activebooleanOptional, returns only isActive true instruments -
orderbook(instrument)- Doc, publicRetrieve the orderbook for a given instrument.
Parameters
Name Type Decription instrumentstringRequired, instrument name -
index- Doc, publicGet price index, BTC-USD rates.
-
currencies- Doc, publicGet all supported currencies.
-
last_trades(instrument, count: count, since: since)- Doc, publicRetrieve the latest trades that have occured for a specific instrument.
Parameters
Name Type Decription instrumentstringRequired, instrument name countintegerOptional, count of trades returned (limitation: max. count is 100) sinceintegerOptional, “since” trade id, the server returns trades newer than that “since” -
summary(instrument)- Doc, publicRetrieve the summary info such as Open Interest, 24H Volume etc for a specific instrument.
Parameters
Name Type Decription instrumentstringRequired, instrument name -
account- Doc, PrivateGet user account summary.
-
buy(instrument, quantity, price, type: "limit", stopPx: stopPx, post_only: post_only, label: label, max_show: max_show, adv: adv)- Doc, privatePlace a buy order in an instrument.
Parameters
Name Type Decription instrumentstringRequired, instrument name quantityintegerRequired, quantity, in contracts ($10 per contract for futures, ฿1 — for options) pricefloatRequired, USD for futures, BTC for options post_onlybooleanOptional, if true then the order will be POST ONLY labelstringOptional, user defined maximum 32-char label for the order typestringRequired, "limit", "market" or for futures only: "stop_limit" stopPxstringRequired, needed for stop_limit order, defines stop price max_showstringOptional, optional parameter, if "0" then the order will be hidden advstringOptional, can be "implv", "usd", or absent (advanced order type) -
sell(instrument, quantity, price, post_only: post_only, label: label, max_show: max_show, adv: adv)- Doc, privatePlace a sell order in an instrument.
Parameters
Name Type Decription instrumentstringRequired, instrument name quantityintegerRequired, quantity, in contracts ($10 per contract for futures, ฿1 — for options) pricefloatRequired, USD for futures, BTC for options post_onlybooleanOptional, if true then the order will be POST ONLY labelstringOptional, user defined maximum 32-char label for the order max_showstringOptional, optional parameter, if "0" then the order will be hidden advstringOptional, can be "implv", "usd", or absent (advanced order type) -
edit(order_id, quantity, price, post_only: post_only, adv: adv)- DocEdit price and/or quantity of the own order. (Authorization is required).
Parameters
Name Type Decription order_idintegerRequired, ID of the order returned by "sell" or "buy" request quantityintegerRequired, quantity, in contracts ($10 per contract for futures, ฿1 — for options) pricefloatRequired, USD for futures, BTC for options post_onlybooleanOptional, if true then the order will be POST ONLY advstringOptional, can be "implv", "usd", or absent (advanced order type)
cancel(order_id) - Doc, private
Cancell own order by id.
Parameters
| Name | Type | Decription |
|---|---|---|
order_id |
integer |
Required, ID of the order returned by "sell" or "buy" request |
-
open_orders(instrument)- Doc, privateRetrieve open orders.
Parameters
Name Type Description instrumentstringOptional, instrument name, use if want orders for specific instrument -
positions- Doc, privateRetreive positions.
-
trade_history(count: count, instrument: instrument, start_trade_id: start_trade_id)- Doc, privateGet private trade history of the account. (Authorization is required). The result is ordered by trade identifiers (trade id-s).
Parameters
Name Type Description countintegerOptional, number of results to fetch. Default: 20 instrumentstringOptional, name of instrument, also aliases “all”, “futures”, “options” are allowed. Default: "all" start_trade_idintegerOptional, number of requested records
Websocket API
require 'deribit'
ws = Deribit::WS.new("KEY", "SECRET")
ws.account
#subscribe
ws.subscribe(['BTC-30NOV18-6500-P'], events: [:order_book])
Handler
Create inheritance class for handling WS notifications
class MyHandler < Deribit::WS::Handler
# event handler
def order_book_event(json)
# your actions here
# json example for order_book_event
# {"state"=>"open", "settlementPrice"=>0.2105, "instrument"=>"BTC-30NOV18-6500-P", "bids"=>[{"quantity"=>10.0, "amount"=>10.0, "price"=>0.2785, "cm"=>10.0, "cm_amount"=>10.0}, {"quantity"=>1.0, "amount"=>1.0, "price"=>0.05, "cm"=>11.0, "cm_amount"=>11.0}], "asks"=>[{"quantity"=>9.0, "amount"=>9.0, "price"=>0.2935, "cm"=>9.0, "cm_amount"=>9.0}], "tstamp"=>1542650516519, "last"=>0.285, "low"=>0.285, "high"=>0.285, "mark"=>0.286, "uPx"=>5060.54, "uIx"=>"index_price", "iR"=>0, "markIv"=>106.0, "askIv"=>110.45, "bidIv"=>0.0, "delta"=>-0.90264, "gamma"=>0.00019, "vega"=>1.48288, "theta"=>-7.42706}
end
# action handler
def instruments(json)
# your actions here
end
end
Available events you can check in the guide https://deribit.com/main#/pages/docs/api WebSockets API section.
Development
After checking out the repo, run bin/setup to install dependencies. Then, run rake spec to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install. To release a new version, update the version number in version.rb, and then run bundle exec rake release, which will create a git tag for the version, push git commits and tags, and push the .gem file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/vizakenjack/deribit. This project is intended to be a safe, welcoming space for collaboration.