> ## Documentation Index
> Fetch the complete documentation index at: https://docs.fairground.fi/llms.txt
> Use this file to discover all available pages before exploring further.

# closePosition

> Python SDK reference for closePosition.

Close a position (fully or partially) and return the updated position object.

<ParamField path="marketId" type="int" required>
  The market ID of the position to close
</ParamField>

<ParamField path="size" type="float">
  Optional size to reduce by. If None, closes the entire position
</ParamField>

<ParamField path="priceThreshold" type="float">
  Optional price threshold for the reduce order
</ParamField>

<ResponseField name="returns" type="string" required>
  Updated position object as a dictionary with modified size field,
  or None if position doesn't exist
</ResponseField>

```python theme={null}
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)
```
