okex 0.1.20 → 0.2.5

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: 75fda5af2580d3fa8aa86fef858ce7ab31872d73a7b2819e9ab8f6e0991760ac
4
- data.tar.gz: 924019880495be349930339802e0dc7c8e2a6876f16653ae605ead7c63389c8e
3
+ metadata.gz: 6e79f7f281cc49fecf294441cfc91c8a0f7e065204a69fae02c968f48b5fc83a
4
+ data.tar.gz: 802b3412745cc52ed7989bf6e16d9e6e61a9d5c962446bb81b29182abcaeeab0
5
5
  SHA512:
6
- metadata.gz: 174b2e94fb591fd9bf89a2c0c08b19f8932c414c6be66488010320bacf751438310feac5bf3fe005e4a12287474a3a8b4c2465840b6aa40c6cd8cb4eae34883e
7
- data.tar.gz: cbc2d4163fd749e194325afbf8cc1e9386cef004b50e19a3ab1d90fe31c96a97f0c80928f7d833342826f0c8db4bdc6f8096d7d5f1b84757e88a965a1cece606
6
+ metadata.gz: 1f00ee33cb15494a908dba476c5fb1b3eaeea37b3b313bfb5d47a95237ac6117b83b6adae1790bbf62ece997b4f972eb98d77a6326dbb3899da4bb116d96cc06
7
+ data.tar.gz: 6a2ddad58a09dfdb945babd65cc78022c427bb6147df57f5c38b5e74438ebf16e75f2746287da0931abfb4e0f643077aeee984f7920ea5954ca47aa75a548783
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- okex (0.1.20)
4
+ okex (0.2.3)
5
5
  faraday (~> 1.5)
6
6
 
7
7
  GEM
data/lib/okex.rb CHANGED
@@ -1,11 +1,10 @@
1
1
  require "okex/version"
2
- require "okex/api"
2
+ require "okex/api_v3"
3
+ require "okex/api_v5"
3
4
  require "okex/client"
4
5
  require "okex/coin"
5
- require "openssl"
6
- require "faraday"
7
-
8
- OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
6
+ require "okex/order"
7
+ require "okex/host"
9
8
 
10
9
  module OKEX
11
10
  class Error < StandardError; end
@@ -0,0 +1,46 @@
1
+ class OKEX::ApiV3
2
+ def initialize(client)
3
+ @client = client
4
+ end
5
+
6
+ def balance(coin)
7
+ client.get("/api/account/v3/wallet/#{coin.code}")
8
+ end
9
+
10
+ def instruments
11
+ client.get("/api/swap/v3/instruments")
12
+ end
13
+
14
+ # 市价做空永续合约
15
+ # amount: 仓位开多少张
16
+ def short_swap(instrument_id, amount)
17
+ param = {
18
+ instrument_id: instrument_id.to_s,
19
+ size: amount.to_s,
20
+ type: "2",
21
+ order_type: "4",
22
+ }
23
+
24
+ client.post("/api/swap/v3/order", param)
25
+ end
26
+
27
+ # 市价平多
28
+ def close_long(instrument_id)
29
+ close_position(instrument_id, "long")
30
+ end
31
+
32
+ # 市价平空
33
+ def close_short(instrument_id)
34
+ close_position(instrument_id, "short")
35
+ end
36
+
37
+ private
38
+
39
+ attr_reader :client
40
+
41
+ def close_position(instrument_id, direction)
42
+ param = {"instrument_id": instrument_id, "direction": direction}
43
+
44
+ client.post("/api/swap/v3/close_position", param)
45
+ end
46
+ end
@@ -0,0 +1,55 @@
1
+ class OKEX::ApiV5
2
+ def initialize(client, host)
3
+ @client = client
4
+ @host = host
5
+ end
6
+
7
+ def instruments
8
+ client.get(host, "/api/v5/public/instruments?instType=SWAP")
9
+ end
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)}
16
+ end
17
+
18
+ []
19
+ end
20
+
21
+ def short_swap(instid, amount)
22
+ params = {
23
+ "instId": instid,
24
+ "tdMode": "cross",
25
+ "side": "sell",
26
+ "posSide": "short",
27
+ "ordType": "market",
28
+ "sz": "1"
29
+ }
30
+
31
+ client.post(host, "/api/v5/trade/order", params)
32
+ end
33
+
34
+ def close_long(instid)
35
+ close_position(instid, "long")
36
+ end
37
+
38
+ def close_short(instid)
39
+ close_position(instid, "short")
40
+ end
41
+
42
+ private
43
+
44
+ attr_reader :client, :host
45
+
46
+ def close_position(instid, direction)
47
+ params = {
48
+ "instId": instid,
49
+ "mgnMode": "cross",
50
+ "posSide": direction
51
+ }
52
+
53
+ client.post(host, "/api/v5/trade/close-position", params)
54
+ end
55
+ end
data/lib/okex/client.rb CHANGED
@@ -1,4 +1,7 @@
1
1
  require 'time'
