Dwarves.BinanceFutures (dwarves_binancex v0.1.18)

Link to this section Summary

Functions

Creates a batch orders on binance.Max 5 orders

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

Get all symbols and current prices listed in binance

get exchange info from binance

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

Link to this section Functions

Link to this function

cancel_all_open_orders(params, api_secret, api_key, is_testnet \\ false)

Cancel all open orders of symbol

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

Returns {:ok, %{ "code": "200", "msg": "The operation of cancel all open order is done." } } or {:error, reason}

Examples

cancel_all_open_orders(%{
    "symbol" => "BTCUSDT"
  },
  "api_secret",
  "api_key",
  true)

Result: {:ok, %{ "code": "200", "msg": "The operation of cancel all open order is done." } }

Link to this function

cancel_multi_orders(params, api_secret, api_key, is_testnet \\ false)

Cancel multi open orders of symbol

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

Please read https://binance-docs.github.io/apidocs/futures/en/#cancel-multiple-orders-trade to understand all the parameters

Returns

{:ok,
  [
    %{"code" => code, "msg" => msg},
    %Binance.Order{}
  ]
}

or

{:error, reason}

Examples

cancel_multi_orders(%{
    "symbol" => "BTCUSDT",
    "order_id_list" => [1234567,2345678]
  },
  "api_secret",
  "api_key",
  true)

Result:

{:ok,
  [
    %{"code" => -2011, "msg" => "Unknown order sent."},
    %{
      "avgPrice" => "0.00000",
      "clientOrderId" => "web_iVF8ONxrHn1Y1UXyGVUN",
      "closePosition" => false,
      "cumQty" => "0",
      "cumQuote" => "0",
      "executedQty" => "0",
      "orderId" => 3033608152,
      "origQty" => "0.002",
      "origType" => "LIMIT",
      "positionSide" => "BOTH",
      "price" => "40000",
      "priceProtect" => false,
      "reduceOnly" => false,
      "side" => "BUY",
      "status" => "CANCELED",
      "stopPrice" => "0",
      "symbol" => "BTCUSDT",
      "timeInForce" => "GTC",
      "type" => "LIMIT",
      "updateTime" => 1650433385291,
      "workingType" => "CONTRACT_PRICE"
    }
  ]}
Link to this function

cancel_order(params, api_secret, api_key, is_testnet \\ false)

Cancel an order

Symbol can be a binance symbol in the form of "ETHBTC" or %Binance.TradePair{}. Either order_id or orig_client_order_id must be sent.

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

Examples

cancel_order(%{
  "symbol" => "BTCUSDT",
  "order_id" => 39334117676
  },
  "api_secret",
  "api_key",
  true)

Result: {:ok, %{ "avgPrice" => "0.00000", "clientOrderId" => "web_SadTLJTOB5f85NPLxaRo", "closePosition" => false, "cumQty" => "0", "cumQuote" => "0", "executedQty" => "0", "orderId" => 39334117676, "origQty" => "0.015", "origType" => "LIMIT", "positionSide" => "BOTH", "price" => "40000", "priceProtect" => false, "reduceOnly" => false, "side" => "BUY", "status" => "CANCELED", "stopPrice" => "0", "symbol" => "BTCUSDT", "timeInForce" => "GTC", "type" => "LIMIT", "updateTime" => 1640710829584, "workingType" => "CONTRACT_PRICE" }}

Link to this function

change_leverage(params, api_secret, api_key, is_testnet \\ false)

Change initial leverage

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

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

Examples

change_leverage(%{
  "symbol" => "BTCUSDT", "leverage" => 5},
  "api_secret",
  "api_key",
  true)

Result: {:ok, %{"leverage" => 5, "maxNotionalValue" => "50000000", "symbol" => "BTCUSDT"}}

Link to this function

create_batch_orders(params, api_secret, api_key, is_testnet \\ false)

Creates a batch orders on binance.Max 5 orders

Returns {:ok, [%{order} or %{code: code, msg: msg}]}.

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://binance-docs.github.io/apidocs/futures/en/#place-multiple-orders-trade to understand all the parameters

Examples

create_batch_orders(%{
    "batch_orders" => [
      %{"symbol" => "BTCUSDT", "quantity" => 1, "side" => "BUY", "type" => "MARKET"},
      %{"symbol" => "ETHUSDT", "quantity" => 1, "side" => "BUY", "type" => "MARKET"}
    ]
  },
  "api_secret",
  "api_key",
  true)

Result:

{:ok,
[
 %{"code" => -2019, "msg" => "Margin is insufficient."},
 %{
   "orderId" => 809666629,
   "origQty" => "1",
   "origType" => "MARKET",
   "positionSide" => "BOTH",
   "price" => "0",
   "side" => "BUY",
   "status" => "NEW",
   "symbol" => "ETHUSDT",
   "type" => "MARKET",
   ...
 }
]}
Link to this function

create_order(params, api_secret, api_key, is_testnet \\ false)

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

