Percentage orders in futures, quoteOrderQty alternative (simple but accurate formula)

I need an exact formula to calculate the maximum amount to open in futures (both long and short) with leverage n, given the current margin m.

In spot I can use quoteOrderQty, but in futures there is no such feature. I noticed some formulas here, but it’s rather complicated, so was hoping to avoid this.

Let’s have a look at the simplest case:

LONG market futures open:

  1. calculate the price:
    assuming price = ask[0] * (1 + 0.05%) , Short order: assuming price = bid[0]

→ question, why +0.05%? Is this to account for transaction fees or slippage? Why is this not in the formula for short?

  1. Calculate the Initial Margin
    Initial margin = Assuming price * Number of Contracts / Leverage

  2. Open loss
    = Number of Contract * Absolute Value {min[0, direction of order * (mark price - assumingprice)]}
    (for open order)

  3. Cost Required to Open a Long Position
    = open loss + Initial margin

So to place an order in the API I need 100% order quantity (i.e. number of contracts). By merging all the above I can get:

quantity = (quoteBalance - open Loss) * leverage / assuming price

whereby quoteBalance is the amount in the account remaining in the quote, e.g. USDT. In order words, what we are willing to spend on the order.

To make this more complete, we can make this a percentage order:

quantity = percentage * (quoteBalance - maintenance margin - open Loss) * leverage / assuming price

Is this the most accurate I can get? I want to avoid any failed orders.

I don’t believe this takes into account ‘maintenance balance’ so I added this to the final formula. What about trading fees?