Hi
I am using API to open a position to short sell in isolated margin acc and then close that position.
Once I have a position open, I use this code to determine the amount I have open position (margin short sell)
currently_open = client.get_isolated_margin_account(symbols='NULSUSDT')
amount_to_close = currently_open['assets'][0]['baseAsset']['borrowed']
remainder_close = Decimal(amount_to_close) % Decimal(1)
final_amount_to_close = round(Decimal(amount_to_close) - Decimal(remainder_close),1)
As you can see, I am using baseAsset borrowed qty to determine the amount I need to close.
And then close the position
order_close = client.create_margin_order(symbol='NULSUSDT',type=ORDER_TYPE_MARKET,side=SIDE_BUY,quantity=final_amount_to_close, sideEffectType='AUTO_REPAY',isIsolated=True)
In this case, this does not close all the position, it will close the amount I had originally sold (in short sell) but it will not include the tiny bit of interest that still shows in positions. (I could try and close the amount + interest but I cannot do that since that goes against the trading rule for this pair(less than 1 NULSUSDT).
So basically it has not completely closed the position. I want to be completely flat at the end of this process.
It seems the only way I can do this is manually using the ‘Close all positions’ feature.
Is there any way to do this using API ?
I am not sure how ‘Close all positions’ manages to make a trade that goes against the step size rule.
Thanks
It is impossible to close a flat position, you always have a balance left in your base asset because of the interest and the stepsize filter, let’s suppose that your interests are 0.000004 and your stepsize is 0.0001 to be able to pay those interests you would have to buy at least an amount equal to the stepsize leaving a balance of 0.000096 in your base asset, what I do is save that balance in a variable and so in the following operations I validate if I have a balance to pay the interest and if it is at 0 there is no other choice but to buy the minimum amount that the stepsize marks you, I leave you part of the code for this situation where the basefree field stores the balance of the base asset
this.market.elapsetime = Math.ceil((Date.now() - this.market.otime) / intervals['1h'].ms)
this.market.interest = this.market.interest * this.market.elapsetime
this.market.qty = (this.market.qty + (this.basefree >= this.market.interest ? 0 : this.stepsize * Math.ceil((this.market.interest - this.basefree) / this.stepsize))).fix(this.fixqty)
marketOrder(this.account.toUpperCase(), this.kl.symbol, 'BUY', this.market.qty, null, 'AUTO_REPAY')
Thanks, So I have tried this , When close a (short sell) position, I buy an extra unit of the coin which will pay the interest and it will leave a certain amount as an asset which I can transfer to my spot wallet. So this works for me for now.
Just a side note, the reason I am fussed about ‘completely’ closing the position is that I need to deactivate the isolated margin pair after this process as I am trade lots of coin (low freq strategy) and I am limited to only 10 isolated pairs at a time as a Regular user.
However, all of this still begs the question, how is the webgui feature ‘Close All positions’ managing to close it essentially bypassing the step size rule at the backend ? And there is API call for that ?