Binance (binance v0.9.0)

Link to this section Summary

Functions

Closes/disables the listen key. To be used when you stop listening to the stream.

Creates a socket listen key that later can be used as parameter to listen for user related events.

Searches and normalizes the symbol as it is listed on binance.

Fetches user account from binance

Get all symbols and current prices listed in binance

Retrieves the bids & asks of the order book up to the depth for the given symbol

Retrieves klines for a symbol, provided a given interval, e.g. "1h".

Get all open orders, alternatively open orders by symbol

Get order by symbol, timestamp and either orderId or origClientOrderId are mandatory

Get binance server time in unix epoch.

Retrieves the current ticker information for the given trade pair.

Socket listen key expires after 30 minutes withouth a pong response, this allows keeping it alive.

Creates a new market buy order

Creates a new market sell order

Pings binance API. Returns {:ok, %{}} if successful, {:error, reason} otherwise

Link to this section Functions

Link to this function

cancel_order(symbol, timestamp, order_id \\ nil, orig_client_order_id \\ nil, new_client_order_id \\ nil, recv_window \\ nil)

Cancel an active order..

Symbol and either orderId or origClientOrderId must be sent.

Returns {:ok, %Binance.Order{}} or {:error, reason}.

Weight: 1

Info: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#cancel-order-trade

Link to this function

close_listen_key(key)

Closes/disables the listen key. To be used when you stop listening to the stream.

Returns {:ok, %{}} or {:error, reason}.

For more context please read https://github.com/binance/binance-spot-api-docs/blob/master/user-data-stream.md#close-a-listenkey

Link to this function

create_listen_key()

Creates a socket listen key that later can be used as parameter to listen for user related events.

Returns {:ok, %Binance.DataStream{}} or {:error, reason}.

Example

{:ok,
  %Binance.DataStream{
    listen_key: "pqia91ma19a5s61cv6a81va65sdf19v8a65a1a5s61cv6a81va65sdf19v8a65a1"
  }
}

For more context please read https://github.com/binance/binance-spot-api-docs/blob/master/user-data-stream.md#create-a-listenkey

Link to this function

create_order(symbol, side, type, quantity, price \\ nil, time_in_force \\ nil, new_client_order_id \\ nil, stop_price \\ nil, iceberg_quantity \\ nil, receiving_window \\ 1000, timestamp \\ nil)

Creates a new order on binance

Returns {:ok, %{}} or {:error, reason}.

In the case of a error on binance, for example with invalid parameters, {:error, {:binance_error, %{code: code, msg: msg}}} will be returned.

Please read https://www.binance.com/restapipub.html#user-content-account-endpoints to understand all the parameters

Link to this function

fetch_order(symbol, timestamp, order_id, orig_client_order_id, recv_window)

Link to this function

find_symbol(tp)

Searches and normalizes the symbol as it is listed on binance.

To retrieve this information, a request to the binance API is done. The result is then cached to ensure the request is done only once.

Order of which symbol comes first, and case sensitivity does not matter.

Returns {:ok, "SYMBOL"} if successfully, or {:error, reason} otherwise.

Examples

These 3 calls will result in the same result string:

find_symbol(%Binance.TradePair{from: "ETH", to: "REQ"})
find_symbol(%Binance.TradePair{from: "REQ", to: "ETH"})
find_symbol(%Binance.TradePair{from: "rEq", to: "eTH"})

Result: {:ok, "REQETH"}

Fetches user account from binance

Returns {:ok, %Binance.Account{}} or {:error, reason}.

In the case of a error on binance, for example with invalid parameters, {:error, {:binance_error, %{code: code, msg: msg}}} will be returned.

Please read https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#account-information-user_data to understand API

Link to this function

get_all_prices()

Get all symbols and current prices listed in binance

Returns {:ok, [%Binance.SymbolPrice{}]} or {:error, reason}.

Example

{:ok,
  [%Binance.SymbolPrice{price: "0.07579300", symbol: "ETHBTC"},
   %Binance.SymbolPrice{price: "0.01670200", symbol: "LTCBTC"},
   %Binance.SymbolPrice{price: "0.00114550", symbol: "BNBBTC"},
   %Binance.SymbolPrice{price: "0.00640000", symbol: "NEOBTC"},
   %Binance.SymbolPrice{price: "0.00030000", symbol: "123456"},
   %Binance.SymbolPrice{price: "0.04895000", symbol: "QTUMETH"},
   ...]}
Link to this function

get_depth(symbol, limit)

Retrieves the bids & asks of the order book up to the depth for the given symbol

Returns {:ok, %{bids: [...], asks: [...], lastUpdateId: 12345}} or {:error, reason}

Example

{:ok,
  %Binance.OrderBook{
    asks: [
      ["8400.00000000", "2.04078100", []],
      ["8405.35000000", "0.50354700", []],
      ["8406.00000000", "0.32769800", []],
      ["8406.33000000", "0.00239000", []],
      ["8406.51000000", "0.03241000", []]
    ],
    bids: [
      ["8393.00000000", "0.20453200", []],
      ["8392.57000000", "0.02639000", []],
      ["8392.00000000", "1.40893300", []],
      ["8390.09000000", "0.07047100", []],
      ["8388.72000000", "0.04577400", []]
    ],
    last_update_id: 113634395
  }
}
Link to this function

