MT5 Expert Advisor Development
You write the rules. We implement them in MQL5 as a working Expert Advisor that runs on your own MetaTrader 5 terminal, …
Read moreThese are not strategy rules. They are the layer that sits outside the strategy and limits what a bug, a gap or a bad day can cost.
Strategy logic decides whether to trade. The risk layer decides whether trading is allowed at all right now. Keeping them apart matters because the risk layer must keep working even when the strategy logic is wrong — and sometimes the strategy logic is wrong.
We build these as a module that runs before any entry, on every EA, regardless of what the strategy does. The numbers are yours; the existence of the layer is not negotiable.
The most useful single limit. It stops the day, not the trade.
input double MaxDailyLossPct = 3.0;
bool DailyLossBreached()
{
static datetime day = 0;
static double dayStart = 0.0;
datetime today = (TimeCurrent() / 86400) * 86400;
if(today != day) // new server day
{
day = today;
dayStart = AccountInfoDouble(ACCOUNT_BALANCE);
}
double equity = AccountInfoDouble(ACCOUNT_EQUITY);
double lossPct = (dayStart - equity) / dayStart * 100.0;
return (lossPct >= MaxDailyLossPct);
}
Two decisions to make explicitly: measure against balance or equity, and does breaching it close open positions or only block new ones? Both are defensible; the software has to do one.
An absolute number below which the EA stops entirely and does not resume on its own. Daily limits reset each morning; the floor does not. It is the backstop for a sequence of bad days that each individually passed the daily check.
A cap on total open volume across every position, not just this EA's. Margin is shared, so a per-EA cap is not a cap on the account.
input double MaxTotalLots = 1.0;
bool ExposureOK(double newLots)
{
double open = 0.0;
for(int i = PositionsTotal()-1; i >= 0; i--)
if(PositionGetTicket(i) > 0)
open += PositionGetDouble(POSITION_VOLUME);
return (open + newLots <= MaxTotalLots);
}
Refuse new entries below a margin level you choose — 300% or 500% are common inputs. This is the one that prevents the cascade where a stop-out closes positions, which worsens margin, which stops out more.
double level = AccountInfoDouble(ACCOUNT_MARGIN_LEVEL);
if(level > 0.0 && level < MinMarginLevel) return false;
Guard for zero — with no open positions, margin level is 0, and a naive comparison blocks every trade forever.
Stop after N losing trades in a row. Not because losses predict losses, but because a run often means conditions have moved outside what the rules were written for — and it forces a human to look before trading resumes.
Refuse entry when the current spread is above a threshold. Essential around session opens, rollover and news, when a spread can briefly be many times normal.
long spread = SymbolInfoInteger(_Symbol, SYMBOL_SPREAD);
if(spread > MaxSpreadPoints)
{
if(!g_spreadLogged)
{
Print("Entry blocked: spread ", spread, " > max ", MaxSpreadPoints);
g_spreadLogged = true;
}
return false;
}
g_spreadLogged = false;
Before every send, verify the lot is within the symbol's min, max and step; the stop respects the broker's minimum distance; and the free margin covers the trade. These prevent the retcodes that otherwise appear only in live trading, at the worst time.
A risk layer that blocks silently is indistinguishable from a broken EA, and you will spend an afternoon debugging a working system. Print the reason once per state change, and expose the current state on the chart if there is a dashboard. "Why did it not trade today" should be a five-second question.
Risk limits constrain software behaviour. They cannot prevent a gap through a stop, a broker outage or a weekend move, and nothing in this article is a claim about outcomes. Trading in financial markets carries a substantial risk of loss, and every decision — including what these numbers should be — remains yours.
You write the rules. We implement them in MQL5 as a working Expert Advisor that runs on your own MetaTrader 5 terminal, …
Read moreSoftware you already own, fixed or extended — and deployed on a VPS so it keeps running when your own machine is o…
Read moreSend your rules, your script or your existing file on WhatsApp and we will reply with a written scope, price and timeline.