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 to trade with all available balance ? #5464

Closed
frodoe7 opened this issue Jul 5, 2019 · 7 comments
Closed

How to trade with all available balance ? #5464

frodoe7 opened this issue Jul 5, 2019 · 7 comments
Assignees
Labels

Comments

@frodoe7
Copy link

frodoe7 commented Jul 5, 2019

This is more a question than issue

I manually fetch the user available balance then make some complicated calculations to calculate the amount to buy or sell

these calculations include fetching all available balance , converting to another coin , calculating the fees ratio , deducting from the amount to buy or sell and so on ..

however my calculations is not very accurate ,
the final amount result is above or less than the expected by very small amount

for example , I have .004 BTC and would like to buy ETH
now I need to calculate the .004 BTC in ETH and also remove the fees , which sometimes (.2%) , let's assume the amount should be 0.15 ETH

sometimes my result is .151 > then it fires error : insufficient balance
sometimes my result is .149 > then it goes well but it keeps a tiny amount in my BTC balance , and I don't need this behavior

my issue with this last parameter (amount)

exchange.createOrder(market , 'market' , side , amount)

any way to tell the exchange , that I'd like to trade with all my available balance ?

@frosty00
Copy link
Member

frosty00 commented Jul 6, 2019

Hi @frodoe7,

I do not think you need to take the fees into consideration, as the exchange will do that for you and give you an amount after taking fees. So here you want to buy 0.15 ETH.

exchange.create_order ('ETH/BTC', 'limit', 'buy', 0.15, 0.0267)

where 0.004 / 0.0267 = 0.15

ETHamount / ETHBTCprice = BTCamount

a cool way to think about markets is as a fraction. so ETH/BTC is literally ETH over BTC (or ETH per BTC).

If you do these calculations you should be able to sell most of your crypto. These is no flag for most exchanges "sell everything", however I agree it would be nice. And if an exchange supports such an option you would be able to do it using https://github.com/ccxt/ccxt/wiki/Manual#implicit-api-methods

@kroitor
Copy link
Member

kroitor commented Jul 6, 2019

And if an exchange supports such an option you would be able to do it using https://github.com/ccxt/ccxt/wiki/Manual#implicit-api-methods

Or, alternatively, https://github.com/ccxt/ccxt/wiki/Manual#overriding-unified-api-params

@frodoe7

however my calculations is not very accurate ,
the final amount result is above or less than the expected by very small amount

In general, because of limits and precision, you won't be able to spend everything to a cent, some dust will always remain there, unless your amounts are large enough to avoid the rounding entirely.
Let us know if that does not answer the question.

@kroitor kroitor closed this as completed Jul 6, 2019
@mr-asad92
Copy link

Hi @frodoe7,

I do not think you need to take the fees into consideration, as the exchange will do that for you and give you an amount after taking fees. So here you want to buy 0.15 ETH.

exchange.create_order ('ETH/BTC', 'limit', 'buy', 0.15, 0.0267)

where 0.004 / 0.0267 = 0.15

ETHamount / ETHBTCprice = BTCamount

a cool way to think about markets is as a fraction. so ETH/BTC is literally ETH over BTC (or ETH per BTC).

If you do these calculations you should be able to sell most of your crypto. These is no flag for most exchanges "sell everything", however I agree it would be nice. And if an exchange supports such an option you would be able to do it using https://github.com/ccxt/ccxt/wiki/Manual#implicit-api-methods

Hi, I'm having the exact same issue. Whenever i calculate some amount to buy, it sometimes gives me error that i dont have enough balance for requested operation. I think this is because I calculated the amount on a price which was increased at the time of order execution. I'm executing market orders and i cant use limit orders because I need it to be closed immediately. Any suggestions how I can handle this?

Thanks.

@kroitor
Copy link
Member

kroitor commented Oct 31, 2019

Hi @mr-asad92

I'm executing market orders and i cant use limit orders because I need it to be closed immediately. Any suggestions how I can handle this?

Do you take the fees and the fee side (base/quote) into account when you calculate the values for your market orders? In any case, there may be price slippage, since orders are not processed immediately. Even if you calculate everything right, you might still hit a limit.

Some exchanges, however, support the "timeInForce" option which basically allows you to control the lifetime of your limit orders. The most common values for timeInForce are:

  • GTC = good till canceled (this is the default in many cases)
  • IOC = immediate or cancel (you might want to look into this one)
  • FOK = fill or kill...

So, I'd suggest to look into those options, depending on the exchange in question.

Another way to approach this is to introduce a "buffer" into your strategy, so that you trade 90%, 99%, 99.9999% (whatever works for you), not 100%...

@mr-asad92
Copy link

Hi @mr-asad92

I'm executing market orders and i cant use limit orders because I need it to be closed immediately. Any suggestions how I can handle this?

Do you take the fees and the fee side (base/quote) into account when you calculate the values for your market orders? In any case, there may be price slippage, since orders are not processed immediately. Even if you calculate everything right, you might still hit a limit.

Some exchanges, however, support the "timeInForce" option which basically allows you to control the lifetime of your limit orders. The most common values for timeInForce are:

* `GTC` = good till canceled (this is the default in many cases)

* `IOC` = immediate or cancel (you might want to look into this one)

* `FOK` = fill or kill...

So, I'd suggest to look into those options, depending on the exchange in question.

Another way to approach this is to introduce a "buffer" into your strategy, so that you trade 90%, 99%, 99.9999% (whatever works for you), not 100%...

Thanks for your response. Yes i'm already changing strategy to use 99% amount. I'll see how that performs, otherwise i'll take a look at timeInForce option. By the way i'm trading on binance.

Thanks again for your time and reply. :)

@kroitor
Copy link
Member

kroitor commented Jan 20, 2020

Just a quick update: we have added the support for total cost for market orders with Binance here: #6391 (comment)

@Hovooo
Copy link

Hovooo commented Jun 12, 2023

Hi @frodoe7,
I do not think you need to take the fees into consideration, as the exchange will do that for you and give you an amount after taking fees. So here you want to buy 0.15 ETH.
exchange.create_order ('ETH/BTC', 'limit', 'buy', 0.15, 0.0267)
where 0.004 / 0.0267 = 0.15
ETHamount / ETHBTCprice = BTCamount
a cool way to think about markets is as a fraction. so ETH/BTC is literally ETH over BTC (or ETH per BTC).
If you do these calculations you should be able to sell most of your crypto. These is no flag for most exchanges "sell everything", however I agree it would be nice. And if an exchange supports such an option you would be able to do it using https://github.com/ccxt/ccxt/wiki/Manual#implicit-api-methods

Hi, I'm having the exact same issue. Whenever i calculate some amount to buy, it sometimes gives me error that i dont have enough balance for requested operation. I think this is because I calculated the amount on a price which was increased at the time of order execution. I'm executing market orders and i cant use limit orders because I need it to be closed immediately. Any suggestions how I can handle this?

Thanks.

Hey!
I've done it the way you suggested and there is a very unexpected behaviour.
In case of making market order for MATIC/USDT it works absolutely fine. I give the amount of 1 usdt and get corresponding amount of MATIC on the other end.
But in case of doing absolutely the same with AVAX/USDT almost all my balance is being used to buy AVAX, nevertheless I specify amount equal to 1 USDT.
Any ideas why is it happening and how can I fix it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants