In MetaTrader 5 programming, MQL5 if-then statements and conditional operators are the backbone of decision-making logic in Expert Advisors (EAs), indicators, and scripts.
Whether you’re triggering trades, filtering signals, or managing risk, understanding how to use if, else, switch, and logical operators in MQL5 conditional statements is essential.
This article breaks down the syntax, showcases practical examples, and offers best practices to level up your trading tools.
MQL5 If Then
If there is one lesson that you really need to understand to code properly, this would be it.
99% of MT5 custom tools and MQL5 programs will contain some kind of logic at some point.
This logic will tell the software to do something rather than something else, depending on some conditions.
Conditions are verified by MQL5 Conditional Operators like if, else, switch.
The if statement evaluates a condition and executes code if it’s true. Here’s a simple example:
|
1 2 3 4 5 6 7 8 |
void OnTick() { double currentPrice = Bid; if(currentPrice > 1.2000) { Print("Price above 1.2000!"); } } |
The condition (currentPrice > 1.2000) uses a comparison operator (>). If true, the code inside {} runs.
This is the heart of MQL5 if-then logic, concise yet powerful.
You can find here also some official documentation.
Expanding MQL5 If with Else and Else If
When you have more execution options for more possible situations you can use else, else if and switch.
Add else to handle the false case, and else if for multiple conditions:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
void OnTick() { double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, 0); if(rsi > 70) { Print("Overbought: RSI = ", rsi); } else if(rsi < 30) { Print("Oversold: RSI = ", rsi); } else { Print("Neutral: RSI = ", rsi); } } |
This checks RSI levels and prints accordingly, a classic setup for signal-based EAs.
Best Practices to Use With MQL5 If, Else and Switch
We like to provide best practices to write maintainable and efficient code, and here are some to use with MQL5 Conditional Operators.
Combine Logical Operators
Logical operators (&& for AND, || for OR, ! for NOT) supercharge your conditions.
Open a trade only if RSI is low and price is above a moving average:
|
1 2 3 4 5 6 7 8 9 10 |
void OnTick() { double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, 0); double ma = iMA(NULL, 0, 20, 0, MODE_SMA, PRICE_CLOSE); if(rsi < 30 && Bid > ma) { OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, 0, 0, "RSI_MA_EA"); Print("Buy triggered!"); } } |
Here, && ensures both conditions are true. Use || for flexibility (e.g., rsi < 30 || Bid > ma) or ! to invert logic (if(!OrdersTotal()) for “no open orders”).
Use Switch for Multi-Value Checks
When checking a variable against multiple specific values, switch beats a long if-else chain.
Example: Adjust lot size based on account balance:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
double GetLotSize() { double balance = AccountBalance(); double lotSize; switch((int)(balance / 1000)) // Balance in thousands { case 1: lotSize = 0.1; break; case 5: lotSize = 0.5; break; default: lotSize = 0.01; // Under 1K or over 5K break; } return lotSize; } void OnTick() { Print("Lot size: ", GetLotSize()); } |
break exits each case; default handles unmatched values. This is cleaner than stacking if-else.
Nest Conditions Sparingly
Nested if statements work but can get messy.
You will need to have nested if in complex tools, but try to limit them and when using them make sure they have comments and clarity:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
void OnTick() { double maFast = iMA(NULL, 0, 10, 0, MODE_EMA, PRICE_CLOSE); double maSlow = iMA(NULL, 0, 50, 0, MODE_EMA, PRICE_CLOSE); if(maFast > maSlow) { // Bullish crossover detected if(OrdersTotal() == 0) { OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, 0, 0); Print("Buy opened on crossover!"); } } } |
If nesting deepens, consider breaking logic into functions for readability.
Test Conditions Thoroughly
Multiple conditions means added complexity and it results in more likelihood of errors.
Debug your logic with Print() or the Strategy Tester.
Example: Why isn’t this trade firing?
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
void OnTick() { double rsi = iRSI(NULL, 0, 14, PRICE_CLOSE, 0); if(rsi < 30) { Print("RSI check passed: ", rsi); if(Bid > 1.2000) { Print("Price check passed: ", Bid); OrderSend(Symbol(), OP_BUY, 0.1, Ask, 3, 0, 0); } } } |
Logs reveal if rsi < 30 or Bid > 1.2000 fails, pinpointing the issue fast.
Conclusion
MQL5 if-then and conditional operators are your tools for smart trading logic.
From basic if checks to switch cases and logical combos, they drive your EAs’ decisions.
Keep conditions clear, test rigorously, and watch your code thrive.
For questions and support please Contact Us.