I am writing a function for an api interface to quickly switch between long and short margin positions. I have more than enough funds in the isolated account to avoid the MIN_NOTATIONAL error. The debt and balance do not update after creating orders and this crashes the program when I call the switch() function because the program asks to borrow from an empty account.
When I try to repay the loan using closeLong() and closeShort() a small balance also remains and I cannot figure out why it is because I use the exact amount of debt pulled from the client.get_isolated_margin_account() dictionary.
EDIT: the pair is OCEAN/USDT which is 3x margin, that's why the multiplier for marginBase and marginQuote is 3
Code:
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',
isIsolated='TRUE',
quoteOrderQty=quoteDebt())
print("Closed Long")
def openShort():
client.create_margin_order(
symbol=sym,
side=SIDE_SELL,
type=ORDER_TYPE_MARKET,
sideEffectType= 'MARGIN_BUY',
isIsolated='TRUE',
quantity=getMarginBase())
print("Opened Short")
def closeShort():
client.create_margin_order(
symbol=sym,
side=SIDE_BUY,
type=ORDER_TYPE_MARKET,
sideEffectType= 'AUTO_REPAY',
isIsolated='TRUE',
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")
This is the output of this code for switching from shorts and switching from longs:
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.