if/else, switch, and the pre-trade gate that stops bad orders before the broker does.
Beginner7 min readBeginner → AdvancedLesson 06 / 17
Try it in MetaEditor
A pre-trade gate using real broker limits
Save as MQL5/Scripts/Lesson6PreTradeGate.mq5. Run it on a few symbols and during different sessions — the spread check alone will change the answer.
//+------------------------------------------------------------------+
//| Lesson 6 - Pre-trade validation gate |
//+------------------------------------------------------------------+
#property copyright "Strategist Academy"
#property version "1.00"
void OnStart()
{
string sym = _Symbol;
double ask = SymbolInfoDouble(sym, SYMBOL_ASK);
double bid = SymbolInfoDouble(sym, SYMBOL_BID);
double point = _Point;
long stopsLevel = SymbolInfoInteger(sym, SYMBOL_TRADE_STOPS_LEVEL);
long tradeMode = SymbolInfoInteger(sym, SYMBOL_TRADE_MODE);
double spreadPoints = (ask - bid) / point;
double stopPts = 200; // candidate stop distance in points
double maxSpread = 20; // our own tolerance
bool spreadOk = (spreadPoints <= maxSpread);
bool stopOk = (stopPts >= (double)stopsLevel);
bool modeOk = (tradeMode == SYMBOL_TRADE_MODE_FULL);
Print("Symbol: ", sym);
Print("Spread: ", DoubleToString(spreadPoints, 1), " points (limit ", maxSpread, ") ok=", spreadOk ? "YES" : "NO");
Print("Stop distance: ", DoubleToString(stopPts, 0), " points (broker min ", stopsLevel, ") ok=", stopOk ? "YES" : "NO");
Print("Trade mode: ", EnumToString((ENUM_SYMBOL_TRADE_MODE)tradeMode), " ok=", modeOk ? "YES" : "NO");
if(spreadOk && stopOk && modeOk)
Print("RESULT: all pre-trade checks passed - safe to send.");
else if(!modeOk)
Print("RESULT: symbol is not fully tradable right now (session or broker restriction).");
else if(!spreadOk)
Print("RESULT: spread too wide - skip this entry, it is not worth the cost.");
else
Print("RESULT: stop too close - the broker would reject this with invalid stops.");
// --- float comparison ----------------------------------------------
double a = 0.1 + 0.2;
Print("");
Print("0.1 + 0.2 == 0.3 ? ", (a == 0.3) ? "true" : "false <-- never compare doubles");
Print("with 1e-6 tolerance: ", (MathAbs(a - 0.3) < 0.000001) ? "true" : "false");
}
To run it
1. Open MetaEditor from MT5 (press F4), and pick the folder this program belongs in — Experts, Indicators, or Scripts.
2. Create a new file, paste this over it, then press F7 to compile. Fix anything the Errors tab reports.
3. Back in MT5, drag it onto a chart — or open the Strategy Tester if it is an Expert Advisor.
The shape of a trading condition
Every entry rule is a boolean expression. In MQL5 you build them from comparisons (>, <, >=, <=, ==, !=) joined by && (and), || (or), and negated with !. Use parentheses liberally — precedence bugs in a condition are invisible until the EA trades when it should not.
switch for enumerations
When you are branching on an enum — order type, trade mode, chart event — a switch reads better than a chain of else if, and the compiler can warn you about missing cases.
Gate before you send
In Pine you can fire an order and let the backtester sort it out. In MQL5 a rejected order is a real event with a real return code. Get in the habit of validating before calling the trade function:
Is trading allowed on this symbol and account right now?
Is the spread acceptable?
Is my stop at least SYMBOL_TRADE_STOPS_LEVEL points away?
Is there enough free margin?
Failing these checks locally is free. Failing them at the broker costs you a log full of retcodes and, worse, a robot that silently does nothing for a week.
The float comparison rule again
Compare prices with a tolerance in points, never with ==. This lesson's script prints the difference explicitly so you can see it.
What you just did
Lesson 06 of 17 in MetaTrader 5 MQL5 for CFD Traders. When you have run the examples or read the section, tick it off and move to the next lesson.