deribit-api 0.1.5 → 2.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,24 @@
1
+ #!/usr/bin/env python
2
+
3
+ import asyncio
4
+ import websockets
5
+ import json
6
+
7
+ # To subscribe to this channel:
8
+ msg = \
9
+ {"jsonrpc": "2.0",
10
+ "method": "public/subscribe",
11
+ "id": 42,
12
+ "params": {
13
+ "channels": ["trades.future.BTC.100ms"]}
14
+ }
15
+
16
+ async def call_api(msg):
17
+ async with websockets.connect('wss://test.deribit.com/ws/api/v2') as websocket:
18
+ await websocket.send(msg)
19
+ while websocket.open:
20
+ response = await websocket.recv()
21
+ # do something with the notifications...
22
+ print(response)
23
+
24
+ asyncio.get_event_loop().run_until_complete(call_api(json.dumps(msg)))
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require 'rubygems'
5
+ require 'websocket-client-simple'
6
+ require 'json'
7
+
8
+ ws = WebSocket::Client::Simple.connect 'wss://www.deribit.com/ws/api/v2'
9
+
10
+ ws.on :message do |msg|
11
+ p "msg: #{msg.data}"
12
+ end
13
+
14
+ ws.on :open do |e|
15
+ p "open: #{e}"
16
+ end
17
+
18
+ ws.on :close do |e|
19
+ p "close: #{e}"
20
+ exit 1
21
+ end
22
+
23
+ ws.on :error do |e|
24
+ p "error: #{e}"
25
+ end
26
+
27
+ sleep 3
28
+
29
+ puts 'Subscribing...'
30
+ payload = {
31
+ 'jsonrpc' => '2.0',
32
+ 'method' => 'public/subscribe',
33
+ 'id' => 66,
34
+ 'params' => {
35
+ 'channels' => ['trades.BTC-PERPETUAL.raw']
36
+ }
37
+ }
38
+ ws.send payload.to_json.to_s
39
+
40
+ while ws.open? do
41
+ sleep 1
42
+ puts 'Running...'
43
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require 'bundler/setup'
4
+ require 'deribit-api'
5
+
6
+ puts "==> Watching BTC/ETH whales...\n"
7
+
8
+ client = Deribit::Client.new
9
+
10
+ # for BTC
11
+ client.trades instrument_name: 'BTC-PERPETUAL' do |trade|
12
+ baseNotional = trade.amount / trade.price
13
+ puts "BTC: #{trade.direction} #{baseNotional} @ #{trade.price}" if baseNotional > 1
14
+ end
15
+
16
+ # for ETH
17
+ client.trades instrument_name: 'ETH-PERPETUAL' do |trade|
18
+ baseNotional = trade.amount / trade.price
19
+ puts "ETH: #{trade.direction} #{baseNotional} @ #{trade.price}" if baseNotional > 10
20
+ end
21
+
22
+ # endless
23
+ loop do
24
+ sleep 1
25
+ end
@@ -7,10 +7,10 @@ Gem::Specification.new do |spec|
7
7
  spec.name = 'deribit-api'
8
8
  spec.version = Deribit::VERSION
9
9
  spec.authors = ['Iulian Costan']
10
- spec.email = ['iulian.costan@gmail.com']
10
+ spec.email = ['deribit-api@iuliancostan.com']
11
11
 
12
- spec.summary = %q{Ruby library for Deribit API}
13
- spec.description = %q{Ruby library for Deribit API}
12
+ spec.summary = %q{Idiomatic Ruby library for Deribit API 2.0}
13
+ spec.description = %q{Idiomatic Ruby library for Deribit API 2.0}
14
14
  spec.homepage = 'https://github.com/icostan/deribit-api-ruby'
15
15
 
16
16
  # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
@@ -37,9 +37,9 @@ Gem::Specification.new do |spec|
37
37
 
38
38
  spec.add_dependency 'faraday'
39
39
  spec.add_dependency 'faraday_middleware'
40
- spec.add_dependency 'hashie'
41
- spec.add_dependency 'faye-websocket'
42
40
  spec.add_dependency 'faraday-detailed_logger'
41
+ spec.add_dependency 'websocket-client-simple'
42
+ spec.add_dependency 'hashie'
43
43
 
44
44
  spec.add_development_dependency 'bundler'
45
45
  spec.add_development_dependency 'dotenv'
@@ -50,4 +50,7 @@ Gem::Specification.new do |spec|
50
50
  spec.add_development_dependency 'pry-doc'
51
51
  spec.add_development_dependency 'reek'
52
52
  spec.add_development_dependency 'simplecov'
53
+ spec.add_development_dependency 'rubocop'
54
+ spec.add_development_dependency 'solargraph'
55
+ spec.add_development_dependency 'irb'
53
56
  end
@@ -1,28 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'openssl'
1
4
  require 'faraday'
2
5
  require 'faraday_middleware'
3
6
  require 'faraday/detailed_logger'
4
7
  require 'hashie'
5
- require 'faye/websocket'
8
+ # require 'faye/websocket'
9
+ require 'websocket-client-simple'
6
10
 
7
11
  require 'deribit/version'
8
12
  require 'deribit/client'
9
13
  require 'deribit/authentication'
10
14
  require 'deribit/websocket'
11
15
  require 'deribit/http'
16
+ require 'deribit/naming'
12
17
 
13
18
  # Root module
14
19
  module Deribit
15
20
  # Base error class
16
21
  class Error < StandardError; end
