In this lesson, you will learn:
void function isvoid functions are used so often in Expert Advisorsvoid functions help keep OnTick() clean
A void function is a function that:
The word void literally means:
“This function returns nothing.”
Instead of returning a value, a void function usually:
Think of a void function as an instruction:
“Go do this job.”
Expert Advisors are not simple programs. They must:
If all of this logic is placed inside OnTick(),
the code becomes very hard to read and very hard to fix.
void functions solve this problem by breaking the EA
into small, clear action blocks.
Many beginners write EAs like this:
void OnTick()
{
double spread = MarketInfo(Symbol(), MODE_SPREAD);
if(spread > 20) return;
if(OrdersTotal() == 0)
{
// Entry logic here
}
if(OrdersTotal() > 0)
{
// Trade management logic here
}
}
This may work at first, but it has serious problems:
Instead of placing logic directly inside OnTick(),
we move the logic into void functions.
Each function does one job only.
void OnTick()
{
CheckTradingConditions();
ManageOpenTrades();
LookForNewEntries();
}
Now OnTick() reads like a checklist.
This is intentional.
Here is a basic void function that checks the spread
and updates a global variable.
// Global variable
bool TradingEnabled = true;
// Void function
void CheckTradingConditions()
{
double spread = MarketInfo(Symbol(), MODE_SPREAD);
if(spread > 20)
TradingEnabled = false;
else
TradingEnabled = true;
}
This function:
Other parts of the EA can now simply check:
if(!TradingEnabled) return;
Imagine you need to check trading conditions:
Without void functions, you would copy and paste the same code.
This is dangerous.
With a void function, you write the logic once:
void CheckTradingConditions()
{
// spread, time, daily limits, etc.
}
Then you reuse it anywhere:
CheckTradingConditions();
This example shows where void functions live
inside a real EA file.
//==============================
// GLOBAL VARIABLES
//==============================
bool TradingEnabled = true;
//==============================
// ONTICK (very short)
//==============================
void OnTick()
{
CheckTradingConditions();
if(!TradingEnabled) return;
ManageOpenTrades();
LookForNewEntries();
}
//==============================
// VOID FUNCTIONS (action blocks)
//==============================
void CheckTradingConditions()
{
double spread = MarketInfo(Symbol(), MODE_SPREAD);
if(spread > 20)
TradingEnabled = false;
else
TradingEnabled = true;
}
void ManageOpenTrades()
{
// trailing stops, exits, etc.
}
void LookForNewEntries()
{
if(OrdersTotal() > 0) return;
// entry logic here
}
This structure is clean, readable, and easy to expand later.
OnTick()void functions that do too many thingsRemember:
One function = one job.
void functions perform actionsOnTick() clean and readableIn the next lesson, we will learn:
Organizing EA Logic Outside OnTick()
You’ll see how professional EAs are structured so they stay readable, debuggable, and expandable as they grow.