4

From what I have seen CCXT does not provide functionality for creating an order of type:
"I want to spend 10 USDT to buy BTC"
Instead, the only thing you can do is to specify the amount of the base currency(BTC) you want to buy, like in the examples below:

const order = await exchange.createOrder(symbol, 'market', 'buy', amount, price);

OR

const symbol = 'BTC/USD';
const amount = 2;
const price = 9000;
cost = amount * price;
const order = await exchange.createMarketBuyOrder (symbol, cost);

Is there something that I have missed that could provide the functionality I need ?
The only solution I could think of is to get the base currency's price and calculate the percentage of it that equals to 10 USDT so I can then use that as the amount in the examples above.

Pmits
  • 165
  • 1
  • 4
  • 17

1 Answers1

2

Most exchange apis only take the base quantity, so you would need to do a conversion based on the price of the market

const ccxt = require("ccxt");

const exchange = new ccxt.binance({
  apiKey: '...',
  secret: '...',
  options: {},
});

const get_amount_from_quote = async (quote_amount, symbol) => {
  const tickers = await exchange.fetch_tickers();
  const price = tickers[symbol]["close"];
  return quote_amount / price;
};

const symbol = "ETH/USDT";
const amount = await get_amount_from_quote(10, symbol);
const order = await exchange.createOrder(
  symbol,
  "market",
  "buy",
  amount,
  price
);

Some exchanges, like Binance, specify that you can pass in quoteOrderQuantity in their api

Binance Api quote order quantity documentation

And you can always pass in exchange specific parameters through the params argument

const order = await exchange.createOrder(
  symbol,
  "market",
  "buy",
  undefined,
  price,
  {
    quoteOrderQty: 10,
  }
);
Sam
  • 1,765
  • 11
  • 82
  • 176