Closed afs091 closed 1 year ago
Hi André,
Thank you very much for raising this issue. I spent quite a bit of time today, and I think I know what's wrong. First of all, you are right - this error should be handled more gracefully.
Why does it happen?
My current theory is that the quantity when selling is broken. When you buy/sell, you pay the fee in the currency you own before the exchange. So when you are buying 12.80000000 ADAs for EURos - you will pay the fee in EURos. Then when you want to sell it, you can't sell 12.80000000 ADA as you need to leave enough for a selling fee (which will be in ADA).
I never realised that this is a problem as I'm paying my fees using BNB (and honestly forgot that it's still set up to do that) :facepalm:
I bet that subtracting the fee from the quantity of ADA on that sell order would fix it.
That would explain the selling. Buying error is a result of trying to buy when you already purchased, but the process died on selling :shrug: (I'm not sure why the state wasn't picked up from the leader :thinking: - completely different problem)
Let me know what do you think about my idea - it shouldn't be complicated to fix this(calculating the correct sell quantity) and give it a try as you have it all set up :wink:
Hi Kamil,
Thank you for helping me with this rookie mistake!
I think that you correctly spot the problem. I don't have currency to pay the fees :facepalm: :facepalm: :facepalm:
I will setup some currency on both trade sides to pay the fees and try it first. But I am afraid that eventually I will run out these currencies that I set aside for paying fees and the error will raise again. If that happens I will try to calculate the sell quantity by subtracting the the fee from the quantity of ADA.
Thank you very much!
Hi Kamil,
Sometimes I received a {:error, %Binance.InsufficientBalanceError{reason: %{code: -2010, msg: "Account has insufficient balance for requested action."}}} message from Binance API while trying your solution for trading crypto in production environment.
I am using the source code of chapter 17 and I have sufficient balance in my Binance account.
This error occurs in Naive.Trader module when it calls:
Here are the logs:
As you can see, I am trading successfully and then suddenly I start getting errors due to insufficient balance but when I checked the Binance app I noticed that I have enough balance for the operation.
When the error occurs in {:ok, %Binance.OrderResponse{} = order} = @binance_client.order_limit_buy(symbol, quantity, price, "GTC") call the supervisor keeps restarting a new trader and eventually the trader receives a {:ok, %Binance.OrderResponse{} = order} message and continues.
In the other case, when the error occurs in {:ok, %Binance.OrderResponse{} = order} = @binance_client.order_limit_sell(symbol, quantity, sell_price, "GTC") the supervisor restarts a new trader which expects a trade event to place a new sell order but this trade event has already broadcasted when the error firstly occur. Consequently, the trader get stuck in that state and I have to manually sell the crypto in Binance app and to restart everything.
I think this error occurs due to some synchronization problem within the Binance data warehouses, so I modified the code to acknowledge this problem.
In Naive.Trader:
And in the Naive.Leader:
So, let me know your thoughts about this kind of problem and if you have another solution to solve it.
Thank you very much for your attention!
André Santos