22
+ class NotImplementedError < Error; end
17
23
 
18
- # @see https://docs.deribit.com/rpc-authentication.html
19
- def self.signature(key, nonce, params, query)
20
- signature_string = params.map { |k, v| "#{k}=#{v}" }.sort.join '&'
21
- signature_string += "&#{query}" if query
24
+ # @see https://docs.deribit.com/#authentication
25
+ def self.http_signature(env, timestamp, nonce, secret)
26
+ # RequestData = UPPERCASE(HTTP_METHOD()) + "\n" + URI() + "\n" + RequestBody + "\n";
27
+ uri = env['url'].path.dup
28
+ uri << '?' << env['url'].query if env['url'].query
29
+ request_data = [env['method'].upcase, uri, env['body'], ''].join "\n"
22
30
 
23
- signature_digest = Digest::SHA256.digest signature_string
24
- signature_hash = Base64.encode64 signature_digest
31
+ signature timestamp, nonce, request_data, secret
32
+ end
25
33
 
26
- "#{key}.#{nonce}.#{signature_hash.chomp}"
34
+ # @see https://docs.deribit.com/#authentication
35
+ def self.signature(timestamp, nonce, data, secret)
36
+ # StringToSign = Timestamp + "\n" + Nonce + "\n" + Data;
37
+ # Signature = HEX_STRING( HMAC-SHA256( ClientSecret, StringToSign ) );
38
+ string_to_sign = [timestamp, nonce, data].join "\n"
39
+ ::OpenSSL::HMAC.hexdigest('SHA256', secret, string_to_sign)
27
40
  end
28
41
  end
@@ -3,7 +3,7 @@ require 'digest'
3
3
 
4
4
  module Deribit
5
5
  # Deribit authentication implemented as Faraday middleware
6
- # @see https://docs.deribit.com/rpc-authentication.html
6
+ # @see https://docs.deribit.com/#authentication
7
7
  class Authentication < Faraday::Middleware
8
8
  def initialize(app, key, secret)
9
9
  super(app)
@@ -12,25 +12,20 @@ module Deribit
12
12
  end
13
13
 
14
14
  def call(env)
15
+ return @app.call(env) if env['url'].path.include? 'public'
15
16
  return @app.call(env) if @key.nil? || @secret.nil?
16
17
 
17
- nonce = Time.now.to_i
18
- env['request_headers']['X-Deribit-Sig'] = signature env, nonce
18
+ timestamp = Time.now.utc.to_i * 1000
19
+ nonce = rand(999999)
20
+ env.request_headers['Authorization'] = header env, timestamp, nonce
19
21
 
20
22
  @app.call env
21
23
  end
22
24
 
23
- def signature(env, nonce)
24
- params = {
25
- _: nonce,
26
- _ackey: @key,
27
- _acsec: @secret,
28
- _action: env['url'].path
29
- }
30
- params.merge! JSON.parse(env['body']) if env['body']
31
- query = env['url'].query
32
-
33
- Deribit.signature @key, nonce, params, query
25
+ # Authorization: deri-hmac-sha256 id=ClientId,ts=Timestamp,sig=Signature,nonce=Nonce
26
+ def header(env, timestamp, nonce)
27
+ signature = Deribit.http_signature env, timestamp, nonce, @secret
28
+ "deri-hmac-sha256 id=#{@key},ts=#{timestamp},sig=#{signature},nonce=#{nonce}"
34
29
  end
35
30
  end
36
31
  end
@@ -1,11 +1,11 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Deribit
2
- # @author Iulian Costan
3
- class Client
4
- # URL for testnet
5
- TESTNET_URL = 'test.deribit.com'
6
- # URL for mainnet
7
- MAINNET_URL = 'www.deribit.com'
4
+ TESTNET_HOST = 'test.deribit.com'
5
+ MAINNET_HOST = 'www.deribit.com'
8
6
 
7
+ # @author Iulian Costan (deribit-api@iuliancostan.com)
8
+ class Client
9
9
  attr_reader :http, :websocket
10
10
 
11
11
  # Create new instance
@@ -15,469 +15,322 @@ module Deribit
15
15
  # @param debug [Boolean] set to true for debug output
16
16
  # @return [Deribit::Client] the instance of client
17
17
  def initialize(key: nil, secret: nil, testnet: false, debug: false)
18
- host = testnet ? TESTNET_URL : MAINNET_URL
19
- @http = Deribit::Http.new host, key: key, secret: secret
18
+ host = testnet ? TESTNET_HOST : MAINNET_HOST
19
+ @http = Deribit::Http.new host, key: key, secret: secret, debug: debug
20
20
  @websocket = Deribit::Websocket.new host, key: key, secret: secret
21
21
  end
22
22
 
