Mt5/Mt4 Code Execution & Expression Styles/Patterns Styles
Angiterver3 min read·Just now--
Part 5
This are code execution styles / patterns, not broker execution. I will be breaking them down from the most basic/slow (in terms of flexibility and verbosity) to the fastest/most optimized styles, detailed explanations with code examples which will help you when creating an EA
1. Fully Expanded / Verbose Execution (Baseline)
Everything is spelled out. No shortcuts. No chaining.
Code
void OnTick()
{
double fast = iMA(_Symbol, PERIOD_CURRENT, 10, 0, MODE_SMA, PRICE_CLOSE, 0);
double slow = iMA(_Symbol, PERIOD_CURRENT, 50, 0, MODE_SMA, PRICE_CLOSE, 0);
if(fast > slow)
{
if(PositionsTotal() == 0)
{
trade.Buy(0.1);
}
}
}Feel
- Safe, Clear, Kinda bulky
2. Condensed If Execution
You remove nesting and compress conditions.
Code
void OnTick()
{
if(iMA(_Symbol,0,10,0,MODE_SMA,PRICE_CLOSE,0) >
iMA(_Symbol,0,50,0,MODE_SMA,PRICE_CLOSE,0)
&& PositionsTotal() == 0)
{
trade.Buy(0.1);
}
}What changed
- No temp variables, Logic inline
Tradeoff
- Harder to debug, Slightly faster (less variable overhead, but negligible)
3. Inline Execution (Single-Statement If)
Now we drop the braces — one condition → one action.
Code
void OnTick()
{
if(iMA(_Symbol,0,10,0,MODE_SMA,PRICE_CLOSE,0) >
iMA(_Symbol,0,50,0,MODE_SMA,PRICE_CLOSE,0)
&& PositionsTotal() == 0)
trade.Buy(0.1);
}Feel
- Cleaner, More “trader style”
4. Ternary Execution (Compact Decision Style)
Now you start using ? : like a sniper.
Code
void OnTick()
{
(iMA(_Symbol,0,10,0,MODE_SMA,PRICE_CLOSE,0) >
iMA(_Symbol,0,50,0,MODE_SMA,PRICE_CLOSE,0))
? trade.Buy(0.1)
: trade.Sell(0.1);
}Reality check
- Looks slick, But dangerous in trading (you don’t always want auto sell)
Most pros only use ternary for values, not orders.
5. True One-Line Execution
This is what you were hinting at.
Code
void OnTick()
{
if(PositionsTotal()==0 && iMA(_Symbol,0,10,0,MODE_SMA,PRICE_CLOSE,0) >
iMA(_Symbol,0,50,0,MODE_SMA,PRICE_CLOSE,0)) trade.Buy(0.1);
}Or even tighter:
if(PositionsTotal()==0 && iMA(_Symbol,0,10,0,MODE_SMA,PRICE_CLOSE,0) > iMA(_Symbol,0,50,0,MODE_SMA,PRICE_CLOSE,0)) trade.Buy(0.1);Feel
- Fast, Aggressive, Zero fluff
Hidden cost
- Re-computes indicators multiple times, Ugly when logic grows
6. Macro / Inline Function Execution
Now this is where experienced devs get clever.
You hide complexity behind tiny wrappers.
Code
#define BUY_SIGNAL (iMA(_Symbol,0,10,0,MODE_SMA,PRICE_CLOSE,0) > iMA(_Symbol,0,50,0,MODE_SMA,PRICE_CLOSE,0))
void OnTick()
{
if(PositionsTotal()==0 && BUY_SIGNAL) trade.Buy(0.1);
}Why this hits
- Still one-line execution, But readable, Reusable
7. Chained Execution Style (Advanced Compact Logic)
You combine multiple conditions + actions in one flow.
Code
void OnTick()
{
if(PositionsTotal()==0 &&
(iMA(_Symbol,0,10,0,MODE_SMA,PRICE_CLOSE,0) >
iMA(_Symbol,0,50,0,MODE_SMA,PRICE_CLOSE,0)
? trade.Buy(0.1)
: trade.Sell(0.1)));
}Honest truth
This is where things start getting too clever.
It works… but if something breaks, you’ll hate your past self 😅
8. Lambda-Style / Expression Execution (MT5 Advanced)
MQL5 supports function-style compact execution patterns.
Code
void Execute(bool condition)
{
if(condition) trade.Buy(0.1);
}
void OnTick()
{
Execute(iMA(_Symbol,0,10,0,MODE_SMA,PRICE_CLOSE,0) >
iMA(_Symbol,0,50,0,MODE_SMA,PRICE_CLOSE,0));
}Feel
- Clean, Reusable, Almost like functional programming
Real Talk (this is the part people don’t say)
When I first discovered one-line execution, I thought:
“Yeah… this is it. This is pro-level.”
Then a few weeks later, debugging hit me like a truck.
Because in trading:
- You don’t just need speed
- You need control + traceability
So the real balance most experienced devs land on is:
- Compact logic, but not cryptic
- One-line triggers, but precomputed signals
The “Sweet Spot” Most Pros Use
Something like this:
bool buy = maFast[0] > maSlow[0];
if(PositionsTotal()==0 && buy) trade.Buy(0.1);Why this wins
- Execution is still one-line, Logic is separated, Debugging is easy
Bottom line
From slow → fast (in style, not just CPU):
- Verbose blocks
- Condensed if
- Inline if
- Ternary
- One-line execution
- Macro-driven execution
- Chained expressions
- Functional/lambda-style
contact me on telegram: t.me/Angiterver
Happy coding and trading and remember, proper risk management is what keeps you in the game.