Class: Deribit::Client
Overview
Deribit API 2.0 client implementation
Instance Attribute Summary collapse
-
#http ⇒ Object
readonly
Returns the value of attribute http.
-
#websocket ⇒ Object
readonly
Returns the value of attribute websocket.
Instance Method Summary collapse
-
#account(currency: 'BTC', ext: false) {|Hashie::Mash| ... } ⇒ Hashie::Mash
Retrieves user account summary.
-
#announcements {|Hashie::Mash| ... } ⇒ Array
Retrieves announcements from last 30 days.
-
#book(options = { instrument_name: 'BTC-PERPETUAL' }) {|Hashie::Mash| ... } ⇒ Hashie::Mash
Notifies about changes to the order book for a certain instrument.
-
#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”.
-
#cancel(order_id) ⇒ Hashie::Mash
Cancels an order, specified by order id.
-
#cancel_all(options = {}) ⇒ Boolean
Cancels all orders, optionally filtered by instrument or instrument type.
-
#close(instrument_name, options = { type: :market }) ⇒ Hashie::Mash
Close a position.
-
#currencies ⇒ Array
Retrieves all cryptocurrencies supported by the API.
-
#edit(order_id, amount, price, options = {}) ⇒ Hashie::Mash
Changes price and/or quantity of the own order.
-
#index(options = { currency: 'BTC' }) ⇒ Hashie::Mash
Retrieves the current index price for the BTC-USD instruments.
-
#initialize(key: nil, secret: nil, testnet: false, debug: false) ⇒ Deribit::Client
constructor
Create new instance.
-
#instruments(options = { currency: 'BTC' }) ⇒ Array
Retrieves available trading instruments.
-
#orders(options) {|Hashie::Mash| ... } ⇒ Array
Retrieves open orders.
-
#positions(options = { currency: 'BTC' }) ⇒ Array
Retrieves current positions.
-
#quote(options = { instrument_name: 'BTC-PERPETUAL' }, &blk) ⇒ Object
Best bid/ask price and size.
-
#sell(instrument_name, amount, options = {}) ⇒ Hashie::Mash
Places a sell order for an instrument.
-
#settlements(filters = { instrument_name: 'BTC-PERPETUAL' }) {|Hashie::Mash| ... } ⇒ Hashie::Mash
Retrieves settlement, delivery and bankruptcy events that have occurred.
-
#ticker(options = { instrument_name: 'BTC-PERPETUAL' }, &blk) ⇒ Object
Key information about the instrument.
-
#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.
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
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
#http ⇒ Object (readonly)
Returns the value of attribute http.
12 13 14 |
# File 'lib/deribit/client.rb', line 12 def http @http end |
#websocket ⇒ Object (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.
174 175 176 177 178 179 180 |
# File 'lib/deribit/client.rb', line 174 def account(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.
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.
70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/deribit/client.rb', line 70 def book( = { instrument_name: 'BTC-PERPETUAL' }, &blk) unless [:instrument_name] raise ArgumentError, 'instrument_name argument is required' end if block_given? channel = book_channel websocket.subscribe channel, params: {}, &blk else http.get '/public/get_order_book', 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)
201 202 203 204 |
# File 'lib/deribit/client.rb', line 201 def buy(instrument_name, amount, = {}) params = .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.
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.
265 266 267 268 |
# File 'lib/deribit/client.rb', line 265 def cancel_all( = {}) uri = cancel_uri http.get uri, end |
#close(instrument_name, options = { type: :market }) ⇒ Hashie::Mash
Close a position
225 226 227 228 |
# File 'lib/deribit/client.rb', line 225 def close(instrument_name, = { type: :market }) params = .merge instrument_name: instrument_name, type: [:type] http.get '/private/close_position', params end |
#currencies ⇒ Array
Retrieves all cryptocurrencies supported by the API.
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.
242 243 244 245 |
# File 'lib/deribit/client.rb', line 242 def edit(order_id, amount, price, = {}) params = .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.
51 52 53 54 55 56 57 |
# File 'lib/deribit/client.rb', line 51 def index( = { currency: 'BTC' }) unless [:currency] raise ArgumentError, 'currency argument is required' end http.get '/public/get_index', end |
#instruments(options = { currency: 'BTC' }) ⇒ Array
Retrieves available trading instruments.
33 34 35 36 37 |
# File 'lib/deribit/client.rb', line 33 def instruments( = { currency: 'BTC' }) raise ArgumentError, 'currency is required' unless [:currency] http.get '/public/get_instruments', end |
#orders(options) {|Hashie::Mash| ... } ⇒ Array
Retrieves open orders.
308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/deribit/client.rb', line 308 def orders(, &blk) raise ArgumentError, 'either :instrument_name or :currency is required' unless [:instrument_name] || [:currency] if block_given? channel = channel 'user.orders', websocket.subscribe channel, params: , &blk else uri = orders_uri http.get uri, end end |
#positions(options = { currency: 'BTC' }) ⇒ Array
Retrieves current positions.
326 327 328 |
# File 'lib/deribit/client.rb', line 326 def positions( = { currency: 'BTC' }) http.get '/private/get_positions', end |
#quote(options = { instrument_name: 'BTC-PERPETUAL' }, &blk) ⇒ Object
Best bid/ask price and size.
274 275 276 277 278 279 280 281 |
# File 'lib/deribit/client.rb', line 274 def quote( = { 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', websocket.subscribe channel, params: , &blk end |
#sell(instrument_name, amount, options = {}) ⇒ Hashie::Mash
Places a sell order for an instrument.
212 213 214 215 |
# File 'lib/deribit/client.rb', line 212 def sell(instrument_name, amount, = {}) params = .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.
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
288 289 290 291 292 293 294 295 |
# File 'lib/deribit/client.rb', line 288 def ticker( = { instrument_name: 'BTC-PERPETUAL' }, &blk) if block_given? channel = channel 'ticker', websocket.subscribe channel, params: , &blk else http.get '/public/ticker', 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.
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 |