MQL4 BASICS COURSE

Writing Clean, Readable EA Code (MQL4)

Lesson Goal

In this lesson, you will learn:


1) Why Clean Code Matters (Even If Your EA Works)

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.


2) Use Clear, Descriptive Names

Good names explain the code without comments.

Bad Variable Names

double x;
bool ok;
int t;

These names tell you nothing.

Good Variable Names

double RiskPercent;
bool TradingAllowed;
int MaxTrades;

You should be able to guess what a variable does just by reading its name.


3) Name Functions Like Actions

Functions should sound like something happening.

Bad Function Names

void Func1();
void DoStuff();
void Check();

Good Function Names

void UpdateTradingRules();
void ManageOpenTrades();
void LookForEntries();

If you read your function names in order, they should describe what the EA does.


4) Keep Functions Small and Focused

Each function should do one job only.

If a function does too many things:

Bad Example (Too Much in One Function)

void DoEverything()
{
   // check spread
   // check time
   // open trades
   // manage trades
   // close trades
}

Good Example (Split by Responsibility)

void UpdateTradingRules();
void LookForEntries();
void ManageOpenTrades();
One function = one responsibility.

5) Use Spacing and Empty Lines

Spacing makes code readable.

Hard to Read

void OnTick(){UpdateTradingRules();if(!TradingAllowed)return;ManageOpenTrades();LookForEntries();}

Easy to Read

void OnTick()
{
   UpdateTradingRules();
   if(!TradingAllowed) return;

   ManageOpenTrades();
   LookForEntries();
}

Whitespace is not wasted space. It is visual clarity.


6) Comment the WHY, Not the WHAT

Avoid comments that repeat the code.

Bad Comments

// increment i
i++;

Good Comments

// Stop trading after daily profit or loss limit is hit
if(!TradingAllowed) return;

Comments should explain:


7) Organize Your EA in Clear Sections

A clean EA follows a predictable structure.

Recommended EA Layout

//==============================
// 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.


8) Full Clean EA Example

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);
}

9) Common Beginner Mistakes


Lesson Summary


End of Section

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