ctubio / Krypto-trading-bot

Self-hosted crypto trading bot (automated high frequency market making) written in C++
https://127.0.0.1:3000
Other
3.33k stars 819 forks source link

Binance error: Account has insufficient balance #1075

Closed flfsbr closed 3 years ago

flfsbr commented 3 years ago

Hi Carles,

I still have the following error at Binance:

03/01 12: 47: 29.843684 GW BINANCE Warrrrning: Error while place order: Account has insufficient balance for requested action .. 03/01 12: 47: 30.183488 GW BINANCE Warrrrning: Error while place order: Account has insufficient balance for requested action .. 03/01 12: 47: 30.276832 GW BINANCE Warrrrning: Error while place order: Account has insufficient balance for requested action ..

There seems to be a time when the bot starts sending orders larger than the available balance.

No matter what I do, even with TPB 50%, PDIV 10% and bid and asksize 5% the bot has this error. This occurs after about 40 minutes of operation. When I restart the bot everything goes back to normal and about 40 minutes later the errors happen again.

flfsbr commented 3 years ago

Hi,

These are the messages that occur before the bot stop sending orders.

03/04 20:17:52.091192 GW BINANCE WS ignored msg: {"error":{"code":2,"msg":"Invalid request: invalid stream"},"id":1}. 03/04 20:17:53.105529 GW BINANCE WS Disconnected, reconnecting in 7s.. 03/04 20:17:59.495610 GW BINANCE WS ignored msg: {"error":{"code":2,"msg":"Invalid request: invalid stream"},"id":2}. 03/04 20:18:00.438521 GW BINANCE WS Disconnected, reconnecting in 7s.. 03/04 20:18:07.492398 GW BINANCE WS ignored msg: {"error":{"code":2,"msg":"Invalid request: invalid stream"},"id":3}.

Danilo-Araujo-Silva commented 3 years ago

I have the same problem. Because of it, it looks like I reached the limit of 200k requests per day (I have about 8 krypto instances running). I'm using a reasonable value in the "--lifetime" option, but it looks it has no effect when this warning / error is happening.

ctubio commented 3 years ago

:koala: sorry can you share on what currency pairs are you having this issue on binance?

Danilo-Araujo-Silva commented 3 years ago

Well, in my case, it looks like it can happen with any pair. But maybe it is because of the strategy I'm using (not sure). I'm pointing all K instances to the same currency and letting them fight for the capital.

Please kindly let me know if you have any configuration changes to suggest, so I can avoid the errors.

Here is an excerpt of my logs:

==> /tmp/docker/container/logs/ktb-binance-t01-btc_brl-01.log <==
03/10 23:47:10.051133 GW BINANCE Warrrrning: Error while place order: Account has insufficient balance for requested action..

==> /tmp/docker/container/logs/ktb-binance-t01-ltc_brl-01.log <==
03/10 23:47:10.765381 GW BINANCE Warrrrning: Error while place order: Account has insufficient balance for requested action..

==> /tmp/docker/container/logs/ktb-binance-t01-doge_brl-01.log <==
03/10 23:47:10.765410 GW BINANCE Warrrrning: Error while place order: Account has insufficient balance for requested action..

==> /tmp/docker/container/logs/ktb-binance-t01-xrp_brl-01.log <==
03/10 23:47:10.895472 GW BINANCE Warrrrning: Error while place order: Account has insufficient balance for requested action..

==> /tmp/docker/container/logs/ktb-binance-t01-eth_brl-01.log <==
03/10 23:47:10.946307 GW BINANCE Warrrrning: Error while place order: Account has insufficient balance for requested action..

==> /tmp/docker/container/logs/ktb-binance-t01-btc_brl-01.log <==
03/10 23:47:11.005629 GW BINANCE Warrrrning: Error while place order: Account has insufficient balance for requested action..

==> /tmp/docker/container/logs/ktb-binance-t01-ltc_brl-01.log <==
03/10 23:47:11.778628 GW BINANCE Warrrrning: Error while place order: Account has insufficient balance for requested action..

==> /tmp/docker/container/logs/ktb-binance-t01-doge_brl-01.log <==
03/10 23:47:11.779042 GW BINANCE Warrrrning: Error while place order: Account has insufficient balance for requested action..

==> /tmp/docker/container/logs/ktb-binance-t01-xrp_brl-01.log <==
03/10 23:47:11.908857 GW BINANCE Warrrrning: Error while place order: Account has insufficient balance for requested action..
ctubio commented 3 years ago

what markets are you using then? do you have a list of currency pairs you are using?

Danilo-Araujo-Silva commented 3 years ago

Yes, sure, for now I'm using all of these on Binance:

where I'm using BRL because I can have more capital to trade comparing to USDT.

ctubio commented 3 years ago