23
- # Retrieves the current time (in ms).
24
- # @return [Integer] current time in milliseconds
25
- # @yield [Integer] current time in milliseconds
26
- # @see https://docs.deribit.com/rpc-endpoints.html#time
27
- def time(&blk)
28
- if block_given?
29
- websocket.subscribe :time, &blk
30
- else
31
- http.get :time
32
- end
33
- end
34
-
35
- # Signals the Websocket connection to send and request heartbeats.
36
- # @param interval [Integer] The heartbeat interval
37
- # @yield [String] 'ok' on success, error message otherwise
38
- # @see https://docs.deribit.com/rpc-endpoints.html#setheartbeat
39
- def enable_heartbeat(interval = 60, &blk)
40
- raise 'This API endpoint cannot be used over HTTP.' unless block_given?
41
-
42
- websocket.subscribe :setheartbeat, params: { interval: interval }, &blk
43
- end
44
-
45
- # Signals the Websocket connection to not send or request heartbeats.
46
- # @yield [String] 'ok' on success, error message otherwise
47
- # @see https://docs.deribit.com/rpc-endpoints.html#cancelheartbeat
48
- def cancel_heartbeat(&blk)
49
- raise 'This API endpoint cannot be used over HTTP.' unless block_given?
50
-
51
- websocket.subscribe :cancelheartbeat, &blk
52
- end
53
-
54
- # Tests the connection to the API server, and returns its version.
55
- # @param exception [any] Provide this parameter force an error message.
56
- # @return [Hashie::Mash] test data
57
- # @yield [Hashie::Mash] test data
58
- # @see https://docs.deribit.com/rpc-endpoints.html#test
59
- def test(exception: false, &blk)
60
- params = { exception: exception }
61
- if block_given?
62
- websocket.subscribe :test, params: params, &blk
63
- else
64
- http.get :test, params: params, raw_body: true
65
- end
66
- end
67
-
68
- # This API endpoint always responds with "pong".
69
- # @return [String] pong
70
- # @yield [String] pong
71
- # @see https://docs.deribit.com/rpc-endpoints.html#ping
72
- def ping(&blk)
73
- if block_given?
74
- websocket.subscribe :ping, &blk
75
- else
76
- http.get :ping
77
- end
78
- end
79
-
80
23
  # Retrieves available trading instruments.
81
- # @param expired [Boolean] Set to true to show expired instruments instead of active ones.
24
+ # @param options [Hash]
25
+ # @option options [String] :currency the currency to get instruments for
26
+ # @option options [String] :kind instrument kind, if not provided instruments of all kinds are considered
27
+ # @option options [Integer] :expired set to true to show expired instruments instead of active ones.
82
28
  # @return [Array] the list of instruments
83
- # @yield [Hashie::Mash] the instrument
84
- # @see https://docs.deribit.com/rpc-endpoints.html#getinstruments
85
- def instruments(expired: false, &blk)
86
- params = { expired: expired }
87
- if block_given?
88
- websocket.subscribe :getinstruments, params: params, &blk
89
- else
90
- http.get :getinstruments, params: params
91
- end
29
+ # @see https://docs.deribit.com/#public-get_instruments
30
+ def instruments(options = { currency: 'BTC' })
31
+ raise ArgumentError, 'currency is required' unless options[:currency]
32
+
33
+ http.get '/public/get_instruments', options
92
34
  end
93
35
 
94
36
  # Retrieves all cryptocurrencies supported by the API.
95
37
  # @return [Array] the list of cryptocurrencies
96
- # @yield [Hashie:Hash] the currency
97
38
  # @see https://docs.deribit.com/rpc-endpoints.html#getcurrencies
98
- def currencies(&blk)
99
- if block_given?
100
- websocket.subscribe :getcurrencies, &blk
101
- else
102
- http.get :getcurrencies
103
- end
39
+ def currencies
40
+ http.get '/public/get_currencies'
104
41
  end
105
42
 
106
43
  # Retrieves the current index price for the BTC-USD instruments.
107
- # @param currency [String] the currency to get index for
44
+ # @param options [Hash]
45
+ # @option options [String] :currency the currency to get instruments for
108
46
  # @return [Hashie::Mash] index price for BTC-USD instrument
109
- # @yield [Hashie::Mash] index price for BTC-USD instrument
110
47
  # @see https://docs.deribit.com/rpc-endpoints.html#index
111
- def index(currency = 'BTC', &blk)
112
- params = { currency: currency }
113
- if block_given?
114
- websocket.subscribe :index, params: params, &blk
115
- else
116
- http.get :index, params: params
48
+ def index(options = { currency: 'BTC' })
49
+ unless options[:currency]
50
+ raise ArgumentError, 'currency argument is required'
117
51
  end
52
+
53
+ http.get '/public/get_index', options
118
54
  end
119
55
 
120
- # Retrieves the order book, along with other market values for a given instrument.
121
- # @param instrument [String] The instrument name for which to retrieve the order book, (see #instruments) to obtain instrument names.
122
- # @param depth [Integer] the depth of the order book
56
+ # Notifies about changes to the order book for a certain instrument.
57
+ # @param instrument_name [String] The instrument name
58
+ # @param options [Hash]
59
+ # @option options [String] :instrument_name (BTC-PERPETUAL) Instrument to return open orders for
60
+ # @option options [Integer] :group (5) Group prices (by rounding): none, 5, 10
61
+ # @option options [Integer] :depth (10) the depth of the order book
62
+ # @option options [String] :interval (raw) Frequency of notifications: raw, 100ms
123
63
  # @return [Hashie::Mash] the order book
124
64
  # @yield [Hashie::Mash] the order book
125
- # @see https://docs.deribit.com/rpc-endpoints.html#getorderbook
126
- def orderbook(instrument, depth: 10, &blk)
127
- raise ArgumentError, 'instrument param is required' unless instrument
65
+ # @see https://docs.deribit.com/#book-instrument_name-group-depth-interval
66
+ # @see https://docs.deribit.com/#book-instrument_name-interval
67
+ def book(options = { instrument_name: 'BTC-PERPETUAL' }, &blk)
68
+ unless options[:instrument_name]
69
+ raise ArgumentError, 'instrument_name argument is required'
70
+ end
128
71
 