get_exchange_info()

Link to this function

get_historical_trades(symbol, limit, from_id)

Get historical trades https://binance-docs.github.io/apidocs/spot/en/#old-trade-lookup

Returns {:ok, [%Binance.HistoricalTrade{}]} or {:error, reason}.

Example

{:ok, [%Binance.HistoricalTrade{ id: 192180149, is_best_match: true, is_buyer_maker: true, price: "1.79878000", qty: "55.50000000", quote_qty: "99.83229000", time: 1618341167715 }] }

Link to this function

get_klines(symbol, interval, limit \\ 500)

Retrieves klines for a symbol, provided a given interval, e.g. "1h".

Function can also take in a 'limit' argument to reduce the number of intervals.

Returns {:ok, [%Binance.Kline{}] or {:error, reason}

Example

{:ok,
[
 %Binance.Kline{
   close: "0.16527000",
   close_time: 1617861599999,
   high: "0.17100000",
   ignore: "0",
   low: "0.16352000",
   number_of_trades: 16167,
   open: "0.17088000",
   open_time: 1617858000000,
   quote_asset_volume: "7713624.32966000",
   taker_buy_base_asset_volume: "22020677.70000000",
   taker_buy_quote_asset_volume: "3668705.43042700",
   volume: "46282422.20000000"
 },
 %Binance.Kline{
 ...
Link to this function

get_open_orders()

Get all open orders, alternatively open orders by symbol

Returns {:ok, [%Binance.Order{}]} or {:error, reason}.

Weight: 1 for a single symbol; 40 when the symbol parameter is omitted

Example

{:ok,
  [%Binance.Order{price: "0.1", origQty: "1.0", executedQty: "0.0", ...},
   %Binance.Order{...},
   %Binance.Order{...},
   %Binance.Order{...},
   %Binance.Order{...},
   %Binance.Order{...},
   ...]}
Link to this function

get_open_orders(symbol)

Link to this function

get_order(symbol, timestamp, order_id \\ nil, orig_client_order_id \\ nil, recv_window \\ nil)

Get order by symbol, timestamp and either orderId or origClientOrderId are mandatory

Returns {:ok, [%Binance.Order{}]} or {:error, reason}.

Weight: 1

Example

{:ok, %Binance.Order{price: "0.1", origQty: "1.0", executedQty: "0.0", ...}}

Info: https://github.com/binance-exchange/binance-official-api-docs/blob/master/rest-api.md#query-order-user_data

Link to this function

get_server_time()

Get binance server time in unix epoch.

Returns {:ok, time} if successful, {:error, reason} otherwise

Example

{:ok, 1515390701097}
Link to this function

get_ticker(symbol)

Retrieves the current ticker information for the given trade pair.

Symbol can be a binance symbol in the form of "ETHBTC" or %Binance.TradePair{}.

Returns {:ok, %Binance.Ticker{}} or {:error, reason}

Example

{:ok,
  %Binance.Ticker{ask_price: "0.07548800", bid_price: "0.07542100",
    close_time: 1515391124878, count: 661676, first_id: 16797673,
    high_price: "0.07948000", last_id: 17459348, last_price: "0.07542000",
    low_price: "0.06330000", open_price: "0.06593800", open_time: 1515304724878,
    prev_close_price: "0.06593800", price_change: "0.00948200",
    price_change_percent: "14.380", volume: "507770.18500000",
    weighted_avg_price: "0.06946930"}}
Link to this function

keep_alive_listen_key(key)

Socket listen key expires after 30 minutes withouth a pong response, this allows keeping it alive.

Returns {:ok, %{}} or {:error, reason}.

For more context please read https://github.com/binance/binance-spot-api-docs/blob/master/user-data-stream.md#pingkeep-alive-a-listenkey

Link to this function

order_limit_buy(symbol, quantity, price, time_in_force \\ "GTC")

Creates a new limit buy order

Symbol can be a binance symbol in the form of "ETHBTC" or %Binance.TradePair{}.

Returns {:ok, %{}} or {:error, reason}

Link to this function

order_limit_sell(symbol, quantity, price, time_in_force \\ "GTC")

Creates a new limit sell order

Symbol can be a binance symbol in the form of "ETHBTC" or %Binance.TradePair{}.

Returns {:ok, %{}} or {:error, reason}

Link to this function

order_market_buy(symbol, quantity)

Creates a new market buy order

Symbol can be a binance symbol in the form of "ETHBTC" or %Binance.TradePair{}.

Returns {:ok, %{}} or {:error, reason}

Link to this function

order_market_sell(symbol, quantity)

Creates a new market sell order

Symbol can be a binance symbol in the form of "ETHBTC" or %Binance.TradePair{}.

Returns {:ok, %{}} or {:error, reason}

Pings binance API. Returns {:ok, %{}} if successful, {:error, reason} otherwise