okex 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: c24e96c13dd676d1ece02650d07e9c32624f124ea092d17bdcf2d87d621c1236
4
- data.tar.gz: b67158ebbd5aece70467880e1fb1d8f955f362a671f78bebc7d285c17764ee1b
3
+ metadata.gz: a104fac8edf7712e91a90bdab72ccccc93cb1e20633400c70aee249b69b22202
4
+ data.tar.gz: 8003c43686c142e4619a4ff52852aa4cbb5c050bcfd161df2af5d0fbe35409fb
5
5
  SHA512:
6
- metadata.gz: f963003215c3bc2b79a8168757f82d4a52bc07e8f8e698f0e231dd296af3e0d6f5e5f9f262f923ace4ad5816d7e01bb9cf75b9c7cb101325cb0d4d678f5873ac
7
- data.tar.gz: 3dcfcf6d73f158e69f4240b267199143201c5a872f6dc312322ee8ffc91d9cee559a10e30c56a3ccc3baea214aa78924071b5b0ceb61f5ccb82425523dbd1335
6
+ metadata.gz: 34a805305702198ec747fc78bd8c34fa8a7a1a67594e63334c63d2eb17591875d81c88273084aae92b4182a8330ae009e115c64ee7fd0f5e49dcc59524b0de80
7
+ data.tar.gz: ad5fb25b020d99ee030272cffa8b706f3ac077442caa26df10c1c6a8a2214b34c6c6842f60f668e9634e6423858f8a930c6b4f221ae16f002b5b6f3a32dda068
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- okex (0.2.7)
4
+ okex (0.3.1)
5
5
  faraday (~> 1.5)
6
6
 
7
7
  GEM
@@ -9,7 +9,7 @@ GEM
9
9
  specs:
10
10
  byebug (11.1.3)
11
11
  diff-lcs (1.4.4)
12
- faraday (1.5.0)
12
+ faraday (1.5.1)
13
13
  faraday-em_http (~> 1.0)
14
14
  faraday-em_synchrony (~> 1.0)
15
15
  faraday-excon (~> 1.1)
@@ -24,7 +24,7 @@ GEM
24
24
  faraday-excon (1.1.0)
25
25
  faraday-httpclient (1.0.1)
26
26
  faraday-net_http (1.0.1)
27
- faraday-net_http_persistent (1.1.0)
27
+ faraday-net_http_persistent (1.2.0)
28
28
  faraday-patron (1.0.0)
29
29
  multipart-post (2.1.1)
30
30
  rake (10.5.0)
@@ -41,7 +41,7 @@ GEM
41
41
  diff-lcs (>= 1.2.0, < 2.0)
42
42
  rspec-support (~> 3.10.0)
43
43
  rspec-support (3.10.2)
44
- ruby2_keywords (0.0.4)
44
+ ruby2_keywords (0.0.5)
45
45
 
46
46
  PLATFORMS
47
47
  ruby
data/README.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # okex
2
2
 
3
- This is the V3 API ruby lib for okex.com
3
+ This is the V5 API ruby lib for okex.com
4
4
 
5
5
  ## Installation
6
6
 
@@ -20,6 +20,7 @@ Or install it yourself as:
20
20
 
21
21
  ## Usage
22
22
 
23
+ Set `OKEX_DEBUG` environment flag to 1 to print debug messages.
23
24
 
24
25
  ## Development
25
26
 
data/lib/okex.rb CHANGED
@@ -2,10 +2,14 @@ require "okex/version"
2
2
  require "okex/api_v3"
3
3
  require "okex/api_v5"
4
4
  require "okex/client"
5
- require "okex/coin"
5
+ require "okex/coin/bitcoin"
6
+ require "okex/coin/usdt"
6
7
  require "okex/order"
7
8
  require "okex/host"
8
9
  require "okex/max_size"
10
+ require "okex/api_error"
11
+ require "okex/balance"
12
+
9
13
 
10
14
 
11
15
  module OKEX
@@ -0,0 +1,13 @@
1
+ module OKEX
2
+ class ApiError < StandardError
3
+ def initialize(code, data, msg)
4
+ @code = code
5
+ @data = data
6
+ @msg = msg
7
+ end
8
+
9
+ def to_s
10
+ "code=#{@code}, data=#{@data}, msg=#{@msg}"
11
+ end
12
+ end
13
+ end
data/lib/okex/api_v5.rb CHANGED
@@ -14,13 +14,23 @@ class OKEX::ApiV5
14
14
  url += "?instId=#{inst_id}"
15
15
  end
16
16
 
17
- resp = client.get(host, url)
18
-
19
- if resp['success']
20
- return resp['data'].map {|params| OKEX::Order.new(params)}
21
- end
17
+ data = client.get(host, url)
22
18
 
23
- []
19
+ data.map {|params| OKEX::Order.new(params)}
20
+ end
21
+
22
+ def balance(ccy, round)
23
+ data = client.get(host, "/api/v5/account/balance?ccy=#{ccy}")
24
+
25
+ details = data[0]['details'][0]
26
+
27
+ OKEX::Balance.new(
28
+ details['ccy'],
29
+ dig_round(details, 'cashBal', round),
30
+ dig_round(details, 'avalEq', round),
31
+ dig_round(details, 'frozenBal', round),
32
+ dig_round(details, 'upl', round)
33
+ )
24
34
  end
