Bug in margin calculations when dynamic leverage is in play

Bug in margin calculations when dynamic leverage is in play
12 Feb 2025, 09:37
Free margin is calculated and indicated incorrectly on both the desktop status line and via the Algo Account.FreeMargin method.
The issue manifests when dynamic leverage is in play. Consider the following example:
- Account size: $200,000
- Traded symbol: BTC at $96,000
- Dynamic leverage: 1:3 for volumes ≤ $100,000 and 1:1 for volumes > $100,000
First open position: 1 BTC
- Margin used: 96,000 / 3 = $32,000 (correct)
Second open position: 1 BTC
- Margin used: (4,000 / 3) + (92,000 / 1) = $93,333 (correct)
Third open position: 0.6 BTC
- Expected margin used: 0.6 × 96,000 / 1 = $57,600
- Displayed margin used: $19,000 (incorrect – 1:3 margin used for calculation instead of 1:1)
The free margin indicated in the status line (and via cTrader Algo) should be:
200,000 - 32,000 - 93,333 - 57,600 = $17,076
However, cTrader shows $55,667 – apparently using the incorrect margin calculation from the third open position.
Interestingly, despite the incorrect display calculation, the trading engine appears to use the correct margin calculation internally. For example, $55,667 of free margin should theoretically allow opening an additional position of:
55,667 / 96,000 ≈ 0.57 BTC
Yet, when attempting to open such a volume, cTrader displays an “Insufficient funds” error message.