Class: BinanceAPI::REST

Inherits:
Base
  • Object
show all
Defined in:
lib/binance_api/rest.rb

Constant Summary

Constants inherited from Base

Base::BASE_URL

Instance Method Summary collapse

Instance Method Details

#account(options = {}) ⇒ Object



237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
# File 'lib/binance_api/rest.rb', line 237

def (options = {})
  recv_window = options.delete(:recv_window) || BinanceAPI.recv_window
  timestamp = options.delete(:timestamp) || Time.now

  params = {
      recvWindow: recv_window,
      timestamp: timestamp.to_i * 1000 # to milliseconds
  }

  response = safe do
    RestClient.get "#{BASE_URL}/api/v3/account",
                   params: params_with_signature(params, api_secret),
                   'X-MBX-APIKEY' => api_key
  end

  build_result response
end

#aggregate_trades_list(symbol, from_id: nil, start_time: nil, end_time: nil, limit: 500) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/binance_api/rest.rb', line 46

def aggregate_trades_list(symbol, from_id: nil, start_time: nil, end_time: nil, limit: 500)
  params = begin
    { symbol: symbol, limit: limit }.tap do |_params|
      _params[:fromId] = from_id unless from_id.nil?
      _params[:startTime] = start_time unless start_time.nil?
      _params[:endTime] = end_time unless end_time.nil?
    end
  end
  response = safe { RestClient.get("#{BASE_URL}/api/v1/aggTrades", params: params) }
  build_result response
end

#all_orders(symbol, options = {}) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# File 'lib/binance_api/rest.rb', line 214

def all_orders(symbol, options = {})
  recv_window = options.delete(:recv_window) || BinanceAPI.recv_window
  timestamp = options.delete(:timestamp) || Time.now
  limit = options.delete(:limit) || 500

  params = {
      symbol: symbol,
      orderId: options.fetch(:order_id, nil),
      limit: limit,
      recvWindow: recv_window,
      timestamp: timestamp.to_i * 1000 # to milliseconds
  }

  response = safe do
    RestClient.get "#{BASE_URL}/api/v3/allOrders",
                   params: params_with_signature(params, api_secret),
                   'X-MBX-APIKEY' => api_key
  end

  build_result response
end

#cancel_order(symbol, options = {}) ⇒ Object



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/binance_api/rest.rb', line 173

def cancel_order(symbol, options = {})
  recv_window = options.delete(:recv_window) || BinanceAPI.recv_window
  timestamp = options.delete(:timestamp) || Time.now

  params = {
      symbol: symbol,
      orderId: options.fetch(:order_id, nil),
      origClientOrderId: options.fetch(:orig_client_order_id, nil),
      newClientOrderId: options.fetch(:new_client_order_id, nil),
      recvWindow: recv_window,
      timestamp: timestamp.to_i * 1000 # to milliseconds
  }

  response = safe do
    RestClient.delete "#{BASE_URL}/api/v3/order",
                      params: params_with_signature(params, api_secret),
                      'X-MBX-APIKEY' => api_key
  end

  build_result response
end

#close_user_data_stream(listen_key) ⇒ Object



294
295
296
297
298
299
300
301
# File 'lib/binance_api/rest.rb', line 294

def close_user_data_stream(listen_key)
  response = safe do
    RestClient.delete "#{BASE_URL}/api/v1/userDataStream",
                      params: {listenKey: listen_key},
                      'X-MBX-APIKEY' => api_key
  build_result response
  end
end

#depth(symbol, limit: 100) ⇒ Object



25
26
27
28
# File 'lib/binance_api/rest.rb', line 25

def depth(symbol, limit: 100)
  response = safe { RestClient.get("#{BASE_URL}/api/v1/depth", params: { symbol: symbol, limit: limit }) }
  build_result response
end

#exchange_infoObject



20
21
22
23
# File 'lib/binance_api/rest.rb', line 20

def exchange_info
  response = safe { RestClient.get("#{BASE_URL}/api/v1/exchangeInfo") }
  build_result response
