Class: Deribit::Client

Inherits:
Object
  • Object
show all
Includes:
Naming
Defined in:
lib/deribit/client.rb

Overview

Deribit API 2.0 client implementation

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Naming

#book_channel, #by_currency, #by_instrument, #cancel_uri, #channel, #channel_for_instrument, #for_currency, #for_instrument, #instrument_with_interval, #orders_uri, #trades_channel, #trades_uri, #with_group_and_depth, #with_interval

Constructor Details

#initialize(key: nil, secret: nil, testnet: false, debug: false) ⇒ Deribit::Client

Create new instance

Parameters:

  • key (String) (defaults to: nil)

    Deribit Access Key

  • secret (String) (defaults to: nil)

    Deribit Secret Key

  • testnet (Boolean) (defaults to: false)

    set to true for testing

  • debug (Boolean) (defaults to: false)

    set to true for debug output



20
21
22
23
24
# File 'lib/deribit/client.rb', line 20

def initialize(key: nil, secret: nil, testnet: false, debug: false)
  host = testnet ? TESTNET_HOST : MAINNET_HOST
  @http = Deribit::Http.new host, key: key, secret: secret, debug: debug
  @websocket = Deribit::Websocket.new host, key: key, secret: secret
end

Instance Attribute Details

#httpObject (readonly)

Returns the value of attribute http.



12
13
14
# File 'lib/deribit/client.rb', line 12

def http
  @http
end

#websocketObject (readonly)

Returns the value of attribute websocket.



12
13
14
# File 'lib/deribit/client.rb', line 12

def websocket
  @websocket
end

Instance Method Details

#account(currency: 'BTC', ext: false) {|Hashie::Mash| ... } ⇒ Hashie::Mash

Retrieves user account summary.

Parameters:

  • currency (String) (defaults to: 'BTC')

    Currency summary

  • ext (Boolean) (defaults to: false)

    Requests additional fields

Yields:

  • (Hashie::Mash)

    the account details

Returns:

  • (Hashie::Mash)

    the account details

See Also:



174
175
176
177
178
179
180
# File 'lib/deribit/client.rb', line 174

def (currency: 'BTC', ext: false)
  if block_given?
    raise Deribit::NotImplementedError, 'not implemented'
  else
    http.get '/private/get_account_summary', currency: currency
  end
end

#announcements {|Hashie::Mash| ... } ⇒ Array

Retrieves announcements from last 30 days.

Yields:

  • (Hashie::Mash)

    the announcement

Returns:

  • (Array)

    the list of announcements

See Also:



135
136
137
138
139
140
141
# File 'lib/deribit/client.rb', line 135

def announcements(&blk)
  if block_given?
    websocket.subscribe 'announcements', &blk
  else
    http.get '/public/get_announcements'
  end
end

#book(options = { instrument_name: 'BTC-PERPETUAL' }) {|Hashie::Mash| ... } ⇒ Hashie::Mash

Notifies about changes to the order book for a certain instrument.

Parameters:

  • instrument_name (String)

    The instrument name

  • options (Hash) (defaults to: { instrument_name: 'BTC-PERPETUAL' })

Options Hash (options):

  • :instrument_name (String) — default: BTC-PERPETUAL

    Instrument to return open orders for

  • :group (Integer) — default: 5

    Group prices (by rounding): none, 5, 10

  • :depth (Integer) — default: 10

    the depth of the order book

  • :interval (String) — default: raw

    Frequency of notifications: raw, 100ms

Yields:

  • (Hashie::Mash)

    the order book

Returns:

  • (Hashie::Mash)

    the order book

See Also:



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/deribit/client.rb', line 70

def book(options = { instrument_name: 'BTC-PERPETUAL' }, &blk)
  unless options[:instrument_name]
    raise ArgumentError, 'instrument_name argument is required'
  end

  if block_given?
    channel = book_channel options
    websocket.subscribe channel, params: {}, &blk
  else
    http.get '/public/get_order_book', options
  end
end

#buy(instrument_name, amount, options = {}) ⇒ Hashie::Mash

@option options [Float] :stop_price price required for stop limit orders (Only valid for stop orders)

@option options [String] :trigger Defines trigger type: index_price mark_price last_price, required for "stop_limit" order type
@option options [String] :advanced Advanced option order type, can be "implv", "usd". (Only valid for options)

