ApeX-Protocol / apexpro-openapi

apexpro openapi python sdk
Other
27 stars 11 forks source link

Change leverage #17

Closed Bennch closed 3 months ago

Bennch commented 10 months ago

Isn't it possible to change the leverage via API ?

MrrDino commented 7 months ago

Hi! Here's a way to do it:

def set_leverage(self, symbol: str, leverage: float):
    new_leverage = round(leverage, 2)
    initialMarginRate = str(round(1 / new_leverage, 5))

    response = self.apex_ex.set_initial_margin_rate_v2(symbol=symbol, initialMarginRate=initialMarginRate)
    try:
        if type(response.get("timeCost")) == int:
            return True
        else:
            return False
    except Exception as e:
        raise Exception(f"Apex set_leverage -> {e}")

And here's handle: https://api-docs.pro.apex.exchange/#privateapi_v2-post-sets-the-initial-margin-rate-of-a-contract-v2