Examples

create_order(
  %{"symbol" => "BTCUSDT", "quantity" => 1, "side" => "BUY", "type" => "MARKET"},
  "api_secret",
  "api_key",
  true)

Result:

{:ok,
 %{
   "orderId" => 809666629,
   "origQty" => "1",
   "origType" => "MARKET",
   "positionSide" => "BOTH",
   "price" => "0",
   "side" => "BUY",
   "status" => "NEW",
   "symbol" => "BTCUSDT",
   "type" => "MARKET",
   ...
 }
}
or
{:error, {:binance_error, %{code: -2019, msg: "Margin is insufficient."}}}
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"}

Link to this function

get_account_info(api_key, secret_key, is_testnet \\ false)

get account info from 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://binance-docs.github.io/apidocs/futures/en/#account-information-v2-user_data to understand all the parameters

Examples

get_account_info("api_key", "api_secret", true)

Result:

{:ok,
 %{
    "timezone": "UTC",
    "serverTime": 1640141838081,
    "futuresType": "U_MARGINED",
    "symbols": [...],
    ...
 }
}
or
{:error, {:binance_error, %{code: -2019, msg: "Margin is insufficient."}}}
Link to this function

get_all_orders(api_key, secret_key, params, is_testnet \\ false)

Get all 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://binance-docs.github.io/apidocs/futures/en/#current-all-open-orders-user_data to understand all the parameters

Examples

get_all_orders(
  "api_key",
  "api_secret",
  %{"symbol" => "BTCUSDT"},
  true
)

Result:

{:ok,
[
  %Binance.OrderResponse{
    avg_price: "43018.24000",
    client_order_id: "web_Y4X2QLr0Inpc7MQYsvuf",
    close_position: false,
    cum_quote: "43018.24000",
    executed_qty: "1",
    order_id: 2960645730,
    orig_qty: "1",
    orig_type: "LIMIT",
    position_side: "BOTH",
    price: "43051.03",
    price_protect: false,
    reduce_only: false,
    side: "BUY",
    status: "FILLED",
    stop_price: "0",
    symbol: "BTCUSDT",
    time: 1641442426359,
    time_in_force: "GTC",
    type: "LIMIT",
    update_time: 1641442426359,
    working_type: "CONTRACT_PRICE"
 }
]}
or
{:error, {:binance_error, %{code: -2019, msg: "Margin is insufficient."}}}
Link to this function

get_all_prices(is_testnet \\ false)

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_endpoint(is_testnet)

Link to this function

get_exchange_info(is_testnet \\ false)

get exchange info from 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://binance-docs.github.io/apidocs/futures/en/#exchange-information to understand all the parameters

Examples

get_exchange_info(true)

Result:

{:ok,
 %{
    "timezone": "UTC",
    "serverTime": 1640141838081,
    "futuresType": "U_MARGINED",
    "symbols": [...],
    ...
 }
}
or
{:error, {:binance_error, %{code: -2019, msg: "Margin is insufficient."}}}
Link to this function

get_income(api_key, secret_key, is_testnet \\ false)

Link to this function

get_open_orders(api_key, secret_key, is_testnet \\ false)

Link to this function

get_order(api_key, secret_key, params, is_testnet \\ false)

Get order info 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://binance-docs.github.io/apidocs/futures/en/#query-order-user_data to understand all the parameters

Examples

get_order(
  "api_key",
  "api_secret",
  %{"symbol" => "BTCUSDT", "order_id" => "3006555462", "orig_client_order_id" => "hol0wkgRc922IB5rnjLjxR0"},
  true
)

Result:

{:ok,
  %Binance.OrderResponse{
    avg_price: "0.00000",
    client_order_id: "hol0wkgRc922IB5rnjLjxR0",
    close_position: false,
    cum_quote: "0",
    executed_qty: "0",
    order_id: 3006555462,
    orig_qty: "0.002",
    orig_type: "LIMIT",
    position_side: "BOTH",
    price: "40000",
    price_protect: false,
    reduce_only: false,
    side: "BUY",
    status: "CANCELED",
    stop_price: "0",
    symbol: "BTCUSDT",
    time: 1648548582813,
    time_in_force: "GTC",
    type: "LIMIT",
    update_time: 1648613715497,
    working_type: "CONTRACT_PRICE"
  }
}
or
{:error, {:binance_error, %{code: -2019, msg: "Margin is insufficient."}}}
Link to this function

order_limit_buy(params, api_secret, api_key, is_testnet \\ false)

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(params, api_secret, api_key, is_testnet \\ false)

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(params, api_secret, api_key, is_testnet \\ false)

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(params, api_secret, api_key, is_testnet \\ false)

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}

Link to this function

parse_account_info(arg)

Link to this function

parse_batch_orders_response(arg)

Link to this function

parse_exchange_info(arg)

Link to this function

parse_order_response(arg)

Link to this function

ping(is_testnet \\ false)

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