[MQL5 Paradise Episode 3] The Magic Box That Stows Data! Master “Variables” to Give Your EA Memory
Hello everyone, this is Pineapple.
In the previous “MQL5 Paradise” Episode 2, we launched MetaEditor and created a small script that displays the message “Hello, MQL5!” on the MT5 screen. When the text appeared successfully, you might have felt a little “Wow!” That small moment of awe is the biggest motivation to keep learning programming. Please treasure it.
Now, for the third installment, the theme is something absolutely unavoidable in the world of programming and the most convenient mechanism:“Variables”.
“Variables… they make my head hurt recalling middle school math.”
If you feel that way, don’t worry. In MQL5, a variable is not a complicated formula. Put simply,“a named magical box to store data”.
Keep your favorite coffee (tea is fine too) nearby, relax, and unravel the secrets of this “box.”
Why do EAs need a “magic box”?
When we trade, our minds process various numbers. “Current USD/JPY price is 150.50” “This time I’m confident, so the lot size will be 0.5” “Acceptable slippage will be 30 points”
In order for a fully automated trading program (EA) to trade on your behalf, you need something similar to“a place to remember numbers and text”.
However, computers can only do what they are told. Even if you say, “remember the last lot size,” unless you specify a place, it will forget quickly. That’s where the “variable” comes in.
Prepare a box named “lot size” and put the number “0.5” into it. Prepare a box named “current price,” and replace its contents as the price moves.
By using such a box (variable) whose contents can be changed according to the situation, the EA can respond flexibly to market changes. It’s precisely a magic box that gives the EA “memory” and “adaptability.”
The four shapes of boxes (data types) you should remember
In fact, this magic box cannot store everything in one box. Depending on the data type, you need to“the shape of the box (data type)”.
Just as you would choose a bottle for liquids or a file box for documents, you choose a box that fits the data in MQL5. For FX programming, there are four commonly used box shapes. For now, remembering only these is enough!
- int (integer)A box for whole numbers without decimals. It holds values like “1,” “100,” or “-50.”FX usage:Magic numbers (EA identifiers), slippage, moving average periods (like 14 days, 20 days)
- double (double: decimal)A box for numbers with decimals. It holds values like “150.235” or “0.01.”FX usage:Currency pair prices (rates), entry lot size, etc. In the world of exchange rates, decimals are crucial, so this
doubleis the most frequently used main pillar. - string (string: text)A box for text and strings. The previous example’s “Hello” is also in this family.FX usage:Currency pair name (e.g., "USDJPY"), messages to log, error reporting, etc.
- bool (boolean)A special box that stores only two values: “true” (yes) or “false” (no). Think of it as a switch’s ON/OFF.FX usage:Decisions like “Did a buy signal appear? (true/false)” and “Should we allow trading today? (true/false)”
Now, create an actual “box” and put contents into it
With the box types understood, let’s actually create variables in MQL5. As in the previous lesson, create a new script and write the following code.
First, take a look at how it behaves by inspecting the code carefully.
//+------------------------------------------------------------------+
//| Hello_MQL5.mq5 |
//| Copyright 2025, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
//| Script program start function |
//+------------------------------------------------------------------+
void OnStart()
{
// 1. Prepare the boxes (variables) and fill them
int my_slippage = 30; // Slippage (integer)
double my_lot = 0.1; // Lot size (decimal)
string my_symbol = "USDJPY"; // Currency pair name (string)
bool is_trading = true; // Trading permission (boolean)
// 2. Output the contents of the boxes with Print
Print("【Current settings】");
Print("Target currency pair:", my_symbol);
Print("Configured lot:", my_lot);
Print("Slippage:", my_slippage);
}
//+------------------------------------------------------------------+
Sample code explanation
How does that sound? You can probably guess what it does.
The rule for declaring (in programming terms, “declaring”) a variable is very simple:“box shape (type)” + “box name” = “value stored inside”;This is the order in which you write it.
For exampledouble my_lot = 0.1;means,double (a box for decimals) namedmy_lot is created and the number0.1 is placed inside it.
The box name can be any name you like, as long as it uses ASCII letters and numbers. my_lot is fine,apple_pie also works, but choosing a descriptive name helps you avoid asking later, “What did I put in this box again?” that’s key to progress.
In the latter part, we’ll use the previously learnedPrint()function to output the box contents to the log.Print("Configured lot:", my_lot);By separating with a comma (,), you can display plain text and the contents of the box together.
A simple and robust trading approach and the deep connection with variables
“Why not just write 0.1 directly in the Print statement instead of making a box?”
Smart question! Indeed, for small scripts it can work. Writing numbers directly into the program is called “hard coding.” However, in practical EA development, hard coding isan absolute mistake to avoid.
If later you want to change the lot size from 0.1 to 0.2, and you’ve hard-coded the number in hundreds of lines, you would need to search through the code to find every occurrence of 0.1 and edit it, risking errors from missed instances.
If you summarize the variable at the top, such asdouble my_lot = 0.1;then changing it once fixes the entire program’s lot size immediately.
I always believe in“avoiding over-optimization and trading simply and robustly.”A well-structured program with clear numbers managed by variables is the essential condition for building a robust (hard to break and easy to maintain) system.
EA built by patching complex logic tends to lose track of which numbers affect what. Use variables skillfully and aim for a clear, transparent “beautiful recipe.” That groundwork will help your EA adapt smoothly to market waves.
Conclusion
This time we explained the magical box called “variable.”int(integer),double(decimal),string(text),bool(switch). With knowledge of these four boxes, your program can remember various data.
“I get the idea of putting numbers in boxes. But how do I use them to trade?”
The true value of the numbers stored in boxes comes when you base actions on them and make“decisions”.
Next time, we’ll study the essence of programming:“if statements (conditional branching)”. Learn how to make the program itself judge the situation and change its actions accordingly.
We’re entering the brain of the EA. Enjoy the gradual expansion of what you can do.
Until then, see you next time in MQL5 Paradise. May simple and beautiful code enhance your trading.
This is Pineapple, signing off. See you again!