2
+ require 'base64'
3
+ require 'json'
4
+ require 'faraday'
2
5
 
3
6
  module OKEX
4
7
  class Client
@@ -8,60 +11,26 @@ module OKEX
8
11
  @passphrase = passphrase
9
12
  end
10
13
 
11
- def balance(coin)
12
- _get("/api/account/v3/wallet/#{coin.code}")
13
- end
14
-
15
- # name: sub account name
16
- def sub_account(name)
17
- _get("/api/account/v3/sub-account?sub-account=#{name}")
18
- end
19
-
20
- def instruments
21
- _get("/api/swap/v3/instruments")
22
- end
23
-
24
- # 市价做空永续合约
25
- # amount: 仓位开多少张
26
- def short_swap(instrument_id, amount)
27
- param = {
28
- instrument_id: instrument_id.to_s,
29
- size: amount.to_s,
30
- type: "2",
31
- order_type: "4",
32
- }
33
-
34
- _post("/api/swap/v3/order", param)
35
- end
36
-
37
- private
38
-
39
- attr_reader :api_key, :api_secret, :passphrase
40
-
41
- HOST = "https://www.okex.com"
42
-
43
- def _get(path)
14
+ def get(host, path)
44
15
  ts = timestamp
45
16
  sig = sign(ts, "GET", path)
46
17
 
47
- _resp(::Faraday.get(HOST + path, nil, headers(ts, sig)))
18
+ _resp(Faraday.get(host + path, nil, headers(ts, sig)))
48
19
  end
49
20
 
50
- def _post(path, payload)
21
+ def post(host, path, payload)
51
22
  payload_json = gen_payload(payload)
52
23
 
53
24
  ts = timestamp
54
25
  sig = sign(ts, "POST", path + payload_json)
55
26
 
56
- begin
57
- conn = ::Faraday.new(HOST + path, :ssl => {:verify => false})
58
- result = conn.post(HOST + path, payload_json, headers(ts, sig))
59
- _resp(result)
60
- rescue => e
61
- puts "[error] #{e.inspect}"
62
- end
27
+ _resp(Faraday.post(host + path, payload_json, headers(ts, sig)))
63
28
  end
64
29
 
30
+ private
31
+
32
+ attr_reader :api_key, :api_secret, :passphrase
33
+
65
34
  def gen_payload(payload)
66
35
  m = payload.map { |k, v| JSON.generate({k => v}, {space: ' '})}
67
36
  m2 = m.join(", ").gsub("{", "").gsub("}", "")
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
data/lib/okex/order.rb ADDED
@@ -0,0 +1,52 @@
1
+ module OKEX
2
+ class Order
3
+ POS_LONG = 'long'.freeze
4
+ POS_SHORT = 'short'.freeze
5
+
6
+ def initialize(params)
7
+ @params = params
8
+ end
9
+
10
+ def instrument_id
11
+ dig("instId")
12
+ end
13
+
14
+ # 杠杆倍数
15
+ def leverage
16
+ dig("lever")
17
+ end
18
+
19
+ def created_at
20
+ Time.at(dig("cTime").to_i / 1000)
21
+ end
22
+
23
+ # 平均开仓价
24
+ def avg_open_price
25
+ dig("avgPx").to_f
26
+ end
27
+
28
+ # 预估强平价
29
+ def est_liq_price
30
+ dig("liqPx").to_f
31
+ end
32
+
33
+ # 持仓方向
34
+ def position_side
35
+ side = dig("posSide")
36
+ if [POS_SHORT, POS_LONG].include?(side)
37
+ return side
38
+ end
39
+ end
40
+
41
+ # 未实现收益
42
+ def upl
43
+ dig("upl").to_f
44
+ end
45
+
46
+ private
47
+
48
+ def dig(key)
49
+ @params[key]
50
+ end
51
+ end
52
+ end
data/lib/okex/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module OKEX
2
- VERSION = "0.1.20"
2
+ VERSION = "0.2.5"
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.1.20
4
+ version: 0.2.5
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-07 00:00:00.000000000 Z
11
+ date: 2021-07-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -85,10 +85,13 @@ files:
85
85
  - bin/console
86
86
  - bin/setup
87
87
  - lib/okex.rb
88
- - lib/okex/api.rb
88
+ - lib/okex/api_v3.rb
89
+ - lib/okex/api_v5.rb
89
90
  - lib/okex/client.rb
90
91
  - lib/okex/coin.rb
91
92
  - lib/okex/coin/bitcoin.rb
93
+ - lib/okex/host.rb
94
+ - lib/okex/order.rb
92
95
  - lib/okex/version.rb
93
96
  - okex.gemspec
94
97
  homepage: https://github.com/daqing/okex
data/lib/okex/api.rb DELETED
@@ -1,15 +0,0 @@
1
- module OKEX
2
- class API
3
- def initialize(client)
4
- @client = client
5
- end
6
-
7
- def method_missing(m, *args, &block)
8
- client.send(m, *args)
9
- end
10
-
11
- private
12
-
13
- attr_reader :client
14
- end
15
- end