binance.ex

Hex.pm VersionHex DocsHex.pm Download Total

Unofficial Elixir wrapper for interacting with the Binance API.

NOTE: v2 is a full rewrite that's now fully auto-generated from the Binance docs. There are breaking changes, and it's not as battle-tested as v1 yet. If you don't need any of the new endpoints, stick to v1 for now.

Installation

  1. The package can be installed by adding binance to your list of dependencies in mix.exs:
def deps do
  [
    {:binance, "~> 2.0.1"}
  ]
end
  1. Add your Binance API credentials to your config.exs file, like so (you can create a new API key here):
config :binance,
  api_key: "xxx",
  secret_key: "xxx",
  end_point: "https://api.binance.us" # Add for the US API end point. The default is for "https://api.binance.com"

Features

iex(24)> h Binance.Trade.post_order

                 def post_order(symbol, side, type, opts \\ [])

  @spec post_order(any(), any(), any(),
          recvWindow: any(),
          newOrderRespType: any(),
          icebergQty: any(),
          trailingDelta: any(),
          stopPrice: any(),
          strategyType: any(),
          strategyId: any(),
          newClientOrderId: any(),
          price: any(),
          quoteOrderQty: any(),
          quantity: any(),
          timeInForce: any(),
          timestamp: any()
        ) :: {:ok, any()} | {:error, any()}

New Order (TRADE)

Send in a new order.

  • LIMIT_MAKER are LIMIT orders that will be rejected if they would
    immediately match and trade as a taker.
  • STOP_LOSS and TAKE_PROFIT will execute a MARKET order when the
    stopPrice is reached.
  • Any LIMIT or LIMIT_MAKER type order can be made an iceberg order by
    sending an icebergQty.
  • Any order with an icebergQty MUST have timeInForce set to GTC.
  • MARKET orders using quantity specifies how much a user wants to buy or
    sell based on the market price.
  • MARKET orders using quoteOrderQty specifies the amount the user wants
    to spend (when buying) or receive (when selling) of the quote asset; the
    correct quantity will be determined based on the market liquidity and
    quoteOrderQty.
  • MARKET orders using quoteOrderQty will not break LOT_SIZE filter rules;
    the order will execute a quantity that will have the notional value as
    close as possible to quoteOrderQty.
  • same newClientOrderId can be accepted only when the previous one is
    filled, otherwise the order will be rejected.

Trigger order price rules against market price for both MARKET and LIMIT
versions:

  • Price above market price: STOP_LOSS BUY, TAKE_PROFIT SELL
  • Price below market price: STOP_LOSS SELL, TAKE_PROFIT BUY

Weight(IP): 1

Details:

  • METHOD: post
  • URL: /api/v3/order

Mandatory params:

  • symbol - Trading symbol, e.g. BNBUSDT
  • side -
  • type - Order type

Optional params:

  • timestamp - timestamp
  • timeInForce - Order time in force
  • quantity - Order quantity
  • quoteOrderQty - Quote quantity
  • price - Order price
  • newClientOrderId - A unique id among open orders. Automatically
    generated if not sent.
  • strategyId -
  • strategyType - The value cannot be less than 1000000
  • stopPrice - Used with STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, and
    TAKE_PROFIT_LIMIT orders.
  • trailingDelta - Used with STOP_LOSS, STOP_LOSS_LIMIT, TAKE_PROFIT, and
    TAKE_PROFIT_LIMIT orders.
  • icebergQty - Used with LIMIT, STOP_LOSS_LIMIT, and TAKE_PROFIT_LIMIT to
    create an iceberg order.
  • newOrderRespType - Set the response JSON. MARKET and LIMIT order types
    default to FULL, all other orders default to ACK.
  • recvWindow - The value cannot be greater than 60000

Supported endpoints

Group: BLVT

Group: BSwap

Group: C2C

Group: Convert

Group: Crypto Loans

Group: Fiat

Group: Futures

Group: Futures Algo

Group: Gift Card

Group: Margin

Group: Market

Group: Mining

Group: NFT

Group: Pay

Group: Portfolio Margin

Group: Rebate

Group: Savings

Group: Staking

Group: Sub-Account

Group: Trade

Group: User Data Stream

Group: Wallet

Group: VIP Loans

Caveats

Usage

Documentation available at https://hexdocs.pm/binance (make sure you have 2.0.0 selected)

Get all prices

iex> Binance.Market.get_ticker_price
{:ok,
 [
   %Binance.Structs.SymbolPrice{symbol: "ETHBTC", price: "0.06275000"},
   %Binance.Structs.SymbolPrice{symbol: "LTCBTC", price: "0.00363100"},
   %Binance.Structs.SymbolPrice{symbol: "BNBBTC", price: "0.00804100"},
   %Binance.Structs.SymbolPrice{symbol: "NEOBTC", price: "0.00031420"},
   %Binance.Structs.SymbolPrice{symbol: "QTUMETH", price: "0.00147800"},
   %Binance.Structs.SymbolPrice{symbol: "EOSETH", price: "0.00039460"},
   %Binance.Structs.SymbolPrice{symbol: "SNTETH", price: "0.00001240"},
   %Binance.Structs.SymbolPrice{symbol: "BNTETH", price: "0.00020200"},
   %Binance.Structs.SymbolPrice{symbol: "BCCBTC", price: "0.07908100"},
   %Binance.Structs.SymbolPrice{symbol: "GASBTC", price: "0.00008800"},
   %Binance.Structs.SymbolPrice{symbol: "BNBETH", price: "0.12810000"},
   %Binance.Structs.SymbolPrice{symbol: "BTCUSDT", price: "30526.74000000"},
   %Binance.Structs.SymbolPrice{symbol: "ETHUSDT", price: "1915.16000000"},
   ...
 ]}

Buy 0.01 BNB for the current market price

iex> Binance.Trade.post_order "BNBUSDT", "BUY", "MARKET", quantity: 0.01

Earn

Get all earn positions

iex> Binance.Staking.get_staking_position "STAKING"

Get all earn products

iex> Binance.Staking.get_staking_product_list "STAKING"

Redeem all flexible staked assets

with {:ok, positions} <- Binance.Savings.get_lending_daily_token_position("") do
  positions
  |> Enum.map(
    &Binance.Savings.post_lending_daily_redeem(&1.productId, &1.totalAmount, "FAST"
    )
  )
end

License

MIT