In this lesson, you will learn:
Many beginners think:
“If the EA works, the code is fine.”
This is not true.
Your EA will eventually need:
If your code is messy, you will:
Clean code is not about being fancy. It is about making your future self’s life easier.
Good names explain the code without comments.
double x;
bool ok;
int t;
These names tell you nothing.
double RiskPercent;
bool TradingAllowed;
int MaxTrades;
You should be able to guess what a variable does just by reading its name.
Functions should sound like something happening.
void Func1();
void DoStuff();
void Check();
void UpdateTradingRules();
void ManageOpenTrades();
void LookForEntries();
If you read your function names in order, they should describe what the EA does.
Each function should do one job only.
If a function does too many things:
void DoEverything()
{
// check spread
// check time
// open trades
// manage trades
// close trades
}
void UpdateTradingRules();
void LookForEntries();
void ManageOpenTrades();
One function = one responsibility.
Spacing makes code readable.
void OnTick(){UpdateTradingRules();if(!TradingAllowed)return;ManageOpenTrades();LookForEntries();}
void OnTick()
{
UpdateTradingRules();
if(!TradingAllowed) return;
ManageOpenTrades();
LookForEntries();
}
Whitespace is not wasted space. It is visual clarity.
Avoid comments that repeat the code.
// increment i
i++;
// Stop trading after daily profit or loss limit is hit
if(!TradingAllowed) return;
Comments should explain:
A clean EA follows a predictable structure.
//==============================
// INPUTS (user settings)
//==============================
//==============================
// GLOBAL VARIABLES (EA state)
//==============================
//==============================
// ONINIT / ONDEINIT
//==============================
//==============================
// ONTICK (controller)
//==============================
//==============================
// LOGIC FUNCTIONS
//==============================
//==============================
// HELPER FUNCTIONS
//==============================
When every EA follows the same structure, you instantly feel at home in the code.
This example shows clean naming, spacing, structure, and readability.
//==============================
// GLOBAL VARIABLES
//==============================
bool TradingAllowed = true;
datetime LastBarTime = 0;
//==============================
// ONTICK
//==============================
void OnTick()
{
UpdateTradingRules();
if(!TradingAllowed) return;
if(IsNewBar())
{
ManageOpenTrades();
LookForEntries();
}
}
//==============================
// LOGIC FUNCTIONS
//==============================
void UpdateTradingRules()
{
TradingAllowed = true;
if(MarketInfo(Symbol(), MODE_SPREAD) > 20)
TradingAllowed = false;
}
void ManageOpenTrades()
{
// trailing stops, exits, breakeven logic
}
void LookForEntries()
{
if(OrdersTotal() > 0) return;
// entry logic here
}
//==============================
// HELPER FUNCTIONS
//==============================
bool IsNewBar()
{
static datetime lastBar = 0;
if(Time[0] != lastBar)
{
lastBar = Time[0];
return(true);
}
return(false);
}
You now understand:
You now have the foundation to build professional-quality Expert Advisors.
Next Up: Functions and Event Handlers -> Predefined and Custom Functions in MQL4