end

#get_order(symbol, options = {}) ⇒ Object



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/binance_api/rest.rb', line 152

def get_order(symbol, options = {})
  recv_window = options.delete(:recv_window) || BinanceAPI.recv_window
  timestamp = options.delete(:timestamp) || Time.now

  params = {
      symbol: symbol,
      orderId: options.fetch(:order_id, nil),
      origClientOrderId: options.fetch(:orig_client_order_id, nil),
      recvWindow: recv_window,
      timestamp: timestamp.to_i * 1000 # to milliseconds
  }

  response = safe do
    RestClient.get "#{BASE_URL}/api/v3/order",
                   params: params_with_signature(params, api_secret),
                   'X-MBX-APIKEY' => api_key
  end

  build_result response
end

#historical_trades(symbol, limit: 500, from_id: nil) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/binance_api/rest.rb', line 35

def historical_trades(symbol, limit: 500, from_id: nil)
  params = { symbol: symbol, limit: limit }
  params = params.merge(fromId: from_id) unless from_id.nil?
  response = safe do
    RestClient.get "#{BASE_URL}/api/v1/historicalTrades",
                   params: params,
                   'X-MBX-APIKEY' => api_key
  end
  build_result response
end

#keep_alive_user_data_stream(listen_key) ⇒ Object



285
286
287
288
289
290
291
292
# File 'lib/binance_api/rest.rb', line 285

def keep_alive_user_data_stream(listen_key)
  response = safe do
    RestClient.put "#{BASE_URL}/api/v1/userDataStream",
                   {listenKey: listen_key},
                   'X-MBX-APIKEY' => api_key
  end
  build_result response
end

#klines(symbol, interval, start_time: nil, end_time: nil, limit: 500) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/binance_api/rest.rb', line 58

def klines(symbol, interval, start_time: nil, end_time: nil, limit: 500)
  params = begin
    { symbol: symbol, interval: interval, limit: limit }.tap do |_params|
      _params[:startTime] = start_time unless start_time.nil?
      _params[:endTime] = end_time unless end_time.nil?
    end
  end

  response = safe { RestClient.get("#{BASE_URL}/api/v1/klines", params: params) }
  build_result response
end

#my_trades(symbol, options = {}) ⇒ Object



255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'lib/binance_api/rest.rb', line 255

def my_trades(symbol, options = {})
  recv_window = options.delete(:recv_window) || BinanceAPI.recv_window
  timestamp = options.delete(:timestamp) || Time.now
  limit = options.delete(:limit) || 500
  params = {
      symbol: symbol,
      limit: limit,
      fromId: options.fetch(:from_id, nil),
      recvWindow: recv_window,
      timestamp: timestamp.to_i * 1000 # to milliseconds
  }

  response = safe do
    RestClient.get "#{BASE_URL}/api/v3/myTrades",
                   params: params_with_signature(params, api_secret),
                   'X-MBX-APIKEY' => api_key
  end

  build_result response
end

#open_orders(symbol, options = {}) ⇒ Object



195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/binance_api/rest.rb', line 195

def open_orders(symbol, options = {})
  recv_window = options.delete(:recv_window) || BinanceAPI.recv_window
  timestamp = options.delete(:timestamp) || Time.now

  params = {
      symbol: symbol,
      recvWindow: recv_window,
      timestamp: timestamp.to_i * 1000 # to milliseconds
  }

  response = safe do
    RestClient.get "#{BASE_URL}/api/v3/openOrders",
                   params: params_with_signature(params, api_secret),
                   'X-MBX-APIKEY' => api_key
  end

  build_result response
end

#order(symbol, side, type, quantity, options = {}) ⇒ Object

symbol: 'LTCBTC',
side: 'BUY',
type: 'LIMIT',
timeInForce: 'GTC',
quantity: 1,
price: 0.1,
recvWindow: BinanceAPI.recv_window,
timestamp: 1499827319559



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/binance_api/rest.rb', line 96

