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
5.04k stars 1.09k forks source link

Bot sometimes immediately forgets/removes last buy price, sells, marks it down as 100% loss #541

Closed DecisiveDesignDE closed 1 year ago

DecisiveDesignDE commented 1 year ago

Hello there, first off thank you for providing your bot and code. I have been testing it for months now and am very happy with the web UI and all the possible customizations.

I have found a big Issue that seems to happen multiple times per day. The Bot buys a specific Coin, but then immediately forgets/deletes the last buy price, then immediately sells 100% of the coin and marks it down as a 100% loss. It thinks it bought the coin normally but sold it for 0 USD which simply isn't possible.

Here is an example of what this looks like in the Web UI: flow1

To compare, here is what it looks like in Binance: flow_binance

As you can see the bot bought FLOW at 1.107 but immediately sold it again 4 minutes after for 1.108 which I assume was the curent price. My profit margin is set to 0.3% so this sell was not triggered by a sell order.

I have seen this happen multiple times in both directions, it happens with profits too: image

Sadly I couldn't grab the logs for these TRX and BTC trades. I do however have the logs for the FLOW trade listed above, attached:
https://drive.google.com/file/d/1uAUHsDuO_xWfJ-p4L0zQhQ0-qOOVOrDA/view?usp=sharing

Issue should've occured around 12:47 time in the log

I have just one theory on why this happens:
It seems to only happen once I raise the purchase amount above 1000USD or so. It seems like when buy or sell orders get stuck because they only get 5-10% partially filled, this issue is more likely to happen

UPDATE: The bot has now bought FLOW again, sold it and added the missing amount to the profit: image

I have uploaded the new log for this as well, tis occured at 13:07 in the log https://drive.google.com/file/d/12mfMIVvkD7fsy1aMmYtm7TwCApj0w3b8/view?usp=sharing

chrisleekr commented 1 year ago

Hi @DecisiveDesignDE After carefully reviewing your log, I found some odd.

The log recorded at "2022-11-29T12:23:05.446Z" with the message "Placed new stop loss limit order for buying of grid trade #2.". At that point, grid trade 1 is recorded as FILLED with 1801.62000000 quantity. However, in your account, FLOW balance is only 249.05000000.

It means you were manually trading FLOW outside of the bot. So the bot does not know what is going on.

Were you buying/selling manually?

DecisiveDesignDE commented 1 year ago

Thanks for the speedy response! I did not touch the bot or trade manually at all during this time. I was simply observing my settings. I feel like you described the issue correctly though - the bot thought it bought 1801 quantity but did not.

I have also seen this happen the other way around, resulting in the bot buying a certain amount of currency but then completely forgetting it did while the currency just sits in my wallet unmonitored. It is very odd that only I seem to be having this issue.

If it helps I'm running everything with docker on an ubuntu server close to japan. Is there any data I can provide you that would help with this issue?

chrisleekr commented 1 year ago

Right, so you are not doing manually.

It's like you have another bot running. Are you sure you are not running another bot?

DecisiveDesignDE commented 1 year ago

I am not running another bot, nor am I trading manually. I am just using your bot.
I have uploade another log for you which might help, this time from the Coin "ZRX"

https://drive.google.com/file/d/10pd4IEzC9ceCfVGkBAo4mYacKHJQlXwT/view?usp=sharing

If you look at timestamps around 14:45 - 14:47 the Bot bought ZRX, but then immediately deleted the last buy price, acted like it never bought it and then tried to buy it again. This time interfere: I cancelled the second order and entered the last buy price manually. But the issue happened before that.

I wish I could help you more, but it might just be an issue with either the server I'm running everything on or even the amounts of coins I'm trying to sell/buy? Because nobody else had this issue so far.

chrisleekr commented 1 year ago

@DecisiveDesignDE

I am not running another bot, nor am I trading manually. I am just using your bot.

Noted.

Let me see the log. And get back to you.

chrisleekr commented 1 year ago

Ok, I found an issue.

image

If you see the log, when there was a FILLED order, ensure-grid-trade-order-executed was triggered 3 times.

It is not an expected behaviour because 1st ensure-grid-trade-order-executed is still running until 2022-12-01T14:47:18.263Z.

2nd/3rd ensure-grid-trade-order-executed were triggered while 1st ensure-grid-trade-order-executed is running.

Firstly, the quantity is calculated wrong. It ended up 15561 quantity, which is incorrect. Secondly, after the grid trade is updated at 2022-12-01T14:47:17.977Z, the last buy is removed because the base quantity is not updated. This is also not expected as ensure-grid-trade-order-executed is still running.