Returns:

  • (Hashie::Mash)

    the details of new order

See Also:



201
202
203
204
# File 'lib/deribit/client.rb', line 201

def buy(instrument_name, amount, options = {})
  params = options.merge instrument_name: instrument_name, amount: amount
  http.get 'private/buy', params
end

#cancel(order_id) ⇒ Hashie::Mash

Cancels an order, specified by order id.

Parameters:

  • order_id (String)

    The order id of the order to be cancelled

Returns:

  • (Hashie::Mash)

    details of the cancelled order

See Also:



251
252
253
# File 'lib/deribit/client.rb', line 251

def cancel(order_id)
  http.get '/private/cancel', order_id: order_id
end

#cancel_all(options = {}) ⇒ Boolean

Cancels all orders, optionally filtered by instrument or instrument type.

Parameters:

  • options (Hash) (defaults to: {})

    extra options

Options Hash (options):

  • :instrument_name (String)

    The name of the instrument for which to cancel all orders

  • :currency (String)

    The currency symbol

  • :type (String)

    Which type of orders to cancel. Valid values are “all”, “futures”, “options”

  • :kind (String)

    Instrument kind, if not provided instruments of all kinds are considered

Returns:

  • (Boolean)

    success or not

See Also:



265
266
267
268
# File 'lib/deribit/client.rb', line 265

def cancel_all(options = {})
  uri = cancel_uri options
  http.get uri, options
end

#close(instrument_name, options = { type: :market }) ⇒ Hashie::Mash

Close a position

Parameters:

  • instrument_name (String)

    Name of the instrument to sell

  • type (String)
  • options (Hash) (defaults to: { type: :market })

    the options

Options Hash (options):

  • :type (String)

    The order type: limit or market

  • :price (String)

    Price for limit close

Returns:

  • (Hashie::Mash)

    the details of closed position

See Also:



225
226
227
228
# File 'lib/deribit/client.rb', line 225

def close(instrument_name, options = { type: :market })
  params = options.merge instrument_name: instrument_name, type: options[:type]
  http.get '/private/close_position', params
end

#currenciesArray

Retrieves all cryptocurrencies supported by the API.

Returns:

  • (Array)

    the list of cryptocurrencies

See Also:



42
43
44
# File 'lib/deribit/client.rb', line 42

def currencies
  http.get '/public/get_currencies'
end

#edit(order_id, amount, price, options = {}) ⇒ Hashie::Mash

Changes price and/or quantity of the own order.

Parameters:

  • order_id (String)

    ID of the order to edit

  • amount (Integer)

    The new order quantity

  • price (Float)

    The new order price

  • options (Hash) (defaults to: {})

    extra options

Options Hash (options):

  • :post_only (Boolean)

    If true, the edited order is considered post-only. If the new price would cause the order to be filled immediately (as taker), the price will be changed to be just below the bid (for buy orders) or just above the ask (for sell orders).

  • :reduce_only (Boolean)

    If true, the order is considered reduce-only which is intended to only reduce a current position

  • :reject_post_only (Boolean)

    If order is considered post-only and this field is set to true than order is put to order book unmodified or request is rejected.

  • :advanced (String)

    Advanced option order type. If you have posted an advanced option order, it is necessary to re-supply this parameter when editing it (Only for options)

  • :stop_price (Float)

    Stop price, required for stop limit orders (Only for stop orders)

Returns:

  • (Hashie::Mash)

    the edited order

See Also:



242
243
244
245
# File 'lib/deribit/client.rb', line 242

def edit(order_id, amount, price, options = {})
  params = options.merge order_id: order_id, amount: amount, price: price
  http.get '/private/edit', params
end

#index(options = { currency: 'BTC' }) ⇒ Hashie::Mash

Retrieves the current index price for the BTC-USD instruments.

Parameters:

  • options (Hash) (defaults to: { currency: 'BTC' })

Options Hash (options):

  • :currency (String)

    the currency to get instruments for

Returns:

  • (Hashie::Mash)

    index price for BTC-USD instrument

See Also:



51
52
53
54
55
56
57
# File 'lib/deribit/client.rb', line 51

def index(options = { currency: 'BTC' })
  unless options[:currency]
    raise ArgumentError, 'currency argument is required'
  end

  http.get '/public/get_index', options
