Hi,
Brokers often round up the required margin internally (especially for high volatility assets like XAUUSD). The difference of $7.77 is about 0.03% of the position, which could be explained by: Commission/reserve buffer, Spread cushion, Internal risk policies & Server-side rounding.
You can try this function to calculate Broker's Accurate Margin Calculation
double GetRequiredMargin(string symbol, double lots, ENUM_ORDER_TYPE order_type) { double margin = 0.0; if(!OrderCalcMargin(order_type, symbol, lots, SymbolInfoDouble(symbol, SYMBOL_ASK), margin)) { Print("OrderCalcMargin failed: ", GetLastError()); return -1.0; } return margin; } // ontick double margin = GetRequiredMargin(_Symbol, 1.0, ORDER_TYPE_BUY); double ask = SymbolInfoDouble(_Symbol, SYMBOL_ASK); double freeMargin = AccountInfoDouble(ACCOUNT_MARGIN_FREE); double marginPerLot; if(OrderCalcMargin(ORDER_TYPE_BUY, _Symbol, 1.0, ask, marginPerLot)) { double maxLots = freeMargin / marginPerLot; Print("Broker-calculated margin per lot: ", marginPerLot); Print("Max lots you can afford: ", maxLots); } else { Print("OrderCalcMargin failed with error ", GetLastError()); }
Let me know if you'd like a reusable function to calculate maximum affordable lot size based on balance + broker margin.
Hi,
Brokers often round up the required margin internally (especially for high volatility assets like XAUUSD). The difference of $7.77 is about 0.03% of the position, which could be explained by: Commission/reserve buffer, Spread cushion, Internal risk policies & Server-side rounding.
You can try this function to calculate Broker's Accurate Margin Calculation
Let me know if you'd like a reusable function to calculate maximum affordable lot size based on balance + broker margin.
Hi @Venkatesan A,
Thank you so much! I don't know how I missed OrderCalcMargin this entire time. It is precisely what I was looking for!
I've tried your example and it works flawlessly without all my manual calculations.
Thanks!
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Hi,
I'm trying to calculate lot size in a such way so that it will fit in account balance and satisfy broker's margin requirement.
The complexity is added by the fact, that my account is in AUD and XAUUSD is in USD.
Here is my current calculation:
This seem to work and calculates maxLotsPerFreeMargin to around 0.8, marginPerContract equals to 24996.43 and marginInitial equals to 0.05
However, if I try to force buying 1 contract without having enough money on the account, in the "NO MONEY" error the broker says that the required margin is 25004.20
Therefore, the difference is 25004.20 - 24996.43 = 7.77
Please help me to clarify, where this difference is coming from and how to make my calculation return the same value as broker expects?
Thanks!