Phemex API Order Placement Error in a Discord Trading Bot Using CCXT

I’m building a trading bot that integrates with Discord and utilizes the CCXT library for managing trades on Phemex. Despite carefully adhering to the guidelines in the documentation and testing both limit and market orders, I keep encountering an error without a comprehensible explanation in either Phemex’s documentation or the CCXT issue tracker. For instance, here’s a command used in my Discord bot:

/start symbol:FETUSDT leverage:3 size:15 line_value:2.43

The error I receive is:

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

Here’s the essential code responsible for order placement:

const placeLimitBuyOrder = async (pair, quantity, cost, risk, direction = 'buy') => {
    try {
        console.log(`Placing limit buy order for ${pair}, qty: ${quantity}, cost: ${cost}, risk: ${risk}, direction: ${direction}`);
        const markets = await phemex.loadMarkets();
        const marketPair = markets[pair].id;
        const orderParams = {
            'leverage': risk,
            'direction': direction
        };
        const result = await phemex.createLimitBuyOrder(marketPair, quantity, cost, orderParams);
        console.log('Limit buy order placed:', result);
        return result;
    } catch (err) {
        console.error('Error placing limit buy order:', err.message);
        throw err;
    }
};

const placeLimitSellOrder = async (pair, quantity, cost, risk, direction = 'sell') => {
    try {
        console.log(`Placing limit sell order for ${pair}, qty: ${quantity}, cost: ${cost}, risk: ${risk}, direction: ${direction}`);
        const markets = await phemex.loadMarkets();
        const marketPair = markets[pair].id;
        const orderParams = {
            'leverage': risk,
            'direction': direction
        };
        const result = await phemex.createLimitSellOrder(marketPair, quantity, cost, orderParams);
        console.log('Limit sell order placed:', result);
        return result;
    } catch (err) {
        console.error('Error placing limit sell order:', err.message);
        throw err;
    }
};

Additionally, here’s the function responsible for sending order requests:

async function sendOrderRequest(orderInfo) {
    const { symbol, type, direction, amount, price, options } = orderInfo;
    const exchange = new ccxt.phemex({
        apiKey: process.env.PHEMEX_API_KEY,
        secret: process.env.PHEMEX_SECRET,
    });
    try {
        let orderResponse;
        switch (type) {
            case 'limit':
                orderResponse = await exchange.createLimitOrder(symbol, direction, amount, price, options);
                break;
            case 'market':
                orderResponse = await exchange.createMarketOrder(symbol, direction, amount, options);
                break;
            // Include other order types as necessary.
            default:
                throw new Error(`Order type ${type} is not supported.`);
        }
        console.log(`Order placed: ${JSON.stringify(orderResponse)}`);
        return orderResponse;
    } catch (err) {
        console.error(`Error placing order for ${symbol} - ${type} - ${direction}: ${err.message}`);
        throw err;
    }
}

Questions:

  1. What does error code 39999 generally indicate on Phemex when using CCXT for order placements?
  2. Are there any particular settings or parameters I might have overlooked that could contribute to this issue?
  3. Is there a possibility that I am placing the orders incorrectly?
  4. Has anyone else faced similar challenges with Phemex on CCXT, and were they able to find a solution?

I have already attempted to switch between market and limit orders and have varied the parameters, yet the problem persists. Any help or guidance would be immensely appreciated!

I faced a similar situation, and the issue was related to incorrect symbol format. Ensure that when providing the symbol to ccxt, it matches precisely with Phemex’s market symbol format, as discrepancies can lead to errors. Additionally, check if you’re managing exceptions and errors properly, especially network issues; sometimes these can masquerade as logical errors. Debugging by printing detailed logs or even using a debugger to step through the process can uncover hidden problems. Reviewing your code settings and Phemex’s API documentation side by side might be beneficial.

hey! Have you checked if the timezone of your bot matches the one expected by Phemex? Sometimes API might need specific timestamps due to time differences. Also, ensure your API key has the right permissions for trading - sometimes they can be limited to only reading or there’s IP restriction settings u might missed.

I’ve encountered similar errors, and one common issue could be with the leverage setting. Make sure that the leverage value you are passing is supported by Phemex for the particular market and symbol. Incorrect leverage values can cause these errors. Additionally, double-check the balance on your account, as placing an order exceeding available funds might trigger such errors. Also, ensure that the bot isn’t placing orders too rapidly, which could lead to rate limit issues and cause unexplained errors.

yo! double check phemex account settings for active markets. some symbols might be delisted or temporarily inactive, causing order issues. also, are you sure the line_value parameter is correct? maybe try placing a manual trade with same params on the phemex web platform to see if it works.