Class: Deribit::Client

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

Overview

Author:

Instance Attribute Summary collapse

Instance Method Summary collapse

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



17
18
19
20
21
# File 'lib/deribit/client.rb', line 17

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.



9
10
11
# File 'lib/deribit/client.rb', line 9

def http
  @http
end

#websocketObject (readonly)

Returns the value of attribute websocket.



9
10
11
# File 'lib/deribit/client.rb', line 9

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:



171
172
173
174
175
176
177
# File 'lib/deribit/client.rb', line 171

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:



132
133
134
135
136
137
138
# File 'lib/deribit/client.rb', line 132

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:



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/deribit/client.rb', line 67

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

  if block_given?
    channel = Naming.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:



198
199
200
201
# File 'lib/deribit/client.rb', line 198

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:



248
249
250
# File 'lib/deribit/client.rb', line 248

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:



262
263
264
265
# File 'lib/deribit/client.rb', line 262

def cancel_all(options = {})
  uri = Naming.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:



222
223
224
225
# File 'lib/deribit/client.rb', line 222

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:



39
40
41
# File 'lib/deribit/client.rb', line 39

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:



239
240
241
242
# File 'lib/deribit/client.rb', line 239

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:



48
49
50
51
52
53
54
# File 'lib/deribit/client.rb', line 48

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:



30
31
32
33
34
# File 'lib/deribit/client.rb', line 30

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

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

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

Retrieves open orders.

Parameters:

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

Options Hash (options):

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

    Instrument to return open orders for

  • :kind (string) — default: any

    Instrument kind, future, option or any

  • :currency (String) — default: any

    The currency symbol, BTC, ETH, any

  • :interval (String) — default: raw

    Frequency of notifications: raw, 100ms

Yields:

  • (Hashie::Mash)

    the order

Returns:

  • (Array)

    the list of open orders

See Also:



303
304
305
306
307
308
309
310
# File 'lib/deribit/client.rb', line 303

def orders(options = { instrument_name: 'BTC-PERPETUAL' }, &blk)
  if block_given?
    channel = Naming.channel 'user.orders', options
    websocket.subscribe channel, params: options, &blk
  else
    http.get '/private/get_open_orders_by_instrument', 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:



318
319
320
# File 'lib/deribit/client.rb', line 318

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:



271
272
273
274
275
276
277
278
# File 'lib/deribit/client.rb', line 271

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

  channel = Naming.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:



209
210
211
212
# File 'lib/deribit/client.rb', line 209

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:



151
152
153
154
155
156
157
158
159
160
161
162
163
# File 'lib/deribit/client.rb', line 151

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:



285
286
287
288
289
290
291
292
# File 'lib/deribit/client.rb', line 285

def ticker(options = { instrument_name: 'BTC-PERPETUAL' }, &blk)
  if block_given?
    channel = Naming.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:



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

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 = Naming.trades_channel filters
    websocket.subscribe channel, params: {}, &blk
  else
    uri = Naming.trades_uri filters
    response = http.get uri, filters
    response.trades
  end
end