How to calculate the max quantity when we have open positions

I have followed this post to calculate the max quantity without any positions.
Max quantity calculation
However, I cannot make the math right when we have open positions. Does anyone know how to calculate the max quantity with open positions? The following is a screenshot from testnet.

Hi there, if you don’t find luck in discovering the formula to calculate the max quantity, I think a best alternative would be directly reaching out to the Customer Support.