129
- params = { instrument: instrument, depth: depth }
130
72
  if block_given?
131
- websocket.subscribe :getorderbook, params: params, &blk
73
+ channel = Naming.book_channel options
74
+ websocket.subscribe channel, params: {}, &blk
132
75
  else
133
- http.get :getorderbook, params: params
76
+ http.get '/public/get_order_book', options
134
77
  end
135
78
  end
136
79
 
137
- # Retrieve the latest trades that have occurred for a specific instrument.
138
- # @param instrument [String] Either the name of the instrument, or "all" for all active instruments, "futures" for all active futures, or "options" for all active options.
80
+ # Retrieve the latest trades that have occurred for instruments in a specific currency symbol/for a specific instrument and optionally within given time range.
139
81
  # @!macro deribit.filters
140
- # @param filters [Hash] extra filters to apply
141
- # @option filters [Integer] :count (10) The number of trades returned (clamped to max 1000)
142
- # @option filters [Integer] :startId The ID of the first trade to be returned
143
- # @option filters [Integer] :endId The ID of the last trade to be returned
144
- # @option filters [Integer] :startSeq The trade sequence of the first trade to be returned
145
- # @option filters [Integer] :endSeq The trade sequence of the last trade to be returned
146
- # @option filters [Integer] :startTimestamp The timestamp (in ms) of the first trade to be returned
147
- # @option filters [Integer] :endTimestamp The timestamp (in ms) of the last trade to be returned
148
- # @option filters [Boolean] :includeOld (false) to get archived trades for expired instruments when true (added from performance considerations)
82
+ # @param filters [Hash] filters to apply
83
+ # @option filters [String] :instrument_name (BTC-PERPETUAL) Instrument name
84
+ # @option filters [String] :currency (BTC, ETH) The currency symbol
85
+ # @option filters [String] :kind (future, option) Instrument kind, if not provided instruments of all kinds are considered
86
+ # @option filters [Integer] :count (10) Number of requested items
87
+ # @option filters [Integer] :start_id The ID of the first trade to be returned
88
+ # @option filters [Integer] :end_id The ID of the last trade to be returned
89
+ # @option filters [Integer] :start_seq The trade sequence of the first trade to be returned
90
+ # @option filters [Integer] :end_seq The trade sequence of the last trade to be returned
91
+ # @option filters [Integer] :start_timestamp The timestamp (in ms) of the first trade to be returned
92
+ # @option filters [Integer] :end_timestamp The timestamp (in ms) of the last trade to be returned
93
+ # @option filters [Boolean] :include_old (false) Include trades older than a few recent days
94
+ # @option filters [Boolean] :sorting (none) Direction of results sorting
95
+ # @option filters [String] :interval (raw) Frequency of notifications.
149
96
  # @return [Array] the list of trades
150
97
  # @yield [Hashie::Mash] new trade
151
- # @see https://docs.deribit.com/rpc-endpoints.html#getlasttrades
152
- def trades(instrument = :all, filters = {}, &blk)
153
- raise ArgumentError, 'instrument param is required' unless instrument
154
-
155
- params = filters.merge(instrument: instrument)
156
- if block_given?
157
- websocket.subscribe :getlasttrades, params: params, &blk
158
- else
159
- http.get :getlasttrades, params: params
98
+ # @see https://docs.deribit.com/#public-get_last_trades_by_currency
99
+ # @see https://docs.deribit.com/#public-get_last_trades_by_currency_and_time
100
+ # @see https://docs.deribit.com/#public-get_last_trades_by_instrument
101
+ # @see https://docs.deribit.com/#public-get_last_trades_by_instrument_and_time
102
+ # @see https://docs.deribit.com/#private-get_user_trades_by_currency
103
+ # @see https://docs.deribit.com/#private-get_user_trades_by_currency_and_time
104
+ # @see https://docs.deribit.com/#private-get_user_trades_by_instrument
105
+ # @see https://docs.deribit.com/#private-get_user_trades_by_instrument_and_time
106
+ # @see https://docs.deribit.com/#trades-instrument_name-interval
107
+ # @see https://docs.deribit.com/#trades-kind-currency-interval
108
+ # @see https://docs.deribit.com/#user-trades-instrument_name-interval
109
+ # @see https://docs.deribit.com/#user-trades-kind-currency-interval
110
+ def trades(filters, &blk)
111
+ instrument_name = filters[:instrument_name]
112
+ currency = filters[:currency]
113
+ unless instrument_name || currency
114
+ raise ArgumentError, 'either :instrument_name or :currency args is required'
160
115
  end
161
- end
162
116
 
163
- # Retrieves the summary information such as open interest, 24h volume, etc. for a specific instrument.
164
- # @param instrument [String] Either the name of the instrument, or 'all' for all active instruments, 'futures' for all active futures, or 'options' for all active options.
165
- # @return [Array, Hashie::Mash] the summary as array or hash based on instrument param
166
- # @yield [Hashie::Mash] the summary
167
- # @see https://docs.deribit.com/rpc-endpoints.html#getsummary
168
- def summary(instrument = :all, &blk)
169
- raise ArgumentError, 'instrument argument is required' unless instrument
170
-
171
- params = { instrument: instrument }
172
- if block_given?
173
- websocket.subscribe :getsummary, params: params, &blk
174
- else
175
- http.get :getsummary, params: params
176
- end
177
- end
178
-
179
- # Retrieves aggregated 24h trade volumes for different instrument types.
180
- # @return [Hashie::Mash] the statistics
181
- # @yield [Hashie::Mash] the statistics
182
- # @see https://docs.deribit.com/rpc-endpoints.html#stats
183
- def stats(&blk)
184
117
  if block_given?
