如何解决尝试创建保证金贷款时,币安 api 函数崩溃
我正在为 api 接口编写一个函数,以在多头和空头保证金头寸之间快速切换。我在隔离帐户中有足够的资金来避免 MIN_NOTATIONAL 错误。创建订单后,债务和余额不会更新,当我调用 switch() 函数时,这会导致程序崩溃,因为程序要求从空账户借钱。
当我尝试使用 closeLong() 和 closeShort() 偿还贷款时,还会有少量余额,我无法弄清楚这是为什么,因为我使用了从 client.get_isolated_margin_account() 字典中提取的确切债务金额。
编辑:该货币对是 OCEAN/USDT,这是 3 倍保证金,这就是为什么 marginBase 和 marginQuote 的乘数是 3
代码:
isLongOpen = False
acct = client.get_isolated_margin_account()
sym = acct['assets'][1]['symbol']
def getBaseBalance():
rawBase = round(Decimal(acct['assets'][1]['baseAsset']['free']),2)
baseBalance = str(rawBase)
return baseBalance
def getMarginBase():
rawBase = round(Decimal(acct['assets'][1]['baseAsset']['free']),2)
marginBase=str(Decimal(3)* rawBase)
return marginBase
def getQuoteBalance():
rawQuote = round(Decimal(acct['assets'][1]['quoteAsset']['free']),2)
baseQuote = str(rawQuote)
return baseQuote
def getMarginQuote():
rawQuote = round(Decimal(acct['assets'][1]['quoteAsset']['free']),2)
marginQuote=str(Decimal(3)* rawQuote)
return marginQuote
def baseDebt():
baseLoan = round(Decimal(acct['assets'][1]['baseAsset']['borrowed']),2)
print("Ocean Debt: "+str(baseLoan))
return baseLoan
def quoteDebt():
quoteLoan = round(Decimal(acct['assets'][1]['quoteAsset']['borrowed']),2)
print("USDT Debt: "+str(quoteLoan))
return quoteLoan
def checkDebt():
if baseDebt()!=0:
print("Short Open")
isLongOpen = False
return isLongOpen
elif quoteDebt()!=0:
print("Long Open")
isLongOpen = True
return isLongOpen
def openLong():
client.create_margin_order(
symbol=sym,side= SIDE_BUY,type= ORDER_TYPE_MARKET,sideEffectType= 'MARGIN_BUY',isIsolated='TRUE',quoteOrderQty=getMarginQuote())
print("Opened Long")
def closeLong():
client.create_margin_order(
symbol=sym,side=SIDE_SELL,type=ORDER_TYPE_MARKET,sideEffectType= 'AUTO_REPAY',quoteOrderQty=quoteDebt())
print("Closed Long")
def openShort():
client.create_margin_order(
symbol=sym,quantity=getMarginBase())
print("Opened Short")
def closeShort():
client.create_margin_order(
symbol=sym,side=SIDE_BUY,quantity=baseDebt())
print("Closed Short")
def switch():
if(checkDebt() == True):
closeLong()
print("Base " +getBaseBalance())
print("Quote "+getQuoteBalance())
openShort()
print("Base " +getBaseBalance())
print("Quote "+getQuoteBalance())
else:
closeShort()
print("Base " +getBaseBalance())
print("Quote "+getQuoteBalance())
openLong()
print("Base " +getBaseBalance())
print("Quote "+getQuoteBalance())
return isLongOpen
print(sym)
print("Base " +getBaseBalance())
print("Quote "+getQuoteBalance())
print(checkDebt())
print(switch())
print("Base " +getBaseBalance())
print("Quote "+getQuoteBalance())
print("END")
OCEANUSDT
Base 39.36
Quote 0.00
Ocean Debt: 0.00
USDT Debt: 39.54
Long Open
True
Ocean Debt: 0.00
USDT Debt: 39.54
Long Open
USDT Debt: 39.54
Closed Long
Base 39.36
Quote 0.00
line 128,in openShort
client.create_margin_order(
BinanceAPIException: APIError(code=-11008): Exceeding the account's maximum borrowable limit.
OCEANUSDT
Base 0.00
Quote 58.42
Ocean Debt: 25.95
Short Open
False
Ocean Debt: 25.95
Short Open
Ocean Debt: 25.95
Closed Short
Base 0.00
Quote 58.42
line 108,in openLong
client.create_margin_order(
BinanceAPIException: APIError(code=-11008): Exceeding the account's maximum borrowable limit.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。