尝试创建保证金贷款时,币安 api 函数崩溃

如何解决尝试创建保证金贷款时,币安 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 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?