185
- websocket.subscribe :stats, &blk
118
+ channel = Naming.trades_channel filters
119
+ websocket.subscribe channel, params: {}, &blk
186
120
  else
187
- http.get :stats
121
+ uri = Naming.trades_uri filters
122
+ response = http.get uri, filters
123
+ response.trades
188
124
  end
189
125
  end
190
126
 
191
127
  # Retrieves announcements from last 30 days.
192
128
  # @return [Array] the list of announcements
193
129
  # @yield [Hashie::Mash] the announcement
194
- # @see https://docs.deribit.com/rpc-endpoints.html#getannouncements
130
+ # @see https://docs.deribit.com/#public-get_announcements
131
+ # @see https://docs.deribit.com/#announcements
195
132
  def announcements(&blk)
196
133
  if block_given?
197
- websocket.subscribe :getannouncements, &blk
134
+ websocket.subscribe 'announcements', &blk
198
135
  else
199
- http.get :getannouncements
136
+ http.get '/public/get_announcements'
200
137
  end
201
138
  end
202
139
 
203
140
  # Retrieves settlement, delivery and bankruptcy events that have occurred.
204
141
  # @param filters [Hash] the filters
205
- # @option filters [String] :instrument The instrument name, or "all" to retrieve settlements for all instruments
206
- # @option filters [Integer] :count (10) The number of entries to be returned. This is clamped to max 1000
207
- # @option filters [String] :type The type of settlements to return. Possible values "settlement", "delivery", "bankruptcy"
208
- # @option filters [Integer] :startTstamp The latest timestamp to return result for
209
- # @option filters [String] :continuation Continuation token for pagination. Each response contains a token to be used for continuation
142
+ # @option filters [String] :instrument_name The instrument name,
143
+ # @option filters [String] :currency The currency of settlements
144
+ # @option filters [String] :type settlement type: settlement delivery bankruptcy
145
+ # @option filters [Integer] :count (20) Number of requested items, default
146
+ # @option filters [String] :continuation Continuation token for pagination
210
147
  # @return [Hashie::Mash] the settlements
211
148
  # @yield [Hashie::Mash] the settlements
212
- # @see https://docs.deribit.com/rpc-endpoints.html#getlastsettlements
213
- def settlements(filters = {}, &blk)
149
+ # @see https://docs.deribit.com/#public-get_last_settlements_by_instrument
150
+ # @see https://docs.deribit.com/#public-get_last_settlements_by_currency
151
+ def settlements(filters = { instrument_name: 'BTC-PERPETUAL' })
152
+ instrument_name = filters[:instrument_name]
153
+ currency = filters[:currency]
154
+ unless instrument_name || currency
155
+ raise ArgumentError, 'either :instrument_name or :currency arg is required'
156
+ end
157
+
214
158
  if block_given?
215
- websocket.subscribe :getlastsettlements, params: filters, &blk
159
+ raise Deribit::NotImplementedError, 'not implemented'
216
160
  else
217
- http.get :getlastsettlements, params: filters
161
+ http.get '/public/get_last_settlements_by_instrument', filters
218
162
  end
219
163
  end
220
164
 
221
165
  # Retrieves user account summary.
166
+ # @param currency [String] Currency summary
222
167
  # @param ext [Boolean] Requests additional fields
223
168
  # @return [Hashie::Mash] the account details
224
169
  # @yield [Hashie::Mash] the account details
225
170
  # @see https://docs.deribit.com/rpc-endpoints.html#account
226
- def account(ext: false, &blk)
171
+ def account(currency: 'BTC', ext: false)
227
172
  if block_given?
228
- websocket.subscribe :account, params: { auth: true }, &blk
173
+ raise Deribit::NotImplementedError, 'not implemented'
229
174
  else
230
- http.get :account, auth: true
175
+ http.get '/private/get_account_summary', currency: currency
231
176
  end
232
177
  end
233
178
 
234
179
  # Places a buy order for an instrument.
235
- # @param instrument [String] Name of the instrument to buy
236
- # @param quantity [Integer] The number of contracts to buy
180
+ # @param instrument_name [String] Name of the instrument to buy
181
+ # @param amount [Integer] The number of contracts to buy
237
182
  # @!macro deribit.options
238
183
  # @param options [Hash] more options for the order
239
184
  # @option options [String] :type (limit) The order type, possible types: "limit", "stop_limit", "market", "stop_market"
240
- # @option options [Float] :price The order price (Only valid for limit and stop_limit orders)
241
185
  # @option options [String] :label user defined label for the order (maximum 32 characters)
186
+ # @option options [Float] :price The order price (Only valid for limit and stop_limit orders)
242
187
  # @option options [String] :time_in_force (good_til_cancelled) Specifies how long the order remains in effect, possible values "good_til_cancelled", "fill_or_kill", "immediate_or_cancel"
243
188
  # @option options [Integer] :max_show Maximum quantity within an order to be shown to other customers, 0 for invisible order.
244
189
  # @option options [String] :post_only (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.