end

#instruments(options = { currency: 'BTC' }) ⇒ Array

Retrieves available trading instruments.

Parameters:

  • options (Hash) (defaults to: { currency: 'BTC' })

Options Hash (options):

  • :currency (String)

    the currency to get instruments for

  • :kind (String)

    instrument kind, if not provided instruments of all kinds are considered

  • :expired (Integer)

    set to true to show expired instruments instead of active ones.

Returns:

  • (Array)

    the list of instruments

Raises:

  • (ArgumentError)

See Also:



33
34
35
36
37
# File 'lib/deribit/client.rb', line 33

def instruments(options = { currency: 'BTC' })
  raise ArgumentError, 'currency is required' unless options[:currency]

  http.get '/public/get_instruments', options
end

#orders(options) {|Hashie::Mash| ... } ⇒ Array

Retrieves open orders.

Parameters:

  • options (Hash)

Options Hash (options):

  • :instrument_name (String)

    Instrument to return open orders for

  • :currency (String)

    The currency symbol, BTC, ETH, any

  • :kind (string) — default: any

    Instrument kind, future, option or any

  • :type (string) — default: all

    Order type: all, limit, stop_all, stop_limit, stop_market

  • :interval (String) — default: raw

    Frequency of notifications: raw, 100ms

Yields:

  • (Hashie::Mash)

    the order

Returns:

  • (Array)

    the list of open orders

Raises:

  • (ArgumentError)

See Also:



308
309
310
311
312
313
314
315
316
317
318
# File 'lib/deribit/client.rb', line 308

def orders(options, &blk)
  raise ArgumentError, 'either :instrument_name or :currency is required' unless options[:instrument_name] || options[:currency]

  if block_given?
    channel = channel 'user.orders', options
    websocket.subscribe channel, params: options, &blk
  else
    uri = orders_uri options
    http.get uri, options
  end
end

#positions(options = { currency: 'BTC' }) ⇒ Array

Retrieves current positions.

Parameters:

  • options (Hash) (defaults to: { currency: 'BTC' })

Options Hash (options):

  • :currency (String) — default: any

    The currency symbol, BTC, ETH

  • :kind (string) — default: any

    Instrument kind, future, option

Returns:

  • (Array)

    the list of positions

See Also:



326
327
328
# File 'lib/deribit/client.rb', line 326

def positions(options = { currency: 'BTC' })
  http.get '/private/get_positions', options
end

#quote(options = { instrument_name: 'BTC-PERPETUAL' }, &blk) ⇒ Object

Best bid/ask price and size.

Parameters:

  • options (Hash) (defaults to: { instrument_name: 'BTC-PERPETUAL' })

Options Hash (options):

  • :instrument_name (String) — default: BTC-PERPETUAL

    Instrument to return open orders for

See Also:



274
275
276
277
278
279
280
281
# File 'lib/deribit/client.rb', line 274

def quote(options = { instrument_name: 'BTC-PERPETUAL' }, &blk)
  unless block_given?
    raise 'block is missing, HTTP-RPC not supported for this endpoint'
  end

  channel = channel_for_instrument 'quote', options
  websocket.subscribe channel, params: options, &blk
end

#sell(instrument_name, amount, options = {}) ⇒ Hashie::Mash

Places a sell order for an instrument.

Parameters:

  • instrument_name (String)

    Name of the instrument to sell

  • amount (Integer)

    The number of contracts to buy

  • options (Hash) (defaults to: {})

    more options for the order

Options Hash (options):

  • :type (String) — default: limit

    The order type, possible types: “limit”, “stop_limit”, “market”, “stop_market”

  • :label (String)

    user defined label for the order (maximum 32 characters)

  • :price (Float)

    The order price (Only valid for limit and stop_limit orders)

  • :time_in_force (String) — default: good_til_cancelled

    Specifies how long the order remains in effect, possible values “good_til_cancelled”, “fill_or_kill”, “immediate_or_cancel”

  • :max_show (Integer)

    Maximum quantity within an order to be shown to other customers, 0 for invisible order.

  • :post_only (String) — default: true

    If true, the order is considered post-only. If the new price would cause the order to be filled immediately (as taker), the price will be changed to be just below the bid.

  • :reject_post_only (String) — default: false

    If order is considered post-only and this field is set to true than order is put to order book unmodified or request is rejected.

  • :reduce_only (String)

    If true, the order is considered reduce-only which is intended to only reduce a current position

