okex 0.2.6 → 0.3.2

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: 012c9b50e5c4dd3fb5522d0eef07764feb177c23a2aee07e1a847399fb7b7d68
4
- data.tar.gz: 43f87e76c36c65bf9b81122392b36af8bee0f5125617b28ff2a6e2ae6a36196a
3
+ metadata.gz: 30adc1b436dc4b0e5d299b64567da2e425c83d8b631c8e73d5c5cf116e3d45aa
4
+ data.tar.gz: 756dd71409995a737c20a5acf06b9c4478b1e91735ef1ab34345a8b3aef9051f
5
5
  SHA512:
6
- metadata.gz: 84533cb2cc12b733a01c29a3f76274e1b179573fa0ad21e2588c87b04e76e9afdca18979c22056b4fa4c75fbec578e7fbb319e198721bedb9150e39e4f819f2a
7
- data.tar.gz: 839673d67fed38b99dce0b5d5f31df562e56d9617c5e7c87cad1772136b616c60d457d6d9397427372bb0b6fd4168c9b035a8d74fb50b972c9bfc68e48defbce
6
+ metadata.gz: aae50477ae52399c19a0f14944a97e8d08182e196e34c436ebbb03c344807acf144e433834ce5894e4d0b36e7e9dba9f396d67ff40ae01c0741427d9e3442c47
7
+ data.tar.gz: 307e1223e5e804412cb7d4ec770edebcee1ba25b65bd46e38b5bd92fc6a95729a22cd39dec8eae1034d91b748482ea834107d8f3a766178f97bafc8155fc56a6
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- okex (0.2.5)
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,11 +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
- require "okex/account_order"
8
- require "okex/trade_order"
9
8
  require "okex/host"
9
+ require "okex/max_size"
10
+ require "okex/api_error"
11
+ require "okex/balance"
12
+
10
13
 
11
14
 
12
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
@@ -8,24 +8,42 @@ class OKEX::ApiV5
8
8
  client.get(host, "/api/v5/public/instruments?instType=SWAP")
9
9
  end
10
10
 
11
- def account_orders
12
- resp = client.get(host, "/api/v5/account/positions")
13
-
14
- if resp['success']
15
- return resp['data'].map {|params| OKEX::AccountOrder.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)}
19
20
  end
20
21
 
21
- def trade_orders(instrument_id)
22
- resp = client.get(host, "/api/v5/trade/order?instId=#{instrument_id}")
23
-
24
- if resp['code'].to_i == 0 && resp['data'].size > 0
25
- return resp['data'].map {|params| OKEX::TradeOrder.new(params)}
26
- end
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
27
35
 
28
- []
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)
29
47
  end
30
48
 
31
49
  def short_swap(instid, amount)
@@ -35,12 +53,28 @@ class OKEX::ApiV5
35
53
  "side": "sell",
36
54
  "posSide": "short",
37
55
  "ordType": "market",
38
- "sz": "1"
56
+ "sz": amount.to_s,
39
57
  }
40
58
 
41
59
  client.post(host, "/api/v5/trade/order", params)
42
60
  end
43
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
+
44
78
  def close_long(instrument_id)
45
79
  close_position(instrument_id, "long")
46
80
  end
@@ -49,6 +83,25 @@ class OKEX::ApiV5
49
83
  close_position(instrument_id, "short")
50
84
  end
51
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
97
+ end
98
+
99
+ def current_leverage(inst_id)
100
+ data = client.get(host, "/api/v5/account/leverage-info?instId=#{inst_id}&mgnMode=cross")
101
+
102
+ data[0]['lever'].to_i
103
+ end
104
+
52
105
  private
53
106
 
54
107
  attr_reader :client, :host
@@ -62,4 +115,8 @@ class OKEX::ApiV5
62
115
 
63
116
  client.post(host, "/api/v5/trade/close-position", params)
64
117
  end
118
+
119
+ def dig_round(obj, key, round)
120
+ obj[key].to_f.round(round)
121
+ end
65
122
  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
@@ -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
 
@@ -71,6 +72,16 @@ module OKEX
71
72
  dig("pos").to_i
72
73
  end
73
74
 
75
+ # 是否正在持仓
76
+ def open?
77
+ pos > 0
78
+ end
79
+
80
+ # 已经平仓
81
+ def closed?
82
+ pos == 0
83
+ end
84
+
74
85
  private
75
86
 
76
87
  def dig(key)
data/lib/okex/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module OKEX
2
- VERSION = "0.2.6"
2
+ VERSION = "0.3.2"
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.6
4
+ version: 0.3.2
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
@@ -99,15 +99,17 @@ files:
99
99
  - bin/console
100
100
  - bin/setup
101
101
  - lib/okex.rb
102
- - lib/okex/account_order.rb
102
+ - lib/okex/api_error.rb
103
103
  - lib/okex/api_v3.rb
104
104
  - lib/okex/api_v5.rb
105
+ - lib/okex/balance.rb
105
106
  - lib/okex/client.rb
106
107
  - lib/okex/coin.rb
107
108
  - lib/okex/coin/bitcoin.rb
109
+ - lib/okex/coin/usdt.rb
108
110
  - lib/okex/host.rb
111
+ - lib/okex/max_size.rb
109
112
  - lib/okex/order.rb
110
- - lib/okex/trade_order.rb
111
113
  - lib/okex/version.rb
112
114
  - okex.gemspec
113
115
  homepage: https://github.com/daqing/okex
@@ -1,4 +0,0 @@
1
- module OKEX
2
- class AccountOrder < Order
3
- end
4
- end
@@ -1,24 +0,0 @@
1
- module OKEX
2
- class TradeOrder < Order
3
-
4
- # 预估强平价
5
- def est_liq_price
6
- dig("liqPx").to_f
7
- end
8
-
9
- # 最新成交价
10
- def last_price
11
- dig("fillPx").to_f
12
- end
13
-
14
- # 未实现收益
15
- def upl
16
- dig("pnl").to_f
17
- end
18
-
19
- # 持仓张数
20
- def pos
21
- dig("sz").to_i
22
- end
23
- end
24
- end