245
- # @option options [Float] :stopPx Stop price required for stop limit orders (Only valid for stop orders)
246
- # @option options [String] :execInst (index_price) Defines trigger type, required for "stop_limit" order type, possible values "index_price", "mark_price" (Only valid for stop orders)
247
- # @option options [String] :adv Advanced option order type, can be "implv", "usd". (Only valid for options)
190
+ # @option options [String] :reject_post_only (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.
191
+ # @option options [String] :reduce_only If true, the order is considered reduce-only which is intended to only reduce a current position
192
+
193
+ # @option options [Float] :stop_price price required for stop limit orders (Only valid for stop orders)
194
+ # @option options [String] :trigger Defines trigger type: index_price mark_price last_price, required for "stop_limit" order type
195
+ # @option options [String] :advanced Advanced option order type, can be "implv", "usd". (Only valid for options)
248
196
  # @return [Hashie::Mash] the details of new order
249
- # @yield [Hashie::Mash] the details of new order
250
- # @see https://docs.deribit.com/rpc-endpoints.html#buy
251
- def buy(instrument, quantity, options = {}, &blk)
252
- params = { instrument: instrument, quantity: quantity, price: options[:price] }
253
- if block_given?
254
- websocket.subscribe :buy, params: params.merge(auth: true), &blk
255
- else
256
- http.post :buy, params
257
- end
197
+ # @see https://docs.deribit.com/#private-buy
198
+ def buy(instrument_name, amount, options = {})
199
+ params = options.merge instrument_name: instrument_name, amount: amount
200
+ http.get 'private/buy', params
258
201
  end
259
202
 
260
203
  # Places a sell order for an instrument.
261
- # @param instrument [String] Name of the instrument to sell
262
- # @param quantity [Integer] The number of contracts to sell
204
+ # @param instrument_name [String] Name of the instrument to sell
205
+ # @param amount [Integer] The number of contracts to buy
263
206
  # @!macro deribit.options
264
207
  # @return [Hashie::Mash] the details of new order
265
- # @yield [Hashie::Mash] the details of new order
266
- # @see https://docs.deribit.com/rpc-endpoints.html#sell
267
- def sell(instrument, quantity, options = {}, &blk)
268
- params = options.merge instrument: instrument, quantity: quantity, auth: true
269
- if block_given?
270
- websocket.subscribe :sell, params: params, &blk
271
- else
272
- http.post :sell, params
273
- end
208
+ # @see https://docs.deribit.com/#private-sell
209
+ def sell(instrument_name, amount, options = {})
210
+ params = options.merge instrument_name: instrument_name, amount: amount
211
+ http.get '/private/sell', params
212
+ end
213
+
214
+ # Close a position
215
+ # @param instrument_name [String] Name of the instrument to sell
216
+ # @param type [String]
217
+ # @param options [Hash] the options
218
+ # @option options [String] :type The order type: limit or market
219
+ # @option options [String] :price Price for limit close
220
+ # @return [Hashie::Mash] the details of closed position
221
+ # @see https://docs.deribit.com/#private-sell
222
+ def close(instrument_name, options = { type: :market })
223
+ params = options.merge instrument_name: instrument_name, type: options[:type]
224
+ http.get '/private/close_position', params
274
225
  end
275
226
 
276
227
  # Changes price and/or quantity of the own order.
277
228
  # @param order_id [String] ID of the order to edit
278
- # @param quantity [Integer] The new order quantity
229
+ # @param amount [Integer] The new order quantity
279
230
  # @param price [Float] The new order price
280
231
  # @param options [Hash] extra options
281
232
  # @option options [Boolean] :post_only 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).
282
- # @option options [String] :adv The new advanced order type (only valid for option orders)
283
- # @option options [Float] :stopPx The new stop price (only valid for stop limit orders)
233
+ # @option options [Boolean] :reduce_only If true, the order is considered reduce-only which is intended to only reduce a current position
234
+ # @option options [Boolean] :reject_post_only 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.
235
+ # @option options [String] :advanced 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)
236
+ # @option options [Float] :stop_price Stop price, required for stop limit orders (Only for stop orders)
284
237
  # @return [Hashie::Mash] the edited order
285
- # @yield [Hashie::Mash] the edited order
286
- # @see https://docs.deribit.com/rpc-endpoints.html#edit
287
- def edit(order_id, quantity, price, options = {}, &blk)
288
- params = options.merge orderId: order_id, quantity: quantity, price: price, auth: true
289
- if block_given?
290
- websocket.subscribe :edit, params: params, &blk
291
- else
292
- http.post :edit, params
293
- end
238
+ # @see https://docs.deribit.com/#private-edit
239
+ def edit(order_id, amount, price, options = {})
240
+ params = options.merge order_id: order_id, amount: amount, price: price
241
+ http.get '/private/edit', params
294
242
  end
295
243
 
296
244
  # Cancels an order, specified by order id.
297
245
  # @param order_id [String] The order id of the order to be cancelled
298
246
  # @return [Hashie::Mash] details of the cancelled order
299
- # @yield [Hashie::Mash] details of the cancelled order
300
- # @see https://docs.deribit.com/rpc-endpoints.html#cancel
301
- def cancel(order_id, &blk)
302
- params = { orderId: order_id, auth: true }
303
- if block_given?
304
- websocket.subscribe :cancel, params: params, &blk
305
- else
306
- http.post :cancel, params
307
- end
247
+ # @see https://docs.deribit.com/#private-cancel
248
+ def cancel(order_id)
249
+ http.get '/private/cancel', order_id: order_id
308
250
  end
309
251
 
310
252
  # Cancels all orders, optionally filtered by instrument or instrument type.
