Python Binance Api Margin Short Order

Viewed 50

Good day to everyone. I do not face any problem when creating margin long order and paying loan back. But when I try to create a short order, I get an error. First, the borrowing function from the system works, but when creating a margin order I am getting APIError(code=-1013): Filter failure: LOT_SIZE. I tried multiplication by 0.99 but it didn't work. I've searched before on many different pages to see if there is an article about it. Please do not redirect to a different page.

class ShortOrderEntry(Order):
    def __init__(self):
        self.acc = Account.getInstance()

    def Execute(self):
        try:
            self.GetBtcLoan()
            order = self.acc.client.create_margin_order(symbol="BTCUSDT", side=SIDE_SELL, isIsolated='TRUE',
                                                    type=ORDER_TYPE_MARKET,
                                                    quantity=self.acc.FloorPrecisionFix(
                                                        self.MaxBTCAmount() / 100 * 99, 6))
             print(order)
        except Exception as e:
             print("Something Went Wrong While Entering Short Enter Position", e)

    def GetBtcLoan(self):
        try:
            self.acc.client.create_margin_loan(asset="BTC", amount=self.MaxBTCAmount(), symbol="BTCUSDT",
                                           isIsolated='TRUE')
        except Exception as e:
            print("Something Went Wrong While taking short entry loan", e)

    def MaxBTCAmount(self):
        return self.acc.FloorPrecisionFix(self.acc.GetMaxMarginAmount("BTC") / 100 * 99, 6)
1 Answers

I'm writing this answer in case anyone will have a similar problem as mine in the future. They have separate precisions for each symbol. For BTCUSDT, this precision is 5. and also you don't need to borrow from the system with code. The system itself takes care of this. So the GetBtcLoan() function is an unnecessary function here.

class ShortOrderEntry(Order):
    def __init__(self):
        self.acc = Account.getInstance()
        self.database = Database.getInstance()
    def Execute(self):
        try:
        # self.GetBtcLoan()
            order = self.acc.client.create_margin_order(symbol="BTCUSDT", side=SIDE_SELL, type="MARKET",quantity=self.MaxBTCAmount(), sideEffectType="MARGIN_BUY",isIsolated=True)
            order['price'] = self.acc.CalculateWeightedAvg(order)
            order['side'] = "SHORTENTRY"
            order['commission'] = self.acc.SumOfCommission(order)
            self.database.CreateNewMarginLog(order)
            ClientData.marginLastPosition = order['price']
            print(order)
        except Exception as e:
            print("Something Went Wrong While Entering Short Enter Position", e)

    def MaxBTCAmount(self):
         return self.acc.FloorPrecisionFix(self.acc.GetMaxMarginAmount("BTC") / 100 * 99, 5)
Related