sammchardy / python-binance

Binance Exchange API python implementation for automated trading
https://python-binance.readthedocs.io/en/latest/
MIT License
5.97k stars 2.19k forks source link

Order based on percent #89

Closed Fredrik81 closed 6 years ago

Fredrik81 commented 6 years ago

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.

sammchardy commented 6 years ago

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.

sm00v commented 6 years ago

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

sammchardy commented 6 years ago

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!

Fredrik81 commented 6 years ago

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?

Kato-Official commented 6 years ago

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?

sm00v commented 6 years ago

You rock @sammchardy. Thanks for the timely response!

sammchardy commented 6 years ago

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

Technorocker commented 6 years ago

@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

JessseG commented 3 years ago

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

ZENALC commented 3 years ago

Following ^

hichiaty commented 3 years ago

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

manuelsilverio commented 3 years ago

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.

hichiaty commented 3 years ago

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)

JessseG commented 3 years ago

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 notifications@github.com 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 .

igomorf commented 3 years ago

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

fortoken01 commented 3 years ago

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

w4ssimh commented 3 years ago

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.

nopice commented 3 years ago

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! 😫

manuelsilverio commented 3 years ago

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.

Iamjrad commented 2 years ago

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)

duart3000 commented 1 year ago

@manuelsilverio Could you share your solution please? I'm stuck with the same problem right now, thank you.

jamestangeres commented 1 year ago

I'm able to sell/buy the 99% of the open positon token by using the position.isolatedMargin as usdt quantity

Example:

20 usdt margin, when the bot is selling it leave 0.01 usdt on the open positon

You can check it on /positionRisk

I will try to use bnb as fees if it will sell or buy 100%.

PS: You need to update from cross to isolated margin in order for it to work.