Returns:

  • (Hashie::Mash)

    the details of new order

See Also:



212
213
214
215
# File 'lib/deribit/client.rb', line 212

def sell(instrument_name, amount, options = {})
  params = options.merge instrument_name: instrument_name, amount: amount
  http.get '/private/sell', params
end

#settlements(filters = { instrument_name: 'BTC-PERPETUAL' }) {|Hashie::Mash| ... } ⇒ Hashie::Mash

Retrieves settlement, delivery and bankruptcy events that have occurred.

Parameters:

  • filters (Hash) (defaults to: { instrument_name: 'BTC-PERPETUAL' })

    the filters

Options Hash (filters):

  • :instrument_name (String)

    The instrument name,

  • :currency (String)

    The currency of settlements

  • :type (String)

    settlement type: settlement delivery bankruptcy

  • :count (Integer) — default: 20

    Number of requested items, default

  • :continuation (String)

    Continuation token for pagination

Yields:

  • (Hashie::Mash)

    the settlements

Returns:

  • (Hashie::Mash)

    the settlements

See Also:



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/deribit/client.rb', line 154

def settlements(filters = { instrument_name: 'BTC-PERPETUAL' })
  instrument_name = filters[:instrument_name]
  currency = filters[:currency]
  unless instrument_name || currency
    raise ArgumentError, 'either :instrument_name or :currency arg is required'
  end

  if block_given?
    raise Deribit::NotImplementedError, 'not implemented'
  else
    http.get '/public/get_last_settlements_by_instrument', filters
  end
end

#ticker(options = { instrument_name: 'BTC-PERPETUAL' }, &blk) ⇒ Object

Key information about the instrument

Parameters:

  • options (Hash) (defaults to: { instrument_name: 'BTC-PERPETUAL' })

Options Hash (options):

  • :instrument_name (String) — default: BTC-PERPETUAL

    Instrument to return open orders for

  • :interval (String) — default: raw

    Frequency of notifications: raw, 100ms

See Also:



288
289
290
291
292
293
294
295
# File 'lib/deribit/client.rb', line 288

def ticker(options = { instrument_name: 'BTC-PERPETUAL' }, &blk)
  if block_given?
    channel = channel 'ticker', options
    websocket.subscribe channel, params: options, &blk
  else
    http.get '/public/ticker', options
  end
end

#trades(filters) {|Hashie::Mash| ... } ⇒ Array

Retrieve the latest trades that have occurred for instruments in a specific currency symbol/for a specific instrument and optionally within given time range.

Parameters:

  • filters (Hash)

    filters to apply

Options Hash (filters):

  • :instrument_name (String) — default: BTC-PERPETUAL

    Instrument name

  • :currency (String) — default: BTC, ETH

    The currency symbol

  • :kind (String) — default: future, option

    Instrument kind, if not provided instruments of all kinds are considered

  • :count (Integer) — default: 10

    Number of requested items

  • :start_id (Integer)

    The ID of the first trade to be returned

  • :end_id (Integer)

    The ID of the last trade to be returned

  • :start_seq (Integer)

    The trade sequence of the first trade to be returned

  • :end_seq (Integer)

    The trade sequence of the last trade to be returned

  • :start_timestamp (Integer)

    The timestamp (in ms) of the first trade to be returned

  • :end_timestamp (Integer)

    The timestamp (in ms) of the last trade to be returned

  • :include_old (Boolean) — default: false

    Include trades older than a few recent days

  • :sorting (Boolean) — default: none

    Direction of results sorting

  • :interval (String) — default: raw

    Frequency of notifications.

Yields:

  • (Hashie::Mash)

    new trade

Returns:

  • (Array)

    the list of trades

See Also:



113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/deribit/client.rb', line 113

def trades(filters, &blk)
  instrument_name = filters[:instrument_name]
  currency = filters[:currency]
  unless instrument_name || currency
    raise ArgumentError, 'either :instrument_name or :currency args is required'
  end

  if block_given?
    channel = trades_channel filters
    websocket.subscribe channel, params: {}, &blk
  else
    uri = trades_uri filters
    response = http.get uri, filters
    response.trades
  end
end