So long story short, this is a bug. I will find a way to fix it.

habibalkhabbaz commented 1 year ago

Hello @chrisleekr @DecisiveDesignDE

May I know on which version you are running the bot @DecisiveDesignDE ?

chrisleekr commented 1 year ago

@DecisiveDesignDE Hey just to make sure, are you running 3 bot instances by any chance? Probably not.

DecisiveDesignDE commented 1 year ago

@habibalkhabbaz I am using the most recent version of the bot on master @chrisleekr nope, just the one! I'm glad you found something. It is in my opinion not a massive issue as you shouldn't leave a bot unattended anyways but definitely worth fixing

habibalkhabbaz commented 1 year ago

Hello @DecisiveDesignDE,

I reached the same conclusion as @chrisleekr

The log recorded at "2022-11-29T12:23:05.446Z" with the message "Placed new stop loss limit order for buying of grid trade https://github.com/chrisleekr/binance-trading-bot/issues/2.". At that point, grid trade 1 is recorded as FILLED with 1801.62000000 quantity. However, in your account, FLOW balance is only 249.05000000.

And based on what you explained here:

If you look at timestamps around 14:45 - 14:47 the Bot bought ZRX, but then immediately deleted the last buy price, acted like it never bought it and then tried to buy it again. This time interfere: I cancelled the second order and entered the last buy price manually. But the issue happened before that.

I will assume that the main issue is last buy price gets removed for ZRX and FLOW.

So what's your last buy price remove threshold setting?

chrisleekr commented 1 year ago

@habibalkhabbaz

I was able to replicate the issue in my local. image

Steps:

  1. When the symbol has enough balance to sell, set the last buy price to trigger the sell action.
  2. The bot will place an order.

However, I don't expect the bot placing the sell order twice. 2nd determine-action is triggered while 1st job is running.

We do have concurrent 1 for the process, which supposes to prevent this kind of concurrency issue. I am still digging.

habibalkhabbaz commented 1 year ago

@chrisleekr

Ops, I see. So the issue might be with the concurrency. The strange thing is that there are around 0.5 seconds between the 1st determine-action and the 2nd determine-action which is enough I think to not trigger two sell orders. I will look into it while you are already checking.

chrisleekr commented 1 year ago

@habibalkhabbaz

Yeah, please take a look. You may find something I miss. Thanks @habibalkhabbaz !!

chrisleekr commented 1 year ago

@DecisiveDesignDE

Hi,

I have a fix for you.

It has many changes, but the main change is just setting the limiter for the queue.

Could you read what's changed, and if you are comfortable, could you test it, please?

https://github.com/chrisleekr/binance-trading-bot/pull/551

I've written the steps to get the test version.

DecisiveDesignDE commented 1 year ago

I have read through it and have been using the new version for around 16 hours. So far the issue is gone!

However idk if it's related to the test-version, but I am now gettng the "failed to load account information" screen after like 16 hours. The bot is still trading and I see all the bubbles popping up, but I see the new "Failed to load your account information." screen.

This issue appears to be fixed though. Thanks a lot.

chrisleekr commented 1 year ago

@DecisiveDesignDE

Interesting. That error message is #544 changes, which is unrelated to this PR.

  1. When you get the "failed to load account information" error, do you get any error notifications in Slack?
  2. Was it only once or continuously happening?
  3. What is your server specs?
habibalkhabbaz commented 1 year ago

@habibalkhabbaz

Yeah, please take a look. You may find something I miss. Thanks @habibalkhabbaz !!

Sorry for the late reply @chrisleekr I did another scenario to replicate the issue but no luck. My queue only processes 1 job concurrently.

I am running Docker on Windows 11.

Here is what I did:

  1. In executeFor function I intentionally added two calls for queue.add which will add 2 jobs for a symbol at the same time.
  2. I added some delay in trailingTrade so that I can see if the jobs waiting for each other or not.
  3. As a result, I see that the queue has 1 job running and the other job is waiting for the 1st job to finish and then start to process.

I am just curious how can the queue process two jobs at the same time. Maybe the environment causes this?

chrisleekr commented 1 year ago

@habibalkhabbaz Yeah, it’s not happening every time. After I took the screenshot, I also could not replicate.

I think with my change, it gives more confident it won’t have multiple jobs running.

@DecisiveDesignDE Can you confirm the fix is working?

DecisiveDesignDE commented 1 year ago

@chrisleekr Yes, ever since switching branches the issue has not occured again. Thanks a lot!