chrisleekr / binance-trading-bot

Automated Binance trading bot - Trade multiple cryptocurrencies. Buy low/sell high with Grid Trading. Integrated with TradingView technical analysis
MIT License
5k stars 1.09k forks source link

Did not trigger sell on target price #163

Closed jl9404 closed 3 years ago

jl9404 commented 3 years ago

Description

When a bot ran for ~1 day, the bot did not trigger sell on target price but it does send the message to slack but normally there should be a binance api respond follow, but it does not show up I am using WSL2 to host the bot, I am not sure it relates to the "bug"? I restart the WSL2 and start the bot again, everything starts working again

To Reproduce

I dont know, after ran for 1 day it happens

Expected Behaviours

It should trigger sell on target price

Screenshots

image

chrisleekr commented 3 years ago

Hmm, I can see you are using PR 77.

As you mentioned, it should've notified with the order result, but it didn't. It means the Binance API threw an exception - https://github.com/chrisleekr/binance-trading-bot/blob/feature-monitoring-simultaneously/app/cronjob/trailingTrade/step/place-sell-order.js#L114

If the Binance API throws an exception, it should be sent to Slack with the execution failed error - https://github.com/chrisleekr/binance-trading-bot/blob/feature-monitoring-simultaneously/app/cronjob/trailingTrade.js#L181

So didn't you get any further message after that?

jl9404 commented 3 years ago

Hmm, I can see you are using PR 77.

As you mentioned, it should've notified with the order result, but it didn't. It means the Binance API threw an exception - https://github.com/chrisleekr/binance-trading-bot/blob/feature-monitoring-simultaneously/app/cronjob/trailingTrade/step/place-sell-order.js#L114

If the Binance API throws an exception, it should be sent to Slack with the execution failed error - https://github.com/chrisleekr/binance-trading-bot/blob/feature-monitoring-simultaneously/app/cronjob/trailingTrade.js#L181

So didn't you get any further message after that?

yep, no more further message, when it happens, it floods my slack channel

chrisleekr commented 3 years ago

@jl9404 Even API limit error should send the slack message. Hmm, I cannot think of another reason. Could the docker container just die? Is it still running? Still, have the same issue?

jl9404 commented 3 years ago

@jl9404 Even API limit error should send the slack message. Hmm, I cannot think of another reason. Could the docker container just die? Is it still running? Still, have the same issue?

when it happens i tried to use my api key & secret to request binance api directly, it seems no problem and not getting blocked. I am using docker-compose to run it tho and it is still running & restart docker doesnt help so i guess it could be WSL2 problem, but i dont know why

chrisleekr commented 3 years ago

You mentioned it floods my slack channel

Is it like over 10 orders? Could you see the timestamp in Slack?

At the current time Binance rate limits are:

1200 requests per minute
10 orders per second
100,000 orders per 24hrs
jl9404 commented 3 years ago

its around 5 coins to be sold

LINKUSDT 
XLMUSDT 
LTCUSDT 
BNBUSDT 
XLMUSDT 
LTCUSDT Sell Action (15:56:00.104): STOP_LOSS_LIMIT
BNBUSDT Sell Action (15:56:00.105): STOP_LOSS_LIMIT
XLMUSDT Sell Action (15:56:00.127): STOP_LOSS_LIMIT
LINKUSDT Sell Action (15:56:05.117): STOP_LOSS_LIMIT
BNBUSDT Sell Action (15:56:06.112): STOP_LOSS_LIMIT
XLMUSDT Sell Action (15:56:06.111): STOP_LOSS_LIMIT
XLMUSDT Sell Action (15:56:12.108): STOP_LOSS_LIMIT
BNBUSDT Sell Action (15:56:12.122): STOP_LOSS_LIMIT
LTCUSDT Sell Action (15:56:12.149): STOP_LOSS_LIMIT
...
chrisleekr commented 3 years ago

Ok, right so 5 symbols trying to place an order actively.

https://github.com/chrisleekr/binance-trading-bot/pull/77#issuecomment-813373963

If you see the above comment, I calculated API usage of the bot. And since I have added an additional order check before removing the last buy price - https://github.com/chrisleekr/binance-trading-bot/blob/feature-monitoring-simultaneously/app/cronjob/trailingTrade/step/remove-last-buy-price.js#L129 - it might be over 1 minute API limit.

I will need to update the weight calculation, but would you be able to count how many orders were trying to make within a minute?

