What is MetaEditor?
MetaEditor is the integrated development environment (IDE) built into MetaTrader 4 and MetaTrader 5 platforms. It allows traders to write, compile, debug, and modify Expert Advisors, custom indicators, and scripts.
MetaEditor Interface Overview
The MetaEditor window consists of these main sections:
How to Create a New EA in MetaEditor
Follow these step-by-step instructions:
1. Open MetaEditor from MT4/MT5: Click Tools > MetaQuotes Language Editor or press F4
2. Click File > New > Expert Advisor > Next
3. Enter EA name (e.g., "MyFirstEA")
4. Select author name and link (optional)
5. Choose template type: Simple Expert Advisor is recommended for beginners
6. Click Finish to generate the template code
Complete EA Template Structure
```mql4
//+------------------------------------------------------------------+
//| MyFirstEA.mq4 |
//| Copyright 2024, YourName |
//| https://www.xxx.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, YourName"
#property link "https://www.xxx.com"
#property version "1.00"
#property strict
// Input parameters
input double LotSize = 0.1; // Trading lot size
input int StopLoss = 50; // Stop loss in points
input int TakeProfit = 100; // Take profit in points
// Global variables
int magicNumber = 12345; // Unique EA identifier
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit() {
Print("EA initialized successfully");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick() {
static datetime lastBarTime = 0;
if(Time[0] == lastBarTime) return;
lastBarTime = Time[0];
// Trading logic goes here
double maFast = iMA(NULL, 0, 10, 0, MODE_SMA, PRICE_CLOSE, 1);
double maSlow = iMA(NULL, 0, 30, 0, MODE_SMA, PRICE_CLOSE, 1);
if(maFast > maSlow) {
// Buy signal
} else if(maFast < maSlow) {
// Sell signal
}
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
Print("EA removed. Reason code: ", reason);
}
//+------------------------------------------------------------------+
```
Compilation Process in MetaEditor
Compilation converts your MQL4/MQL5 source code into executable EX4/EX5 files. Here's how to do it:
1. Click the Compile button on the toolbar (or press F7)
2. Check the Toolbox panel at the bottom for error messages
3. If compilation succeeds, you'll see "Compilation successful" with 0 errors, 0 warnings
4. The compiled EX4 file is saved in the Experts folder of your MT4/MT5 installation
Common Compilation Errors and Solutions
| Error Code | Error Message | Solution |
|------------|---------------|----------|
| 1 | 'variable' - undeclared identifier | Declare the variable before using it |
| 17 | 'return' - cannot convert type | Check function return type matches declared type |
| 30 | ';' - semicolon expected | Add missing semicolon at end of statement |
| 31 | '}' - unexpected token | Check for missing or extra braces |
| 33 | 'if' - condition expected | Add parentheses around if condition |
| 130 | 'OrderSend' - stops are invalid | Check stop loss/take profit distance requirements |
| 4051 | 'iMA' - wrong parameter count | Verify number of arguments matches function definition |
Debugging Techniques in MetaEditor
1. Print Statement Debugging
```mql4
void OnTick() {
double bid = MarketInfo(Symbol(), MODE_BID);
Print("Current Bid: ", bid, " | Time: ", TimeToString(TimeCurrent()));
if(/* condition */) {
Print("Buy signal detected at bar: ", iBars(Symbol(), PERIOD_M1));
}
}
```
2. Visual Debugging with Comments and Objects
```mql4
void OnTick() {
// Draw text on chart for visual feedback
Comment("EA Running | Account Balance: ", AccountBalance());
// Draw arrow when signal occurs
if(buySignal) {
ObjectCreate(0, "BuyArrow"+TimeCurrent(), OBJ_ARROW_UP, 0, Time[0], Low[0]-10);
ObjectSetInteger(0, "BuyArrow"+TimeCurrent(), OBJPROP_COLOR, clrGreen);
}
}
```
3. Using Alert for Immediate Notification
```mql4
void OnTick() {
if(/* Important signal */) {
Alert(Symbol(), " - Signal detected at ", TimeToString(TimeCurrent()));
}
}
```
Debugging Checklist
MetaEditor Shortcut Keys
| Shortcut | Function |
|----------|----------|
| F4 | Open MetaEditor |
| F7 | Compile current file |
| F5 | Start debugging mode |
| F9 | Insert template (if/for/while) |
| Ctrl+F | Find text |
| Ctrl+H | Replace text |
| Ctrl+G | Go to line number |
| Ctrl+Space | Auto-complete function/variable |
How to Open Compiled EA in MT4/MT5
1. After successful compilation, switch to MT4/MT5
2. Open Navigator window (Ctrl+N)
3. Expand "Expert Advisors" folder
4. Find your EA by the name you saved
5. Drag and drop the EA onto any chart
6. Configure input parameters in the popup window
7. Click OK to start the EA
Reference:
9. Next Step
Part 4 will explain Hello World EA - Your First Automated Trading Program – Writing the simplest EA code, understanding code structure, and running it on a demo chart.