I opened my Margin Wallet and transferred some USDT into that wallet. I can place a long order with "create_margin_order" function in my code, but I cannot borrow BTC with "create_margin_loan" function before I open a short position.
Here is my code:
def btn_test_Clicked(self):
current_time = datetime.now().strftime("%Y%m%d%H%M%S.%f")
# check the amount which I can borrow
order_result = self.binance_client.get_max_margin_loan(asset="BTC")
print("Binance Max Loan = " + str(order_result))
# borrowing the BTC
order_result = self.binance_client.create_margin_loan(asset="BTC", amount=1.5)
print("Binance Loan Result = " + str(order_result))
# Place an order
self.order_result = self.binance_client.create_margin_order(symbol="BTCUSDT", side=SIDE_SELL,type=ORDER_TYPE_LIMIT, timeInForce=TIME_IN_FORCE_GTC, quantity=1.5, price="8000")
print("Binance Margin Order Result = " + str(order_result))
I use Python and the IDE is PyCharm. After clicking the button, I can see the response about the max borrowing amount. After that, my program was terminated and the message is:
Process finished with exit code -1073740791 (0xC0000409)
It is obviously that my code is wrong about the borrowing part. What is the right way to borrow with API in Binance? Thank you.
There is no need to borrow first, Binance support automatic borrow and sell function.
Here is the code:
order_result = client.create_margin_order(symbol="BTCUSDT", side=SIDE_BUY, type=ORDER_TYPE_LIMIT, timeInForce=TIME_IN_FORCE_GTC, sideEffectType="MARGIN_BUY", quantity=0.5, price=3000)
The important part is the "sideEffectType" parameter option. Borrow buy and borrow sell to open the position are all set to "MARGIN_BUY". And set to "AUTO_REPAY" to close the position. It can square the position and repay the debt at the same time.