Page 1 of 1

An EA that checks free margin before every order

Posted: Thu Oct 22, 2026 6:26 am
by Fairman
An EA that checks free margin before every order

It sounds obvious, but a surprising number of EAs I've looked at send orders without checking whether the account has enough free margin. They rely on the server rejecting the order if there isn't enough. That works in a basic sense, but it creates problems.

When the server rejects an order for insufficient margin, the EA usually treats it like any other error and retries. Several retries in a row can trigger rate limits at some brokers. And if the EA calculates lot size from balance instead of equity, it can try to open positions the account can't support when other trades are in drawdown.

So my EAs now check before sending. MQL has OrderCalcMargin in MT5, and in MT4 you can use MarketInfo with MODE_MARGINREQUIRED to estimate the margin for a given lot size. The EA compares that with the free margin and leaves a buffer on top. If there's not enough room, it reduces the lot size or skips the trade and logs why.

This also protects against a quieter problem. If the account has several EAs running at once, one of them can use up margin the others were counting on. A margin check before every order means each EA respects what's actually available at that moment.

The buffer is a judgment call. I keep enough margin spare that a normal adverse move on open positions won't push the account toward a margin call.

Re: An EA that checks free margin before every order

Posted: Wed Oct 28, 2026 3:52 pm
by FXS HongKong Scalper
Checking free margin before every order is a good habit. In MQL5 the OrderCalcMargin function gives the exact margin a planned order would use, which is more reliable than calculating it by hand, especially for crosses and metals. Comparing that with ACCOUNT_MARGIN_FREE, with a buffer of a few percent, avoids sending orders that will be rejected. It is also worth checking the margin level after the order would be placed, so the EA does not open a trade that puts the account close to a margin call.

Re: An EA that checks free margin before every order

Posted: Thu Oct 29, 2026 4:29 pm
by Fairman
Margin requirements can change with the time of day or before weekends at some brokers. A check that passed on Friday morning might fail on Friday afternoon if the broker raises margin before the close. Running the check before every order, rather than once at startup, handles that automatically. It also catches changes after a broker announcement that you might have missed. Brokers usually announce these changes by email or on their website. An EA checking every time is a safety net for the times you didn't see the announcement, or saw it and forgot to adjust anything.

Re: An EA that checks free margin before every order

Posted: Thu Oct 29, 2026 6:54 pm
by Fairman
Hedged positions complicate the calculation. Some brokers charge margin only on the larger side of a hedge, others on both, and some partially. OrderCalcMargin gives you the margin for the new order alone, not the change in total margin after it's added. If you hedge, check how your broker calculates it, because the estimate can be wrong in either direction. Asking support how margin is calculated on hedged positions is the simplest way to find out. Their answer lets you adjust the EA's estimate so it's neither too cautious nor too generous.

Re: An EA that checks free margin before every order

Posted: Fri Oct 30, 2026 5:52 am
by Fairman
A related check is the margin level after the trade. Calculate what the margin level would be if the order is filled, and compare with the broker's margin call level. If the new trade would bring the account close to it, skip the trade. That's slightly more complex than checking free margin, but it gives a more direct picture of how close you'd be to trouble. The calculation is current equity divided by the total margin after the new trade, multiplied by a hundred. Compare it with the broker's margin call level plus a buffer, and skip the trade if the result is too close.

Re: An EA that checks free margin before every order

Posted: Fri Oct 30, 2026 6:43 am
by Fairman
Log every time the margin check blocks or reduces a trade. If it happens often, your sizing is too aggressive for the account, or too many strategies are sharing it. That's useful information for adjusting risk. A margin check that quietly reduces sizes without you knowing can make results look strange when you review them later. A simple counter in the log, with the date and the amount by which the size was cut, is enough. Reviewing it monthly tells you whether the account's risk settings need to change. It also shows which sessions trigger it most, which usually points to the times when several strategies overlap on the account.

Re: An EA that checks free margin before every order

Posted: Sat Oct 31, 2026 8:56 pm
by Shadow Trader
Some brokers raise margin requirements before the weekend, particularly on pairs with event risk. A leverage of 1:500 on Thursday can become 1:100 on Friday afternoon, and then return on Monday.

The change is usually in the fine print. Positions that were comfortable on Thursday might be under pressure on Friday if the margin rises and the account's free margin shrinks. A margin call or stop-out can hit without any price movement against the trade.

Event risk triggers it too: elections, central bank meetings, referendums. A broker anticipating volatility cuts leverage on affected pairs, sometimes with only a few hours' notice.

The effect on scalping is limited if positions are flat by the weekend. But for accounts that carry anything overnight, or that run several positions near the limit, a surprise margin change can force closure at a bad time.

Checking the margin requirement on the symbol specification before the weekend is quick. A note on how it changed in the past gives a sense of when it tends to happen.

With how much notice did a leverage cut last arrive, and did an open trade get caught by it?