Class: OKEX::ApiV5

Inherits:
Object
  • Object
show all
Defined in:
lib/okex/api_v5.rb

Instance Method Summary collapse

Constructor Details

#initialize(client, host) ⇒ ApiV5

Returns a new instance of ApiV5.



2
3
4
5
# File 'lib/okex/api_v5.rb', line 2

def initialize(client, host)
  @client = client
  @host = host
end

Instance Method Details

#algo_order_id(inst_id) ⇒ Object

查询当前止损订单的ID 可用于后续取消止损订单

Parameters:

  • inst_id (String)

    合约名称



135
136
137
138
139
140
141
142
# File 'lib/okex/api_v5.rb', line 135

def algo_order_id(inst_id)
  result = client.get(host, "/api/v5/trade/orders-algo-pending?instId=#{inst_id}&instType=SWAP&ordType=conditional")

  return "" if result.empty?
  return result[0]["algoId"] if result.size == 1

  raise "invalid result: #{result.inspect}"
end

#balance(ccy, round) ⇒ Object



22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/okex/api_v5.rb', line 22

def balance(ccy, round)
  data = client.get(host, "/api/v5/account/balance?ccy=#{ccy}")

  details = data[0]['details'][0]

  OKEX::Balance.new(
    details['ccy'],
    dig_round(details, 'cashBal', round),
    dig_round(details, 'avalEq', round),
    dig_round(details, 'frozenBal', round),
    dig_round(details, 'upl', round)
  )
end

#cancel_algo_order(algo_id, inst_id) ⇒ Object

撤销委托单

Parameters:

  • algo_id (String)

    策略委托单ID

  • inst_id (String)

    合约ID



147
148
149
150
151
152
153
154
# File 'lib/okex/api_v5.rb', line 147

def cancel_algo_order(algo_id, inst_id)
  params = {
    "algoId": algo_id,
    "instId": inst_id
  }

  client.post(host, "/api/v5/trade/cancel-algos", [params])
end

#close_long(instrument_id) ⇒ Object



62
63
64
# File 'lib/okex/api_v5.rb', line 62

def close_long(instrument_id)
  close_position(instrument_id, "long")
end

#close_short(instrument_id) ⇒ Object



66
67
68
# File 'lib/okex/api_v5.rb', line 66

def close_short(instrument_id)
  close_position(instrument_id, "short")
end

#current_leverage(inst_id) ⇒ Object



83
84
85
86
87
# File 'lib/okex/api_v5.rb', line 83

def current_leverage(inst_id)
  data = client.get(host, "/api/v5/account/leverage-info?instId=#{inst_id}&mgnMode=cross")

  data[0]['lever'].to_i
end

#instrumentsObject



7
8
9
# File 'lib/okex/api_v5.rb', line 7

def instruments
  client.get(host, "/api/v5/public/instruments?instType=SWAP")
end

#long_swap(instid, amount) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/okex/api_v5.rb', line 36

def long_swap(instid, amount)
  params = {
    "instId": instid,
    "tdMode": "cross",
    "side": "buy",
    "posSide": "long",
    "ordType": "market",
    "sz": amount.to_s,
  }

  client.post(host, "/api/v5/trade/order", params)
end

#max_size(instrument_id) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/okex/api_v5.rb', line 70

def max_size(instrument_id)
  data = client.get(host, "/api/v5/account/max-size?instId=#{instrument_id}&tdMode=cross")

  ret = OKEX::MaxSize.new(-1, -1)

  el = data[0]
  if el.present? && el['maxBuy'].to_i > 0 && el['maxSell'].to_i > 0
    ret = OKEX::MaxSize.new(el['maxBuy'].to_i, el['maxSell'].to_i)
  end

  ret
end

#orders(inst_id = nil) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/okex/api_v5.rb', line 11

def orders(inst_id=nil)
  url = "/api/v5/account/positions"
  if inst_id.present?
    url += "?instId=#{inst_id}"
  end

  data = client.get(host, url)

  data.map {|params| OKEX::Order.new(params)}
end

#set_leverage(inst_id, leverage) ⇒ Object



89
90
91
92
93
94
95
96
97
# File 'lib/okex/api_v5.rb', line 89

def set_leverage(inst_id, leverage)
  params = {
    "instId": inst_id,
    "lever": leverage,
    "mgnMode": "cross"
  }

  client.post(host, "/api/v5/account/set-leverage", params)
end

#set_stop_loss(inst_id, posSide, sz, price) ⇒ Object

设置止损价格

Parameters:

  • inst_id (String)

    合约名称

  • posSide (String)

    持仓方向

  • sz (Integer)

    持仓数量(张)

  • price (Float)

    止损价格



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/okex/api_v5.rb', line 104

def set_stop_loss(inst_id, posSide, sz, price)
  side = (posSide == OKEX::Order::POS_LONG) ? "sell" : "buy"

  params = {
    "instId": inst_id,
    "tdMode": "cross",
    "side": side,
    "posSide": posSide.to_s,
    "sz": sz.to_s,
    "ordType": "conditional",
    "slTriggerPx": price.to_s,
    "slOrdPx": "-1"
  }

  client.post(host, "/api/v5/trade/order-algo", params)
end

#short_swap(instid, amount) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/okex/api_v5.rb', line 49

def short_swap(instid, amount)
  params = {
    "instId": instid,
    "tdMode": "cross",
    "side": "sell",
    "posSide": "short",
    "ordType": "market",
    "sz": amount.to_s,
  }

  client.post(host, "/api/v5/trade/order", params)
end

#stop_loss_price(inst_id) ⇒ Object

查询当前设置的止损价格

Parameters:

  • inst_id (String)

    合约名称



123
124
125
126
127
128
129
130
# File 'lib/okex/api_v5.rb', line 123

def stop_loss_price(inst_id)
  result = client.get(host, "/api/v5/trade/orders-algo-pending?instId=#{inst_id}&instType=SWAP&ordType=conditional")

  return -1 if result.empty?
  return result[0]["slTriggerPx"].to_f if result.size == 1

  raise "invalid result: #{result.inspect}"
end