okex 0.2.5 → 0.3.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6e79f7f281cc49fecf294441cfc91c8a0f7e065204a69fae02c968f48b5fc83a
4
- data.tar.gz: 802b3412745cc52ed7989bf6e16d9e6e61a9d5c962446bb81b29182abcaeeab0
3
+ metadata.gz: a104fac8edf7712e91a90bdab72ccccc93cb1e20633400c70aee249b69b22202
4
+ data.tar.gz: 8003c43686c142e4619a4ff52852aa4cbb5c050bcfd161df2af5d0fbe35409fb
5
5
  SHA512:
6
- metadata.gz: 1f00ee33cb15494a908dba476c5fb1b3eaeea37b3b313bfb5d47a95237ac6117b83b6adae1790bbf62ece997b4f972eb98d77a6326dbb3899da4bb116d96cc06
7
- data.tar.gz: 6a2ddad58a09dfdb945babd65cc78022c427bb6147df57f5c38b5e74438ebf16e75f2746287da0931abfb4e0f643077aeee984f7920ea5954ca47aa75a548783
6
+ metadata.gz: 34a805305702198ec747fc78bd8c34fa8a7a1a67594e63334c63d2eb17591875d81c88273084aae92b4182a8330ae009e115c64ee7fd0f5e49dcc59524b0de80
7
+ data.tar.gz: ad5fb25b020d99ee030272cffa8b706f3ac077442caa26df10c1c6a8a2214b34c6c6842f60f668e9634e6423858f8a930c6b4f221ae16f002b5b6f3a32dda068
data/Gemfile.lock CHANGED
@@ -1,14 +1,15 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- okex (0.2.3)
4
+ okex (0.3.1)
5
5
  faraday (~> 1.5)
6
6
 
7
7
  GEM
8
8
  remote: https://rubygems.org/
9
9
  specs:
10
+ byebug (11.1.3)
10
11
  diff-lcs (1.4.4)
11
- faraday (1.5.0)
12
+ faraday (1.5.1)
12
13
  faraday-em_http (~> 1.0)
13
14
  faraday-em_synchrony (~> 1.0)
14
15
  faraday-excon (~> 1.1)
@@ -23,7 +24,7 @@ GEM
23
24
  faraday-excon (1.1.0)
24
25
  faraday-httpclient (1.0.1)
25
26
  faraday-net_http (1.0.1)
26
- faraday-net_http_persistent (1.1.0)
27
+ faraday-net_http_persistent (1.2.0)
27
28
  faraday-patron (1.0.0)
28
29
  multipart-post (2.1.1)
29
30
  rake (10.5.0)
@@ -40,13 +41,14 @@ GEM
40
41
  diff-lcs (>= 1.2.0, < 2.0)
41
42
  rspec-support (~> 3.10.0)
42
43
  rspec-support (3.10.2)
43
- ruby2_keywords (0.0.4)
44
+ ruby2_keywords (0.0.5)
44
45
 
45
46
  PLATFORMS
46
47
  ruby
47
48
 
48
49
  DEPENDENCIES
49
50
  bundler (~> 1.17)
51
+ byebug (~> 11.1)
50
52
  okex!
51
53
  rake (~> 10.0)
52
54
  rspec (~> 3.0)
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,9 +2,15 @@ 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"
9
+ require "okex/max_size"
10
+ require "okex/api_error"
11
+ require "okex/balance"
12
+
13
+
8
14
 
9
15
  module OKEX
10
16
  class Error < StandardError; end
@@ -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
@@ -8,14 +8,42 @@ class OKEX::ApiV5
8
8
  client.get(host, "/api/v5/public/instruments?instType=SWAP")
9
9
  end
10
10
 
11
- def orders
12
- resp = client.get(host, "/api/v5/account/positions")
13
-
14
- if resp['code'].to_i == 0 && resp['data'].size > 0
15
- return resp['data'].map {|params| OKEX::Order.new(params)}
11
+ def orders(inst_id=nil)
12
+ url = "/api/v5/account/positions"
13
+ if inst_id.present?
14
+ url += "?instId=#{inst_id}"
16
15
  end
17
16
 
18
- []
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
35
+
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)
19
47
  end
20
48
 
21
49
  def short_swap(instid, amount)
@@ -25,31 +53,64 @@ class OKEX::ApiV5
25
53
  "side": "sell",
26
54
  "posSide": "short",
27
55
  "ordType": "market",
28
- "sz": "1"
56
+ "sz": amount.to_s,
29
57
  }
30
58
 
31
59
  client.post(host, "/api/v5/trade/order", params)
32
60
  end
33
61
 
34
- def close_long(instid)
35
- close_position(instid, "long")
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
+
78
+ def close_long(instrument_id)
79
+ close_position(instrument_id, "long")
36
80
  end
37
81
 
