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:
- What does error code
39999generally indicate on Phemex when using CCXT for order placements? - Are there any particular settings or parameters I might have overlooked that could contribute to this issue?
- Is there a possibility that I am placing the orders incorrectly?
- 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!