def order(symbol, side, type, quantity, options = {})
  recv_window = options.delete(:recv_window) || BinanceAPI.recv_window
  timestamp = options.delete(:timestamp) || Time.now

  params = {
      symbol: symbol,
      side: side,
      type: type,
      timeInForce: options.fetch(:time_in_force, nil),
      quantity: quantity,
      price: options.fetch(:price, nil),
      newClientOrderId: options.fetch(:new_client_order_id, nil),
      stopPrice: options.fetch(:stop_price, nil),
      icebergQty: options.fetch(:iceberg_qty, nil),
      newOrderRespType: options.fetch(:new_order_resp_type, nil),
      recvWindow: recv_window,
      timestamp: timestamp.to_i * 1000 # to milliseconds
  }

  response = safe do
    RestClient.post "#{BASE_URL}/api/v3/order",
                    params_with_signature(params, api_secret),
                    'X-MBX-APIKEY' => api_key
  end

  build_result response
end

#order_test(symbol, side, type, quantity, options = {}) ⇒ Object



124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/binance_api/rest.rb', line 124

def order_test(symbol, side, type, quantity, options = {})
  recv_window = options.delete(:recv_window) || BinanceAPI.recv_window
  timestamp = options.delete(:timestamp) || Time.now

  params = {
    symbol: symbol,
    side: side,
    type: type,
    timeInForce: options.fetch(:time_in_force, nil),
    quantity: quantity,
    price: options.fetch(:price, nil),
    newClientOrderId: options.fetch(:new_client_order_id, nil),
    stopPrice: options.fetch(:stop_price, nil),
    icebergQty: options.fetch(:iceberg_qty, nil),
    newOrderRespType: options.fetch(:new_order_resp_type, nil),
    recvWindow: recv_window,
    timestamp: timestamp.to_i * 1000 # to milliseconds
  }

  response = safe do
    RestClient.post "#{BASE_URL}/api/v3/order/test",
                    params_with_signature(params, api_secret),
                    'X-MBX-APIKEY' => api_key
  end

  build_result response
end

#pingObject



10
11
12
13
# File 'lib/binance_api/rest.rb', line 10

def ping
  response = safe { RestClient.get("#{BASE_URL}/api/v1/ping") }
  build_result response
end

#server_timeObject



15
16
17
18
# File 'lib/binance_api/rest.rb', line 15

def server_time
  response = safe { RestClient.get("#{BASE_URL}/api/v1/time") }
  build_result response
end

#start_user_data_streamObject



276
277
278
279
280
281
282
283
# File 'lib/binance_api/rest.rb', line 276

def start_user_data_stream
  response = safe do
    RestClient.post "#{BASE_URL}/api/v1/userDataStream",
                    {},
                    'X-MBX-APIKEY' => api_key
  end
  build_result response
end

#ticker_24hr(symbol) ⇒ Object



70
71
72
73
# File 'lib/binance_api/rest.rb', line 70

def ticker_24hr(symbol)
  response = safe { RestClient.get("#{BASE_URL}/api/v1/ticker/24hr", params: { symbol: symbol }) }
  build_result response
end

#ticker_book(symbol) ⇒ Object



80
81
82
83
# File 'lib/binance_api/rest.rb', line 80

def ticker_book(symbol)
  response = safe { RestClient.get("#{BASE_URL}/api/v3/ticker/bookTicker", params: { symbol: symbol }) }
  build_result response
end

#ticker_price(symbol) ⇒ Object



75
76
77
78
# File 'lib/binance_api/rest.rb', line 75

def ticker_price(symbol)
  response = safe { RestClient.get("#{BASE_URL}/api/v3/ticker/price", params: { symbol: symbol }) }
  build_result response
end

#trades(symbol, limit: 500) ⇒ Object



30
31
32
33
# File 'lib/binance_api/rest.rb', line 30

def trades(symbol, limit: 500)
  response = safe { RestClient.get("#{BASE_URL}/api/v1/trades", params: { symbol: symbol, limit: limit }) }
  build_result response
end