jl9404 commented 3 years ago
LTCUSDT Sell Action (15:59:00.129): STOP_LOSS_LIMIT
BNBUSDT Sell Action (15:59:00.130): STOP_LOSS_LIMIT
XLMUSDT Sell Action (15:59:00.148): STOP_LOSS_LIMIT
LINKUSDT Sell Action (15:59:05.126): STOP_LOSS_LIMIT
BNBUSDT Sell Action (15:59:06.108): STOP_LOSS_LIMIT
XLMUSDT Sell Action (15:59:06.107): STOP_LOSS_LIMIT
BNBUSDT Sell Action (15:59:12.113): STOP_LOSS_LIMIT
XLMUSDT Sell Action (15:59:12.114): STOP_LOSS_LIMIT
LTCUSDT Sell Action (15:59:12.130): STOP_LOSS_LIMIT
LINKUSDT Sell Action (15:59:17.132): STOP_LOSS_LIMIT
BNBUSDT Sell Action (15:59:18.113): STOP_LOSS_LIMIT
XLMUSDT Sell Action (15:59:18.124): STOP_LOSS_LIMIT
LTCUSDT Sell Action (15:59:18.138): STOP_LOSS_LIMIT
LINKUSDT Sell Action (15:59:23.160): STOP_LOSS_LIMIT
XLMUSDT Sell Action (15:59:24.134): STOP_LOSS_LIMIT
LTCUSDT Sell Action (15:59:24.135): STOP_LOSS_LIMIT
BNBUSDT Sell Action (15:59:24.135): STOP_LOSS_LIMIT
LINKUSDT Sell Action (15:59:29.144): STOP_LOSS_LIMIT

18 orders

chrisleekr commented 3 years ago

@jl9404 Let me redo the calculation. Just a quick confirm, is it operating ok now?

jl9404 commented 3 years ago

@jl9404 Let me redo the calculation. Just a quick confirm, is it operating ok now?

yeah, after i restart the WSL2 it starts to sell, but the weird point is restarting docker doesnt help 🤔

chrisleekr commented 3 years ago

I still suspect API limit. I mean you restarted docker and then a few mins later, you restarted WSL2 right?

jl9404 commented 3 years ago

I still suspect API limit. I mean you restarted docker and then a few mins later, you restarted WSL2 right?

i tried docker restart binance-bot first, it doesnt work, and then just wsl --shutdown to reboot the wsl 🙈

chrisleekr commented 3 years ago

That is odd.

Let's start a bit by bit. Let me stop my bot and calculate API weights. And make sure it was not that.

chrisleekr commented 3 years ago

It's definitely API limt. Odd that was not reported to Slack.

Binance has changed weight for accountInfo to 11 weights from 5 weights.

Trailing Trade Indicator - single coin per second
  - get symbol configuration: N/A
  - get account info: 11 weights
  - get indicators for the symbol: 1 weight
  - get open orders for the symbol: 3 weights

Total weight: 15 weight * 60 seconds = 900 weights

Trailing Trade - all coins per second
  - get symbol configuration: N/A
  - get symbol info:N/A assumming all information cached
  - ensure order placed: N/A
  - get balances: N/A assumming all information cached
  - get open orders: N/A
  - get indicators: N/A
  - handle open orders: assumming there are open orders
      - cancel order: 1 weight
      - get open orders: 3 weights assumming cancelling is failed due to high volume
      - get account info: 11 weights
  - get balances: N/A
  - determine actions: N/A
  - place buy order: N/A because all orders were sell
  - place sell order: 
      - place order: 1 weight
      - get open orders: 3 weights
      - get account info: 11 weights
  - place sell stop loss order: N/A
  - get balances: N/A
  - remove last buy price: N/A assumming cache worked correclty
  - save data to cache: N/A

Total weight: 30 weight * 18 orders = 540 weight

Estimated API weight usage: 1440

I will need to check the used weight and put the bot on hold the bot until API weight is cleared.

jl9404 commented 3 years ago

thanks!

chrisleekr commented 3 years ago

It will take some time because I need to refactor some code. I wanna say try not to use aggressive configuration for now, but :shrug: it's up to you!

chrisleekr commented 3 years ago

Ok @jl9404

I have updated PR #77 to check API limit before placing an order.

Try to update your bot and see whether that issue occurs again or not. And also in the frontend, you will be able to see how much API weight used per minutes.

image

Let me know.

jl9404 commented 3 years ago

hmm, still having the issue after pulling the #77

Used Weight (1m): 106

and those sellable symbols are locked 🤔

Update 1: it seems a clock sync issue, but it wont send error to slack

Error: Timestamp for this request is outside of the recvWindow

https://github.com/chrisleekr/binance-trading-bot/blob/feature-monitoring-simultaneously/app/cronjob/trailingTrade.js#L173

chrisleekr commented 3 years ago

Oh oh, nice catch on error.

Error: Timestamp for this request is outside of the recvWindow

That can cause when the response is not received within the expected time or your machine time is not synced

Refer this https://github.com/ccxt/ccxt/issues/936#issuecomment-425248158

Try with syncing the time first.

jl9404 commented 3 years ago

Oh oh, nice catch on error.

Error: Timestamp for this request is outside of the recvWindow

That can cause when the response is not received within the expected time or your machine time is not synced

Refer this ccxt/ccxt#936 (comment)

Try with syncing the time first.

yep, it works fine after i sync the clock but should we stop trading if there are recurring errors like this? since it wont send to slack, so it will be a bit hard to debug

chrisleekr commented 3 years ago

@jl9404

Ahhhh good to hear.

Actually, now I understand why you didn't receive the following message because I excluded that error! That explains a lot.

but should we stop trading if there are recurring errors like this?

Not actually, I found the error is happening based on your network situation. I exclude it because it happens too often and works well after. It will spam your slack if include that error.

Very good. So this was a time sync issue again.

Would you mind putting your steps to Wiki, please? https://github.com/chrisleekr/binance-trading-bot/wiki/Troubleshooting

And close the issue :)