okex 0.2.3 → 0.3.0

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: 119599f81a10e3904790ddb620ce5e73c5f8f880c542d0b1cc34e56463cc88d4
4
- data.tar.gz: a0b3c1f7f95ddfbacb37703461ffb92f3df372fa6e6f64e91c6027127d513a9a
3
+ metadata.gz: c24e96c13dd676d1ece02650d07e9c32624f124ea092d17bdcf2d87d621c1236
4
+ data.tar.gz: b67158ebbd5aece70467880e1fb1d8f955f362a671f78bebc7d285c17764ee1b
5
5
  SHA512:
6
- metadata.gz: e40edaca89bbfad96f815190516ba4ac2a47244a442189d54aca236c0a908b03141f4878a00c8153e2496c63a1aef9d25b747419c624bc3eea82caf89ec72efe
7
- data.tar.gz: 17d81b29e609e2a11614b214ca77c3143391654a1276eefe4d702b63fc81e415ff92cf6a554d28c9f55d2c41c0734c9409f5d3617ad46cdd5a9d167660847403
6
+ metadata.gz: f963003215c3bc2b79a8168757f82d4a52bc07e8f8e698f0e231dd296af3e0d6f5e5f9f262f923ace4ad5816d7e01bb9cf75b9c7cb101325cb0d4d678f5873ac
7
+ data.tar.gz: 3dcfcf6d73f158e69f4240b267199143201c5a872f6dc312322ee8ffc91d9cee559a10e30c56a3ccc3baea214aa78924071b5b0ceb61f5ccb82425523dbd1335
data/Gemfile.lock CHANGED
@@ -1,12 +1,13 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- okex (0.2.3)
4
+ okex (0.2.7)
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
12
  faraday (1.5.0)
12
13
  faraday-em_http (~> 1.0)
@@ -47,6 +48,7 @@ PLATFORMS
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/lib/okex.rb CHANGED
@@ -4,8 +4,9 @@ require "okex/api_v5"
4
4
  require "okex/client"
5
5
  require "okex/coin"
6
6
  require "okex/order"
7
- require "openssl"
8
- require "faraday"
7
+ require "okex/host"
8
+ require "okex/max_size"
9
+
9
10
 
10
11
  module OKEX
11
12
  class Error < StandardError; end
data/lib/okex/api_v5.rb CHANGED
@@ -1,22 +1,41 @@
1
1
  class OKEX::ApiV5
2
- def initialize(client)
2
+ def initialize(client, host)
3
3
  @client = client
4
+ @host = host
4
5
  end
5
6
 
6
7
  def instruments
7
- client.get("/api/v5/public/instruments")
8
+ client.get(host, "/api/v5/public/instruments?instType=SWAP")
8
9
  end
9
10
 
10
- def orders
11
- resp = client.get("/api/v5/account/positions")
11
+ def orders(inst_id=nil)
12
+ url = "/api/v5/account/positions"
13
+ if inst_id.present?
14
+ url += "?instId=#{inst_id}"
15
+ end
16
+
17
+ resp = client.get(host, url)
12
18
 
13
- if resp['code'].to_i == 0 && resp['data'].size > 0
19
+ if resp['success']
14
20
  return resp['data'].map {|params| OKEX::Order.new(params)}
15
21
  end
16
22
 
17
23
  []
18
24
  end
19
25
 
26
+ def long_swap(instid, amount)
27
+ params = {
28
+ "instId": instid,
29
+ "tdMode": "cross",
30
+ "side": "buy",
31
+ "posSide": "long",
32
+ "ordType": "market",
33
+ "sz": amount.to_s,
34
+ }
35
+
36
+ client.post(host, "/api/v5/trade/order", params)
37
+ end
38
+
20
39
  def short_swap(instid, amount)
21
40
  params = {
22
41
  "instId": instid,
@@ -24,31 +43,45 @@ class OKEX::ApiV5
24
43
  "side": "sell",
25
44
  "posSide": "short",
26
45
  "ordType": "market",
27
- "sz": "1"
46
+ "sz": amount.to_s,
28
47
  }
29
48
 
30
- client.post("/api/v5/trade/order", params)
49
+ client.post(host, "/api/v5/trade/order", params)
50
+ end
51
+
52
+ def close_long(instrument_id)
53
+ close_position(instrument_id, "long")
31
54
  end
