class BinanceAPI::REST

Public Instance Methods

account(options = {}) click to toggle source
# File lib/binance_api/rest.rb, line 237
def account(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) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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_info() click to toggle source
# 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 = {}) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source
# 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 = {}) click to toggle source

{

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

}

# 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 = {}) click to toggle source
# 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
ping() click to toggle source
# File lib/binance_api/rest.rb, line 10
def ping
  response = safe { RestClient.get("#{BASE_URL}/api/v1/ping") }
  build_result response
end
server_time() click to toggle source
# 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_stream() click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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

Protected Instance Methods

build_result(response) click to toggle source
# File lib/binance_api/rest.rb, line 305
def build_result(response)
  json = JSON.parse(response.body, symbolize_names: true)
  BinanceAPI::Result.new(json, response.code == 200)
end