25
35
 
26
36
  def long_swap(instid, amount)
@@ -49,6 +59,22 @@ class OKEX::ApiV5
49
59
  client.post(host, "/api/v5/trade/order", params)
50
60
  end
51
61
 
62
+ # # 开多带止损
63
+ # def long_swap_stop(instid, amount, stop_price)
64
+ # params = {
65
+ # "instId": instid,
66
+ # "tdMode": "cross",
67
+ # "side": "buy",
68
+ # "posSide": "long",
69
+ # "ordType": "conditional",
70
+ # "sz": amount.to_s,
71
+ # "slTriggerPx": stop_price.to_s,
72
+ # "slOrdPx": "-1", # 执行市价止损
73
+ # }
74
+
75
+ # client.post(host, "/api/v5/trade/order-algo", params)
76
+ # end
77
+
52
78
  def close_long(instrument_id)
53
79
  close_position(instrument_id, "long")
54
80
  end
@@ -58,16 +84,15 @@ class OKEX::ApiV5
58
84
  end
59
85
 
60
86
  def max_size(instrument_id)
61
- result = client.get(host, "/api/v5/account/max-size?instId=#{instrument_id}&tdMode=cross")
87
+ data = client.get(host, "/api/v5/account/max-size?instId=#{instrument_id}&tdMode=cross")
62
88
 
63
89
  ret = OKEX::MaxSize.new(-1, -1)
64
- if result['success']
65
- el = result['data'][0]
66
- if el.present? && el['maxBuy'].to_i > 0 && el['maxSell'].to_i > 0
67
- ret = OKEX::MaxSize.new(el['maxBuy'].to_i, el['maxSell'].to_i)
68
- end
90
+
91
+ el = data[0]
92
+ if el.present? && el['maxBuy'].to_i > 0 && el['maxSell'].to_i > 0
93
+ ret = OKEX::MaxSize.new(el['maxBuy'].to_i, el['maxSell'].to_i)
69
94
  end
70
-
95
+
71
96
  ret
72
97
  end
73
98
 
@@ -84,4 +109,8 @@ class OKEX::ApiV5
84
109
 
85
110
  client.post(host, "/api/v5/trade/close-position", params)
86
111
  end
112
+
113
+ def dig_round(obj, key, round)
114
+ obj[key].to_f.round(round)
115
+ end
87
116
  end
@@ -0,0 +1,2 @@
1
+ class OKEX::Balance < Struct.new(:currency, :total, :available, :frozen, :upl)
2
+ end
data/lib/okex/client.rb CHANGED
@@ -12,19 +12,27 @@ module OKEX
12
12
  end
13
13
 
14
14
  def get(host, path)
15
+ url = host + path
15
16
  ts = timestamp
16
17
  sig = sign(ts, "GET", path)
18
+ _h = headers(ts, sig)
17
19
 
18
- _resp(Faraday.get(host + path, nil, headers(ts, sig)))
20
+ puts "---> GET: url=#{url}, headers=#{_h}" if ENV['OKEX_DEBUG'].to_i > 0
21
+
22
+ _resp(Faraday.get(url, nil, _h))
19
23
  end
20
24
 
21
25
  def post(host, path, payload)
22
26
  payload_json = gen_payload(payload)
23
27
 
28
+ url = host + path
24
29
  ts = timestamp
25
30
  sig = sign(ts, "POST", path + payload_json)
31
+ _h = headers(ts, sig)
32
+
33
+ puts "---> POST: url=#{url}, payload=#{payload_json}, headers=#{_h}" if ENV['OKEX_DEBUG'].to_i > 0
26
34
 
27
- _resp(Faraday.post(host + path, payload_json, headers(ts, sig)))
35
+ _resp(Faraday.post(url, payload_json, _h))
28
36
  end
29
37
 
30
38
  private
@@ -64,10 +72,10 @@ module OKEX
64
72
 
65
73
  code = result['code'].to_i
66
74
  if code == 0 && result['msg'].empty?
67
- return {'success' => true, 'data' => result['data']}
68
- else
69
- return {'success' => false, 'code' => code, 'msg' => result['msg']}
75
+ return result['data']
70
76
  end
77
+
78
+ raise OKEX::ApiError.new(code, result['data'], result['msg'])
71
79
  end
72
80
  end
73
81
  end
@@ -0,0 +1,11 @@
1
+ module OKEX
2
+ module Coin
3
+ class USDT
4
+ CODE = "USDT".freeze
5
+
6
+ def self.code
7
+ CODE
8
+ end
9
+ end
10
+ end
11
+ end
data/lib/okex/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module OKEX
2
- VERSION = "0.3.0"
2
+ VERSION = "0.3.1"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: okex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Zhang
@@ -99,11 +99,14 @@ files:
99
99
  - bin/console
100
100
  - bin/setup
101
101
  - lib/okex.rb
102
+ - lib/okex/api_error.rb
102
103
  - lib/okex/api_v3.rb
103
104
  - lib/okex/api_v5.rb
105
+ - lib/okex/balance.rb
104
106
  - lib/okex/client.rb
105
107
  - lib/okex/coin.rb
106
108
  - lib/okex/coin/bitcoin.rb
109
+ - lib/okex/coin/usdt.rb
107
110
  - lib/okex/host.rb
108
111
  - lib/okex/max_size.rb
109
112
  - lib/okex/order.rb