Summary: 提供完整的均线交叉策略MT4 EA源码,含MQL4代码、参数调节指南及编译修改教程,帮助交易者快速掌握趋势跟踪EA的开发与使用。




# 免费MT4 EA源码:移动平均线交叉策略

本文提供一套完整可编译的移动平均线交叉策略MT4 EA源码,适合EA编程初学者学习,也适合趋势交易者直接使用或二次开发。

策略逻辑



当快速移动平均线(如MA 5)上穿慢速移动平均线(如MA 20)时产生买入信号;下穿时产生卖出信号。包含固定止损、止盈及简单的仓位管理。

完整MQL4源码



```mql4
//+------------------------------------------------------------------+
//| MACrossoverEA.mq4 |
//| |
//| |
//+------------------------------------------------------------------+
#property copyright "自主编译"
#property link ""
#property version "1.00"
#property strict

//--- 输入参数
input double LotSize = 0.1; // 手数
input int FastMAPeriod = 5; // 快线周期
input int SlowMAPeriod = 20; // 慢线周期
input int MAPrice = PRICE_CLOSE; // 均线价格:0=收盘,1=开盘,2=最高,3=最低
input int MAMethod = MODE_SMA; // 均线方法:0=SMA,1=EMA,2=LWMA
input int StopLoss = 50; // 止损点数
input int TakeProfit = 100; // 止盈点数
input int MagicNumber = 12345; // EA唯一标识

//--- 全局变量
double fastMA, slowMA;
int ticket;

//+------------------------------------------------------------------+
//| EA初始化函数 |
//+------------------------------------------------------------------+
int OnInit()
{
if(StopLoss <= 0 || TakeProfit <= 0)
{
Print("止损和止盈必须为正数");
return(INIT_PARAMETERS_INCORRECT);
}
return(INIT_SUCCEEDED);
}

//+------------------------------------------------------------------+
//| EA反初始化函数 |
//+------------------------------------------------------------------+
void OnDeinit(const int reason)
{
Comment("");
}

//+------------------------------------------------------------------+
//| EA核心Tick函数 |
//+------------------------------------------------------------------+
void OnTick()
{
// 计算移动平均线
fastMA = iMA(NULL, 0, FastMAPeriod, 0, MAMethod, MAPrice, 1);
slowMA = iMA(NULL, 0, SlowMAPeriod, 0, MAMethod, MAPrice, 1);
double fastMA_prev = iMA(NULL, 0, FastMAPeriod, 0, MAMethod, MAPrice, 2);
double slowMA_prev = iMA(NULL, 0, SlowMAPeriod, 0, MAMethod, MAPrice, 2);

// 检查当前无持仓
if(CountOpenOrders() == 0)
{
// 买入信号:快线上穿慢线
if(fastMA_prev <= slowMA_prev && fastMA > slowMA)
{
OpenBuyOrder();
}
// 卖出信号:快线下穿慢线
else if(fastMA_prev >= slowMA_prev && fastMA < slowMA)
{
OpenSellOrder();
}
}
}

//+------------------------------------------------------------------+
//| 开多单函数 |
//+------------------------------------------------------------------+
void OpenBuyOrder()
{
double sl = 0, tp = 0;
if(StopLoss > 0)
sl = Ask - StopLoss * Point * 10;
if(TakeProfit > 0)
tp = Ask + TakeProfit * Point * 10;

ticket = OrderSend(Symbol(), OP_BUY, LotSize, Ask, 3, sl, tp, "MA Crossover EA", MagicNumber, 0, clrGreen);
if(ticket < 0)
Print("多单开仓失败: ", GetLastError());
}

//+------------------------------------------------------------------+
//| 开空单函数 |
//+------------------------------------------------------------------+
void OpenSellOrder()
{
double sl = 0, tp = 0;
if(StopLoss > 0)
sl = Bid + StopLoss * Point * 10;
if(TakeProfit > 0)
tp = Bid - TakeProfit * Point * 10;

ticket = OrderSend(Symbol(), OP_SELL, LotSize, Bid, 3, sl, tp, "MA Crossover EA", MagicNumber, 0, clrRed);
if(ticket < 0)
Print("空单开仓失败: ", GetLastError());
}

//+------------------------------------------------------------------+
//| 统计当前魔术号持仓数量 |
//+------------------------------------------------------------------+
int CountOpenOrders()
{
int count = 0;
for(int i = 0; i < OrdersTotal(); i++)
{
if(OrderSelect(i, SELECT_BY_POS, MODE_TRADES))
{
if(OrderSymbol() == Symbol() && OrderMagicNumber() == MagicNumber)
count++;
}
}
return count;
}
//+------------------------------------------------------------------+
```

使用说明



1. 编译:将代码保存为 `MACrossoverEA.mq4` 放入 `MQL4/Experts/` 文件夹,在MetaEditor中按F7编译。
2. 加载:将EA拖拽到任意MT4图表(推荐EURUSD,H1周期)。
3. 参数调节:在输入选项卡中调整快慢线周期、止损止盈点数等。

核心参数解释



| 参数 | 说明 | 推荐范围 |
|------|------|----------|
| LotSize | 交易手数 | 0.01 - 1.0 |
| FastMAPeriod | 快线周期 | 3 - 10 |
| SlowMAPeriod | 慢线周期 | 14 - 50 |
| StopLoss | 止损点数 | 20 - 100 |
| TakeProfit | 止盈点数 | 50 - 200 |
| MagicNumber | EA唯一识别号 | 任意整数 |

编译与修改教程



  • 编译报错:检查是否包含 `#property strict`,确认MT4版本兼容性。

  • 修改均线类型:将 `MAMethod` 改为1(EMA)或2(LWMA)。

  • 添加移动止损:在 `OnTick()` 中增加持仓遍历及移动止损逻辑。

  • 历史回测:使用MT4策略测试器,确保建模质量90%以上。


  • 参考来源



  • 自主编译,基于MQL4官方文档标准语法

  • 均线概念参考MetaQuotes官方 `iMA()` 函数说明


  • 如需更高级的策略源码及付费EA工具(含完整技术支持),欢迎订阅我们的专业EA库。