bitmex-api 0.0.1 → 0.0.2
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/.gitignore +1 -0
- data/CHANGELOG.md +8 -1
- data/Gemfile.lock +3 -1
- data/README.md +46 -5
- data/TODOs.org +16 -0
- data/bin/whales-watching.rb +7 -30
- data/bitmex.gemspec +1 -0
- data/lib/bitmex.rb +10 -1
- data/lib/bitmex/client.rb +142 -28
- data/lib/bitmex/mash.rb +5 -0
- data/lib/bitmex/user.rb +166 -0
- data/lib/bitmex/version.rb +1 -1
- metadata +19 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 06e316d8a60be97f37a23ff0f9f7055d02fd1cd743a12f84c4982feff80ea476
|
|
4
|
+
data.tar.gz: 4868b686189f662a8b47e1305ac6c6f854922984256297b68531082571cb7afd
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8ad1b9fbc349b5c8a80deb584c2bf61a5a62fbe744d8d2a4ff43b2b730f4b56aabcc92aa0175df962b785e257cecedb64da3b2eefc5a7f9f253795d670b2147e
|
|
7
|
+
data.tar.gz: 307c8ffd8cd00b41bd13c79e20533e1a64950fb7e8c229e917e1d161d8b08a63d8e470290cddeb13815768b111b2d712851b03f8555f34a583efdd9a1939ef55
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
|
@@ -5,6 +5,13 @@
|
|
|
5
5
|
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
|
|
6
6
|
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
|
|
7
7
|
|
|
8
|
+
## [0.0.2] - 2019-01-22
|
|
9
|
+
### Added
|
|
10
|
+
- Initial Websocket API implementation
|
|
11
|
+
- Access private REST API
|
|
12
|
+
### Changed
|
|
13
|
+
- Account operations
|
|
14
|
+
|
|
8
15
|
## [0.0.1] - 2019-01-03
|
|
9
16
|
### Added
|
|
10
|
-
-
|
|
17
|
+
- Basic public REST API support
|
data/Gemfile.lock
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
PATH
|
|
2
2
|
remote: .
|
|
3
3
|
specs:
|
|
4
|
-
bitmex-api (0.0.
|
|
4
|
+
bitmex-api (0.0.2)
|
|
5
5
|
eventmachine
|
|
6
6
|
faye-websocket
|
|
7
7
|
hashie
|
|
@@ -14,6 +14,7 @@ GEM
|
|
|
14
14
|
coderay (1.1.2)
|
|
15
15
|
diff-lcs (1.3)
|
|
16
16
|
docile (1.3.1)
|
|
17
|
+
dotenv (2.6.0)
|
|
17
18
|
eventmachine (1.2.7)
|
|
18
19
|
faye-websocket (0.10.7)
|
|
19
20
|
eventmachine (>= 0.12.0)
|
|
@@ -61,6 +62,7 @@ DEPENDENCIES
|
|
|
61
62
|
bitmex-api!
|
|
62
63
|
bump
|
|
63
64
|
bundler
|
|
65
|
+
dotenv
|
|
64
66
|
pry
|
|
65
67
|
rake
|
|
66
68
|
rspec
|
data/README.md
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
[](https://codeclimate.com/github/icostan/bitmex-api-ruby/test_coverage)
|
|
6
6
|
[](https://badge.fury.io/rb/bitmex-api)
|
|
7
7
|
|
|
8
|
-
Ruby library for BitMEX
|
|
8
|
+
Idiomatic Ruby library for [BitMEX API](https://www.bitmex.com/app/apiOverview).
|
|
9
9
|
|
|
10
10
|
## Installation
|
|
11
11
|
|
|
@@ -25,20 +25,61 @@ Or install it yourself as:
|
|
|
25
25
|
|
|
26
26
|
## Usage
|
|
27
27
|
|
|
28
|
+
### Bitmex client
|
|
29
|
+
|
|
28
30
|
```ruby
|
|
29
|
-
require 'bitmex'
|
|
31
|
+
require 'bitmex-api'
|
|
30
32
|
|
|
31
33
|
client = Bitmex::Client.new
|
|
32
34
|
|
|
33
|
-
#
|
|
34
|
-
|
|
35
|
+
# or add key and secret args if you want to access private API
|
|
36
|
+
client = Bitmex::Client.new api_key: 'KEY', api_secret: 'SECRET'
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
### Trades
|
|
40
|
+
|
|
41
|
+
#### Using REST API
|
|
42
|
+
|
|
43
|
+
Load first 15 trades after Jan 1st for XBTUSD.
|
|
44
|
+
|
|
45
|
+
```ruby
|
|
46
|
+
trades = client.trade symbol: 'XBTUSD', count: 15, startTime: '2019-01-01'
|
|
35
47
|
trades.size
|
|
36
48
|
trades.first
|
|
37
49
|
```
|
|
38
50
|
|
|
51
|
+
#### Using Websocket API
|
|
52
|
+
|
|
53
|
+
Listen for new trades and print the ones greater than 10 XBT.
|
|
54
|
+
|
|
55
|
+
```ruby
|
|
56
|
+
client.listen trade: 'XBTUSD' do |trade|
|
|
57
|
+
puts trade if trade.homeNotional > 10
|
|
58
|
+
|
|
59
|
+
# when done call client.stop
|
|
60
|
+
# client.stop
|
|
61
|
+
end
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### Account operations
|
|
65
|
+
|
|
66
|
+
Fetch user's preferences, wallet, history, events and much more.
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
|
|
70
|
+
user = client.user
|
|
71
|
+
puts user.firstname
|
|
72
|
+
|
|
73
|
+
wallet = client.user.wallet
|
|
74
|
+
puts wallet.amount
|
|
75
|
+
|
|
76
|
+
events = client.user.events
|
|
77
|
+
puts events.last.type
|
|
78
|
+
```
|
|
79
|
+
|
|
39
80
|
## Development
|
|
40
81
|
|
|
41
|
-
After checking out the repo, run `
|
|
82
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `bundle exec rake` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
42
83
|
|
|
43
84
|
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
|
44
85
|
|
data/TODOs.org
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
* Tasks :BITMEX:
|
|
2
|
+
** DONE initial gem release
|
|
3
|
+
SCHEDULED: <2019-01-03 Thu> CLOSED: <2019-01-03 Thu>
|
|
4
|
+
** DONE websocket API
|
|
5
|
+
CLOSED: [2019-01-14] SCHEDULED: <2019-01-14 Fri>
|
|
6
|
+
** DONE auth for REST-API endpoints
|
|
7
|
+
CLOSED: [2019-01-16 Wed] SCHEDULED: <2019-01-15 Tue> DEADLINE: <2019-01-16 Wed>
|
|
8
|
+
** TODO auth for private subscription topics
|
|
9
|
+
DEADLINE: <2019-01-25 Fri>
|
|
10
|
+
** move from rspec to minitest
|
|
11
|
+
** configure autorun
|
|
12
|
+
** TODO post/put rest-api endpoints
|
|
13
|
+
** hearbeat, ping/pong
|
|
14
|
+
** DONE implement user actions get/put/post
|
|
15
|
+
CLOSED: [2019-01-22 Tue] SCHEDULED: <2019-01-17 Thu>
|
|
16
|
+
** order actions
|
data/bin/whales-watching.rb
CHANGED
|
@@ -1,36 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env ruby
|
|
2
2
|
|
|
3
|
-
require '
|
|
4
|
-
require '
|
|
5
|
-
require 'eventmachine'
|
|
3
|
+
require 'bundler/setup'
|
|
4
|
+
require 'bitmex'
|
|
6
5
|
|
|
7
|
-
product = (ARGV.first || '
|
|
6
|
+
product = (ARGV.first || 'XBTUSD')
|
|
8
7
|
limit = (ARGV[1] || 10).to_i
|
|
9
8
|
puts "==> Filter trades > #{limit} #{product}"
|
|
10
9
|
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
p [:open]
|
|
16
|
-
ws.send "{\"op\": \"subscribe\", \"args\": [\"trade:#{product}\"]}"
|
|
17
|
-
end
|
|
18
|
-
|
|
19
|
-
ws.on :message do |event|
|
|
20
|
-
json = JSON.parse event.data
|
|
21
|
-
data = json['data']
|
|
22
|
-
|
|
23
|
-
data && data.select{ |trade| trade['homeNotional'].to_i > limit }.each do |trade|
|
|
24
|
-
p trade
|
|
25
|
-
end
|
|
26
|
-
end
|
|
27
|
-
|
|
28
|
-
ws.on :error do |event|
|
|
29
|
-
p [:error, event.data]
|
|
30
|
-
end
|
|
31
|
-
|
|
32
|
-
ws.on :close do |event|
|
|
33
|
-
p [:close, event.code, event.reason]
|
|
34
|
-
ws = nil
|
|
35
|
-
end
|
|
36
|
-
}
|
|
10
|
+
client = Bitmex::Client.new
|
|
11
|
+
client.listen trade: product do |trade|
|
|
12
|
+
puts trade if trade.homeNotional > limit
|
|
13
|
+
end
|
data/bitmex.gemspec
CHANGED
data/lib/bitmex.rb
CHANGED
|
@@ -2,9 +2,18 @@ require 'httparty'
|
|
|
2
2
|
require 'hashie'
|
|
3
3
|
|
|
4
4
|
require 'bitmex/version'
|
|
5
|
+
require 'bitmex/mash'
|
|
6
|
+
require 'bitmex/user'
|
|
5
7
|
require 'bitmex/client'
|
|
6
8
|
|
|
7
9
|
module Bitmex
|
|
8
10
|
class Error < StandardError; end
|
|
9
|
-
|
|
11
|
+
|
|
12
|
+
def self.signature(api_secret, verb, path, expires, params)
|
|
13
|
+
params = '' if params.nil?
|
|
14
|
+
params = params.to_s unless params.is_a? String
|
|
15
|
+
|
|
16
|
+
data = verb + path + expires.to_s + params
|
|
17
|
+
OpenSSL::HMAC.hexdigest 'SHA256', api_secret, data
|
|
18
|
+
end
|
|
10
19
|
end
|
data/lib/bitmex/client.rb
CHANGED
|
@@ -1,53 +1,124 @@
|
|
|
1
|
+
require 'json'
|
|
2
|
+
require 'faye/websocket'
|
|
3
|
+
require 'eventmachine'
|
|
4
|
+
require 'logger'
|
|
5
|
+
|
|
1
6
|
module Bitmex
|
|
2
7
|
class Client
|
|
3
8
|
include HTTParty
|
|
9
|
+
# logger ::Logger.new(STDOUT), :debug, :curl
|
|
4
10
|
|
|
5
11
|
ANNOUNCEMENT_ARGS = %w(urgent).freeze
|
|
6
|
-
|
|
12
|
+
APIKEY_ARGS = %w().freeze
|
|
13
|
+
CHAT_ARGS = %w(channels connected).freeze
|
|
14
|
+
EXECUTION_ARGS = %w(tradehistory).freeze
|
|
7
15
|
FUNDING_ARGS = %w().freeze
|
|
8
16
|
GLOBALNOTIFICATION_ARGS = %w().freeze
|
|
9
|
-
INSTRUMENT_ARGS = %w(active
|
|
17
|
+
INSTRUMENT_ARGS = %w(active activeandindices activeintervals compositeindex indices).freeze
|
|
10
18
|
INSURANCE_ARGS = %w().freeze
|
|
11
19
|
LEADERBOARD_ARGS = %w().freeze
|
|
12
20
|
LIQUIDATION_ARGS = %w().freeze
|
|
13
21
|
ORDER_ARGS = %w().freeze
|
|
14
22
|
ORDERBOOK_ARGS = %w(L2).freeze
|
|
23
|
+
POSITION_ARGS = %w().freeze
|
|
15
24
|
QUOTE_ARGS = %w(bucketed).freeze
|
|
16
|
-
SCHEMA_ARGS = %w(
|
|
25
|
+
SCHEMA_ARGS = %w(websockethelp).freeze
|
|
17
26
|
SETTLEMENT_ARGS = %w().freeze
|
|
18
|
-
STATS_ARGS = %w(history
|
|
27
|
+
STATS_ARGS = %w(history historyusd).freeze
|
|
19
28
|
TRADE_ARGS = %w(bucketed).freeze
|
|
20
29
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
def initialize(testnet: false)
|
|
24
|
-
@base_url = testnet ? 'https://testnet.bitmex.com/api/v1' : 'https://www.bitmex.com/api/v1'
|
|
25
|
-
end
|
|
30
|
+
TESTNET_HOST = 'testnet.bitmex.com'.freeze
|
|
31
|
+
MAINNET_HOST = 'www.bitmex.com'.freeze
|
|
26
32
|
|
|
27
|
-
|
|
28
|
-
end
|
|
33
|
+
AUTHORIZATIONS = %w(apikey execution position globalnotification order leaderboard quote user userevent)
|
|
29
34
|
|
|
30
|
-
|
|
31
|
-
end
|
|
35
|
+
attr_reader :host, :api_key, :api_secret
|
|
32
36
|
|
|
33
|
-
def
|
|
37
|
+
def initialize(testnet: false, api_key: nil, api_secret: nil)
|
|
38
|
+
@host = testnet ? TESTNET_HOST : MAINNET_HOST
|
|
39
|
+
@api_key = api_key
|
|
40
|
+
@api_secret = api_secret
|
|
34
41
|
end
|
|
35
42
|
|
|
36
|
-
def
|
|
37
|
-
execute 'orderbook', 'L2',
|
|
43
|
+
def orderbook(symbol)
|
|
44
|
+
execute 'orderbook', 'L2', { symbol: symbol } do |response|
|
|
38
45
|
response.to_a.map do |s|
|
|
39
|
-
|
|
46
|
+
Bitmex::Mash.new s
|
|
40
47
|
end
|
|
41
48
|
end
|
|
42
49
|
end
|
|
43
50
|
|
|
44
|
-
|
|
51
|
+
#
|
|
52
|
+
# WebSocket API
|
|
53
|
+
#
|
|
54
|
+
# https://www.bitmex.com/app/wsAPI
|
|
55
|
+
#
|
|
56
|
+
def listen(options, &ablock)
|
|
57
|
+
EM.run do
|
|
58
|
+
ws = Faye::WebSocket::Client.new realtime_url
|
|
59
|
+
|
|
60
|
+
topics = options.map{ |key, value| "#{key}:#{value}"}
|
|
61
|
+
subscription = { op: :subscribe, args: topics }
|
|
62
|
+
# puts subscription
|
|
63
|
+
|
|
64
|
+
ws.on :open do |event|
|
|
65
|
+
ws.send subscription.to_json.to_s
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
ws.on :message do |event|
|
|
69
|
+
json = JSON.parse event.data
|
|
70
|
+
data = json['data']
|
|
71
|
+
|
|
72
|
+
data&.each do |payload|
|
|
73
|
+
if block_given?
|
|
74
|
+
yield Bitmex::Mash.new(payload.merge topic: json['table'])
|
|
75
|
+
else
|
|
76
|
+
p value
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
ws.on :error do |event|
|
|
82
|
+
raise [:error, event.data]
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
ws.on :close do |event|
|
|
86
|
+
# p [:close, event.reason]
|
|
87
|
+
ws = nil
|
|
88
|
+
end
|
|
89
|
+
end
|
|
45
90
|
end
|
|
46
91
|
|
|
47
92
|
def user
|
|
93
|
+
Bitmex::User.new self
|
|
48
94
|
end
|
|
49
95
|
|
|
50
|
-
|
|
96
|
+
#
|
|
97
|
+
# Stop websocket listener
|
|
98
|
+
#
|
|
99
|
+
def stop
|
|
100
|
+
EM.stop_event_loop
|
|
101
|
+
end
|
|
102
|
+
|
|
103
|
+
def get(path, params: {}, auth: false)
|
|
104
|
+
options = {}
|
|
105
|
+
options[:query] = params unless params.empty?
|
|
106
|
+
options[:headers] = headers 'GET', path, '' if auth
|
|
107
|
+
|
|
108
|
+
response = self.class.get "#{domain_url}#{path}", options
|
|
109
|
+
yield response
|
|
110
|
+
end
|
|
111
|
+
|
|
112
|
+
def put(path, params: {}, auth: true, json: true)
|
|
113
|
+
body = json ? params.to_json.to_s : URI.encode_www_form(params)
|
|
114
|
+
|
|
115
|
+
options = {}
|
|
116
|
+
options[:body] = body
|
|
117
|
+
options[:headers] = headers 'PUT', path, body, json: json if auth
|
|
118
|
+
|
|
119
|
+
response = self.class.put "#{domain_url}#{path}", options
|
|
120
|
+
puts response.body
|
|
121
|
+
yield response
|
|
51
122
|
end
|
|
52
123
|
|
|
53
124
|
private
|
|
@@ -55,31 +126,74 @@ module Bitmex
|
|
|
55
126
|
def method_missing(m, *args, &ablock)
|
|
56
127
|
name = m.to_s.gsub '_', ''
|
|
57
128
|
params = args.first || {}
|
|
58
|
-
type = params
|
|
129
|
+
type = params&.delete :type
|
|
59
130
|
types = self.class.const_get "#{name.upcase}_ARGS"
|
|
60
|
-
|
|
131
|
+
check! type, types
|
|
132
|
+
|
|
133
|
+
params[:auth] = auth_required? name
|
|
134
|
+
|
|
135
|
+
execute name, type, params do |response|
|
|
136
|
+
# p response.body
|
|
61
137
|
if response.parsed_response.is_a? Array
|
|
62
138
|
response.to_a.map do |s|
|
|
63
|
-
|
|
139
|
+
Bitmex::Mash.new s
|
|
64
140
|
end
|
|
65
141
|
else
|
|
66
|
-
|
|
142
|
+
Bitmex::Mash.new response
|
|
67
143
|
end
|
|
68
144
|
end
|
|
69
145
|
end
|
|
70
146
|
|
|
71
|
-
def
|
|
72
|
-
|
|
147
|
+
def auth_required?(action)
|
|
148
|
+
AUTHORIZATIONS.include? action
|
|
149
|
+
end
|
|
150
|
+
|
|
151
|
+
def execute(endpoint, type, params, &ablock)
|
|
152
|
+
url = "#{rest_url}/#{endpoint}/#{type}"
|
|
153
|
+
path = "/api/v1/#{endpoint}/#{type}"
|
|
154
|
+
auth = params&.delete(:auth)
|
|
155
|
+
params = nil if params&.empty?
|
|
156
|
+
|
|
157
|
+
options = { query: params }
|
|
158
|
+
options[:headers] = headers 'GET', path, '' if auth
|
|
73
159
|
|
|
74
|
-
|
|
75
|
-
response
|
|
76
|
-
fail response.message unless response.success?
|
|
160
|
+
response = self.class.get url, options
|
|
161
|
+
fail response.body unless response.success?
|
|
77
162
|
yield response
|
|
78
163
|
end
|
|
79
164
|
|
|
165
|
+
def headers(verb, path, body, json: true)
|
|
166
|
+
raise 'api_key and api_secret are required' unless api_key || api_secret
|
|
167
|
+
|
|
168
|
+
expires = Time.now.utc.to_i + 60
|
|
169
|
+
headers = {
|
|
170
|
+
'api-expires' => expires.to_s,
|
|
171
|
+
'api-key' => api_key,
|
|
172
|
+
'api-signature' => Bitmex.signature(api_secret, verb, path, expires, body)
|
|
173
|
+
}
|
|
174
|
+
if json
|
|
175
|
+
headers['Content-Type'] = 'application/json'
|
|
176
|
+
else
|
|
177
|
+
headers['Content-Type'] = 'application/x-www-form-urlencoded'
|
|
178
|
+
end
|
|
179
|
+
headers
|
|
180
|
+
end
|
|
181
|
+
|
|
80
182
|
def check!(type, types)
|
|
81
183
|
return true if type.nil? or type == ''
|
|
82
184
|
raise ArgumentError, "invalid argument #{type}, only #{types} are supported" if !types.include?(type.to_s)
|
|
83
185
|
end
|
|
186
|
+
|
|
187
|
+
def rest_url
|
|
188
|
+
"https://#{host}/api/v1"
|
|
189
|
+
end
|
|
190
|
+
|
|
191
|
+
def domain_url
|
|
192
|
+
"https://#{host}"
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
def realtime_url
|
|
196
|
+
"wss://#{host}/realtime"
|
|
197
|
+
end
|
|
84
198
|
end
|
|
85
199
|
end
|
data/lib/bitmex/mash.rb
ADDED
data/lib/bitmex/user.rb
ADDED
|
@@ -0,0 +1,166 @@
|
|
|
1
|
+
module Bitmex
|
|
2
|
+
# Account Operations
|
|
3
|
+
#
|
|
4
|
+
# All read-only operations to load user's data are implemented and work as described in docs.
|
|
5
|
+
# Multiple PUT/POST endpoints return 'Access Denied' and are not implemented. It seems that they are meant to be used internally by BitMEX only.
|
|
6
|
+
#
|
|
7
|
+
# @author Iulian Costan
|
|
8
|
+
class User
|
|
9
|
+
attr_reader :client
|
|
10
|
+
|
|
11
|
+
# @param client [Bitmex::Client] the HTTP client
|
|
12
|
+
def initialize(client)
|
|
13
|
+
@client = client
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
# Get your current affiliate/referral status.
|
|
17
|
+
# @return [Hash] the affiliate status
|
|
18
|
+
def affiliate_status
|
|
19
|
+
get 'affiliateStatus'
|
|
20
|
+
end
|
|
21
|
+
|
|
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
|
|
24
|
+
# @return [Decimal, nil] the discount or nil
|
|
25
|
+
def check_referral_code(code)
|
|
26
|
+
get 'checkReferralCode', referralCode: code do |response|
|
|
27
|
+
return nil if !response.success? && [404, 451].include?(response.code)
|
|
28
|
+
|
|
29
|
+
response.to_f
|
|
30
|
+
end
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
# Get your account's commission status
|
|
34
|
+
# @return [Hash] the commission by each product
|
|
35
|
+
def commission
|
|
36
|
+
get 'commission'
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Get a deposit address
|
|
40
|
+
# @param currency [String] currency symbol
|
|
41
|
+
# @return [String] the address
|
|
42
|
+
def deposit_address(currency = 'XBt')
|
|
43
|
+
get 'depositAddress', currency: currency do |response|
|
|
44
|
+
fail response.body unless response.success?
|
|
45
|
+
|
|
46
|
+
response.to_s
|
|
47
|
+
end
|
|
48
|
+
end
|
|
49
|
+
|
|
50
|
+
# Get the execution history by day
|
|
51
|
+
# @param symbol [String] the symbol to get the history for
|
|
52
|
+
# @param timestamp [Datetime] the datetime to filter the history for
|
|
53
|
+
# @return [Array] the history
|
|
54
|
+
def execution_history(symbol = 'XBTUSD', timestamp = Date.today)
|
|
55
|
+
get 'executionHistory', symbol: symbol, timestamp: timestamp
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
# Get your account's margin status
|
|
59
|
+
# @param currency [String] the currency to filter by
|
|
60
|
+
# @return [Hash] the margin
|
|
61
|
+
def margin(currency = 'XBt')
|
|
62
|
+
get 'margin', currency: currency
|
|
63
|
+
end
|
|
64
|
+
|
|
65
|
+
# Get the minimum withdrawal fee for a currency
|
|
66
|
+
# This is changed based on network conditions to ensure timely withdrawals. During network congestion, this may be high. The fee is returned in the same currency.
|
|
67
|
+
# @param currency [String] the currency to get the fee for
|
|
68
|
+
# @return [Hash] the fee
|
|
69
|
+
def min_withdrawal_fee(currency = 'XBt')
|
|
70
|
+
get 'minWithdrawalFee', currency: currency
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
# Get your current wallet information
|
|
74
|
+
# @return [Hash] the current wallet
|
|
75
|
+
def wallet
|
|
76
|
+
get 'wallet'
|
|
77
|
+
end
|
|
78
|
+
|
|
79
|
+
# Get a history of all of your wallet transactions (deposits, withdrawals, PNL)
|
|
80
|
+
# @return [Array] the wallet history
|
|
81
|
+
def wallet_history
|
|
82
|
+
get 'walletHistory'
|
|
83
|
+
end
|
|
84
|
+
|
|
85
|
+
# Get a summary of all of your wallet transactions (deposits, withdrawals, PNL)
|
|
86
|
+
# @return [Array] the wallet summary
|
|
87
|
+
def wallet_summary
|
|
88
|
+
get 'walletSummary'
|
|
89
|
+
end
|
|
90
|
+
|
|
91
|
+
# Get your user events
|
|
92
|
+
# @return [Array] the events
|
|
93
|
+
def events
|
|
94
|
+
data = get '', resource: 'userEvent'
|
|
95
|
+
data.userEvents
|
|
96
|
+
end
|
|
97
|
+
|
|
98
|
+
private
|
|
99
|
+
|
|
100
|
+
def method_missing(m, *args, &ablock)
|
|
101
|
+
if @data.nil?
|
|
102
|
+
get '' do |response|
|
|
103
|
+
fail response.body unless response.success?
|
|
104
|
+
|
|
105
|
+
@data = Bitmex::Mash.new response
|
|
106
|
+
end
|
|
107
|
+
end
|
|
108
|
+
@data.send m
|
|
109
|
+
end
|
|
110
|
+
|
|
111
|
+
def put(resource, params, &ablock)
|
|
112
|
+
path = user_path resource
|
|
113
|
+
client.put path, params: params, auth: true do |response|
|
|
114
|
+
if block_given?
|
|
115
|
+
yield response
|
|
116
|
+
else
|
|
117
|
+
fail response.body unless response.success?
|
|
118
|
+
|
|
119
|
+
response_to_mash response
|
|
120
|
+
end
|
|
121
|
+
end
|
|
122
|
+
end
|
|
123
|
+
|
|
124
|
+
def get(resource, params = {}, &ablock)
|
|
125
|
+
path = user_path resource, params
|
|
126
|
+
client.get path, auth: true do |response|
|
|
127
|
+
if block_given?
|
|
128
|
+
yield response
|
|
129
|
+
else
|
|
130
|
+
fail response.body unless response.success?
|
|
131
|
+
|
|
132
|
+
response_to_mash response
|
|
133
|
+
end
|
|
134
|
+
end
|
|
135
|
+
end
|
|
136
|
+
|
|
137
|
+
def user_path(action, params = {})
|
|
138
|
+
resource = params.delete(:resource) || 'user'
|
|
139
|
+
path = "/api/v1/#{resource}/#{action}"
|
|
140
|
+
# TODO: find a better way to handle multiple parameters, dig into HTTParty
|
|
141
|
+
path += '?' if params.size.positive?
|
|
142
|
+
params.each do |key, value|
|
|
143
|
+
path += "#{key}=#{value}&"
|
|
144
|
+
end
|
|
145
|
+
path
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
def response_handler(response, ablock)
|
|
149
|
+
if ablock
|
|
150
|
+
ablock.yield response
|
|
151
|
+
else
|
|
152
|
+
fail response.body unless response.success?
|
|
153
|
+
|
|
154
|
+
response_to_mash response
|
|
155
|
+
end
|
|
156
|
+
end
|
|
157
|
+
|
|
158
|
+
def response_to_mash(response)
|
|
159
|
+
if response.parsed_response.is_a? Array
|
|
160
|
+
response.to_a.map { |s| Bitmex::Mash.new s }
|
|
161
|
+
else
|
|
162
|
+
Bitmex::Mash.new response
|
|
163
|
+
end
|
|
164
|
+
end
|
|
165
|
+
end
|
|
166
|
+
end
|
data/lib/bitmex/version.rb
CHANGED
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.2
|
|
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-22 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: httparty
|
|
@@ -150,6 +150,20 @@ dependencies:
|
|
|
150
150
|
- - ">="
|
|
151
151
|
- !ruby/object:Gem::Version
|
|
152
152
|
version: '0'
|
|
153
|
+
- !ruby/object:Gem::Dependency
|
|
154
|
+
name: dotenv
|
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
|
156
|
+
requirements:
|
|
157
|
+
- - ">="
|
|
158
|
+
- !ruby/object:Gem::Version
|
|
159
|
+
version: '0'
|
|
160
|
+
type: :development
|
|
161
|
+
prerelease: false
|
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
163
|
+
requirements:
|
|
164
|
+
- - ">="
|
|
165
|
+
- !ruby/object:Gem::Version
|
|
166
|
+
version: '0'
|
|
153
167
|
description: Ruby library for BitMEX API
|
|
154
168
|
email:
|
|
155
169
|
- iulian.costan@gmail.com
|
|
@@ -167,6 +181,7 @@ files:
|
|
|
167
181
|
- Gemfile.lock
|
|
168
182
|
- README.md
|
|
169
183
|
- Rakefile
|
|
184
|
+
- TODOs.org
|
|
170
185
|
- bin/console
|
|
171
186
|
- bin/setup
|
|
172
187
|
- bin/whales-watching.rb
|
|
@@ -174,6 +189,8 @@ files:
|
|
|
174
189
|
- lib/bitmex-api.rb
|
|
175
190
|
- lib/bitmex.rb
|
|
176
191
|
- lib/bitmex/client.rb
|
|
192
|
+
- lib/bitmex/mash.rb
|
|
193
|
+
- lib/bitmex/user.rb
|
|
177
194
|
- lib/bitmex/version.rb
|
|
178
195
|
homepage: https://github.com/icostan/bitmex-api-ruby
|
|
179
196
|
licenses: []
|