python-binance icon indicating copy to clipboard operation
python-binance copied to clipboard

Order based on percent

Open Fredrik81 opened this issue 8 years ago • 22 comments

Hello, How can i make an order (buy or sell) for 25% or 50% or 100%. I can get the amount in fund i have but let's say im buying on NAVBTC then i need to enter the amount of NAV i want to buy i at this time and i don't know how many NAV coins are 50% of my BTC fund.

Fredrik81 avatar Jan 08 '18 17:01 Fredrik81

Hi @Fredrik81 there is no simple way to do this.

It helps if you're using BNB for fees as then you don't have to factor in fees into the coins your trading, well unless that includes BNB... Anyway it's generally recommended to use BNB to pay fees.

If you make a limit order then you know the price you want to buy or sell and then you just have to divide the relevant balance by that to get the quantity.

sammchardy avatar Jan 09 '18 09:01 sammchardy

What if you want to place a market order? @sammchardy

sm00v avatar Jan 09 '18 09:01 sm00v

Why do you have to ask the hard questions @sm00v :)

Ok, if you have a recent order book update and can place a market order very soon after receiving that order book update then you may be able to assume it will go through at the price you see.

If so then you follow the above, dividing your balance by the price to get a quantity.

There's no getting around having to send an actual quantity value, you can't send quantity "100%". If you think you should then you'll have to take it up with Binance!

sammchardy avatar Jan 09 '18 10:01 sammchardy

Thanks @sammchardy I got a little further so now i have done this: truncate(MyBTCFund / BuyCoinPrice, 8) That will get me the quantity of coins i can buy (100%) to buy but i totally missed the fee part so thanks i will add something for that aswell. So i belive i have to reduce the the total amount of coins with the fee so there are some spare room for binance.

Where i strugle at the moment is adding the correct (just now) price. At this time the market POEBTC is at 0.00001154 and when i enter that price it will fail. if i add 0.0000115 then it works but that will no go thru... Any is this a API issue or am i doing it wrong?

Fredrik81 avatar Jan 09 '18 11:01 Fredrik81

Hi Fredrik81, have you tried to set at market in that moment as suggested by Sam? Also how long after the price fetch are you placing the order?

Kato-Official avatar Jan 10 '18 00:01 Kato-Official

You rock @sammchardy. Thanks for the timely response!

sm00v avatar Jan 11 '18 05:01 sm00v

I might write something explaining how to do this, and how to make sure you're working within all the filters.

sammchardy avatar Jan 17 '18 02:01 sammchardy

@sammchardy Did you end up writing a bit on the buy sell max amount? I keep runing into this problem myself and would appreciate some insight as to getting all the filters approved so I stop seeing this error

Thanks for your time

Technorocker avatar Feb 10 '18 20:02 Technorocker

Were you guys ever able to implement this selling of a max amount in Python?

JessseG avatar Jan 05 '21 07:01 JessseG

Following ^

ZENALC avatar Jan 30 '21 15:01 ZENALC

I genuinely cannot believe Binance's oversight on this, especially with a market order. Do I really have to divide balance by price?

hichiaty avatar Feb 02 '21 23:02 hichiaty

This is really frustrating. I have to round the buy and sell amounts with precision of 6.. but in Binance BTC has precision of 8. so even if I get the balance of a BTC and try to sell them I can't because I need to truncate my float to precision 6.

manuelsilverio avatar Feb 21 '21 13:02 manuelsilverio

This is really frustrating. I have to round the buy and sell amounts with precision of 6.. but in Binance BTC has precision of 8. so even if I get the balance of a BTC and try to sell them I can't because I need to truncate my float to precision 6.

Yes had that issue, ended up using this little string formatting snippet amount = '{:.{}f}'.format(amount, precisions)

hichiaty avatar Feb 21 '21 15:02 hichiaty

No you actually don’t need to do the calculation when it comes to market orders . Check out quoteAssetQuantity. It allows you to put in your quote asset balance and submit that as your buy order and it’ll do the calculation for you. Only market orders

On Sun, Feb 21, 2021 at 10:38 AM hichiaty [email protected] wrote:

This is really frustrating. I have to round the buy and sell amounts with precision of 6.. but in Binance BTC has precision of 8. so even if I get the balance of a BTC and try to sell them I can't because I need to truncate my float to precision 6.

Yes had that issue, ended up using this little string formatting snippet amount = '{:.{}f}'.format(amount, precisions)

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/sammchardy/python-binance/issues/89#issuecomment-782877220, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOW3YCJT7TPA2SAGWNSYTFDTAESHTANCNFSM4EK2VORA .

JessseG avatar Feb 22 '21 00:02 JessseG

balanceBNB = client.get_asset_balance(asset='BNB') TotalBNB=float(balanceBNB['free']) round(((TotalBNB/Price) * 0,25),precision) #Buy/Sell 25% of a total

igomorf avatar Feb 28 '21 22:02 igomorf

So basically we can't make exact buy/sell orders because we need to round the numbers? time to check the binance api then

fortoken01 avatar Apr 28 '21 23:04 fortoken01

Yess, i fall in the same problem. We are in 2021 znd we still can't sell 100% with the api. Rounding the value may increase it so this is no more the maxValue but a 101% which will lead to an error.

w4ssimh avatar Jun 07 '21 06:06 w4ssimh

Yess, i fall in the same problem. We are in 2021 znd we still can't sell 100% with the api. Rounding the value may increase it so this is no more the maxValue but a 101% which will lead to an error.

Yeah, can't believe this has been an issue for several years. Apart from that rounding process, we also have to calculate this complicate stuff about open loss MANUALLY if we trade on Futures. That's insane! 😫

nopice avatar Jun 12 '21 23:06 nopice

An update on this problem. How I solved it for my bot was doing the following: I calculated the 99.9% of the amount instead of 100%. It is technically not everything but it avoided my bot from crashing. If the amount you are trading is big then even 99.999% might be useful. The number of 9s after the decimal point depends on the amount. Just avoid 100% and your bot will work. Hopefully, Binance should just allow us to sell 100% one day via programming, for now, this should cover it.

manuelsilverio avatar Jun 13 '21 17:06 manuelsilverio

An update on this problem. How I solved it for my bot was doing the following: I calculated the 99.9% of the amount instead of 100%. It is technically not everything but it avoided my bot from crashing. If the amount you are trading is big then even 99.999% might be useful. The number of 9s after the decimal point depends on the amount. Just avoid 100% and your bot will work. Hopefully, Binance should just allow us to sell 100% one day via programming, for now, this should cover it.

@manuelsilverio what is the python script you used to make this work?

This is what I have so far: def strategy(pair, qty, open_position=False): df = getminutedata(pair, '1m', '100') applytechnicals(df) inst = Signals(df, 10) inst.decide() print(f'current Close is ' + str(df.Close.iloc[-1])) if df.Buy.iloc[-1]: order = client.create_order(symbol=pair, side='BUY', type='MARKET', quantity= qty) print(order) buyprice = float(order['fills'][0]['price']) open_position = True while open_position: time.sleep(0.5) df = getminutedata(pair,'1m', '2') print(f'current Close ' + str(df.Close.iloc[-1])) print(f'current Target ' + str(buyprice * 1.005)) print(f'current Stop ' + str(buyprice * 0.995)) if df.Close[-1] <= buyprice *0.995 or df.Close[-1] >= 1.005 * buyprice: order = client.create_order(symbol=pair, side='SELL', type='MARKET', quantity= qty) print(order) break

while True: strategy('HBARUSD', 550) time.sleep(0.5)

Iamjrad avatar May 04 '22 15:05 Iamjrad