Max quantity calculation

what’s the calculation to get this max value for both long and short?

image

I tried: available balance * leverage / mark price

best bid, and best ask too but it appears to not be that

thanks

There’s a position limit at your current leverage level, which is called maxNotionalValue.
You can check the trading rules.

Yeah but nope. maxNotionalValue gives us the max authorized, if you have less balance you can’t go until this value. Furthermore, we clearly see that for long and shorting it’s not the same value

Within your maxNotionalValue, if you want to put a limit order with a bid price higher than the mark price now, the system will regard the total price difference as your loss, and the remain balance * leverage / bid price will be the approximate quantity you can order. The short side is the same,

Mhh not sure to understand but I will try to find. Here’s what I write it down:

  • mark price: 0.02454893
  • last price: 0.02455
  • best ask: 0.024549
  • best bid: 0.024547
  • leverage: 50
  • available balance: 165.46531454
  • already -3000 1000SHIB in the position (short)

and the binance ui show me these max:

  • buy: 332713
  • sell: 336976

I have caculated the fomula for your question to figure out the maxPosition you can open for both sides.

When you’re trying to open Long Side, take the plus. And take the minus if you want to open Short Side.

Use BTCUSDT as a simple example, with MarkPrice of 39330.4, Leverage of 5, and no Opened Positions.

When I put a Limit Bid Order at the price of 40888.8, my fomula can give you the answer of 223.989.

This is only an approximate quantity because the MarkPrice changes every millisecond.

1 Like

Just test and it doesn’t work for me. Maybe because I look with market order. I have to adjust with this I think: Comment calculer le coût requis pour ouvrir une position sur un contrat à terme perpétuel | Binance Support

@passingsword can you tell me in your formula the open loss part to adjust for market orders please?

The value displayed by Binance UI is calculated by the browser through Javascript, and it is not calculated in real time, so it is inevitable that there is a gap with the theoretical value.

If you have find any mistakes or new discoveries, of the formula, just point out and refine it for me. :innocent:

The formula above is for limit orders, try below formula for market orders:

In this formula of Max, what does below variable stand for:
notional
price

I know there is a gap but I have a big gap here ^^ are you available in discord? (DevChris#2112)

I’m not using discord…
Don’t know why your gap is so big. :rofl:
I tried and it works for me.

Where can we discuss in a voice channel if you’re ok?
Maybe the formula is wrong with already open position or idk I think there is something to do with this max notional. Maybe if max = X, we can go to -X to X then if we’re shorting 1 and max is 10 we can go to 11 in long nope?

@passingsword Yeah I really think you are missing when there is already open position, and maybe the open loss

Here’s the formula:

First you need to get the available balance and take into account the position if you have already notional in it.

nBalance = available balance * leverage
price = side == "BUY"  ? best ask * 1.0005 : max(best bid, mark price)
dir = side == "BUY" ? 1 : -1
available = min(nBalance, maxNotional)

IF side == "BUY" AND notional > 0 OR side == "SELL" AND notional > 0 {
  available = available + abs(notional)
}

and after that you need to take into account the open loss:

o = abs { min(0, (mark price - price) * dir) }
maxAmount = available / (o * leverage + price)

IF side == "BUY" AND notional > 0 OR side == "SELL" AND notional > 0 {
  maxAmount = maxAmount + abs(position amount)
}

then you have your max amount for market order :slight_smile:

hello, i dont understand your solution. what programming language is this? i want to implement it in python. thanks

Here’s my function in typescript, ask if you don’t understand something in there: maxQtyAllowed · GitHub

i am new to programming so it is only python i understand. is there a formula you use in calculating that i can just implement in python? thanks

I was able to use @passingsword formula for LIMIT but i noticed that it only works for LONG if you set the condition below, while the SHORT formula works fine.

if price < markPrice:
min(price,markPrice)

else:
max(price,markPrice)

Which is basically the same as using just “price” for LONG instead of the max(price,markPrice) in the formula

For the MARKET formula, set Notional to your orderAmount and price to int(1). Then it works too.