Troubleshooting CCXT order placement issues with Phemex API in Discord bot

Hey folks, I’m stuck with a problem in my Discord trading bot. It’s using CCXT to place orders on Phemex, but keeps failing. The bot command looks like this:

/start symbol:BTCUSD leverage:5 size:10 line_value:30000

But I’m getting this weird error:

error: Critical: Error executing buy market order for BTCUSD: phemex {"code":39999,"msg":"Error in place order","data":null} {"timestamp":"2024-04-20T18:31:52.978Z"}

I’ve tried both market and limit orders, but no luck. Here’s a snippet of my order placement function:

async function sendOrder(orderInfo) {
  const { coin, orderType, direction, quantity, targetPrice, settings } = orderInfo;
  const phemex = new ccxt.phemex({
    apiKey: process.env.API_KEY, 
    secret: process.env.API_SECRET, 
  });

  try {
    let result;
    if (orderType === 'market') {
      result = await phemex.createMarketOrder(coin, direction, quantity, settings);
    } else {
      result = await phemex.createLimitOrder(coin, direction, quantity, targetPrice, settings);
    }
    console.log('Order placed:', result);
    return result;
  } catch (err) {
    console.error('Order failed:', err.message);
    throw err;
  }
}

What’s causing this error 39999? Am I missing something in my setup? Has anyone run into this before? I’m out of ideas here. Help!

I encountered a similar issue when working with Phemex through CCXT. One crucial factor to consider is the contract specifications for BTCUSD. Phemex uses a contract value of 1 USD, so your order size might be off. Try adjusting your quantity calculation to account for this.

Also, ensure you’re using the correct market type (spot or futures) in your CCXT setup. Phemex has different endpoints for each, and mixing them up can cause unexpected errors.

If the problem persists, consider implementing exponential backoff and retry logic in your order placement function. Sometimes these errors are transient and resolve themselves on a subsequent attempt.

Lastly, reach out to Phemex support with your exact API request and response. They can often provide insights that aren’t immediately apparent from the error message alone.

hey man, i had a similar issue with phemex API. make sure ur account has enough funds and check ur api permissions. also, double-check the symbol format (BTCUSD vs BTC/USD). if that doesnt help, try using the phemex-specific methods in CCXT instead of generic ones. good luck!

I’ve dealt with Phemex integration before, and that error code is usually pretty vague. One thing to check is your account’s trading status - sometimes Phemex temporarily restricts trading for various reasons. Also, make sure you’re using the correct endpoint for testnet vs mainnet.

Another potential issue could be with the order parameters. Phemex can be picky about things like quantity and price precision. Try rounding your quantity to the nearest whole number or adjusting the decimal places.

If none of that works, I’d recommend enabling verbose mode in CCXT to get more detailed error messages. You can do this by adding enableRateLimit: true, verbose: true to your Phemex instance options. This might give you more insight into what’s going wrong during the API call.

Lastly, don’t forget to check Phemex’s status page for any ongoing issues or maintenance that could be affecting order placement. Sometimes it’s just a temporary glitch on their end.