38
- def close_short(instid)
39
- close_position(instid, "short")
82
+ def close_short(instrument_id)
83
+ close_position(instrument_id, "short")
84
+ end
85
+
86
+ def max_size(instrument_id)
87
+ data = client.get(host, "/api/v5/account/max-size?instId=#{instrument_id}&tdMode=cross")
88
+
89
+ ret = OKEX::MaxSize.new(-1, -1)
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)
94
+ end
95
+
96
+ ret
40
97
  end
41
98
 
42
99
  private
43
100
 
44
101
  attr_reader :client, :host
45
102
 
46
- def close_position(instid, direction)
103
+ def close_position(instrument_id, direction)
47
104
  params = {
48
- "instId": instid,
105
+ "instId": instrument_id,
49
106
  "mgnMode": "cross",
50
107
  "posSide": direction
51
108
  }
52
109
 
53
110
  client.post(host, "/api/v5/trade/close-position", params)
54
111
  end
112
+
113
+ def dig_round(obj, key, round)
114
+ obj[key].to_f.round(round)
115
+ end
55
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
@@ -60,7 +68,14 @@ module OKEX
60
68
  end
61
69
 
62
70
  def _resp(result)
63
- JSON.parse(result.body)
71
+ result = JSON.parse(result.body)
72
+
73
+ code = result['code'].to_i
74
+ if code == 0 && result['msg'].empty?
75
+ return result['data']
76
+ end
77
+
78
+ raise OKEX::ApiError.new(code, result['data'], result['msg'])
64
79
  end
65
80
  end
66
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
@@ -0,0 +1,2 @@
1
+ class OKEX::MaxSize < Struct.new(:buy, :sell)
2
+ end
data/lib/okex/order.rb CHANGED
@@ -7,7 +7,8 @@ module OKEX
7
7
  @params = params
8
8
  end
9
9
 
10
- def instrument_id
10
+ # 合约名称,例如 BTC-USDT-SWAP
11
+ def inst_id
11
12
  dig("instId")
12
13
  end
13
14
 
@@ -30,6 +31,11 @@ module OKEX
30
31
  dig("liqPx").to_f
31
32
  end
32
33
 
34
+ # 最新成交价
35
+ def last_price
36
+ dig("last").to_f
37
+ end
38
+
33
39
  # 持仓方向
34
40
  def position_side
35
41
  side = dig("posSide")
@@ -38,11 +44,44 @@ module OKEX
38
44
  end
39
45
  end
40
46
 
47
+ def long?
48
+ position_side == POS_LONG
49
+ end
50
+
51
+ def short?
52
+ position_side == POS_SHORT
53
+ end
54
+
55
+ # 开仓方向名称
56
+ def position_name
57
+ case position_side
58
+ when POS_SHORT
59
+ '空'
60
+ when POS_LONG
61
+ '多'
62
+ end
63
+ end
64
+
41
65
  # 未实现收益
42
66
  def upl
43
67
  dig("upl").to_f
44
68
  end
45
69
 
70
+ # 持仓张数
71
+ def pos
72
+ dig("pos").to_i
73
+ end
74
+
75
+ # 是否正在持仓
76
+ def open?
77
+ pos > 0
78
+ end
79
+
80
+ # 已经平仓
81
+ def closed?
82
+ pos == 0
83
+ end
84
+
46
85
  private
47
86
 
48
87
  def dig(key)
data/lib/okex/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module OKEX
2
- VERSION = "0.2.5"
2
+ VERSION = "0.3.1"
3
3
  end
data/okex.gemspec CHANGED
@@ -38,6 +38,7 @@ Gem::Specification.new do |spec|
38
38
  spec.add_development_dependency "bundler", "~> 1.17"
39
39
  spec.add_development_dependency "rake", "~> 10.0"
40
40
  spec.add_development_dependency "rspec", "~> 3.0"
41
+ spec.add_development_dependency "byebug", "~> 11.1"
41
42
 
42
43
  spec.add_runtime_dependency "faraday", "~> 1.5"
43
44
  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.5
4
+ version: 0.3.1
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-24 00:00:00.000000000 Z
11
+ date: 2021-07-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -52,6 +52,20 @@ dependencies:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
54
  version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '11.1'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '11.1'
55
69
  - !ruby/object:Gem::Dependency
56
70
  name: faraday
57
71
  requirement: !ruby/object:Gem::Requirement
@@ -85,12 +99,16 @@ files:
85
99
  - bin/console
86
100
  - bin/setup
87
101
  - lib/okex.rb
102
+ - lib/okex/api_error.rb
88
103
  - lib/okex/api_v3.rb
89
104
  - lib/okex/api_v5.rb
105
+ - lib/okex/balance.rb
90
106
  - lib/okex/client.rb
91
107
  - lib/okex/coin.rb
92
108
  - lib/okex/coin/bitcoin.rb
109
+ - lib/okex/coin/usdt.rb
93
110
  - lib/okex/host.rb
111
+ - lib/okex/max_size.rb
94
112
  - lib/okex/order.rb
95
113
  - lib/okex/version.rb
96
114
  - okex.gemspec