Update position margin and return the updated position object.
The market ID of the position to update
The new target margin amount
Updated position object as a dictionary with modified allocated_margin field,
or None if position doesn’t exist
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)