Get Free Consultation!
We are ready to answer right now! Sign up for a free consultation.
I consent to the processing of personal data and agree with the user agreement and privacy policy
If you’re learning mql4 object oriented programming basics, you’re probably building or improving Expert Advisors (EAs), indicators, or scripts for MetaTrader 4. Maybe your code already works, but it’s long, messy, and hard to change. That’s where object-oriented programming (OOP) comes in.
OOP helps you organize your code into logical blocks called classes and objects. Instead of one giant file full of functions and global variables, you split your logic into smaller, reusable pieces. In this guide, we’ll walk through the core ideas of MQL4 OOP in simple language so you can start using it in your trading robots.
MQL4 (MetaQuotes Language 4) is the programming language used in MetaTrader 4. It lets you create:
If you trade forex or CFDs on MT4, you’ve already seen MQL4 in action — every EA, every custom indicator is powered by this language.
Most beginners start with procedural MQL4:
OpenBuy(), CheckSignals(), TrailStop().mq4 fileThis works for small strategies, but as the EA grows, the code becomes hard to read, debug, or extend.
With OOP, instead of just functions, you create classes:
CTradeManager class to handle tradesCSignal class to detect entriesCRiskManager class to control lot sizesEach class takes care of one responsibility, which makes changes easier and less risky.
Using mql4 object oriented programming basics gives you:
In short, OOP helps you think like an architect, not just a coder.
Think of a class as a blueprint and an object as the actual item built from that blueprint.
CCar (defines wheels, engine, methods like Start())myCar (a specific car with a specific color and plate number)In MQL4:
class CExample
{
public:
int value;
void SetValue(int v) { value = v; }
};
Here, CExample is a class. When we create an instance of it in our EA, that instance is an object with its own value.
These four words often sound scary, but they’re simple ideas:
For trading:
CTradeManager class holds all trade-related functions and data.trade.OpenBuy() without worrying about all the internal checks.CTrendTradeManager can inherit from CTradeManager and add trend logic.CSignal subclasses can implement their own Check() method.To use classes, you often put them in .mqh include files:
Classes\TradeManager.mqhClasses\Signal.mqhThen you include them in your EA:
#include <Classes\TradeManager.mqh>
#include <Classes\Signal.mqh>
This keeps your main EA file short and readable.
Here’s a tiny example class:
class CCounter
{
private:
int m_count;
public:
CCounter() { m_count = 0; }
void Increment() { m_count++; }
int GetCount() { return m_count; }
};
In your EA:
CCounter counter;
int OnInit()
{
counter.Increment();
Print("Current count = ", counter.GetCount());
return(INIT_SUCCEEDED);
}
Now you’ve used a class and an object in MQL4 — a solid step into mql4 object oriented programming basics.
Class members store data:
class CTradeManager
{
private:
double m_lotSize;
int m_magic;
};
These variables (often called properties or fields) hold information related to the class.
Methods are functions inside the class:
class CTradeManager
{
public:
void SetLotSize(double lots) { m_lotSize = lots; }
double GetLotSize() { return m_lotSize; }
};
They operate on the class’ own data.
public: Accessible from outside the class (EA, other classes)private: Only accessible inside this classprotected: Accessible in this class and its child classesGood practice: keep internal details private or protected and expose a simple public interface.
A constructor is a special method called automatically when you create an object:
class CTradeManager
{
public:
CTradeManager(double lots, int magic)
{
m_lotSize = lots;
m_magic = magic;
}
};
This lets you initialize the object in one line:
CTradeManager trade(0.1, 12345);
A destructor runs when the object is destroyed:
class CTradeManager
{
public:
~CTradeManager()
{
Print("TradeManager destroyed");
}
};
You rarely need complex destructors in MQL4, but they’re useful for logging or cleanup.
Global objects live as long as the EA is running. Local objects inside functions exist only while that function runs. Knowing this helps you avoid accessing objects that no longer exist.
You might create a base class:
class CBaseStrategy
{
public:
virtual bool CheckSignal() { return(false); }
};
Then you build specific strategies:
class CBreakoutStrategy : public CBaseStrategy
{
public:
virtual bool CheckSignal()
{
// breakout logic here
return(true);
}
};
The child class changes (overrides) the behavior of CheckSignal(). You can pass around CBaseStrategy* pointers and call CheckSignal() — each child strategy responds in its own way.
Let’s sketch a simple CTradeManager:
A simplified version:
class CTradeManager
{
private:
double m_lot;
int m_magic;
public:
CTradeManager(double lot, int magic)
{
m_lot = lot;
m_magic = magic;
}
bool Buy(string symbol, double sl, double tp)
{
// basic order send (simplified, no checks)
int ticket = OrderSend(symbol, OP_BUY, m_lot, Ask, 3, sl, tp, "OOP Buy", m_magic, 0, clrNONE);
return(ticket > 0);
}
bool Sell(string symbol, double sl, double tp)
{
int ticket = OrderSend(symbol, OP_SELL, m_lot, Bid, 3, sl, tp, "OOP Sell", m_magic, 0, clrNONE);
return(ticket > 0);
}
};
In your EA:
#include <Classes\TradeManager.mqh>
CTradeManager trade(0.1, 12345);
int OnTick()
{
// example: open a buy when some condition is true
// if(Condition())
// trade.Buy(Symbol(), 0, 0);
return(0);
}
This shows how mql4 object oriented programming basics directly help you create neat trading components.
Common issues:
; after class definitionAlways check the line numbers and messages in the MetaEditor for clues.
Use Print() to inspect object values:
Print("LotSize = ", trade.GetLotSize());
Check the Experts and Journal tabs in MT4 to see what’s happening.
A clean structure might look like:
Experts\MyEA.mq4Include\Classes\TradeManager.mqhInclude\Classes\Signal.mqhInclude\Classes\RiskManager.mqhSeparate:
Each one becomes a reusable block.
Once you trust your CTradeManager, you can plug it into any EA that needs trade handling — no need to rewrite the logic each time.
Objects use memory for their data, but for typical EAs, this isn’t a problem. The overhead of OOP is usually small compared to network and broker latency.
Use OOP when:
Simple functions are fine for tiny scripts or quick tests.
CTradeManager)Q1. Do I need OOP to write a profitable EA?
No, but OOP makes your EA easier to maintain and extend. As your strategy grows, OOP can save you a lot of time and reduce bugs.
Q2. Can I mix procedural and OOP code in MQL4?
Yes. You can still use normal functions and global variables while also using classes and objects. Many projects start procedural and gradually move to OOP.
Q3. Is OOP in MQL4 different from C++ or other languages?
The core ideas (classes, inheritance, access specifiers) are similar, but MQL4 is more limited and focused on trading tasks. If you know OOP in C++, you’ll feel at home quickly.
Q4. Where can I read the official documentation for MQL4 OOP?
You can find the official language reference and examples on the MetaQuotes documentation website (MQL4 Reference). It covers classes, special methods, and more in detail.
Q5. How do I start practicing mql4 object oriented programming basics?
Begin with a tiny project: create a CCounter or CTradeManager class, use it in an EA, and slowly add more features. Don’t try to rebuild a big EA in one step.
Q6. Can I use OOP in custom indicators as well?
Yes. Indicators can also include classes to handle calculations, buffers, and signals. The pattern is similar: put classes in .mqh files and include them in your indicator.
You’ve now walked through mql4 object oriented programming basics — from classes and objects to constructors, inheritance, and a simple trade manager example. With these foundations, you can start turning your EAs and indicators into clean, modular, and reusable systems.
Your next steps:
As you get comfortable, you’ll find that changes that once took hours now take minutes, and your code becomes easier to share, test, and reuse.