many thanks for the info''' i suspect the issue happens when running multiple markets with the same currency (BRL in your case).

cos i was not able to reproduce yet the error Account has insufficient balance for requested action, but im currently only on BTC/EUR at binance (tomorrow i will try with multiple EUR markets).

:koala: meanwhile no other solution i would consider using --wallet-limit argument so to avoid all bots using the same funds

flfsbr commented 3 years ago

With me the error occurs in all pairs that I tested.

BTC / USDT BTC / USDC BTC / EUR

I am not running multiple instances, just one and even then the error occurs.

ctubio commented 3 years ago

ok well, thank you for the info, i keep trying to reproduce it (and fixing other errors meanwhile :D)

Danilo-Araujo-Silva commented 3 years ago

Not sure if this could help, but since I'm running K here and having the errors appearing, I'm sharing this image with one of the configurations I'm using:

image

where I'm using those values in bidSize and askSize to try to limit the amount used in the orders

Danilo-Araujo-Silva commented 3 years ago

I'm running K this way:

K-trading-bot --lifetime 4320 --port 10006 --matryoshka https://test.local:10007/# --free-version --wallet-limit 0.87191091 --title "Binance - LINK/BRL" --exchange BINANCE --currency LINK/BRL --database /root/database/binance/link_brl.db --apikey XXX --secret XXX --passphrase NULL --colors --naked "$@"

In the logs I received the following:

==> /tmp/docker/container/logs/ktb-binance-t01-link_brl-01.log <==
https://api.binance.com/api/POST/order?symbol=LINKBRL&side=BUY&type=LIMIT&timeInForce=GTC&quantity=0.10874&price=165.72&newClientOrderId=XXX&timestamp=1615499220838&signature=XXX
03/11 18:47:01.373888 GW BINANCE Warrrrning: Error while place order: Account has insufficient balance for requested action..
executionReport{"C":"","E":1615499220820,"F":"0.00000000","I":6580366,"L":"0.00000000","M":false,"N":null,"O":1615499220819,"P":"0.00000000","Q":"0.00000000","S":"SELL","T":1615499220819,"X":"NEW","Y":"0.00000000","Z":"0.00000000","c":"XXX","e":"executionReport","f":"GTC","g":-1,"i":3213093,"l":"0.00000000","m":false,"n":"0","o":"LIMIT","p":"0.31250000","q":"58.16000000","r":"NONE","s":"DOGEBRL","t":-1,"w":true,"x":"NEW","z":"0.00000000"}

It is kind of strange that inside the LINK logs there's this message saying something about DOGE. But I've seen that it could appear XRP or other ones as well.

Danilo-Araujo-Silva commented 3 years ago

Hi @ctubio,

so I kind of new to K and don't fully understand the code and the concepts of it (actually I still don't understand a lot of its options and I may ask more about this in the furture ;P)

I was inspecting this error and my guess is that it is related with the scenario that K tries to place an order and because it didn't work, it continuously tries to place new orders again (which fail because of there's no fund available).

So, if this is the case, one possible solution would be checking if the error returned was related with insufficient funds and update the current balance and skip the order placing and let the bot try again in the future (checking if the balance is sufficient before placing)

or if the exchange returned an error when placing the order, try to respect the lifetime parameter to try again (I think the parameter is respected when the order is placed successfully but not when the exchange returns an error when placing orders).

Inspecting the code, it looks like the problem could be inside this function here, but from what I've seen this is the non-free part of the bot.

When you have time, could you please share some more information about this? Or maybe try to pointing where we could try to change?

Thanks in advance

nithi2023 commented 3 years ago

@flfsbr Please check your API keys have no restriction for the IP address which K bot are connecting from. I faced the same issue, I noticed my IP address was not whitelisted on Binance API page.

03/04 20:17:52.091192 GW BINANCE WS ignored msg: {"error":{"code":2,"msg":"Invalid request: invalid stream"},"id":1}. 03/04 20:17:53.105529 GW BINANCE WS Disconnected, reconnecting in 7s.. 03/04 20:17:59.495610 GW BINANCE WS ignored msg: {"error":{"code":2,"msg":"Invalid request: invalid stream"},"id":2}. 03/04 20:18:00.438521 GW BINANCE WS Disconnected, reconnecting in 7s.. 03/04 20:18:07.492398 GW BINANCE WS ignored msg: {"error":{"code":2,"msg":"Invalid request: invalid stream"},"id":3}.

ctubio commented 3 years ago

well after next commit, we will reread the balances before continue placing new orders everytime an error is reported while placing an order; also before placing new orders a small delay of a few seconds was added to prevent the battery of errors (and ban from binance) in case that the reread of the balance didnt solve the related error

:koala: note lifetime arg is used to avoid orders to be closed, not to avoid orders to be created