What is Hello World EA?
"Hello World" is the traditional first program when learning any programming language. For Forex EA development, a Hello World EA doesn't place real trades but prints messages to confirm the EA is running correctly and helps you understand the basic code structure.
Complete Hello World EA Source Code
```mql4
//+------------------------------------------------------------------+
//| HelloWorld.mq4 |
//| Copyright 2024, ForexEA傻瓜教程 |
//| https://www.xxx.com |
//+------------------------------------------------------------------+
#property copyright "Copyright 2024, ForexEA傻瓜教程"
#property link "https://www.xxx.com"
#property version "1.00"
#property strict
//+------------------------------------------------------------------+
//| Expert initialization function |
//+------------------------------------------------------------------+
int OnInit() {
Print("================== HELLO WORLD EA ==================");
Print("EA初始化成功!时间:", TimeToString(TimeCurrent()));
Print("当前图表品种:", Symbol());
Print("当前时间周期:", PeriodToString(Period()));
Print("====================================================");
return(INIT_SUCCEEDED);
}
//+------------------------------------------------------------------+
//| Expert tick function |
//+------------------------------------------------------------------+
void OnTick() {
static int tickCounter = 0;
tickCounter++;
// 每100个Tick打印一次信息,避免日志过多
if(tickCounter % 100 == 1) {
Print("第", tickCounter, "个Tick | 当前买价:", MarketInfo(Symbol(), MODE_BID),
" | 当前卖价:", MarketInfo(Symbol(), MODE_ASK));
}
// 在图表上实时显示信息
Comment("=== Hello World EA 运行中 ===\n",
"品种:", Symbol(), "\n",
"周期:", PeriodToString(Period()), "\n",
"Tick计数:", tickCounter, "\n",
"当前买价:", MarketInfo(Symbol(), MODE_BID), "\n",
"当前卖价:", MarketInfo(Symbol(), MODE_ASK), "\n",
"账户余额:", AccountBalance(), "\n",
"EA状态:运行正常");
}
//+------------------------------------------------------------------+
//| Expert deinitialization function |
//+------------------------------------------------------------------+
void OnDeinit(const int reason) {
Print("================== EA已停止 ==================");
Print("停止原因代码:", reason);
Print("停止原因说明:", GetDeinitReasonText(reason));
Print("总Tick处理数:", tickCounter);
Print("=============================================");
// 清除图表上的注释
Comment("");
}
//+------------------------------------------------------------------+
//| 辅助函数:将周期数值转换为可读字符串 |
//+------------------------------------------------------------------+
string PeriodToString(int period) {
switch(period) {
case PERIOD_M1: return("M1 (1分钟)");
case PERIOD_M5: return("M5 (5分钟)");
case PERIOD_M15: return("M15 (15分钟)");
case PERIOD_M30: return("M30 (30分钟)");
case PERIOD_H1: return("H1 (1小时)");
case PERIOD_H4: return("H4 (4小时)");
case PERIOD_D1: return("D1 (日线)");
case PERIOD_W1: return("W1 (周线)");
case PERIOD_MN1: return("MN1 (月线)");
default: return("未知周期");
}
}
//+------------------------------------------------------------------+
//| 辅助函数:获取反初始化原因说明 |
//+------------------------------------------------------------------+
string GetDeinitReasonText(int reason) {
switch(reason) {
case REASON_REMOVE: return("用户从图表上移除了EA");
case REASON_RECOMPILE: return("用户重新编译了EA");
case REASON_CHARTCLOSE: return("用户关闭了图表");
case REASON_PARAMETERS: return("用户修改了输入参数");
case REASON_ACCOUNT: return("账户切换或重新登录");
default: return("其他原因");
}
}
//+------------------------------------------------------------------+
```
Step-by-Step Code Explanation
1. Property Declarations (第5-8行)
| Property | Purpose |
|----------|---------|
| #property copyright | Sets copyright information shown in EA properties |
| #property link | Sets website link for the EA |
| #property version | Defines version number for tracking updates |
| #property strict | Enables strict syntax checking to catch errors early |
2. OnInit() Function (第12-20行)
This function runs once when the EA is first attached to a chart. Use it for:
3. OnTick() Function (第24-37行)
This function runs on every price tick. Key points:
4. OnDeinit() Function (第41-51行)
This function runs when EA is removed. Use it for:
How to Compile and Run Hello World EA
Step 1: Create New EA File
1. Open MetaEditor (F4)
2. Click File > New > Expert Advisor > Next
3. Name it "HelloWorld"
4. Click Finish
Step 2: Replace Template Code
1. Select all generated code (Ctrl+A)
2. Delete it
3. Paste the complete Hello World EA code above (Ctrl+V)
Step 3: Compile the Code
1. Click Compile button or press F7
2. Check bottom panel for "Compilation successful"
3. If errors appear, fix them according to the error messages
Step 4: Run on Demo Chart
1. Switch to MT4/MT5
2. Open Navigator (Ctrl+N)
3. Expand "Expert Advisors" folder
4. Find "HelloWorld"
5. Drag it onto any demo chart
6. Click OK in the popup window
Expected Output
After successful loading, you will see:
Understanding Comment() Output on Chart
```
=== Hello World EA 运行中 ===
品种:EURUSD
周期:H1 (1小时)
Tick计数:1523
当前买价:1.09250
当前卖价:1.09260
账户余额:10000.00
EA状态:运行正常
```
Common Issues and Solutions
| Issue | Cause | Solution |
|-------|-------|----------|
| EA doesn't appear in Navigator | Compilation failed | Check for red error messages in MetaEditor |
| No smiley face on chart | EA stopped due to error | Check Experts tab for error messages |
| Comment not showing | Chart font too small | Increase chart font size or zoom in |
| Print messages missing | Log level too high | Check Terminal > Experts tab is visible |
| EA stops immediately | Auto-trading disabled | Click "AutoTrading" button on MT4 toolbar |
Testing Checklist
Reference:
9. Next Step
Part 5 will explain EA Execution Mechanism on Charts – Understanding EA lifecycle, initialization phase, tick loop execution, timing events, and deinitialization cleanup.