okex 0.3.0 → 0.3.4

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: 463b9adff7eb98e1fa712f06153edbc29b80870aede2cd2913681da84f0e5b66
4
+ data.tar.gz: 428d38ce826c5e3e01ddcd43c3a9e8e4e5b2de1d4f41139497ad5beee818b452
5
5
  SHA512:
6
- metadata.gz: f963003215c3bc2b79a8168757f82d4a52bc07e8f8e698f0e231dd296af3e0d6f5e5f9f262f923ace4ad5816d7e01bb9cf75b9c7cb101325cb0d4d678f5873ac
7
- data.tar.gz: 3dcfcf6d73f158e69f4240b267199143201c5a872f6dc312322ee8ffc91d9cee559a10e30c56a3ccc3baea214aa78924071b5b0ceb61f5ccb82425523dbd1335
6
+ metadata.gz: 615e679ffb2f370020af864cdc753bcf7cb836a83e73a127046f09712b1a81a4d20dcbe94643e19177f1c61578fb8e3bfd44f752673fed56ab4208d4eeda7ee0
7
+ data.tar.gz: dc96eacc3985d1889eac587fee049b2c174eca9e245d03e66a4bfb3f1cb8c1871c99cb73ef70f5b418703883f4e6927888c7806492841fb76a71f9b4b43a332a
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.4)
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)
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]
22
26
 
23
- []
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)
@@ -58,19 +68,55 @@ class OKEX::ApiV5
58
68
  end
59
69
 
60
70
  def max_size(instrument_id)
61
- 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")
62
72
 
63
73
  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
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)
69
78
  end
70
-
79
+
71
80
  ret
72
81
  end
73
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
+ # @param inst_id [String] 合约名称
91
+ # @param posSide [String] 持仓方向
92
+ # @param sz [Integer] 持仓数量(张)
93
+ # @param price [Float] 止损价格
94
+ def set_stop_loss(inst_id, posSide, sz, price)
95
+ side = (posSide == OKEX::Order::POS_LONG) ? "sell" : "buy"
96
+
97
+ params = {
98
+ "instId": inst_id,
99
+ "tdMode": "cross",
100
+ "side": side,
101
+ "posSide": posSide.to_s,
102
+ "sz": sz.to_s,
103
+ "ordType": "conditional",
104
+ "slTriggerPx": price.to_s,
105
+ "slOrdPx": "-1"
106
+ }
107
+
108
+ client.post(host, "/api/v5/trade/order-algo", params)
109
+ end
110
+
111
+ # 查询当前设置的止损价格
112
+ # @param inst_id [String] 合约名称
113
+ def stop_loss_price(inst_id)
114
+ result = client.get(host, "/api/v5/trade/orders-algo-pending?instId=#{inst_id}&instType=SWAP&ordType=conditional")
115
+ return result[0]["slTriggerPx"].to_i if result.size == 1
116
+
117
+ raise "invalid result: #{result.inspect}"
118
+ end
119
+
74
120
  private
75
121
 
76
122
  attr_reader :client, :host
@@ -84,4 +130,8 @@ class OKEX::ApiV5
84
130
 
85
131
  client.post(host, "/api/v5/trade/close-position", params)
86
132
  end
87
- end
133
+
134
+ def dig_round(obj, key, round)
135
+ obj[key].to_f.round(round)
136
+ end
137
+ 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.4"
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.3.0
4
+ version: 0.3.4
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