Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I calculate qty in USDT Perpetual with leverage #139

Open
rohitbhirud opened this issue Feb 15, 2022 · 3 comments
Open

How do I calculate qty in USDT Perpetual with leverage #139

rohitbhirud opened this issue Feb 15, 2022 · 3 comments
Labels
api question General question about exchange APIs

Comments

@rohitbhirud
Copy link

rohitbhirud commented Feb 15, 2022

First of all, amazing library.

I'm creating a conditional order but don't know how do I calculate quantity. Is it based on leveraged amount or original?

for example

I have balance of 50 USDT and want to use 100% per trade with following conditions.

BTC at price 44,089.50 with 50x leverage.
SHIB at price 0.030810 with 50x leverage.

How do I calculate the qty parameter?

@noobSam1122
Copy link

First of all, amazing library.

I'm creating a conditional order but don't know how do I calculate quantity. Is it based on leveraged amount or original?

for example

I have balance of 50 USDT and want to use 100% per trade with following conditions.

BTC at price 44,089.50 with 50x leverage. SHIB at price 0.030810 with 50x leverage.

How do I calculate the qty parameter?

I have the same problem here, did you figure it out?

@tiagosiebler tiagosiebler added the api question General question about exchange APIs label Mar 17, 2022
@BattlefieldDuck
Copy link
Contributor

BattlefieldDuck commented May 12, 2022

I did some maths and wrote a function a long time ago. It supports both inverse and usdt perpetual.

/**
 * Calculate order quantity
 * @param inverse true = Inverse Perpetual, false = USDT Perpetual
 * @param balance Free balance (Margin asset)
 * @param side true = long, false = short
 * @param price Entry price
 * @param leverage Leverage
 * @param takerFee Taker fee
 * @returns 
 */
const orderQty = (inverse: boolean, balance: number, side: boolean, price: number, leverage: number, takerFee: number) => {
  return inverse ? 
    balance * price * leverage / (1 + takerFee * (2 * leverage + (side ? 1 : -1))) :
    balance / (price * ((1 - takerFee * (side ? 1 : -1)) / leverage + 2 * takerFee));
}

In your case

// BTC at price 44,089.50 with 50x leverage, 50 USDT free balance, 0.06% taker fee
console.log(orderQty(false, 50, true, 44089.5, 50, 0.0006)); // LONG qty 0.05352354453516336
console.log(orderQty(false, 50, false, 44089.5, 50, 0.0006)); // SHORT qty 0.053462986121584066

// SHIB at price 0.030810 with 50x leverage, 50 USDT free balance, 0.06% taker fee
console.log(orderQty(false, 50, true, 0.03081, 50, 0.0006)); // LONG qty 76592.86974304073
console.log(orderQty(false, 50, false, 0.03081, 50, 0.0006)); // SHORT qty 76506.20988664657

You should round down the qty before calling the api. Good Luck.

@rafaelfaria
Copy link

I'm struggling to understand the API.

Why do you need to know the qty?

shouldn't you be able to put the amount you want to trade, the leverage, and the market order? Boom... done?
or am I simplifying things?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api question General question about exchange APIs
Projects
None yet
Development

No branches or pull requests

5 participants