Open-Trader/opentrader

Insufficient balance for sell orders due to fee deduction

Open

#99 opened on Jan 15, 2025

 (3 comments) (2 reactions) (1 assignee)TypeScript (319 forks)auto 404
bugexchangesgood first issuehelp wanted

Repository metrics

Stars
 (2,749 stars)
PR merge metrics
 (PR metrics pending)

Description

Problem

When executing trades, the bot encounters an issue with insufficient balance for sell orders due to fee deductions. Specifically:

  1. The bot places a buy order for 1 BTC.
  2. After the exchange deducts a trading fee (e.g., 0.1%), the account ends up with slightly less than 1 BTC (e.g., 0.999 BTC).
  3. When the bot attempts to place a sell order for 1 BTC, the order is rejected due to insufficient funds.

Proposed solution

The bot should calculate the buy order amount in a way that ensures the target amount (e.g., 1 BTC) is available after fees are deducted.

Example

Let's take OKX as an example where the fee is taken in the base currency for buy orders. The fee rate is 0.1%.

  1. Target amount: 1 BTC
  2. Required buy amount (including fee): 𝐵 = 1 ÷ ( 1 − 0.001 ) = 1.001001 BTC
  3. Fee paid: 1.001001 × 0.001 = 0.001001 BTC
  4. Net BTC after fees: 1.001001 − 0.001001 = 1 BTC

Note: Some exchanges may have a complex fee structure, so in certain cases, we may need to cover slightly more than the expected fee.

Checklist

  • Update the bot's logic to calculate the correct buy amount to cover fees.
  • Research the trading fee rate for every exchange (supported exchanges)
  • Test the updated logic to confirm that the balance after buying matches the target amount.

Contributor guide