Code (EA) attached to reproduce it. The symbols must be selected in the Market Watch, the code doesn't take care of it.
EDIT: It doesn't always give such error, it also depends of the broker. And as you can see it also depends of OP_BUY or OP_SELL is used.In my own EAs, I calculate the margin myself and have never used this function, so do you know if this only happens on this build or if it was happening in previous builds too?
In my own EAs, I calculate the margin myself and have never used this function, so do you know if this only happens on this build or if it was happening in previous builds too?
I noticed it with this build. The underlying code is running in production for more than 6 months and no issue was reported (which doesn't mean it doesn't exist).
How are calculating the margin yourself ? You would need the margin percentage by mql4 but it's not available. Did I missed something ?
or
How are calculating the margin yourself ? You would need the margin percentage by mql4 but it's not available. Did I missed something ?
In MQL4, I use these:
MODE_MARGINCALCMODE
28
Margin calculation mode. 0 - Forex; 1 - CFD; 2 - Futures; 3 - CFD for indices
MODE_MARGININIT
29
Initial margin requirements for 1 lot
MODE_MARGINMAINTENANCE
30
Margin to maintain open orders calculated for 1 lot
MODE_MARGINHEDGED
31
Hedged margin calculated for 1 lot
MODE_MARGINREQUIRED
32
Free margin required to open 1 lot for buying
In MQL4, I use these:
MODE_MARGINCALCMODE
28
Margin calculation mode. 0 - Forex; 1 - CFD; 2 - Futures; 3 - CFD for indices
MODE_MARGININIT
29
Initial margin requirements for 1 lot
MODE_MARGINMAINTENANCE
30
Margin to maintain open orders calculated for 1 lot
MODE_MARGINHEDGED
31
Hedged margin calculated for 1 lot
MODE_MARGINREQUIRED
32
Free margin required to open 1 lot for buying
Also, the Margin % and Leverage Ratio are the same thing expressed in a different way. So you can also use AccountLeverage() to get the Margin % and calculate the margin manually as well.
Does the following help?
Forum on trading, automated trading systems and testing trading strategies
Margin requirements calculation
gordon, 2010.04.08 04:42
Margin requirement for 1 lot is given by MarketInfo(symbol,MODE_MARGINREQUIRED). Calculating this value seems to be different among some brokers (specifically ones that use a bridge).
The majority of brokers use this calculation (taken from the article 'Forex Trading ABC' -> https://www.mql5.com/en/articles/1453):
double MarginCalculate(string symbol, double volume) { string first = StringSubstr(symbol,0,3); // the first symbol, for example, EUR string second = StringSubstr(symbol,3,3); // the second symbol, for example, USD string currency = AccountCurrency(); // deposit currency, for example, USD double leverage = AccountLeverage(); // leverage, for example, 100 // contract size, for example, 100000 double contract = MarketInfo(symbol, MODE_LOTSIZE); double bid = MarketInfo(symbol, MODE_BID); // Bid price //---- allow only standard forex symbols like XXXYYY if(StringLen(symbol) != 6) { Print("MarginCalculate: '",symbol,"' must be standard forex symbol XXXYYY"); return(0.0); } //---- check for data availability if(bid <= 0 || contract <= 0) { Print("MarginCalculate: no market information for '",symbol,"'"); return(0.0); } //---- check the simplest variations - without cross currencies if(first == currency) return(contract*volume / leverage); // USDxxx if(second == currency) return(contract*bid*volume / leverage); // xxxUSD //---- check normal cross currencies, search for direct conversion through deposit currency string base = currency + first; // USDxxx if(MarketInfo(base, MODE_BID) > 0) return(contract / MarketInfo(base, MODE_BID)*volume / leverage); //---- try vice versa base = first + currency; // xxxUSD if(MarketInfo(base, MODE_BID) > 0) return(contract*MarketInfo(base, MODE_BID)*volume / leverage); //---- direct conversion is impossible Print("MarginCalculate: can not convert '",symbol,"'"); return(0.0); }But some brokers do not factor 'bid' price when calculating (regardless of symbol):
double MarginCalculate(string symbol, double volume) { double leverage = AccountLeverage(); double contract = MarketInfo(symbol, MODE_LOTSIZE); return(contract*volume / leverage); }
Note that MODE_MARGINREQUIRED is 'static' in Tester and that both methods of calculation cannot be used in Tester (they rely on MarketInfo() of other symbols...).
MODE_MARGINREQUIRED is what I need to workaround this issue. Being accustomed with mql5, I am used to rely on SymbolInfoDouble() functions but it seems to have no equivalent to MARGINREQUIRED, so I overlooked it.
Thank you Fernando.
The problem is that AccountFreeMarginCheck returns 0, if there's a position open in the opposite direction.
Is anybody else having this problem? It's been going on for months, probably since this thread started. It only affects hedging EA's.
My only conclusion is that multiple brokers have problems giving correct information with this Function.
The workaround above in this tread give problems with special names on instruments, that a these brokers have.
So my temporary solution has been to remove the money check, and take the appropriate action depending on what error message comes back from the server.
Workarounds are good, but I think this needs to be fixed somehow.
What's confusing about this is that it must affect hundreds of customers/EA's out there, but it feels like nobody is addressing it.
I'm having this issue with BDSwiss(Viverno) and with Pepperstone. It works on IC Markets.
The problem is that AccountFreeMarginCheck returns 0, if there's a position open in the opposite direction.
Is anybody else having this problem? It's been going on for months, probably since this thread started. It only affects hedging EA's.
My only conclusion is that multiple brokers have problems giving correct information with this Function.
The workaround above in this tread give problems with special names on instruments, that a these brokers have.
So my temporary solution has been to remove the money check, and take the appropriate action depending on what error message comes back from the server.
Workarounds are good, but I think this needs to be fixed somehow.
What's confusing about this is that it must affect hundreds of customers/EA's out there, but it feels like nobody is addressing it.
What do you mean, what problems ?
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
MT4 build 1353.
AccountFreeMarginCheck() is not reliable, sometimes it returns strange value GREATER then the current freemargin (resulting in a negative margin). I have also seen positive margin but completely incorrect (0.12 instead of 217 for example).
For example on my broker (running on all 28 main Forex symbols).
2022.04.04 12:29:47.293 AccountFreeMarginCheckIssue GBPJPY,H1: Account leverage : 200 Free margin = 100328.42 USD
2022.04.04 12:29:47.293 AccountFreeMarginCheckIssue GBPJPY,H1: EURJPY: WRONG Account margin after BUY 1 lot(0) = 100441.15 GREATER than free margin, margin calc. = -112.73
2022.04.04 12:29:47.293 AccountFreeMarginCheckIssue GBPJPY,H1: GBPJPY: WRONG Account margin after BUY 1 lot(0) = 100334.40 GREATER than free margin, margin calc. = -5.98
2022.04.04 12:29:47.293 AccountFreeMarginCheckIssue GBPJPY,H1: AUDNZD: WRONG Account margin after BUY 1 lot(0) = 100384.25 GREATER than free margin, margin calc. = -55.83
2022.04.04 12:29:47.293 AccountFreeMarginCheckIssue GBPJPY,H1: GBPCAD: WRONG Account margin after SELL 1 lot(0) = 100463.90 GREATER than free margin, margin calc. = -135.48
Code (EA) attached to reproduce it. The symbols must be selected in the Market Watch, the code doesn't take care of it.
EDIT: It doesn't always give such error, it also depends of the broker. And as you can see it also depends of OP_BUY or OP_SELL is used.