MQL5 Structures, Classes, and Object-Oriented Programming

When coding in MetaTrader 5, MQL5 structures, classes, and object-oriented programming (OOP) offer powerful ways to organize and manage your trading logic.

These tools, rooted in MQL5’s C++-like capabilities, let you bundle data and behavior into reusable units, ideal for complex Expert Advisors (EAs), indicators, or scripts.

But for simpler projects, traditional procedural coding might still be the better fit.

In this article we introduce MQL5 structures and classes with basic examples, explore OOP’s potential, and reflect on when to keep it simple with functions.

Understanding these concepts will allow you to become a better coder and will open new opportunities to build your tools.

Metatrader-MT5-MQL5-Structures-Classes-OOP-cover

MQL5 Structures

We have seen in other articles variables and arrays, I define structures as complex variables that have a set structure.

A MQL5 structure is a custom data type that groups related variables. Think of it as a lightweight container without functions.

Here’s a simple example to store trade settings:

This groups lotSize, stopLossPips, and takeProfitPips under one name, settings.

Structures are perfect for passing multiple values in a more organized way, no mess of separate variables.

They’re simple, efficient, and don’t require OOP knowledge, making them a great first step beyond basic variables.

Some content also available in the original documentation.

MQL5 Classes

MQL5 Classes are an evolution of MQL5 structures.

A class takes structures further by adding methods (functions) to manipulate the data.

Classes are the building blocks of MQL5 OOP.

Here’s a basic class for managing trades:

Here, TradeManager holds data (lotSize, entryPrice) and a method (OpenTrade()).

The constructor (TradeManager(double lots)) initializes the object.

This encapsulation, as in keeping data and logic together, is OOP’s core strength.

You can create multiple TradeManager objects with different settings, reusing the class across your code.

MQL5 Object-Oriented Programming

Object oriented programming is not a variable or a data type, it is a way of coding and developing software.

In Object-Oriented Programming (OOP), there is a preference to use MQL5 structures, classes and objects to develop your application or tool.

OOP is useful in complex application or when there is a high necessity or reusing code, however it comes with a cost in efficiency and complexity.

Some content is also existing in the original documentation.

MQL5 OOP revolves around principles like:

  • Encapsulation: Bundling data and methods (e.g., TradeManager hides trade logic).
  • Inheritance: Extending classes (e.g., a TrendTradeManager subclass could inherit from TradeManager).
  • Polymorphism: Methods behaving differently based on context (less common in basic EAs).

Here’s a taste of inheritance:

This builds on TradeManager, adding trend-checking logic.

OOP shines for complex EAs with multiple strategies or reusable components.

Simple Examples in Action

Let’s tie it together.

Use a structure to pass settings to a class:

This blends structures and classes for clarity and reuse — a stepping stone to full OOP.

Reflection: Stick to Procedural for Simple EAs

While MQL5 structures and classes unlock advanced possibilities, they are not always the best fit.

You will find that in the source code in this website we will rarely use structures and classes and most often we opt for classic procedural style and functions.

I find it more simple and faster to deliver for this level of complexity.

For simple EAs like a basic RSI-based bot, traditional procedural coding with functions is often smarter:

Why Procedural Here?

  • Speed: No object overhead — critical for lightweight scripts.
  • Simplicity: Less code, fewer concepts to juggle.
  • Clarity: For a single-task EA, functions in OnTick() are intuitive.

For beginner MQL5 programmers, procedural coding keeps learners focused on trading logic, not syntax complexity.

Reserve OOP for when your EA and indicators grow more complex.

Conclusion

MQL5 structures, classes, and object-oriented programming offer a structured, scalable way to code trading tools.

Structures organize data, classes add behavior, and OOP ties it into reusable systems.

But for simple EAs, procedural coding’s simplicity often wins.

Start with functions, explore structures, and graduate to classes as your projects demand.

For questions and suggestions please Contact Us.

MQL5 If Then and Conditional Operators

Trade Assistant For MT4

Leave a Comment