okex 0.2.8 → 0.3.3

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: 9d0150ce45ec59cd9ded8310ef2d1346c4dc4bac69d2c90fc676ef12edc4b50c
4
- data.tar.gz: 96b70fdc8c95ee1cd3e0ff980a93c7bbb0b97e1a8917f98a4f471072f8ccbb39
3
+ metadata.gz: 2103a60d7a2cbb14582569e691dfa577bc69a2d02214d200cfd0c9ae17d122a9
4
+ data.tar.gz: 5754ed1518c2486162d94bd6c63a9d402cc573aae1ead9a90932a7ce4acbf2f3
5
5
  SHA512:
6
- metadata.gz: 6ca66d2608fcea9ed02dc4881b113650e22b262478bce2a885383dcb96dda5818f59f61340c8f87613eefa49b3e8d86f95677bb34a75245395178e41d3156746
7
- data.tar.gz: 8f1a140fffe151688a64304a41ff5162f085fdc5bb4bb61daabef30c3a276946863e574242e26c277f07f79d53e7f4cdf9f86287dd7c661c68eaf90315aa220b
6
+ metadata.gz: 52ac7f191021bdfd1179898ee340be1f468498e4d0fd5e318ca1f670443ef769dfdca3b7a33136b1406330ea40bcb82c53f366f47d0b36db07796c0e24e8fcc6
7
+ data.tar.gz: ea3ce28a4e2eda19f3c882f03ab012955d7025d6e7c5a11a3e3c092d71e5c4492fc0aebedcb6dc1ec08425a6bcb10e8b51a47490fbef518d46150c7c80f74f70
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.3)
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,36 @@ 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)
18
+
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
+ )
34
+ end
22
35
 
23
- []
36
+ def long_swap(instid, amount)
37
+ params = {
38
+ "instId": instid,
39
+ "tdMode": "cross",
40
+ "side": "buy",
41
+ "posSide": "long",
42
+ "ordType": "market",
43
+ "sz": amount.to_s,
44
+ }
45
+
46
+ client.post(host, "/api/v5/trade/order", params)
24
47
  end
25
48
 
26
49
  def short_swap(instid, amount)
@@ -30,7 +53,7 @@ class OKEX::ApiV5
30
53
  "side": "sell",
31
54
  "posSide": "short",
32
55
  "ordType": "market",
33
- "sz": "1"
56
+ "sz": amount.to_s,
34
57
  }
35
58
 
36
59
  client.post(host, "/api/v5/trade/order", params)
@@ -45,19 +68,42 @@ class OKEX::ApiV5
45
68
  end
46
69
 
47
70
  def max_size(instrument_id)
48
- result = client.get(host, "/api/v5/account/max-size?instId=#{instrument_id}&tdMode=cross")
71
+ data = client.get(host, "/api/v5/account/max-size?instId=#{instrument_id}&tdMode=cross")
49
72
 
50
73
  ret = OKEX::MaxSize.new(-1, -1)
51
- if result['success']
52
- el = result['data'][0]
53
- if el.present? && el['maxBuy'].to_i > 0 && el['maxSell'].to_i > 0
54
- ret = OKEX::MaxSize.new(el['maxBuy'].to_i, el['maxSell'].to_i)
55
- end
74
+
75
+ el = data[0]
76
+ if el.present? && el['maxBuy'].to_i > 0 && el['maxSell'].to_i > 0
77
+ ret = OKEX::MaxSize.new(el['maxBuy'].to_i, el['maxSell'].to_i)
56
78
  end
57
-
79
+
58
80
  ret
59
81
  end
60
82
 
83
+ def current_leverage(inst_id)
84
+ data = client.get(host, "/api/v5/account/leverage-info?instId=#{inst_id}&mgnMode=cross")
85
+
86
+ data[0]['lever'].to_i
87
+ end
88
+
89
+ # 设置止损价格
90
+ def set_stop_loss(inst_id, posSide, sz, price)
91
+ side = (posSide == OKEX::Order::POS_LONG) ? "sell" : "buy"
92
+
93
+ params = {
94
+ "instId": inst_id,
95
+ "tdMode": "cross",
96
+ "side": side,
97
+ "posSide": posSide.to_s,
98
+ "sz": sz.to_s,
99
+ "ordType": "conditional",
100
+ "slTriggerPx": price.to_s,
101
+ "slOrdPx": "-1"
102
+ }
103
+
104
+ client.post(host, "/api/v5/trade/order-algo", params)
105
+ end
106
+
61
107
  private
62
108
 
63
109
  attr_reader :client, :host
@@ -71,4 +117,8 @@ class OKEX::ApiV5
71
117
 
72
118
  client.post(host, "/api/v5/trade/close-position", params)
73
119
  end
74
- end
120
+
121
+ def dig_round(obj, key, round)
122
+ obj[key].to_f.round(round)
123
+ end
124
+ 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/max_size.rb CHANGED
@@ -1,3 +1,2 @@
1
1
  class OKEX::MaxSize < Struct.new(:buy, :sell)
2
- def
3
2
  end
data/lib/okex/order.rb CHANGED
@@ -7,6 +7,7 @@ module OKEX
7
7
  @params = params
8
8
  end
9
9
 
10
+ # 合约名称,例如 BTC-USDT-SWAP
10
11
  def inst_id
11
12
  dig("instId")
12
13
  end
data/lib/okex/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module OKEX
2
- VERSION = "0.2.8"
2
+ VERSION = "0.3.3"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: okex
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.8
4
+ version: 0.3.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Zhang
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2021-07-25 00:00:00.000000000 Z
11
+ date: 2021-07-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -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