Hi!
I’m facing the same problem, getting error:{"code":-2019,msg:"Margin is insufficient."}
.
I do understand the following:
When placing a market (buy) order, we set the quantity of the symbol we want to buy, not the amount of USDT we want to “spend”. During high market volatility, when price changes fast, it is easy to miscalculate the quantity we can buy. Example:
I have 120 USDT in my balance. I want to buy $XYZ coin (with 10x leverage) which price was 0.3 USDT the last time my script checked. This means with leverage I can use 120x10=1200 USDT for the trade meaning that I can buy 1200/0.3=4000 XYZ. The problem is that if I send the market buy order for 4000 XYZ (trying to use 100% of my balance), and the price increased only just a little bit (for example to 0.301) this means that the 4000 XYZ coin will cost 4000x0.301=1204 USDT - which (with 10x leverage) would require 1204/10=120.4 USDT in my balance and this way it will result in a “Margin is insufficient” error.
Because of this I edited my algorithm to use only 75% of my balance, but I STILL got the “Margin is insufficient” error. In my case I’ve checked the price of the coin in a 1 second(!) chart and even if the price spiked, using only 75% of my balance for the calculations, the order should have been successful. I also tested my algorithm in a non volatile market, and the order was successful every single time, so the problem is NOT with my algorithm.
Really looking for a solution!