> ## 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.

# updatePositionMargin

> Python SDK reference for updatePositionMargin.

Update position margin and return the updated position object.

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

<ParamField path="margin" type="float" required>
  The new target margin amount
</ParamField>

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

```python theme={null}
async def updatePositionMargin(marketId: int, margin: float):
    existing = await retrieveExistingPosition(marketId)
    if existing is None:
        print('No position to update')
        return None
    original_margin = existing.allocated_margin
    if margin > original_margin:
        amount = margin - original_margin
        print(f'Increasing margin from {original_margin} to {margin} (adding {amount})')
        await addMargin(marketId, amount)
    elif margin < original_margin:
        amount_to_extract = original_margin - margin
        print(f'Decreasing margin from {original_margin} to {margin} (extracting {amount_to_extract})')
        await extractMargin(marketId, amount_to_extract)
    else:
        print(f'Margin unchanged at {margin}')
        return MessageToDict(existing, always_print_fields_with_no_presence=True)
    existing.allocated_margin = margin
    print(f'Transaction submitted successfully (margin: {original_margin} → {margin})')
    return MessageToDict(existing, always_print_fields_with_no_presence=True)
```