311
- # @param type [all futures options] Which type of orders to cancel. Valid values are "all", "futures", "options"
312
253
  # @param options [Hash] extra options
313
- # @option options [String] :instrument The name of the instrument for which to cancel all orders
254
+ # @option options [String] :instrument_name The name of the instrument for which to cancel all orders
255
+ # @option options [String] :currency The currency symbol
256
+ # @option options [String] :type Which type of orders to cancel. Valid values are "all", "futures", "options"
257
+ # @option options [String] :kind Instrument kind, if not provided instruments of all kinds are considered
314
258
  # @return [Boolean] success or not
315
- # @yield [Boolean] success or not
316
- # @see https://docs.deribit.com/rpc-endpoints.html#cancelall
317
- def cancelall(type = :all, options = {}, &blk)
318
- params = options.merge type: type, auth: true
259
+ # @see https://docs.deribit.com/#private-cancel_all
260
+ # @see https://docs.deribit.com/#private-cancel_all_by_currency
261
+ # @see https://docs.deribit.com/#private-cancel_all_by_instrument
262
+ def cancel_all(options = {})
263
+ uri = Naming.cancel_uri options
264
+ http.get uri, options
265
+ end
266
+
267
+ # Best bid/ask price and size.
268
+ # @param options [Hash]
269
+ # @option options [String] :instrument_name (BTC-PERPETUAL) Instrument to return open orders for
270
+ # @see https://docs.deribit.com/?shell#quote-instrument_name
271
+ def quote(options = { instrument_name: 'BTC-PERPETUAL' }, &blk)
272
+ unless block_given?
273
+ raise 'block is missing, HTTP-RPC not supported for this endpoint'
274
+ end
275
+
276
+ channel = Naming.channel_for_instrument 'quote', options
277
+ websocket.subscribe channel, params: options, &blk
278
+ end
279
+
280
+ # Key information about the instrument
281
+ # @param options [Hash]
282
+ # @option options [String] :instrument_name (BTC-PERPETUAL) Instrument to return open orders for
283
+ # @option options [String] :interval (raw) Frequency of notifications: raw, 100ms
284
+ # @see https://docs.deribit.com/?shell#ticker-instrument_name-interval
285
+ def ticker(options = { instrument_name: 'BTC-PERPETUAL' }, &blk)
319
286
  if block_given?
320
- websocket.subscribe :cancelall, params: params, &blk
287
+ channel = Naming.channel 'ticker', options
288
+ websocket.subscribe channel, params: options, &blk
321
289
  else
322
- http.post :cancelall, params
290
+ http.get '/public/ticker', options
323
291
  end
324
292
  end
325
293
 
326
294
  # Retrieves open orders.
327
295
  # @param options [Hash]
328
- # @option options [String] :instrument Instrument to return open orders for
329
- # @option options [string] ;orderId order ID
330
- # @option options [String] :type Order types to return. Valid values include "limit", "stop_limit", "any"
296
+ # @option options [String] :instrument_name (BTC-PERPETUAL) Instrument to return open orders for
297
+ # @option options [string] :kind (any) Instrument kind, future, option or any
298
+ # @option options [String] :currency (any) The currency symbol, BTC, ETH, any
299
+ # @option options [String] :interval (raw) Frequency of notifications: raw, 100ms
331
300
  # @return [Array] the list of open orders
332
301
  # @yield [Hashie::Mash] the order
333
302
  # @see https://docs.deribit.com/rpc-endpoints.html#getopenorders
334
- def orders(options = {}, &blk)
303
+ def orders(options = { instrument_name: 'BTC-PERPETUAL' }, &blk)
335
304
  if block_given?
336
- websocket.subscribe :getopenorders, params: options.merge(auth: true), &blk
305
+ channel = Naming.channel 'user.orders', options
306
+ websocket.subscribe channel, params: options, &blk
337
307
  else
338
- http.get :getopenorders, auth: true, params: options
308
+ http.get '/private/get_open_orders_by_instrument', options
339
309
  end
340
310
  end
341
311
 
342
312
  # Retrieves current positions.
313
+ # @param options [Hash]
314
+ # @option options [String] :currency (any) The currency symbol, BTC, ETH
315
+ # @option options [string] :kind (any) Instrument kind, future, option
343
316
  # @return [Array] the list of positions
344
- # @yield [Hashie::Mash] the position
345
317
  # @see https://docs.deribit.com/rpc-endpoints.html#positions
346
- def positions(&blk)
347
- params = { auth: true }
348
- if block_given?
349
- websocket.subscribe :positions, params: params, &blk
350
- else
351
- http.get :positions, params
352
- end
353
- end
354
-
355
- # Retrieves history of orders that have been partially or fully filled.
356
- # @param options [Hash]
357
- # @option options [String] :instrument Instrument to return open orders for
358
- # @option options [String] :count The number of items to be returned.
359
- # @option options [string] :offset The offset for pagination
360
- # @return [Array] the list of history orders
361
- # @yield [Hashie::Mash] the order
362
- # @see https://docs.deribit.com/rpc-endpoints.html#orderhistory
363
- def orders_history(options = {}, &blk)
364
- if block_given?
365
- websocket.subscribe :orderhistory, params: options.merge(auth: true), &blk
366
- else
367
- http.get :orderhistory, auth: true, params: options
368
- end
318
+ def positions(options = { currency: 'BTC' })
319
+ http.get '/private/get_positions', options
369
320
  end
370
321
 
371
322
  # Retrieve order details state by order id.
