7 Risk Management Rules Every Expert Advisor Should Have
By the Victoryalgo team · · 7 min read
An Expert Advisor (EA) can open trades faster than any person, so a small mistake can repeat many times before you notice. Good risk rules help stop that.
This guide explains 7 rules most EAs should have: what each one does and how an EA does it in MT4 and MT5. New to EAs? Start with our complete guide to Expert Advisors.
Remember: These rules reduce some risks, but they cannot remove market risk. All numbers below are only examples.
1. Risk-Based Lot Size
What it does: Instead of the same lot on every trade, the EA sizes each trade so that hitting the stop loss costs about a set percent of the account.
How the EA does it: It multiplies the balance by your risk percent, then divides that amount by the loss per lot at the stop loss. It works out the loss per lot from the broker's tick value and tick size (SYMBOL_TRADE_TICK_VALUE and SYMBOL_TRADE_TICK_SIZE in MQL5, MODE_TICKVALUE and MODE_TICKSIZE in MQL4). Then it rounds the lot down to the broker's lot step, within the minimum and maximum lot.
Example (maths only): a 10,000 USD account at 1% risk has 100 USD at risk. On EURUSD, a 50 pip stop loss costs about 500 USD per standard lot, so the lot size is 100 / 500 = 0.20. Check your own numbers with our free lot size calculator.
2. A Stop Loss on Every Trade
What it does: A stop loss closes a trade at a set price, so one bad trade cannot keep growing.
How the EA does it: It sends the stop loss with the order: the sl field of the MQL5 trade request, or the stop loss value in MQL4's OrderSend(). On some accounts, the EA opens the trade first and adds the stop right after. Either way, it checks that the stop is really there and is not closer than the broker's minimum stop distance, which causes an "invalid stops" error.
A "hidden" stop inside the code only works while the terminal is running, so a real stop on the broker's server is safer. Gaps and slippage can still close a trade at a worse price.
3. A Max Spread Filter
What it does: The spread is the gap between the ask and bid price, a cost on every trade. It can widen a lot around news, at the daily rollover and in quiet hours. This filter blocks new trades when the spread is above your limit.
How the EA does it: Before each trade, it reads the current spread (SYMBOL_SPREAD in MQL5, MODE_SPREAD in MQL4). If it is too wide, the EA skips the trade and logs it. The spread is given in points, and on most 5-digit forex prices 1 pip is 10 points.
4. A Daily Loss Limit or Equity Stop
What it does: A daily loss limit stops new trades after the account loses a set amount in one day. An equity stop closes the EA's trades if equity falls to a level you choose.
How the EA does it: At a reset time in broker server time, the EA saves the day's starting balance or equity in a terminal global variable or a file, so a restart does not reset the day. It then checks equity (AccountInfoDouble(ACCOUNT_EQUITY) in MQL5, AccountEquity() in MQL4). At the limit, it blocks new trades until the next reset. An equity stop also closes its positions and deletes its pending orders.
Leave room above any hard limit, because gaps can make the result worse. For prop firm accounts, read our guide to prop firm drawdown and max loss rules and use the prop firm drawdown calculator.
5. Max Open Trades and Max Trades per Day
What it does: These limits stop the EA from overtrading. They also protect you if a bug or a choppy market makes a signal fire again and again.
How the EA does it: It counts only positions with its own magic number and symbol, using PositionsTotal() and POSITION_MAGIC in MQL5, or OrdersTotal() and OrderMagicNumber() in MQL4. For trades per day, it counts its trades in the account history since the daily reset. This count stays correct after a restart, unlike a counter kept only in memory.
An MT5 netting account has only one position per symbol, so these limits work differently there (see MT4 vs MT5).
6. Session and News Filters
What it does: A session filter allows new trades only in the hours you choose. A news filter blocks new trades for some minutes before and after high-impact news.
How the EA does it: It checks TimeCurrent(), the broker's server time, so set your hours in server time. For news, MQL5 code can read the economic calendar built into MT5. MT4 has no built-in calendar, so news times come from a list you enter or an outside source. Calendar data may not be available in the Strategy Tester, so test news filters on demo.
7. Safe Handling of Errors and Reconnects
What it does: Orders do not always go through, and the terminal or VPS can restart. This rule makes the EA react safely.
Common errors are a requote (the price changed), off quotes (no price at that moment) and not enough money (free margin is too small). In MQL5 they are TRADE_RETCODE_REQUOTE, TRADE_RETCODE_PRICE_OFF and TRADE_RETCODE_NO_MONEY. In MQL4 they are errors 138, 136 and 134.
How the EA does it:
- Retry with care: after a requote or off quotes, it gets a fresh price, waits a moment and retries only a few times, if the signal and spread are still valid.
- No retry for "not enough money": this will not fix itself. The EA checks margin before sending, then skips the trade and logs it.
- No order loops: it pauses between attempts and stops after a set limit.
- Checks after a restart: when the terminal restarts or you change the inputs, the EA starts again. It must check its open positions by magic number and remember which candle it already traded, so it does not repeat a trade.
Read the EA's messages in the Experts and Journal tabs, as shown in our EA install guide.
Summary Table
| Rule | What it does | How the EA does it |
|---|---|---|
| 1. Risk-based lot size | Keeps each loss near a set percent | Risk %, stop distance and tick value |
| 2. Stop loss on every trade | Limits the loss of each trade | Sends a real stop and checks it is placed |
| 3. Max spread filter | Avoids trades when costs are high | Compares the spread with your limit |
| 4. Daily loss limit or equity stop | Stops trading after a set loss | Saves the start-of-day value and checks equity |
| 5. Max open trades and trades per day | Stops overtrading | Counts its trades by magic number |
| 6. Session and news filters | Trades only at the times you choose | Checks server time and news times |
| 7. Errors and reconnects | Reacts safely to failed orders and restarts | Limited retries, margin and restart checks |
Test Every Rule Before You Go Live
In the Strategy Tester, set tiny limits on purpose and check that the EA stops. Then check the reset time and the filters on demo. Our MT5 backtesting guide shows how.
A bot follows your rules, but results depend on your strategy and the market. You can lose money, so always test on a demo account first.
Get These Rules Coded
Victoryalgo can code these rules into a new EA, or into an existing one if you have its source code, through our MT5 EA development, MT4 EA development and prop firm EA services. Before you hire anyone, read How to Choose an EA Developer.
Have a strategy you want to run as an EA? Send us your strategy on WhatsApp for a free consultation. The Victoryalgo team will read your rules and explain how they can be coded and set up.
Victoryalgo is a software development company, not registered with SEBI or any financial regulator. This guide is general education about software, not investment advice. Forex and CFD trading is high risk. Indian residents may trade forex only through RBI-authorised channels.
Frequently Asked Questions
Which risk rule matters most in an EA?
No single rule is enough. Each one covers a different problem. A stop loss and a risk-based lot size are a common base, and a daily loss limit, trade limits and filters add more safety.
How does an EA work out the lot size from a risk percent?
It multiplies the balance by the risk percent, then divides that amount by the loss per lot at the stop loss. It reads the tick value from the broker and rounds the result down to the lot step.
Does a stop loss protect me from every loss?
No. A stop loss limits the loss in normal conditions, but gaps and slippage can close a trade at a worse price. It is still one of the most important safety rules.
Why did my EA open the same trade again after a restart?
The EA probably did not check its open positions or the last signal it traded when it started again. A well-coded EA checks its trades by magic number and remembers the last traded candle, so a restart does not repeat a trade.
More Guides & Free Tools
- Expert Advisors (EA): The Complete Guide for Traders →
- EA Settings Explained: Lot Size, Stop Loss and Profit Targets →
- How to Choose an EA Developer: 10 Questions to Ask Before You Pay →
- How to Backtest an EA in the MT5 Strategy Tester (Step by Step) →
- Free Lot Size Calculator →
- Free Prop Firm Drawdown Calculator →
Want your strategy coded?
Send your rules on WhatsApp. Free consultation, clear quote.
WhatsApp Your Strategy