From 6a5ca70301c8b21343a98698a4f824e855048806 Mon Sep 17 00:00:00 2001 From: Tom Date: Wed, 15 Nov 2023 11:05:23 +0000 Subject: [PATCH] feat: isolated margin tweaks (#2055) * feat: Clarifying and improving isolated margin specs --- protocol/0019-MCAL-margin_calculator.md | 24 ++++++++++++++++-------- wordlist.txt | 1 + 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/protocol/0019-MCAL-margin_calculator.md b/protocol/0019-MCAL-margin_calculator.md index 872127220..430ff143c 100644 --- a/protocol/0019-MCAL-margin_calculator.md +++ b/protocol/0019-MCAL-margin_calculator.md @@ -157,19 +157,19 @@ When submitting, amending, or deleting an order in isolated margin mode and cont NB: This means that a party's order could partially match, with a trade executed and some funds moved to the margin account with correct leverage whilst the rest of the order is immediately stopped. -When submitting, amending, or deleting an order in isolated margin mode and an auction is active there is no concept of an order trading immediately on entry, however the case of someone putting in a sell order for a very low price must be handled (as it is likely to execute at a much higher price). To handle this, when in an auction the amount taken into the order margin account should be the larger of either `limit price * size * margin factor` or `max(mark price, indicative uncrossing price) * size * margin factor`. All other steps are as above. +When submitting, amending, or deleting an order in isolated margin mode and an auction is active there is no concept of an order trading immediately on entry, however the case of someone putting in a sell order for a very low price must be handled (as it is likely to execute at a much higher price). To handle this, when in an auction the amount taken into the order margin account should be the larger of either `limit price * size * margin factor` or `max(mark price, indicative uncrossing price) * size * margin factor`. After uncrossing, all remaining open order volume should be rebased back to simply `limit price * size * margin factor` in the order margin account. All other steps are as above. ### Increasing Position When an order trades which increases the position (increasing the absolute value of the trader's position), the target amount to be transferred is calculated as: -```math +$$ \text{margin to add} = \text{margin factor} \cdot \text{sum across executed trades}(|\text{trade size}| \cdot \text{trade price}) -``` +$$ This will be taken by performing three steps: - 1. The margin to add as calculated here is compared to the margin which would have been placed into the order margin account for this order, `limit price * remaining size * margin factor`. + 1. The margin to add as calculated here is compared to the margin which would have been placed into the order margin account for this order, `limit price * total traded size * margin factor`. 2. If it is equal, this amount is taken from the order margin account and placed into the margin account 3. If the order traded at a price which means less margin should be taken then the amount `margin to add` above is taken from the order margin account into the margin account and the excess is returned from the order margin account to the general account @@ -177,11 +177,19 @@ NB: In implementation, for any volume that trades immediately on entry, the addi ### Reducing Position -When an order trades which reduces the trader's current position the amount to be withdrawn from the margin account is determined by the fraction of the position which is being closed. However, this fraction should also take into account that the entire position's margin may be due to change since the current trading price may have diverged from the last mark price update. As such the margin released should be calculated as: +When an order trades which reduces the trader's current position the amount to be withdrawn from the margin account is determined by the fraction of the position which is being closed. If the entire position is being closed (either as a result of changing sides as below or just moving to `0`) then the entirety of the margin balance will be moved back to the general account. -```math -\text{margin to remove} = \text{margin required for entire position at VWAP trade price} \cdot \frac{|\text{total size of new trades}|}{|\text{entire position prior to trade}|} -``` +However, if only a fraction is being closed then this fraction should also take into account that the entire position's margin may be due to change, since the current trading price may have diverged from the last mark price update. As such the margin released should be calculated by first calculating what the `theoretical account balance` of the margin account would be if the entire pre-existing position (i.e. the position prior to any reduction) were margined at the VWAP of the executed trade, then taking the proportion of that corresponding to the proportion of the position closed. + +$$ +\text{margin to remove} = \text{theoretical account balance} \cdot \frac{|\text{total size of new trades}|}{|\text{entire position prior to trade}|} +$$ + +Concretely, this should resolve to: + +$$ +\text{margin to remove} = (\text{balance before} + \text{position before} \cdot (\text{new trade VWAP} - \text{mark price})) \cdot \frac{|\text{total size of new trades}|}{|\text{entire position prior to trade}|} +$$ Note: This requires a calculation of the position's margin at trade time. diff --git a/wordlist.txt b/wordlist.txt index bae7b667c..4e8ae5725 100644 --- a/wordlist.txt +++ b/wordlist.txt @@ -216,6 +216,7 @@ pseudorandom pubkey quant rata +rebased recollateralise ReferralSet reimplemented