372
323
  # @param order_id [String] the ID of the order to be retrieved
373
324
  # @return [Hashie::Mash] the details of the order
374
325
  # @yield [Hashie::Mash] the details of the order
375
- # @see https://docs.deribit.com/rpc-endpoints.html#orderstate
376
- def order(order_id, &blk)
377
- params = { orderId: order_id, auth: true }
378
- if block_given?
379
- websocket.subscribe :orderstate, params: params, &blk
380
- else
381
- http.get :orderstate, auth: true, params: params
382
- end
383
- end
384
-
385
- # Retrieve the trade history of the account
386
- # @param instrument [String] Either the name of the instrument, or "all" for instruments, "futures" for all futures, or "options" for all options.
387
- # @!macro deribit.filters
388
- # @return [Array] the list of trades
389
- # @yield [Hashie::Mash] the trade
390
- # @see https://docs.deribit.com/rpc-endpoints.html?q=#tradehistory
391
- def trades_history(instrument = :all, filters = {}, &blk)
392
- params = filters.merge(instrument: instrument, auth: true)
393
- if block_given?
394
- websocket.subscribe :tradehistory, params: params, &blk
395
- else
396
- http.get :tradehistory, auth: true, params: params
397
- end
398
- end
399
-
400
- # Retrieves announcements that have not been marked read by the current user.
401
- # @return [Array] the list of new announcements
402
- # @yield [Hashie::Mash] the announcement
403
- def new_announcements(&blk)
404
- if block_given?
405
- websocket.subscribe :newannouncements, params: { auth: true }, &blk
406
- else
407
- http.get :newannouncements, auth: true
408
- end
409
- end
410
-
411
- # Logs out the websocket connection.
412
- # @yield [Boolean] success or not
413
- # @see https://docs.deribit.com/rpc-endpoints.html#logout
414
- def logout(&blk)
415
- raise 'This API endpoint cannot be used over HTTP.' unless block_given?
416
-
417
- websocket.subscribe :logout, params: {}, &blk
418
- end
419
-
420
- # Enables or disables "COD" (cancel on disconnect) for the current connection.
421
- # @param state [String] Whether COD is to be enabled for this connection. "enabled" or "disabled"
422
- # @yield [Boolean] success or not
423
- # @see https://docs.deribit.com/rpc-endpoints.html#cancelondisconnect
424
- def cancelondisconnect(state, &blk)
425
- raise 'This API endpoint cannot be used over HTTP.' unless block_given?
426
-
427
- websocket.subscribe :cancelondisconnect, params: { state: state, auth: true }, &blk
428
- end
429
-
430
- # Retrieves the language to be used for emails.
431
- # @return [String] the language name (e.g. "en", "ko", "zh")
432
- # @yield [String] the language name (e.g. "en", "ko", "zh")
433
- def getemaillang(&blk)
434
- if block_given?
435
- websocket.subscribe :getemaillang, params: { auth: true }, &blk
436
- else
437
- http.get :getemaillang, auth: true
438
- end
439
- end
440
-
441
- # Changes the language to be used for emails.
442
- # @param lang [String] the abbreviated language name. Valid values include "en", "ko", "zh"
443
- # @return [Boolean] success or not
444
- # @yield [Boolean] success or not
445
- def setemaillang(lang, &blk)
446
- if block_given?
447
- websocket.subscribe :setemaillang, params: { lang: lang, auth: true }, &blk
448
- else
449
- http.post :setemaillang, lang: lang
450
- end
451
- end
452
-
453
- # Marks an announcement as read, so it will not be shown in newannouncements
454
- # @param announcement_id [String] the ID of the announcement
455
- # @return [String] ok
456
- # @yield [String] ok
457
- def setannouncementasread(announcement_id, &blk)
458
- if block_given?
459
- websocket.subscribe :setannouncementasread, params: { announcementid: announcement_id, auth: true }, &blk
460
- else
461
- http.post :setannouncementasread, announcementid: announcement_id
462
- end
463
- end
464
-
465
- # Retrieves settlement, delivery and bankruptcy events that have affected your account.
466
- # @param filters [Hash] the filters
467
- # @option filters [String] :instrument The instrument name, or "all" to retrieve settlements for all instruments
468
- # @option filters [Integer] :count (10) The number of entries to be returned. This is clamped to max 1000
469
- # @option filters [String] :type The type of settlements to return. Possible values "settlement", "delivery", "bankruptcy"
470
- # @option filters [Integer] :startTstamp The latest timestamp to return result for
471
- # @option filters [String] :continuation Continuation token for pagination. Each response contains a token to be used for continuation
472
- # @return [Hashie::Mash] the settlements
473
- # @yield [Hashie::Mash] the settlement
474
- # @see https://docs.deribit.com/rpc-endpoints.html#settlementhistory
475
- def settlements_history(filters = {}, &blk)
476
- if block_given?
477
- websocket.subscribe :settlementhistory, params: filters.merge(auth: true), &blk
478
- else
479
- http.get :settlementhistory, auth: true, params: filters
480
- end
481
- end
326
+ # see https://docs.deribit.com/rpc-endpoints.html#orderstate
327
+ # def order(order_id, &blk)
328
+ # params = { orderId: order_id, auth: true }
329
+ # if block_given?
330
+ # websocket.subscribe :orderstate, params: params, &blk
331
+ # else
332
+ # http.get :orderstate, auth: true, params: params
333
+ # end
334
+ # end
482
335
  end
483
336
  end