32
55
 
33
- def close_long(instid)
34
- close_position(instid, "long")
56
+ def close_short(instrument_id)
57
+ close_position(instrument_id, "short")
35
58
  end
36
59
 
37
- def close_short(instid)
38
- close_position(instid, "short")
60
+ def max_size(instrument_id)
61
+ result = client.get(host, "/api/v5/account/max-size?instId=#{instrument_id}&tdMode=cross")
62
+
63
+ 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
69
+ end
70
+
71
+ ret
39
72
  end
40
73
 
41
74
  private
42
75
 
43
- attr_reader :client
76
+ attr_reader :client, :host
44
77
 
45
- def close_position(instid, direction)
78
+ def close_position(instrument_id, direction)
46
79
  params = {
47
- "instId": instid,
80
+ "instId": instrument_id,
48
81
  "mgnMode": "cross",
49
82
  "posSide": direction
50
83
  }
51
84
 
52
- client.post("/api/v5/trade/close-position", params)
85
+ client.post(host, "/api/v5/trade/close-position", params)
53
86
  end
54
87
  end
data/lib/okex/client.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'time'
2
2
  require 'base64'
3
3
  require 'json'
4
+ require 'faraday'
4
5
 
5
6
  module OKEX
6
7
  class Client
@@ -10,34 +11,26 @@ module OKEX
10
11
  @passphrase = passphrase
11
12
  end
12
13
 
13
- def get(path)
14
+ def get(host, path)
14
15
  ts = timestamp
15
16
  sig = sign(ts, "GET", path)
16
17
 
17
- _resp(::Faraday.get(HOST + path, nil, headers(ts, sig)))
18
+ _resp(Faraday.get(host + path, nil, headers(ts, sig)))
18
19
  end
19
20
 
20
- def post(path, payload)
21
+ def post(host, path, payload)
21
22
  payload_json = gen_payload(payload)
22
23
 
23
24
  ts = timestamp
24
25
  sig = sign(ts, "POST", path + payload_json)
25
26
 
26
- begin
27
- conn = ::Faraday.new(HOST + path, :ssl => {:verify => false})
28
- result = conn.post(HOST + path, payload_json, headers(ts, sig))
29
- _resp(result)
30
- rescue => e
31
- puts "[error] #{e.inspect}"
32
- end
27
+ _resp(Faraday.post(host + path, payload_json, headers(ts, sig)))
33
28
  end
34
29
 
35
30
  private
36
31
 
37
32
  attr_reader :api_key, :api_secret, :passphrase
38
33
 
39
- HOST = "https://www.okex.com"
40
-
41
34
  def gen_payload(payload)
42
35
  m = payload.map { |k, v| JSON.generate({k => v}, {space: ' '})}
43
36
  m2 = m.join(", ").gsub("{", "").gsub("}", "")
@@ -67,7 +60,14 @@ module OKEX
67
60
  end
68
61
 
69
62
  def _resp(result)
70
- JSON.parse(result.body)
63
+ result = JSON.parse(result.body)
64
+
65
+ code = result['code'].to_i
66
+ if code == 0 && result['msg'].empty?
67
+ return {'success' => true, 'data' => result['data']}
68
+ else
69
+ return {'success' => false, 'code' => code, 'msg' => result['msg']}
70
+ end
71
71
  end
72
72
  end
73
73
  end
data/lib/okex/host.rb ADDED
@@ -0,0 +1,12 @@
1
+ class OKEX::Host
2
+ OUYI_HOST = "https://www.ouyi.cc".freeze
3
+ OKEX_HOST = "https://www.okex.com".freeze
4
+
5
+ def self.ouyi
6
+ OUYI_HOST
7
+ end
8
+
9
+ def self.okex
10
+ OKEX_HOST
11
+ end
12
+ 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.3"
2
+ VERSION = "0.3.0"
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.3
4
+ version: 0.3.0
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-23 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
@@ -90,6 +104,8 @@ files:
90
104
  - lib/okex/client.rb
91
105
  - lib/okex/coin.rb
92
106
  - lib/okex/coin/bitcoin.rb
107
+ - lib/okex/host.rb
108
+ - lib/okex/max_size.rb
93
109
  - lib/okex/order.rb
94
110
  - lib/okex/version.rb
95
111
  - okex.gemspec