bitmex-api 0.0.2 → 0.0.3
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.
- checksums.yaml +4 -4
- data/CHANGELOG.md +10 -0
- data/Gemfile.lock +36 -1
- data/README.md +104 -15
- data/TODOs.org +12 -3
- data/bin/chat.rb +11 -0
- data/bin/whales-watching.rb +2 -2
- data/bitmex.gemspec +3 -0
- data/lib/bitmex.rb +16 -1
- data/lib/bitmex/apikey.rb +45 -0
- data/lib/bitmex/base.rb +26 -0
- data/lib/bitmex/chat.rb +57 -0
- data/lib/bitmex/client.rb +186 -61
- data/lib/bitmex/instrument.rb +53 -0
- data/lib/bitmex/order.rb +78 -0
- data/lib/bitmex/position.rb +77 -0
- data/lib/bitmex/quote.rb +32 -0
- data/lib/bitmex/stats.rb +35 -0
- data/lib/bitmex/trade.rb +41 -0
- data/lib/bitmex/user.rb +37 -11
- data/lib/bitmex/version.rb +1 -1
- data/lib/bitmex/websocket.rb +67 -0
- metadata +55 -2
data/lib/bitmex/quote.rb
ADDED
@@ -0,0 +1,32 @@
|
|
1
|
+
module Bitmex
|
2
|
+
# Best Bid/Offer Snapshots & Historical Bins
|
3
|
+
# TODO: looks like all quotes related methods are forbidden
|
4
|
+
# @author Iulian Costan
|
5
|
+
class Quote < Base
|
6
|
+
# Get all quotes
|
7
|
+
# @!macro bitmex.filters
|
8
|
+
# @return [Array] the quotes
|
9
|
+
def all(filters = {})
|
10
|
+
client.get quotes_path, params: filters do |response|
|
11
|
+
response_handler response
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
# Get previous quotes in time buckets
|
16
|
+
# @param binSize ['1m','5m','1h','1d'] the interval to bucket by
|
17
|
+
# @!macro bitmex.filters
|
18
|
+
# @return [Array] the quotes by bucket
|
19
|
+
def bucketed(binSize = '1h', filters = {})
|
20
|
+
params = filters.merge binSize: binSize
|
21
|
+
client.get quotes_path(:bucketed), params: params do |response|
|
22
|
+
response_handler response
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def quotes_path(action = '')
|
29
|
+
client.base_path :quote, action
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
data/lib/bitmex/stats.rb
ADDED
@@ -0,0 +1,35 @@
|
|
1
|
+
module Bitmex
|
2
|
+
# Exchange statistics
|
3
|
+
# @author Iulian Costan
|
4
|
+
class Stats < Base
|
5
|
+
# Get exchange-wide and per-series turnover and volume statistics
|
6
|
+
# @return [Array] the statistics
|
7
|
+
def current
|
8
|
+
client.get stats_path do |response|
|
9
|
+
response_handler response
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
# Get historical exchange-wide and per-series turnover and volume statistics
|
14
|
+
# @return [Array] the history in XBT
|
15
|
+
def history
|
16
|
+
client.get stats_path(:history) do |response|
|
17
|
+
response_handler response
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# Get a summary of exchange statistics in USD
|
22
|
+
# @return [Array] the history in USD
|
23
|
+
def history_usd
|
24
|
+
client.get stats_path(:historyUSD) do |response|
|
25
|
+
response_handler response
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def stats_path(action = '')
|
32
|
+
base_path :stats, action
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
data/lib/bitmex/trade.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
module Bitmex
|
2
|
+
# Individual and bucketed trades
|
3
|
+
# @author Iulian Costan
|
4
|
+
class Trade < Base
|
5
|
+
# Get all trades
|
6
|
+
# @example Get first 10 traders starting Jan 1st for XBTUSD
|
7
|
+
# client.trades.all symbol: 'XBTUSD', startTime: '2019-01-01', count: 10
|
8
|
+
# @!macro bitmex.filters
|
9
|
+
# @return [Array] the trades
|
10
|
+
# @yield [trade] the trade
|
11
|
+
def all(filters = {}, &callback)
|
12
|
+
if block_given?
|
13
|
+
# TODO: investigate eventmachine + faye
|
14
|
+
EM.run { client.websocket.subscribe :trade, filters[:symbol], &callback }
|
15
|
+
else
|
16
|
+
client.get trade_path, params: filters do |response|
|
17
|
+
response_handler response
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
# Get previous trades in time buckets
|
23
|
+
# @example Get last hour in 2018 and first hour in 2019 in reverse order
|
24
|
+
# client.trades.bucketed '1h', symbol: 'XBTUSD', endTime: Date.new(2019, 1, 1), count: 2, reverse: true
|
25
|
+
# @param binSize ['1m','5m','1h','1d'] the interval to bucket by
|
26
|
+
# @!macro bitmex.filters
|
27
|
+
# @return [Array] the trades by bucket
|
28
|
+
def bucketed(binSize = '1h', filters = {})
|
29
|
+
params = filters.merge binSize: binSize
|
30
|
+
client.get trade_path(:bucketed), params: params do |response|
|
31
|
+
response_handler response
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def trade_path(action = '')
|
38
|
+
base_path :trade, action
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
data/lib/bitmex/user.rb
CHANGED
@@ -20,7 +20,7 @@ module Bitmex
|
|
20
20
|
end
|
21
21
|
|
22
22
|
# Check if a referral code is valid. If the code is valid, responds with the referral code's discount (e.g. 0.1 for 10%) and false otherwise
|
23
|
-
# @param referral code
|
23
|
+
# @param code [String] the referral code
|
24
24
|
# @return [Decimal, nil] the discount or nil
|
25
25
|
def check_referral_code(code)
|
26
26
|
get 'checkReferralCode', referralCode: code do |response|
|
@@ -89,18 +89,44 @@ module Bitmex
|
|
89
89
|
end
|
90
90
|
|
91
91
|
# Get your user events
|
92
|
+
# @param count [Integer] number of results to fetch
|
93
|
+
# @param startId [Double] cursor for pagination
|
92
94
|
# @return [Array] the events
|
93
|
-
def events
|
94
|
-
data = get '', resource: 'userEvent'
|
95
|
+
def events(count: 150, startId: nil)
|
96
|
+
data = get '', resource: 'userEvent', count: count, startId: startId
|
95
97
|
data.userEvents
|
96
98
|
end
|
97
99
|
|
100
|
+
# Get your alias on the leaderboard
|
101
|
+
# @return [String] the alias on the leaderboard
|
102
|
+
def nickname
|
103
|
+
data = get 'name', resource: 'leaderboard'
|
104
|
+
data.name
|
105
|
+
end
|
106
|
+
|
107
|
+
# Get all raw executions for your account
|
108
|
+
# @!macro bitmex.filters
|
109
|
+
# @param filters [Hash] the filters to apply
|
110
|
+
# @option filters [String] :symbol the instrument symbol
|
111
|
+
# @option filters [String] :filter generic table filter, send key/value pairs {https://www.bitmex.com/app/restAPI#Timestamp-Filters Timestamp Filters}
|
112
|
+
# @option filters [String] :columns array of column names to fetch; if omitted, will return all columns.
|
113
|
+
# @option filters [Double] :count (100) number of results to fetch.
|
114
|
+
# @option filters [Double] :start Starting point for results.
|
115
|
+
# @option filters [Boolean] :reverse (false) if true, will sort results newest first.
|
116
|
+
# @option filters [Datetime, String] :startTime Starting date filter for results.
|
117
|
+
# @option filters [Datetime, String] :endTime Ending date filter for results
|
118
|
+
# @return [Array] the raw transactions
|
119
|
+
def executions(filters = {})
|
120
|
+
params = filters.merge resource: :execution
|
121
|
+
get '', params
|
122
|
+
end
|
123
|
+
|
98
124
|
private
|
99
125
|
|
100
126
|
def method_missing(m, *args, &ablock)
|
101
127
|
if @data.nil?
|
102
128
|
get '' do |response|
|
103
|
-
|
129
|
+
raise response.body unless response.success?
|
104
130
|
|
105
131
|
@data = Bitmex::Mash.new response
|
106
132
|
end
|
@@ -108,26 +134,26 @@ module Bitmex
|
|
108
134
|
@data.send m
|
109
135
|
end
|
110
136
|
|
111
|
-
def put(
|
112
|
-
path = user_path
|
137
|
+
def put(action, params, &ablock)
|
138
|
+
path = user_path action
|
113
139
|
client.put path, params: params, auth: true do |response|
|
114
140
|
if block_given?
|
115
141
|
yield response
|
116
142
|
else
|
117
|
-
|
143
|
+
raise response.body unless response.success?
|
118
144
|
|
119
145
|
response_to_mash response
|
120
146
|
end
|
121
147
|
end
|
122
148
|
end
|
123
149
|
|
124
|
-
def get(
|
125
|
-
path = user_path
|
150
|
+
def get(action, params = {}, &ablock)
|
151
|
+
path = user_path action, params
|
126
152
|
client.get path, auth: true do |response|
|
127
153
|
if block_given?
|
128
154
|
yield response
|
129
155
|
else
|
130
|
-
|
156
|
+
raise response.body unless response.success?
|
131
157
|
|
132
158
|
response_to_mash response
|
133
159
|
end
|
@@ -149,7 +175,7 @@ module Bitmex
|
|
149
175
|
if ablock
|
150
176
|
ablock.yield response
|
151
177
|
else
|
152
|
-
|
178
|
+
raise response.body unless response.success?
|
153
179
|
|
154
180
|
response_to_mash response
|
155
181
|
end
|
data/lib/bitmex/version.rb
CHANGED
@@ -0,0 +1,67 @@
|
|
1
|
+
module Bitmex
|
2
|
+
# Websocket API
|
3
|
+
# https://www.bitmex.com/app/wsAPI
|
4
|
+
class Websocket
|
5
|
+
# Create new websocket instance
|
6
|
+
# @param url the URL to connect to
|
7
|
+
# @return new websocket instance
|
8
|
+
def initialize(url)
|
9
|
+
@callbacks = {}
|
10
|
+
@faye = Faye::WebSocket::Client.new url
|
11
|
+
@faye.on :open do |event|
|
12
|
+
# puts [:open, event]
|
13
|
+
end
|
14
|
+
@faye.on :error do |event|
|
15
|
+
raise [:error, event.data]
|
16
|
+
end
|
17
|
+
@faye.on :close do |event|
|
18
|
+
# puts [:close, event.reason]
|
19
|
+
@faye = nil
|
20
|
+
end
|
21
|
+
@faye.on :message do |event|
|
22
|
+
json = JSON.parse event.data
|
23
|
+
topic = json['table']
|
24
|
+
data = json['data']
|
25
|
+
|
26
|
+
callback = @callbacks[topic]
|
27
|
+
if callback
|
28
|
+
data&.each do |payload|
|
29
|
+
callback.yield Bitmex::Mash.new(payload)
|
30
|
+
end
|
31
|
+
else
|
32
|
+
puts "==> #{event.data}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
# Subscribe to a specific topic and optionally filter by symbol
|
38
|
+
# @param topic [String] topic to subscribe to e.g. 'trade'
|
39
|
+
# @param symbol [String] symbol to filter by e.g. 'XBTUSD'
|
40
|
+
def subscribe(topic, symbol = nil, &callback)
|
41
|
+
raise 'callback block is required' unless block_given?
|
42
|
+
|
43
|
+
@callbacks[topic.to_s] = callback
|
44
|
+
|
45
|
+
payload = { op: :subscribe, args: [subscription(topic, symbol)] }
|
46
|
+
@faye.send payload.to_json.to_s
|
47
|
+
end
|
48
|
+
|
49
|
+
# Unsubscribe from a specific topic and symbol
|
50
|
+
# @param topic (see #subscribe)
|
51
|
+
# @param symbol (see #subscribe)
|
52
|
+
def unsubscribe(topic, symbol = nil)
|
53
|
+
@callbacks[topic.to_s] = nil
|
54
|
+
|
55
|
+
payload = { op: :unsubscribe, args: [subscription(topic, symbol)] }
|
56
|
+
@faye.send payload.to_json.to_s
|
57
|
+
end
|
58
|
+
|
59
|
+
private
|
60
|
+
|
61
|
+
def subscription(topic, symbol)
|
62
|
+
subscription = topic.to_s
|
63
|
+
subscription += ":#{symbol}" if symbol
|
64
|
+
subscription
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: bitmex-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Iulian Costan
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2019-01-
|
11
|
+
date: 2019-01-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: httparty
|
@@ -136,6 +136,20 @@ dependencies:
|
|
136
136
|
- - ">="
|
137
137
|
- !ruby/object:Gem::Version
|
138
138
|
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: pry-doc
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - ">="
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0'
|
146
|
+
type: :development
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - ">="
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0'
|
139
153
|
- !ruby/object:Gem::Dependency
|
140
154
|
name: simplecov
|
141
155
|
requirement: !ruby/object:Gem::Requirement
|
@@ -164,6 +178,34 @@ dependencies:
|
|
164
178
|
- - ">="
|
165
179
|
- !ruby/object:Gem::Version
|
166
180
|
version: '0'
|
181
|
+
- !ruby/object:Gem::Dependency
|
182
|
+
name: rubocop
|
183
|
+
requirement: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - ">="
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: '0'
|
188
|
+
type: :development
|
189
|
+
prerelease: false
|
190
|
+
version_requirements: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - ">="
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '0'
|
195
|
+
- !ruby/object:Gem::Dependency
|
196
|
+
name: reek
|
197
|
+
requirement: !ruby/object:Gem::Requirement
|
198
|
+
requirements:
|
199
|
+
- - ">="
|
200
|
+
- !ruby/object:Gem::Version
|
201
|
+
version: '0'
|
202
|
+
type: :development
|
203
|
+
prerelease: false
|
204
|
+
version_requirements: !ruby/object:Gem::Requirement
|
205
|
+
requirements:
|
206
|
+
- - ">="
|
207
|
+
- !ruby/object:Gem::Version
|
208
|
+
version: '0'
|
167
209
|
description: Ruby library for BitMEX API
|
168
210
|
email:
|
169
211
|
- iulian.costan@gmail.com
|
@@ -182,16 +224,27 @@ files:
|
|
182
224
|
- README.md
|
183
225
|
- Rakefile
|
184
226
|
- TODOs.org
|
227
|
+
- bin/chat.rb
|
185
228
|
- bin/console
|
186
229
|
- bin/setup
|
187
230
|
- bin/whales-watching.rb
|
188
231
|
- bitmex.gemspec
|
189
232
|
- lib/bitmex-api.rb
|
190
233
|
- lib/bitmex.rb
|
234
|
+
- lib/bitmex/apikey.rb
|
235
|
+
- lib/bitmex/base.rb
|
236
|
+
- lib/bitmex/chat.rb
|
191
237
|
- lib/bitmex/client.rb
|
238
|
+
- lib/bitmex/instrument.rb
|
192
239
|
- lib/bitmex/mash.rb
|
240
|
+
- lib/bitmex/order.rb
|
241
|
+
- lib/bitmex/position.rb
|
242
|
+
- lib/bitmex/quote.rb
|
243
|
+
- lib/bitmex/stats.rb
|
244
|
+
- lib/bitmex/trade.rb
|
193
245
|
- lib/bitmex/user.rb
|
194
246
|
- lib/bitmex/version.rb
|
247
|
+
- lib/bitmex/websocket.rb
|
195
248
|
homepage: https://github.com/icostan/bitmex-api-ruby
|
196
249
|
licenses: []
|
197
250
|
metadata:
|