Skip to main content
Update position leverage and return the updated position object.
marketId
int
required
The market ID of the position to update
leverage
float
required
The new target leverage
returns
string
required
Updated position object as a dictionary with modified leverage field, or None if position doesn’t exist
async def updatePositionLeverage(marketId: int, leverage: float):
    existing = await retrieveExistingPosition(marketId)
    if existing is None:
        print('No position to update')
        return None
    original_leverage = existing.leverage
    if leverage > original_leverage:
        is_long = existing.side == Side.SIDE_LONG
        print(f'Increasing leverage from {original_leverage}x to {leverage}x')
        await openOrder(marketId, existing.allocated_margin, convertFloatToUINT(leverage, LEVERAGE_SCALAR), is_long, existing.entry_price)
    elif leverage < original_leverage:
        print(f'Decreasing leverage from {original_leverage}x to {leverage}x')
        size_reduction = existing.nominalSize * (1 - leverage / original_leverage)
        orderId = existing.associated_orders[0].order_id
        await reduceOrder(marketId, orderId, convertFloatToUINT(size_reduction, LEVERAGE_SCALAR), existing.entry_price)
    else:
        print(f'Leverage unchanged at {leverage}x')
        return MessageToDict(existing, always_print_fields_with_no_presence=True)
    existing.leverage = leverage
    print(f'Transaction submitted successfully (leverage: {original_leverage}x → {leverage}x)')
    return MessageToDict(existing, always_print_fields_with_no_presence=True)