Skip to main content
Close a position (fully or partially) and return the updated position object.
marketId
int
required
The market ID of the position to close
size
float
Optional size to reduce by. If None, closes the entire position
priceThreshold
float
Optional price threshold for the reduce order
returns
string
required
Updated position object as a dictionary with modified size field, or None if position doesn’t exist
async def closePosition(marketId: int, size: float=None, priceThreshold: float=0):
    existing = await retrieveExistingPosition(marketId)
    if existing is None:
        print('No position to close')
        return None
    original_size = existing.nominalSize
    orderId = existing.position_id
    print('position', existing)
    if size is None:
        size_to_reduce = original_size
        print(f'Closing full position (size: {original_size})')
    else:
        size_to_reduce = size
        print(f'Partially closing position (reducing size by {size} from {original_size})')
    await reduceOrder(marketId, orderId, size_to_reduce, priceThreshold)
    new_size = original_size - size_to_reduce
    existing.nominalSize = new_size
    print(f'Transaction submitted successfully (size: {original_size}{new_size})')
    return MessageToDict(existing, always_print_fields_with_no_presence=True)